UNPKG

@interaktiv/mibuilder-core

Version:

Core libraries to interact with MiBuilder projects.

128 lines (117 loc) 4.62 kB
"use strict"; var _testSetup2 = require("../test-setup"); var _config = require("./config"); var _configAggregator = require("./config-aggregator"); var _configFile = require("./config-file"); // Setup the test environment. const _testSetup = (0, _testSetup2.testSetup)(); // eslint-disable-next-line max-lines-per-function describe('ConfigAggregator', () => { let id; beforeEach(() => { // Testing config functionality, so restore global mocks. _testSetup.sandboxes.config.restore(); id = _testSetup.uniqid(); _testSetup.sandbox.spyOn(_configFile.ConfigFile, 'resolveRootDir').mockImplementation(isGlobal => _testSetup.rootPathRetriever(isGlobal, id)); }); afterEach(() => { delete process.env.MIBUILDER_APP_WORKSPACE; }); describe('instantiation', () => { it('creates local and global config', async () => { const aggregator = await _configAggregator.ConfigAggregator.create(); expect(aggregator.getLocalConfig()).toBeDefined(); expect(aggregator.getGlobalConfig()).toBeDefined(); }); it('converts env vars', async () => { process.env.MIBUILDER_APP_WORKSPACE = '/path'; const aggregator = await _configAggregator.ConfigAggregator.create(); expect(aggregator.getPropertyValue('app.workspace')).toBe('/path'); }); it('with no workspace does not have a local config', async () => { expect(await _configAggregator.ConfigAggregator.create()).toEqual(expect.any(_configAggregator.ConfigAggregator)); }); }); describe('initialization', () => { beforeEach(() => { _testSetup.sandbox.spyOn(_config.Config.prototype, 'read').mockImplementation(async function () { const config = this.isGlobal() ? await Promise.resolve({ app: { workspace: '/global' } }) : await Promise.resolve({ app: { workspace: '/local' } }); this.setContents(config); return config; }); }); it('local overrides global', async () => { const aggregator = await _configAggregator.ConfigAggregator.create(); expect(aggregator.getPropertyValue('app.workspace')).toEqual('/local'); }); it('env overrides local and global', async () => { process.env.MIBUILDER_APP_WORKSPACE = '/env-path'; const aggregator = await _configAggregator.ConfigAggregator.create(); expect(await aggregator.getPropertyValue('app.workspace')).toEqual('/env-path'); }); }); describe('locations', () => { beforeEach(() => { _testSetup.sandbox.spyOn(_config.Config.prototype, 'read').mockImplementation(async function () { const config = await Promise.resolve({}); this.setContents(config); return config; }); }); it('local', async () => { _testSetup.sandbox.spyOn(_config.Config.prototype, 'read').mockImplementation(async function () { const config = this.isGlobal() ? await Promise.resolve({ app: { workspace: '/global' } }) : await Promise.resolve({ app: { workspace: '/local' } }); this.setContents(config); return config; }); const aggregator = await _configAggregator.ConfigAggregator.create(); expect(aggregator.getLocation('app.workspace')).toBe('Local'); }); it('global', async () => { _testSetup.sandbox.spyOn(_config.Config.prototype, 'read').mockImplementation(async function () { const config = this.isGlobal() ? await Promise.resolve({ app: { workspace: '/global' } }) : await Promise.resolve({}); this.setContents(config); return this.getContents(); }); const aggregator = await _configAggregator.ConfigAggregator.create(); expect(aggregator.getLocation('app.workspace')).toBe('Global'); }); it('env', async () => { process.env.MIBUILDER_APP_WORKSPACE = '/env-path'; const aggregator = await _configAggregator.ConfigAggregator.create(); expect(aggregator.getLocation('app.workspace')).toBe('Environment'); }); it('configInfo', async () => { process.env.MIBUILDER_APP_WORKSPACE = '/env-path-2'; const aggregator = await _configAggregator.ConfigAggregator.create(); const info = aggregator.getConfigInfo().find(({ key }) => key === 'app.workspace'); expect(info).toBeDefined(); expect(info).toEqual(expect.objectContaining({ key: 'app.workspace', value: '/env-path-2', location: 'Environment' })); }); }); });