2019-01-03 18:23:22 +03:00
|
|
|
const rewire = require('rewire');
|
|
|
|
const should = require('should');
|
2020-07-01 19:16:57 +03:00
|
|
|
const sinon = require('sinon');
|
2019-01-03 18:23:22 +03:00
|
|
|
const _ = require('lodash');
|
2021-06-30 16:56:57 +03:00
|
|
|
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');
|
2017-06-04 13:52:22 +03:00
|
|
|
|
|
|
|
should.equal(true, true);
|
|
|
|
|
|
|
|
describe('UNIT: settings cache', function () {
|
2019-01-03 18:23:22 +03:00
|
|
|
beforeEach(function () {
|
2021-06-30 16:56:57 +03:00
|
|
|
cache = rewire('../../../core/shared/settings-cache/cache');
|
2019-01-03 18:23:22 +03:00
|
|
|
});
|
|
|
|
|
2020-07-01 19:16:57 +03:00
|
|
|
afterEach(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2017-06-04 13:52:22 +03:00
|
|
|
it('does not auto convert string into number', function () {
|
|
|
|
cache.set('key1', {value: '1'});
|
|
|
|
(typeof cache.get('key1')).should.eql('string');
|
|
|
|
});
|
|
|
|
|
2017-10-26 14:54:18 +03:00
|
|
|
it('does not auto convert string into number: float', function () {
|
|
|
|
cache.set('key1', {value: '1.4'});
|
|
|
|
(typeof cache.get('key1')).should.eql('string');
|
|
|
|
});
|
|
|
|
|
2017-06-04 13:52:22 +03:00
|
|
|
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);
|
|
|
|
});
|
2019-01-03 18:23:22 +03:00
|
|
|
|
|
|
|
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'});
|
2020-06-22 14:21:00 +03:00
|
|
|
cache.set('timezone', {value: 'PST'});
|
2019-01-03 18:23:22 +03:00
|
|
|
|
|
|
|
cache.getAll().should.eql({
|
|
|
|
key1: {value: 'something'},
|
|
|
|
title: {value: 'hello world'},
|
2020-06-22 14:21:00 +03:00
|
|
|
timezone: {value: 'PST'}
|
2019-01-03 18:23:22 +03:00
|
|
|
});
|
|
|
|
|
2021-03-04 08:35:02 +03:00
|
|
|
let values = _.zipObject(_.keys(publicSettings), _.fill(Array(_.size(publicSettings)), null));
|
2019-01-03 18:23:22 +03:00
|
|
|
values.title = 'hello world';
|
|
|
|
values.timezone = 'PST';
|
|
|
|
|
|
|
|
cache.getPublic().should.eql(values);
|
|
|
|
});
|
2020-07-01 19:16:57 +03:00
|
|
|
|
2021-06-30 12:32:04 +03:00
|
|
|
it('can reset and init without double handling of events', function () {
|
2020-07-01 19:16:57 +03:00
|
|
|
const setSpy = sinon.spy(cache, 'set');
|
|
|
|
|
|
|
|
const settingsCollection = {
|
|
|
|
models: [{
|
|
|
|
get: () => 'key1',
|
|
|
|
toJSON: () => ({value: 'init value'})
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
2021-06-30 16:26:59 +03:00
|
|
|
cache.init(events, settingsCollection);
|
2020-07-01 19:16:57 +03:00
|
|
|
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');
|
|
|
|
|
2021-06-30 12:32:04 +03:00
|
|
|
// init does a reset by default
|
2021-06-30 16:26:59 +03:00
|
|
|
cache.init(events, settingsCollection);
|
2020-07-01 19:16:57 +03:00
|
|
|
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');
|
|
|
|
});
|
2017-06-04 13:52:22 +03:00
|
|
|
});
|