import { ConfigNode } from '../../src/service'; describe(`ConfigNode`, () => { it('should return value of settings passed in', async () => { const initialSettings = { a: 1, b: 2, c: { c1: 1, c2: 2, c3: 3, }, }; const node = await ConfigNode.create(initialSettings); expect(node.value).toEqual(initialSettings); }); it('should return value modified after creation', async () => { const initialSettings = { a: 1, b: 2, c: { c1: 1, c2: 2, c3: 3, }, }; const node = await ConfigNode.create(initialSettings); const result = { a: 1, b: 2, c: { c1: 1, c2: 2, c3: 3, }, d: 3, }; await node.applyValue({ d: 3 }); expect(node.value).toEqual(result); }); it('should return value from node retrieved by property path', async () => { const initialSettings = { a: 1, b: 2, c: { c1: 1, c2: 2, c3: 3, }, }; const node = await ConfigNode.create(initialSettings); const propNode = node.getPropertyNode('c.c1'); expect(propNode.value).toEqual(1); }); it('should properly inherit settings', async () => { const base1 = { c: 3, }; const base2 = { b: 2 }; const settings = { a: 1 }; const base1Node = await ConfigNode.create(base1); const base2Node = await ConfigNode.create(base2, base1Node); const node = await ConfigNode.create(settings, base2Node); const results = { a: 1, b: 2, c: 3 } expect(node.value).toEqual(results); }); it('should return proper values from direct reference to property node', async () => { const base1 = { c: 3, }; const base2 = { b: 2 }; const settings = { a: 1 }; const base1Node = await ConfigNode.create(base1); const base2Node = await ConfigNode.create(base2, base1Node); const node = await ConfigNode.create(settings, base2Node); const propNodeA = node.getPropertyNode('a'); expect(propNodeA.value).toEqual(1); }); it('should return proper values from direct reference to property node only defined in inherited node', async () => { const base1 = { c: 3, }; const base2 = { b: 2 }; const settings = { a: 1 }; const base1Node = await ConfigNode.create(base1); const base2Node = await ConfigNode.create(base2, base1Node); const node = await ConfigNode.create(settings, base2Node); const propNodeB = node.getPropertyNode('b'); expect(propNodeB.value).toEqual(2); }); it('should properly over-ride inherited values', async () => { const base1 = { c: 3, d: 3, e: 3, }; const base2 = { b: 2, c: 3, d: 2, }; const settings = { a: 1, b: 1, }; const base1Node = await ConfigNode.create(base1); const base2Node = await ConfigNode.create(base2, base1Node); const node = await ConfigNode.create(settings, base2Node); const result = { a: 1, b: 1, c: 3, d: 2, e: 3, } expect(node.value).toEqual(result); }); it('should properly over-ride inherited values when separately defined as static and objects', async () => { const base1 = { c: 3, d: 3, e: 3, f: 3, g: { g1: 3 }, h: 1, }; const base2 = { b: 2, c: 3, d: 2, f: { f1: 2, }, h: { h1: 1, } }; const settings = { a: 1, b: 1, g: 1, }; const base1Node = await ConfigNode.create(base1); const base2Node = await ConfigNode.create(base2, base1Node); const node = await ConfigNode.create(settings, base2Node); const result = { a: 1, b: 1, c: 3, d: 2, e: 3, f: { f1: 2, }, g: 1, h: { h1: 1, } } expect(node.value).toEqual(result); }); it('should handle inheriting same node mutlipe times', async () => { const base1 = { c: 3, d: 3, e: 3, f: 3, g: { g1: 3 } }; const base2 = { b: 2, c: 3, d: 2, f: { f1: 2, } }; const settings = { a: 1, b: 1, g: 1, }; const base1Node = await ConfigNode.create(base1); const base2Node = await ConfigNode.create(base2, base1Node); const node = await ConfigNode.create(settings, [base1Node, base2Node]); const result = { a: 1, b: 1, c: 3, d: 2, e: 3, f: { f1: 2, }, g: 1 } expect(node.value).toEqual(result); }); it('should handle inheriting same node mutlipe times', async () => { const base1 = { c: 3, d: 3, e: 3, f: 3, g: { g1: 3 } }; const base2 = { b: 2, c: 3, d: 2, f: { f1: 2, } }; const settings = { a: 1, b: 1, g: 1, }; let node: ConfigNode; const base1Node = await ConfigNode.create(base1, node); const base2Node = await ConfigNode.create(base2, base1Node); node = await ConfigNode.create(settings, [base1Node, base2Node]); const result = { a: 1, b: 1, c: 3, d: 2, e: 3, f: { f1: 2, }, g: 1 } expect(node.value).toEqual(result); }); it('should inheriting same node mutlipe times', async () => { const base1 = { c: 3, d: 3, e: 3, f: 3, g: { g1: 3 } }; const base2 = { b: 2, c: 3, d: 2, f: { f1: 2, } }; const settings = { a: 1, b: 1, g: 1, }; let node: ConfigNode; const base1Node = await ConfigNode.create(base1, node); const base2Node = await ConfigNode.create(base2, base1Node); node = await ConfigNode.create(settings, [base1Node, base2Node]); const result = { a: 1, b: 1, c: 3, d: 2, e: 3, f: { f1: 2, }, g: 1 } expect(node.value).toEqual(result); }); it('should handle assigning inherited nodes after construction', async () => { const base1 = { c: 3, d: 3, e: 3, f: 3, g: { g1: 3 } }; const base2 = { b: 2, c: 3, d: 2, f: { f1: 2, } }; const settings = { a: 1, b: 1, g: 1, }; const base1Node = await ConfigNode.create(base1); const base2Node = await ConfigNode.create(base2); base2Node.addInheritedNode(base1Node); const node = await ConfigNode.create(settings); node.addInheritedNode(base2Node) const result = { a: 1, b: 1, c: 3, d: 2, e: 3, f: { f1: 2, }, g: 1 } expect(node.value).toEqual(result); }); // it('should handle circular inheritence', async () => { // const base1 = { // c: 3, // d: 3, // e: 3, // f: 3, // g: { // g1: 3 // } // }; // const base2 = { // b: 2, // c: 3, // d: 2, // f: { // f1: 2, // } // }; // const settings = { // a: 1, // b: 1, // g: 1, // }; // const base1Node = await ConfigNode.create(base1); // const base2Node = await ConfigNode.create(base2); // base2Node.addInheritedNode(base1Node); // const node = await ConfigNode.create(settings); // node.addInheritedNode(base2Node) // base1Node.addInheritedNode(node); // const result = { // a: 1, // b: 1, // c: 3, // d: 2, // e: 3, // f: { // f1: 2, // }, // g: 1 // } // expect(node.value).toEqual(result); // }); });