Ghost/test/unit/services/url/Urls_spec.js
Hannah Wolfe 22e13acd65 Updated var declarations to const/let and no lists
- All var declarations are now const or let as per ES6
- All comma-separated lists / chained declarations are now one declaration per line
- This is for clarity/readability but also made running the var-to-const/let switch smoother
- ESLint rules updated to match

How this was done:

- npm install -g jscodeshift
- git clone https://github.com/cpojer/js-codemod.git
- git clone git@github.com:TryGhost/Ghost.git shallow-ghost
- cd shallow-ghost
- jscodeshift -t ../js-codemod/transforms/unchain-variables.js . -v=2
- jscodeshift -t ../js-codemod/transforms/no-vars.js . -v=2
- yarn
- yarn test
- yarn lint / fix various lint errors (almost all indent) by opening files and saving in vscode
- grunt test-regression
- sorted!
2020-04-29 16:51:13 +01:00

114 lines
3.0 KiB
JavaScript

const _ = require('lodash');
const Promise = require('bluebird');
const should = require('should');
const jsonpath = require('jsonpath');
const sinon = require('sinon');
const common = require('../../../../core/server/lib/common');
const Urls = require('../../../../core/frontend/services/url/Urls');
describe('Unit: services/url/Urls', function () {
let urls;
let eventsToRemember;
beforeEach(function () {
urls = new Urls();
urls.add({
url: '/test/',
resource: {
data: {
id: 'object-id-1'
}
},
generatorId: 2
});
urls.add({
url: '/something/',
resource: {
data: {
id: 'object-id-2'
}
},
generatorId: 1
});
urls.add({
url: '/casper/',
resource: {
data: {
id: 'object-id-3'
}
},
generatorId: 2
});
eventsToRemember = {};
sinon.stub(common.events, 'emit').callsFake(function (eventName, data) {
eventsToRemember[eventName] = data;
});
});
afterEach(function () {
sinon.restore();
});
it('fn: add', function () {
urls.add({
url: '/test/',
resource: {
data: {
id: 'object-id-x',
slug: 'a'
}
},
generatorId: 1
});
should.exist(eventsToRemember['url.added']);
eventsToRemember['url.added'].url.absolute.should.eql('http://127.0.0.1:2369/test/');
eventsToRemember['url.added'].url.relative.should.eql('/test/');
should.exist(eventsToRemember['url.added'].resource);
should.exist(eventsToRemember['url.added'].resource.data);
urls.getByResourceId('object-id-x').resource.data.slug.should.eql('a');
// add duplicate
urls.add({
url: '/test/',
resource: {
data: {
id: 'object-id-x',
slug: 'b'
}
},
generatorId: 1
});
should.exist(eventsToRemember['url.added']);
urls.getByResourceId('object-id-x').resource.data.slug.should.eql('b');
});
it('fn: getByResourceId', function () {
urls.getByResourceId('object-id-2').url.should.eql('/something/');
should.exist(urls.getByResourceId('object-id-2').generatorId);
urls.getByResourceId('object-id-2').generatorId.should.eql(1);
});
it('fn: getByGeneratorId', function () {
urls.getByGeneratorId(2).length.should.eql(2);
});
it('fn: getByUrl', function () {
urls.getByUrl('/something/').length.should.eql(1);
});
it('fn: removeResourceId', function () {
urls.removeResourceId('object-id-2');
should.not.exist(urls.getByResourceId('object-id-2'));
urls.removeResourceId('does not exist');
});
});