UNPKG

@interaktiv/mibuilder-core

Version:

Core libraries to interact with MiBuilder projects.

155 lines (132 loc) 6.37 kB
"use strict"; var _json = require("@interaktiv/json"); var _mibuilderError = require("../mibuilder-error"); var _testSetup2 = require("../test-setup"); var _config = require("./config"); var _configFile = require("./config-file"); // Setup the test environment. const _testSetup = (0, _testSetup2.testSetup)(); const configFileContents = { app: { workspace: 'configTest_app_workspace' }, template: { sshPrivateKeyPath: 'configTest_template_sshPrivateKeyPath', sshPublicKeyPath: 'configTest_template_sshPublicKeyPath' }, api: { getContentEndpoint: 'configTest_api_getContentEndPoint' }, android: { googlePlayJsonKeyPath: 'configTest_android_googlePlayJsonKeyPath' }, match: { gitUrl: 'configTest_match_gitUrl' }, flint: { gitUrl: 'configTest_flint_gitUrl' } }; // eslint-disable-next-line max-lines-per-function describe('Config', () => { let id; beforeEach(() => { // Testing config functionality, so restore global stubs. _testSetup.sandboxes.config.restore(); id = _testSetup.uniqid(); _testSetup.sandbox.spyOn(_configFile.ConfigFile, 'resolveRootDir').mockImplementation(isGlobal => _testSetup.rootPathRetriever(isGlobal, id)); }); describe('instantiation', () => { it('should using global config', async () => { const config = await _config.Config.create(_config.Config.getDefaultOptions(true)); expect(config.getPath()).toEqual(expect.not.stringContaining(await _testSetup.localPathRetriever(id))); expect(config.getPath()).toEqual(expect.stringContaining('.mibuilder')); expect(config.getPath()).toEqual(expect.stringContaining('mibuilder-config.json')); }); it('should not using global if should use local', async () => { const config = await _config.Config.create(_config.Config.getDefaultOptions(false)); expect(config.getPath()).toEqual(expect.stringContaining(await _testSetup.localPathRetriever(id))); expect(config.getPath()).toEqual(expect.stringContaining('.mibuilder')); expect(config.getPath()).toEqual(expect.stringContaining('mibuilder-config.json')); }); }); describe('read', () => { it('should add content of the config file from this.path to this.contents', async () => { const config = await _config.Config.create(_config.Config.getDefaultOptions(true)); _testSetup.sandbox.spyOn(_config.Config.prototype, 'read').mockImplementation(async function () { let cfg; if (this.getPath() === config.getPath()) { cfg = await Promise.resolve((0, _json.cloneJson)(configFileContents)); } else { cfg = await Promise.resolve({}); } this.setContents(cfg); return cfg; }); const content = await config.read(); expect(content.app.workspace).toEqual(configFileContents.app.workspace); expect(content.match.gitUrl).toEqual(configFileContents.match.gitUrl); expect(config.toObject()).toEqual(configFileContents); }); }); describe('set', () => { it('should call Config.write with updated file contents', async () => { _testSetup.sandbox.spyOn(_config.Config.prototype, 'read').mockImplementation(async function () { const cfg = await Promise.resolve((0, _json.cloneJson)(configFileContents)); this.setContents(cfg); return cfg; }); const writeMock = _testSetup.sandbox.spyOn(_config.Config.prototype, 'write'); const expectedFileContents = (0, _json.cloneJson)(configFileContents); const newAppWorkspace = 'updated_val'; expectedFileContents.app.workspace = newAppWorkspace; await _config.Config.update(false, 'app.workspace', newAppWorkspace); expect(writeMock).toHaveBeenCalledTimes(2); expect(writeMock).toHaveBeenCalledWith(expectedFileContents); }); it('should call Config.write with deleted file contents', async () => { const expectedFileContents = (0, _json.cloneJson)(configFileContents); const newAppWorkspace = 'updated_val'; expectedFileContents.app.workspace = newAppWorkspace; await _config.Config.update(false, 'app.workspace', newAppWorkspace); _testSetup.sandbox.spyOn(_config.Config.prototype, 'read').mockImplementation(async function () { const cfg = await Promise.resolve((0, _json.cloneJson)(configFileContents)); this.setContents(cfg); return cfg; }); const writeMock = _testSetup.sandbox.spyOn(_config.Config.prototype, 'write'); await _config.Config.update(false, 'app.workspace'); expect(writeMock).toHaveBeenCalledTimes(2); const lastExpectedFileContents = (0, _json.cloneJson)(configFileContents); lastExpectedFileContents.app.workspace = undefined; expect(writeMock).toHaveBeenNthCalledWith(1, lastExpectedFileContents); }); it('should throw MiBuilderError with name UnknownConfigKey for unknown key', async () => { const config = await _config.Config.create(_config.Config.getDefaultOptions(true)); expect.assertions(2); try { config.set('foo', 'bar'); } catch (err) { // eslint-disable-next-line jest/no-try-expect, jest/no-conditional-expect expect(err).toEqual(expect.any(_mibuilderError.MiBuilderError)); // eslint-disable-next-line jest/no-try-expect, jest/no-conditional-expect expect(err.name).toBe('UnknownConfigKey'); } }); it('should throw MiBuilderError with name InvalidConfigValue for invalid value', async () => { const config = await _config.Config.create(_config.Config.getDefaultOptions(true)); expect.assertions(2); try { config.set('app.workspace', '/my-invalid-path><!'); } catch (err) { // eslint-disable-next-line jest/no-try-expect, jest/no-conditional-expect expect(err).toEqual(expect.any(_mibuilderError.MiBuilderError)); // eslint-disable-next-line jest/no-try-expect, jest/no-conditional-expect expect(err.name).toBe('InvalidConfigValue'); } }); it('should pass PropertyInput validation if valid value given', async () => { const config = await _config.Config.create(_config.Config.getDefaultOptions(true)); const expected = '/my-app-workspace'; config.set('app.workspace', expected); expect(config.getInGroup('workspace', 'app')).toBe(expected); }); }); });