UNPKG

state-switch

Version:

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

110 lines 5.36 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 state_switch_js_1 = require("./state-switch.js"); (0, tstest_1.test)('active()', async (t) => { const ss = new state_switch_js_1.StateSwitch(); t.ok(ss.inactive(), 'default is not active'); ss.active('pending'); t.equal(ss.active(), 'pending', 'should be state pending'); ss.active(true); t.equal(ss.active(), true, 'should be active `true`'); t.notOk(ss.inactive(), 'should not inactive'); ss.inactive(true); t.notOk(ss.active(), 'should not active after call inactive(true)'); }); (0, tstest_1.test)('inactive()', async (t) => { const ss = new state_switch_js_1.StateSwitch(); t.ok(ss.inactive(), 'default is inactive'); t.equal(ss.inactive(), true, 'should in state true'); ss.inactive('pending'); t.equal(ss.inactive(), 'pending', 'should be state pending'); ss.inactive(true); t.equal(ss.inactive(), true, 'should be state true'); t.notOk(ss.active(), 'should not active'); ss.active(true); t.notOk(ss.inactive(), 'should not inactive after called active()'); }); (0, tstest_1.test)('pending', async (t) => { const ss = new state_switch_js_1.StateSwitch(); t.notOk(ss.pending(), 'default is not pending'); ss.active('pending'); t.ok(ss.pending(), 'should in pending state'); ss.active(true); t.notOk(ss.pending(), 'should not in pending state'); ss.inactive('pending'); t.ok(ss.pending(), 'should in pending state'); }); (0, tstest_1.test)('name', async (t) => { const CLIENT_NAME = 'StateSwitchTest'; const ss = new state_switch_js_1.StateSwitch(CLIENT_NAME); t.equal(ss.name(), CLIENT_NAME, 'should get the same client name as init'); }); (0, tstest_1.test)('version()', async (t) => { const ss = new state_switch_js_1.StateSwitch(); t.ok(ss.version(), 'should get version'); }); (0, tstest_1.test)('stable()', async (t) => { const spy = tstest_1.sinon.spy(); const ss = new state_switch_js_1.StateSwitch(); ss.stable('inactive').then(() => spy('inactive')).catch(() => t.fail('rejection')); await new Promise(resolve => setImmediate(resolve)); t.equal(spy.callCount, 1, 'should be stable for inactive at the initial state'); spy.resetHistory(); await t.rejects(() => ss.stable('active', true), 'should catch the exception when noCross=true'); spy.resetHistory(); const future = t.resolves(() => ss.stable('active'), 'should stable(active)'); ss.active(true); await future; spy.resetHistory(); await t.resolves(() => ss.stable('active'), 'should stable(active) when already on'); spy.resetHistory(); ss.stable('inactive').then(() => spy('inactive')).catch(() => t.fail('rejection')); await new Promise(resolve => setImmediate(resolve)); t.equal(spy.callCount, 0, 'should not stable(inactive) when its on'); ss.inactive(true); await new Promise(resolve => setImmediate(resolve)); t.equal(spy.callCount, 1, 'should stable(inactive) after call inactive(true)'); }); (0, tstest_1.test)('stable() without default arg for waiting current state', async (t) => { const spy = tstest_1.sinon.spy(); const ss = new state_switch_js_1.StateSwitch(); ss.inactive('pending'); ss.stable().then(() => spy('inactive')).catch(() => t.fail('rejection')); ss.inactive(true); await new Promise(resolve => setImmediate(resolve)); t.equal(spy.callCount, 1, 'should be stable() for inactive(true)'); spy.resetHistory(); ss.active('pending'); ss.stable().then(() => spy('active')).catch(() => t.fail('rejection')); ss.active(true); await new Promise(resolve => setImmediate(resolve)); t.equal(spy.callCount, 1, 'should be stable() for active(true)'); }); (0, tstest_1.test)('active/inactive events emitting', async (t) => { const spyActive = tstest_1.sinon.spy(); const spyInactive = tstest_1.sinon.spy(); const ss = new state_switch_js_1.StateSwitch(); ss.addListener('active', spyActive); ss.addListener('inactive', spyInactive); t.ok(spyActive.notCalled, 'spyActive is not called'); t.ok(spyInactive.notCalled, 'spyInactive is not called'); ss.active('pending'); t.ok(spyActive.calledOnce, 'spyActive is called once after on(pending)'); t.same(spyActive.args[0], ['pending'], 'spyActive should be called with `pending` arg'); t.ok(spyInactive.notCalled, 'spyInactive is not called'); ss.active(true); t.ok(spyActive.calledTwice, 'spyActive is called once after active(pending)'); t.same(spyActive.args[1], [true], 'spyActive should be called with `true` arg'); t.ok(spyInactive.notCalled, 'spyInactive is not called'); ss.inactive('pending'); await Promise.resolve(); t.ok(spyInactive.calledOnce, 'spyInactive is called once after inactive(pending)'); t.same(spyInactive.args[0], ['pending'], 'spyInactive should be called with `pending` arg'); ss.inactive(true); t.ok(spyInactive.calledTwice, 'spyInactive is called twice after inactive(true)'); t.same(spyInactive.args[1], [true], 'spyInactive should be called with `true` arg'); t.ok(spyActive.calledTwice, 'spyActive called twice at last'); }); //# sourceMappingURL=state-switch.spec.js.map