UNPKG

hyperflow

Version:

A javascript state flow and mutation management toolkit & library for developing universal app.

172 lines (160 loc) 6.03 kB
/** * Copyright 2015-present Tuan Le. * * Licensed under the MIT License. * You may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://opensource.org/licenses/mit-license.html * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * *------------------------------------------------------------------------ * * @module AgentFactory * @description - A generic test agent factory module. * * @author Tuan Le (tuan.t.lei@gmail.com) * * @flow */ 'use strict'; // eslint-disable-line import CommonElement from '../elements/common-element'; /* load Composer */ import Composer from '../composer'; /* factory Ids */ import { AGENT_FACTORY_CODE, FIXTURE_FACTORY_CODE } from './factory-code'; const Hf = CommonElement(); export default Composer({ state: { name: { value: `unnamed`, required: true }, fId: { computable: { contexts: [ `name` ], compute () { return `${AGENT_FACTORY_CODE}-${this.name}`; } } } }, AgentFactory () { /* ----- Private Variables ------------- */ let _testRunner; let _fixtures = []; /* ----- Public Functions -------------- */ /** * @description - Initialize service. * * @method $init * @return void */ this.$init = function () { Hf.log(`warn0`, `AgentFactory.$init - Method is not implemented by default.`); }; /** * @description - Get agent test runner. * * @method getTestRunner * @return {object} */ this.getTestRunner = function () { const agent = this; if (Hf.DEVELOPMENT) { if (!Hf.isObject(_testRunner)) { Hf.log(`error`, `AgentFactory.getTestRunner - Test agent:${agent.name} is not registered with a test runner.`); } } return _testRunner; }; /** * @description - Register testable domain, interface, service, or store fixtures. * * @method register * @param {object} definition - Test agent registration definition for domain, interface, service, or store fixtures. * @return {object} */ this.register = function (definition) { const agent = this; if (!Hf.isSchema({ testRunner: `object`, fixtures: [ `object` ] }).of(definition)) { Hf.log(`error`, `AgentFactory.register - Input definition is invalid.`); } else { const { testRunner, fixtures } = definition; if (!fixtures.every((fixture) => { return Hf.isSchema({ fId: `string`, name: `string`, registerTestRunner: `function`, hasStarted: `function`, start: `function`, restart: `function`, setup: `function`, teardown: `function`, observe: `function`, activateIncomingStream: `function`, activateOutgoingStream: `function`, deactivateIncomingStream: `function`, deactivateOutgoingStream: `function` }).of(fixture) && fixture.fId.substr(0, FIXTURE_FACTORY_CODE.length) === FIXTURE_FACTORY_CODE; })) { Hf.log(`error`, `AgentFactory.register - Input test fixtures are invalid.`); } else { _testRunner = testRunner; _fixtures = _fixtures.concat(fixtures.filter((fixture) => { if (_fixtures.some((_fixture) => _fixture.name === fixture.name)) { Hf.log(`warn1`, `AgentFactory.register - Test fixture:${fixture.name} is already registered.`); } Hf.log(`info1`, `Test agent:${agent.name} registered test fixture:${fixture.name}.`); return true; })); _fixtures.forEach((fixture) => fixture.registerTestRunner(testRunner)); } } return agent; }; /** * @description - Run the test agent. * * @method run * @param {object} option * @return void */ this.run = function (option = {}) { const agent = this; option = Hf.isObject(option) ? option : {}; if (!Hf.isNonEmptyArray(_fixtures)) { Hf.log(`error`, `AgentFactory.run - Test agent:${agent.name} is not registered with a test fixture.`); } else { _fixtures.forEach((fixture) => { if (!fixture.hasStarted()) { fixture.start(() => { Hf.log(`info1`, `Running test fixture:${fixture.name}...`); }, option); } else { fixture.restart(() => { Hf.log(`info1`, `Rerunning test fixture:${fixture.name}...`); }, option); Hf.log(`warn1`, `AgentFactory.run - Test fixture:${fixture.name} is already running. Restarting...`); } }); } }; } });