2015-09-16 20:02:06 +03:00
|
|
|
/* jshint expr:true */
|
|
|
|
import { expect } from 'chai';
|
|
|
|
import {
|
|
|
|
describe,
|
|
|
|
it
|
|
|
|
} from 'mocha';
|
2016-05-24 15:06:59 +03:00
|
|
|
import validator from 'ghost-admin/validators/nav-item';
|
|
|
|
import NavItem from 'ghost-admin/models/navigation-item';
|
2015-09-16 20:02:06 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
const testInvalidUrl = function (url) {
|
|
|
|
let navItem = NavItem.create({url});
|
2015-09-16 20:02:06 +03:00
|
|
|
|
|
|
|
validator.check(navItem, 'url');
|
|
|
|
|
|
|
|
expect(validator.get('passed'), `"${url}" passed`).to.be.false;
|
|
|
|
expect(navItem.get('errors').errorsFor('url')).to.deep.equal([{
|
|
|
|
attribute: 'url',
|
|
|
|
message: 'You must specify a valid URL or relative path'
|
|
|
|
}]);
|
|
|
|
expect(navItem.get('hasValidated')).to.include('url');
|
|
|
|
};
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
const testValidUrl = function (url) {
|
|
|
|
let navItem = NavItem.create({url});
|
2015-09-16 20:02:06 +03:00
|
|
|
|
|
|
|
validator.check(navItem, 'url');
|
|
|
|
|
|
|
|
expect(validator.get('passed'), `"${url}" failed`).to.be.true;
|
|
|
|
expect(navItem.get('hasValidated')).to.include('url');
|
|
|
|
};
|
|
|
|
|
2015-10-06 19:31:03 +03:00
|
|
|
describe('Unit: Validator: nav-item', function () {
|
2015-09-16 20:02:06 +03:00
|
|
|
it('requires label presence', function () {
|
|
|
|
let navItem = NavItem.create();
|
|
|
|
|
|
|
|
validator.check(navItem, 'label');
|
|
|
|
|
|
|
|
expect(validator.get('passed')).to.be.false;
|
|
|
|
expect(navItem.get('errors').errorsFor('label')).to.deep.equal([{
|
|
|
|
attribute: 'label',
|
|
|
|
message: 'You must specify a label'
|
|
|
|
}]);
|
|
|
|
expect(navItem.get('hasValidated')).to.include('label');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('requires url presence', function () {
|
|
|
|
let navItem = NavItem.create();
|
|
|
|
|
|
|
|
validator.check(navItem, 'url');
|
|
|
|
|
|
|
|
expect(validator.get('passed')).to.be.false;
|
|
|
|
expect(navItem.get('errors').errorsFor('url')).to.deep.equal([{
|
|
|
|
attribute: 'url',
|
|
|
|
message: 'You must specify a URL or relative path'
|
|
|
|
}]);
|
|
|
|
expect(navItem.get('hasValidated')).to.include('url');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails on invalid url values', function () {
|
|
|
|
let invalidUrls = [
|
|
|
|
'test@example.com',
|
|
|
|
'/has spaces',
|
|
|
|
'no-leading-slash',
|
|
|
|
'http://example.com/with spaces'
|
|
|
|
];
|
|
|
|
|
|
|
|
invalidUrls.forEach(function (url) {
|
|
|
|
testInvalidUrl(url);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes on valid url values', function () {
|
|
|
|
let validUrls = [
|
|
|
|
'http://localhost:2368',
|
|
|
|
'http://localhost:2368/some-path',
|
|
|
|
'https://localhost:2368/some-path',
|
|
|
|
'//localhost:2368/some-path',
|
|
|
|
'http://localhost:2368/#test',
|
|
|
|
'http://localhost:2368/?query=test&another=example',
|
|
|
|
'http://localhost:2368/?query=test&another=example#test',
|
|
|
|
'tel:01234-567890',
|
|
|
|
'mailto:test@example.com',
|
|
|
|
'http://some:user@example.com:1234',
|
|
|
|
'/relative/path'
|
|
|
|
];
|
|
|
|
|
|
|
|
validUrls.forEach(function (url) {
|
|
|
|
testValidUrl(url);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('validates url and label by default', function () {
|
|
|
|
let navItem = NavItem.create();
|
|
|
|
|
|
|
|
validator.check(navItem);
|
|
|
|
|
|
|
|
expect(navItem.get('errors').errorsFor('label')).to.not.be.empty;
|
|
|
|
expect(navItem.get('errors').errorsFor('url')).to.not.be.empty;
|
|
|
|
expect(validator.get('passed')).to.be.false;
|
|
|
|
});
|
|
|
|
});
|