Ghost/test/unit/shared/settings-cache.test.js
Hannah Wolfe f08a55c21f
Renamed tests to .test.js & updated commands
refs: https://github.com/TryGhost/Team/issues/856
refs: https://github.com/TryGhost/Team/issues/756

- The .test.js extension is better than _spec.js as it's more obvious that it's an extension
- It also meaans we can use the --extension parameter in mocha, which should result in a better default behaviour for `yarn test`
- It also highlights that some of our tests were named incorrectly and were not (and still will not be) run (see https://github.com/TryGhost/Team/issues/856)
- Note: even with this change, `yarn test` is throwing errors, I believe because of this issue https://github.com/TryGhost/Team/issues/756
2021-07-06 20:45:01 +01:00

101 lines
3.3 KiB
JavaScript

const rewire = require('rewire');
const should = require('should');
const sinon = require('sinon');
const _ = require('lodash');
const events = require('../../../core/server/lib/common/events');
// Testing the Private API
let cache = rewire('../../../core/shared/settings-cache/cache');
const publicSettings = require('../../../core/shared/settings-cache/public');
should.equal(true, true);
describe('UNIT: settings cache', function () {
beforeEach(function () {
cache = rewire('../../../core/shared/settings-cache/cache');
});
afterEach(function () {
sinon.restore();
});
it('does not auto convert string into number', function () {
cache.set('key1', {value: '1'});
(typeof cache.get('key1')).should.eql('string');
});
it('does not auto convert string into number: float', function () {
cache.set('key1', {value: '1.4'});
(typeof cache.get('key1')).should.eql('string');
});
it('stringified JSON get\'s parsed', function () {
cache.set('key2', {value: '{"a":"1","b":"hallo","c":{"d":[]},"e":2}'});
(typeof cache.get('key2')).should.eql('object');
cache.get('key2').a.should.eql('1');
cache.get('key2').b.should.eql('hallo');
cache.get('key2').c.should.eql({d: []});
cache.get('key2').e.should.eql(2);
});
it('can get all values', function () {
cache.set('key1', {value: '1'});
cache.get('key1').should.eql('1');
cache.getAll().should.eql({key1: {value: '1'}});
});
it('correctly filters and formats public values', function () {
cache.set('key1', {value: 'something'});
cache.set('title', {value: 'hello world'});
cache.set('timezone', {value: 'PST'});
cache.getAll().should.eql({
key1: {value: 'something'},
title: {value: 'hello world'},
timezone: {value: 'PST'}
});
let values = _.zipObject(_.keys(publicSettings), _.fill(Array(_.size(publicSettings)), null));
values.title = 'hello world';
values.timezone = 'PST';
cache.getPublic().should.eql(values);
});
it('can reset and init without double handling of events', function () {
const setSpy = sinon.spy(cache, 'set');
const settingsCollection = {
models: [{
get: () => 'key1',
toJSON: () => ({value: 'init value'})
}]
};
cache.init(events, settingsCollection);
cache.get('key1').should.equal('init value');
// check handler only called once on settings.edit
setSpy.callCount.should.equal(1);
events.emit('settings.edited', {
get: () => 'key1',
toJSON: () => ({value: 'first edit'})
});
setSpy.callCount.should.equal(2);
cache.get('key1').should.equal('first edit');
// init does a reset by default
cache.init(events, settingsCollection);
setSpy.callCount.should.equal(3);
cache.get('key1').should.equal('init value');
// edit again, check event only fired once
events.emit('settings.edited', {
get: () => 'key1',
toJSON: () => ({value: 'second edit'})
});
setSpy.callCount.should.equal(4);
cache.get('key1').should.equal('second edit');
});
});