nconf-lite/test/stores/memory-store.test.js
Adrien Becchis b8686aeff0 Migrating test to jest (#292)
* set up jest dependencies

* add parser option to eslint to support es6

* migrate first test to jest

* migrate the argv test to shpec

* convert the env and literal store tests

* convert the file-store tests

* convert the memory-store tests

* convert the hierarchy tests

* convert the provider-save test

* convert the complete test

* convert the provider test

* convert the conf test

* tweak a test that was no longer working (context changed)

* replace in place the helpers file

* remove vows dependency

* update the test invocation to rely on jest

* update the argv test to be able to use the jest --verbose option

* Some tweaks to the test to have them working

* Update node version tested (+10 +12 -9)

* Replace const by var until we drop 0.10/0.12/4 node

* Replace let by var until we drop 0.10/0.12/4 node
2019-04-29 22:26:02 -04:00

114 lines
4 KiB
JavaScript

/*
* memory-store-test.js: Tests for the nconf Memory store.
*
* (C) 2011, Charlie Robbins and the Contributors.
*
*/
var nconf = require('../../lib/nconf');
var merge = require('../fixtures/data').merge;
describe('nconf/stores/memory', () => {
describe("When using the nconf memory store", () => {
var store = new nconf.Memory();
it("the set() method should respond with true", () => {
expect(store.set('foo:bar:bazz', 'buzz')).toBeTruthy();
expect(store.set('falsy:number', 0)).toBeTruthy();
expect(store.set('falsy:string:empty', '')).toBeTruthy();
expect(store.set('falsy:string:value', 'value')).toBeTruthy();
expect(store.set('falsy:boolean', false)).toBeTruthy();
expect(store.set('falsy:object', null)).toBeTruthy();
});
it("the get() method should respond with the correct value", () => {
expect(store.get('foo:bar:bazz')).toEqual('buzz');
expect(store.get('falsy:number')).toEqual(0);
expect(store.get('falsy:string:empty')).toEqual('');
expect(store.get('falsy:string:value')).toEqual('value');
expect(store.get('falsy:boolean')).toEqual(false);
expect(store.get('falsy:object')).toEqual(null);
});
describe("the get() method should not fail when retrieving non-existent keys", () => {
it("at the root level", () => {
expect(store.get('this:key:does:not:exist')).toEqual(undefined);
});
it("within numbers", () => {
expect(store.get('falsy:number:not:exist')).toEqual(undefined);
});
it("within booleans", () => {
expect(store.get('falsy:boolean:not:exist')).toEqual(undefined);
});
it("within objects", () => {
expect(store.get('falsy:object:not:exist')).toEqual(undefined);
});
it("within empty strings", () => {
expect(store.get('falsy:string:empty:not:exist')).toEqual(undefined);
});
it("within non-empty strings", () => {
expect(store.get('falsy:string:value:not:exist')).toEqual(undefined);
});
});
it("the clear() method, should respond with the true", () => {
expect(store.get('foo:bar:bazz')).toEqual('buzz');
expect(store.clear('foo:bar:bazz')).toBeTruthy();
expect(typeof store.get('foo:bar:bazz') === 'undefined').toBeTruthy();
});
describe("the merge() method", () => {
it("when overriding an existing literal value", () => {
store.set('merge:literal', 'string-value');
store.merge('merge:literal', merge);
expect(store.get('merge:literal')).toEqual(merge);
});
it("when overriding an existing Array value", () => {
store.set('merge:array', [1, 2, 3, 4]);
store.merge('merge:array', merge);
expect(store.get('merge:literal')).toEqual(merge);
});
it("when merging into an existing Object value", () => {
store.set('merge:object', {
prop1: 2,
prop2: 'prop2',
prop3: {
bazz: 'bazz'
},
prop4: ['foo', 'bar']
});
store.merge('merge:object', merge);
expect(store.get('merge:object:prop1')).toEqual(1);
expect(store.get('merge:object:prop2').length).toEqual(3);
expect(store.get('merge:object:prop3')).toEqual({
foo: 'bar',
bar: 'foo',
bazz: 'bazz'
});
expect(store.get('merge:object:prop4').length).toEqual(2);
});
});
});
describe("When using the nconf memory store with different logical separator", () => {
var store = new nconf.Memory({logicalSeparator: '||'});
it("when storing with : (colon), should store the config atomicly", () => {
store.set('foo:bar:bazz', 'buzz');
expect(typeof store.get('foo:bar') === 'undefined').toBeTruthy();
expect(store.get('foo:bar:bazz')).toEqual('buzz');
});
it("when storing with separator, should be able to read the object", () => {
store.set('foo||bar||bazz', 'buzz');
expect(store.get('foo||bar').bazz).toEqual('buzz');
expect(store.get('foo').bar.bazz).toEqual('buzz');
})
});
});