import { ConfigNode, ProfilesConfigNode } from '../../src/service'; describe(`ProfileConfigNode`, () => { it('should merge simple profile values defined in single object', async () => { const settings = { profile: 'p1', profiles: { p1: { a: 1, b: 1, }, p2: { a: 2, b: 2 } }, }; const node = await ProfilesConfigNode.create(settings); let results = { a: 1, b: 1, profile: 'p1', } expect(node.value).toEqual(results); await node.setProfile('p2'); results = { a: 2, b: 2, profile: 'p2', } expect(node.value).toEqual(results); }); it('should return empty object value when no profile set', async () => { const settings = { profiles: { p1: { a: 1, b: 1, }, p2: { a: 2, b: 2 } }, }; const node = await ProfilesConfigNode.create(settings); const results = {}; expect(node.value).toEqual(results); }); it('should use appropriate profile values from inherited nodes and use entire profile without merging with other profiles', async () => { const base0 = { profiles: { p0: { a: 0, b: 0, c: 0 }, p2: { a: 2, b: 2, } } } const base = { profiles: { p0: { a: 10, b: 10, }, } } const settings = { profile: 'p0', profiles: { p4: { a: 4, b: 4, }, p5: { a: 5, b: 5, }, } }; const base0Node = await ProfilesConfigNode.create(base0); const baseNode = await ProfilesConfigNode.create(base, base0Node); const node = await ProfilesConfigNode.create(settings, baseNode); let results = { profile: 'p0', a: 10, b: 10, } expect(node.value).toEqual(results); await node.setProfile('p2'); results = { profile: 'p2', a: 2, b: 2, } expect(node.value).toEqual(results); }); it('should merge profile values with values in root', async () => { const base0 = { profiles: { p0: { a: 0, b: 0, c: 0 }, p2: { a: 2, b: 2, } } } const base = { profiles: { p0: { a: 10, b: 10, }, } } const settings = { profile: 'p0', a: 'settings', x: 'settings', profiles: { p4: { a: 4, b: 4, }, p5: { a: 5, b: 5, }, } }; const base0Node = await ProfilesConfigNode.create(base0); const baseNode = await ProfilesConfigNode.create(base, base0Node); const node = await ProfilesConfigNode.create(settings, baseNode); let results = { profile: 'p0', a: 'settings', b: 10, x: 'settings' } expect(node.value).toEqual(results); await node.setProfile('p2'); results = { profile: 'p2', a: 'settings', b: 2, x: 'settings' } expect(node.value).toEqual(results); }); it('should handle __extends to reference another profile to inherit settings from', async () => { const base0 = { profiles: { p0: { a: 0, b: 0, c: 0 }, p2: { a: 2, b: 2, } } } const base = { profiles: { p0: { a: 10, b: 10, }, } } const settings = { profile: 'p5', a: 'settings', x: 'settings', profiles: { p3: { x: 1, c: 44, }, p4: { '__extends': 'p3', a: 4, b: 4, c: 1, d: 1, }, p5: { '__extends': 'p4', a: 5, b: 5, }, } }; const base0Node = await ProfilesConfigNode.create(base0); const baseNode = await ProfilesConfigNode.create(base, base0Node); const node = await ProfilesConfigNode.create(settings, baseNode); const results = { a: 'settings', b: 5, c: 1, d: 1, profile: 'p5', x: 'settings' } expect(node.value).toEqual(results); }); it('should return value for profile name passed to getValueForProfile method and not defined on config node', async () => { const base0 = { profiles: { p0: { a: 0, b: 0, c: 0 }, p2: { a: 2, b: 2, } } } const base = { profiles: { p0: { a: 10, b: 10, }, } } const settings = { a: 'settings', x: 'settings', profiles: { p3: { x: 1, c: 44, }, p4: { '__extends': 'p3', a: 4, b: 4, c: 1, d: 1, }, p5: { '__extends': 'p4', a: 5, b: 5, }, } }; const base0Node = await ProfilesConfigNode.create(base0); const baseNode = await ProfilesConfigNode.create(base, base0Node); const node = await ProfilesConfigNode.create(settings, baseNode); const results = { a: 'settings', b: 5, c: 1, d: 1, x: 'settings' } expect(node.getValueForProfile('p5')).toEqual(results); }); it('should retrieve individual profile node with proper values from getProfileNode method', async () => { const base0 = { profiles: { p0: { a: 0, b: 0, c: 0 }, p2: { a: 2, b: 2, } } } const base = { profiles: { p0: { a: 10, b: 10, }, } } const settings = { profile: 'p5', a: 'settings', x: 'settings', profiles: { p3: { x: 1, c: 44, }, p4: { '__extends': 'p3', a: 4, b: 4, c: 1, d: 1, }, p5: { '__extends': 'p4', a: 5, b: 5, }, } }; const base0Node = await ProfilesConfigNode.create(base0); const baseNode = await ProfilesConfigNode.create(base, base0Node); const node = await ProfilesConfigNode.create(settings, baseNode); const p5Results = { a: 5, b: 5, c: 1, d: 1, x: 1 } const p5Node = node.getProfileNode('p5') expect(p5Node.value).toEqual(p5Results); }); });