UNPKG

state-switch

Version:

State Switch is a Change Monitor/Guarder for Async Actions.

72 lines 3.03 kB
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tstest_1 = require("tstest"); const events_1 = require("events"); const service_ctl_fsm_js_1 = require("./service-ctl-fsm.js"); (0, tstest_1.test)('ServiceCtlFsm smoke testing', async (t) => { const sandbox = tstest_1.sinon.createSandbox(); const onStartSpy = sandbox.spy(); const onStopSpy = sandbox.spy(); class ServiceCtlFsmImpl extends service_ctl_fsm_js_1.ServiceCtlFsm { async onStart() { onStartSpy(); } async onStop() { onStopSpy(); } } const ctl = new ServiceCtlFsmImpl(); await ctl.start(); t.ok(onStartSpy.calledOnce, 'should call onStart()'); t.ok(onStopSpy.notCalled, 'should not call onStop()'); await ctl.stop(); t.ok(onStopSpy.calledOnce, 'should call onStop()'); t.throws(() => ctl.reset(), 'should reject when calling reset() with an inactive service'); await ctl.start(); sandbox.resetHistory(); await t.resolves(() => ctl.reset(), 'should be able to reset with an active service'); t.ok(onStartSpy.calledOnce, 'should call onStart() via reset()'); t.ok(onStopSpy.calledOnce, 'should call onStop() via reset()'); }); (0, tstest_1.test)('serviceCtlFsmMixin()', async (t) => { const sandbox = tstest_1.sinon.createSandbox(); const childOnStartSpy = sandbox.spy(); const childOnStopSpy = sandbox.spy(); const parentStartSpy = sandbox.spy(); const parentStopSpy = sandbox.spy(); class ServiceableBase extends events_1.EventEmitter { start() { parentStartSpy(); } stop() { parentStopSpy(); } } const mixinBase = (0, service_ctl_fsm_js_1.serviceCtlFsmMixin)()(ServiceableBase); class ServiceCtlFsmImpl extends mixinBase { async onStart() { childOnStartSpy(); } async onStop() { childOnStopSpy(); } } const ctl = new ServiceCtlFsmImpl(); await ctl.start(); t.ok(childOnStartSpy.calledOnce, 'should call onStart()'); t.ok(parentStartSpy.calledOnce, 'should call parent start()'); t.ok(childOnStopSpy.notCalled, 'should not call onStop()'); await ctl.stop(); t.ok(childOnStopSpy.calledOnce, 'should call onStop()'); t.ok(parentStopSpy.calledOnce, 'should call parent stop()'); t.throws(() => ctl.reset(), 'should reject when calling reset() with an inactive service'); await ctl.start(); sandbox.resetHistory(); await t.resolves(() => ctl.reset(), 'should be able to reset with an active service'); t.ok(childOnStartSpy.calledOnce, 'should call onStart() via reset()'); t.ok(childOnStopSpy.calledOnce, 'should call onStop() via reset()'); t.ok(parentStartSpy.calledOnce, 'should call parent start() via reset()'); t.ok(parentStopSpy.calledOnce, 'should call parent stop() via reset()'); }); //# sourceMappingURL=service-ctl-fsm.spec.js.map