Ghost/test/unit/shared/settings-cache.test.js
Hannah Wolfe a31069d49d
Removed unnecessary use of rewire
refs: https://github.com/TryGhost/Ghost/commit/e68cb8b31

- I found that some of the places we use rewire are totally unnecessary
- Rewire seems to mess with coverage sometimes
- It's also a code smell in general so I've ripped it out where possible
2022-03-24 19:32:06 +00:00

149 lines
5.1 KiB
JavaScript

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 = require('../../../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.init(events);
});
afterEach(function () {
sinon.restore();
});
it('.get() does not auto convert string into number', function () {
cache.set('key1', {value: '1'});
(typeof cache.get('key1')).should.eql('string');
});
it('.get() oes not auto convert string into number: float', function () {
cache.set('key1', {value: '1.4'});
(typeof cache.get('key1')).should.eql('string');
});
it('.get() parses stringified JSON', 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('.get() respects the resolve option', function () {
cache.set('foo', {value: 'bar'});
cache.get('foo', {resolve: false}).should.be.an.Object().with.property('value', 'bar');
cache.get('foo', {resolve: true}).should.be.a.String().and.eql('bar');
});
it('.get() can handle miscellaneous values', function () {
// THis value is not set
should(cache.get('bar')).be.undefined();
// Using set with a string instead of an object
cache.set('foo', 'bar');
should(cache.get('foo')).eql(null);
// Various built-in values
cache.set('null', {value: null});
cache.set('nan', {value: NaN});
cache.set('true', {value: true});
cache.set('false', {value: false});
cache.set('object', {value: {}});
cache.set('array', {value: []});
should(cache.get('null')).eql(null);
should(cache.get('nan')).eql(null);
should(cache.get('true')).eql(true);
should(cache.get('false')).eql(null); // EEEK! this is falsy but should be false
should(cache.get('object')).eql({});
should(cache.get('array')).eql([]);
// Built-ins as strings
cache.set('empty', {value: ''});
cache.set('stringnull', {value: 'null'});
cache.set('stringnan', {value: 'NaN'});
cache.set('stringtrue', {value: 'true'});
cache.set('stringfalse', {value: 'false'});
cache.set('stringobj', {value: '{}'});
cache.set('stringarr', {value: '[]'});
should(cache.get('empty')).eql(null);
should(cache.get('stringnull')).eql(null);
should(cache.get('stringnan')).eql('NaN');
should(cache.get('stringtrue')).eql(true);
should(cache.get('stringfalse')).eql(null); // EEEK! this is falsy but should be false
should(cache.get('stringobj')).eql({});
should(cache.get('stringarr')).eql([]);
});
it('.getAll() returns all values', function () {
cache.set('key1', {value: '1'});
cache.get('key1').should.eql('1');
cache.getAll().should.eql({key1: {value: '1'}});
});
it('.getPublic() 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('.reset() and .init() do not double up 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');
});
});