import { CommonCfg } from './CommonCfg'; import { RootCfg } from './RootCfg'; test('RootCfg: constructor works', () => { const rc = new RootCfg({ logLevels: ['ohno', 'huh', 'lolwut'], }); expect(rc).toMatchInlineSnapshot(` RootCfg { "logLevels": Array [ "ohno", "huh", "lolwut", ], "name": "root", "parent": undefined, } `); }); test('RootCfg: will choose the most permissive logLevel if none specified', () => { const rc = new RootCfg({ logLevels: ['ohno', 'huh', 'lolwut'], }); expect(rc.getConfig().logLevel).toEqual('lolwut'); }); test('CommonCfg: a child will inherit logLevel', () => { const rc = new RootCfg({ logLevels: ['ohno', 'huh', 'lolwut'], }); const cc = rc.getChild('son of rc'); expect(cc.getConfig().logLevel).toEqual('lolwut'); }); test('CommonCfg: a child can override logLevel', () => { const rc = new RootCfg({ logLevels: ['ohno', 'huh', 'lolwut'], }); const cc = rc.getChild('son of rc', { logLevel: 'huh', }); expect(cc.getConfig().logLevel).toEqual('huh'); }); test('CommonCfg: a child of a child will inherit logLevel from root', () => { const rc = new RootCfg({ logLevels: ['ohno', 'huh', 'lolwut'], }); const cc = rc.getChild('son of rc'); const ccc = cc.getChild('son of cc'); expect(ccc.getConfig().logLevel).toEqual('lolwut'); }); test('CommonCfg: a child will inherit logLevels', () => { const rc = new RootCfg({ logLevels: ['ohno', 'huh', 'lolwut'], }); const cc = rc.getChild('son of rc'); expect(cc.getConfig().logLevels).toEqual(['ohno', 'huh', 'lolwut']); }); test('CommonCfg (context): a child will inherit context', () => { const rc = new RootCfg({ logLevels: ['ohno', 'huh', 'lolwut'], context: { isFrom: 'rootcfg', }, }); const cc = rc.getChild('son of rc'); expect(cc.getConfig().context).toEqual({ isFrom: 'rootcfg', }); }); test('CommonCfg (context): a cfg and replace its own context', () => { const rc = new RootCfg({ logLevels: ['ohno', 'huh', 'lolwut'], context: { isFrom: 'rootcfg', }, }); rc.setContext({ somethingNew: 'is here', }) expect(rc.getConfig().context).toEqual({ somethingNew: 'is here', }); }); test('CommonCfg (context): a cfg and add/modify its own context', () => { const rc = new RootCfg({ logLevels: ['ohno', 'huh', 'lolwut'], context: { isFrom: 'rootcfg', }, }); rc.addContext({ somethingNew: 'is here', }) expect(rc.getConfig().context).toEqual({ isFrom: 'rootcfg', somethingNew: 'is here', }); }); test('CommonCfg (context): a cfg and add/modify its own context even if it is undefined', () => { const rc = new RootCfg({ logLevels: ['ohno', 'huh', 'lolwut'], }); rc.addContext({ somethingNew: 'is here', }) expect(rc.getConfig().context).toEqual({ somethingNew: 'is here', }); }); test('CommonCfg: can replace its own config', () => { const rc = new CommonCfg('something', { logLevel: 'something', debugVar: 'abc', }); rc.setConfig({ logLevel: 'somethingElse', }, true); expect(rc.getLogLevel()).toEqual('somethingElse'); expect(rc.getDebugVar()).toEqual(undefined); });