UNPKG

wechaty-redux

Version:

Wechaty Redux Plugin Powered By Ducks

133 lines 8.59 kB
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const tstest_1 = require("tstest"); const rxjs_1 = require("rxjs"); const wechaty_puppet_mock_1 = require("wechaty-puppet-mock"); const wechaty_1 = require("wechaty"); const duck = __importStar(require("../duck/mod.js")); const register_puppet_js_1 = require("./register-puppet.js"); (0, tstest_1.test)('increasePuppetReferenceInRegistry() & decreasePuppetReferenceInRegistry()', async (t) => { const puppet = new wechaty_puppet_mock_1.PuppetMock(); const registry = new Map(); t.equal(registry.size, 0, 'should be 0 puppet in the registry at initial'); t.equal(register_puppet_js_1.puppetRef.size, 0, 'should be 0 puppet in ref at initial'); let counter; counter = (0, register_puppet_js_1.increasePuppetReferenceInRegistry)(registry)(puppet); t.equal(counter, 1, 'should be 1 ref after remember once'); t.equal(registry.size, 1, 'should be 1 puppet in the registry after remember once'); t.equal(register_puppet_js_1.puppetRef.size, 1, 'should be 1 puppet in ref after remember once'); t.equal(registry.get(puppet.id), puppet, 'should get puppet from the registry after remember once'); t.equal(register_puppet_js_1.puppetRef.get(puppet.id), 1, 'should get 1 puppet ref after remember once'); counter = (0, register_puppet_js_1.increasePuppetReferenceInRegistry)(registry)(puppet); t.equal(counter, 2, 'should be 2 ref after remember twice'); t.equal(registry.size, 1, 'should be 1 puppet in the registry after remember twice'); t.equal(register_puppet_js_1.puppetRef.size, 1, 'should be 1 puppet in ref after remember twice'); t.equal(registry.get(puppet.id), puppet, 'should get puppet from the registry after remember twice'); t.equal(register_puppet_js_1.puppetRef.get(puppet.id), 2, 'should get 2 puppet ref after remember twice'); counter = (0, register_puppet_js_1.decreasePuppetReferenceInRegistry)(registry)(puppet); t.equal(counter, 1, 'should be 1 ref after forget once'); t.equal(registry.size, 1, 'should be 1 puppet in the registry after forget once'); t.equal(register_puppet_js_1.puppetRef.size, 1, 'should be 1 puppet in ref after forget once'); t.equal(registry.get(puppet.id), puppet, 'should get puppet from the registry after forget once'); t.equal(register_puppet_js_1.puppetRef.get(puppet.id), 1, 'should get 1 puppet ref after forget once'); counter = (0, register_puppet_js_1.decreasePuppetReferenceInRegistry)(registry)(puppet); t.equal(counter, 0, 'should be 1 ref after forget twice'); t.equal(registry.size, 0, 'should be 1 puppet in the registry after forget twice'); t.equal(register_puppet_js_1.puppetRef.size, 0, 'should be 1 puppet in ref after forget twice'); }); (0, tstest_1.test)('registerPuppetInRegistry()', async (t) => { const spy = tstest_1.sinon.spy(); const puppet = new wechaty_puppet_mock_1.PuppetMock(); const registry = new Map(); const $ = new rxjs_1.Subject().pipe((0, register_puppet_js_1.registerPuppetInRegistry)(registry)(puppet)); t.equal(registry.size, 0, 'should be 0 puppet in the registry at initial'); t.equal(register_puppet_js_1.puppetRef.size, 0, 'should be 0 puppet in ref at initial'); const sub1 = $.subscribe(spy); t.equal(registry.size, 1, 'should be 1 puppet in the registry after subscribe once'); t.equal(register_puppet_js_1.puppetRef.size, 1, 'should be 1 puppet in ref after subscribe once'); t.equal(registry.get(puppet.id), puppet, 'should get puppet from the registry after subscribe once'); t.equal(register_puppet_js_1.puppetRef.get(puppet.id), 1, 'should get 1 puppet ref after subscribe once'); const sub2 = $.subscribe(spy); t.equal(registry.size, 1, 'should be 1 puppet in the registry after subscribe twice'); t.equal(register_puppet_js_1.puppetRef.size, 1, 'should be 1 puppet in ref after subscribe twice'); t.equal(registry.get(puppet.id), puppet, 'should get puppet from the registry after subscribe twice'); t.equal(register_puppet_js_1.puppetRef.get(puppet.id), 2, 'should get 1 puppet ref after subscribe twice'); sub1.unsubscribe(); t.equal(registry.size, 1, 'should be 1 puppet in the registry after unsubscribe once'); t.equal(register_puppet_js_1.puppetRef.size, 1, 'should be 1 puppet in ref after unsubscribe once'); t.equal(registry.get(puppet.id), puppet, 'should get puppet from the registry after unsubscribe once'); t.equal(register_puppet_js_1.puppetRef.get(puppet.id), 1, 'should get 1 puppet ref after unsubscribe once'); sub2.unsubscribe(); t.equal(registry.size, 0, 'should be 1 puppet in the registry after unsubscribe twice'); t.equal(register_puppet_js_1.puppetRef.size, 0, 'should be 1 puppet in ref after unsubscribe twice'); }); (0, tstest_1.test)('registerPuppetInRegistry() with duck.actions.registerPuppet()', async (t) => { const spy = tstest_1.sinon.spy(); const puppet = new wechaty_puppet_mock_1.PuppetMock(); const pool = new Map(); const $ = new rxjs_1.Subject().pipe((0, register_puppet_js_1.registerPuppetInRegistry)(pool)(puppet)); t.ok(spy.notCalled, 'should be a clean spy'); const sub1 = $.subscribe(spy); t.ok(spy.calledOnce, 'should receive one action'); t.same(spy.args[0][0], duck.actions.REGISTER_PUPPET_COMMAND(puppet.id), 'should emit puppet register action after subscribe once'); const sub2 = $.subscribe(spy); t.ok(spy.calledOnce, 'should receive no more actions after the 2+ subscription'); sub1.unsubscribe(); sub2.unsubscribe(); t.ok(spy.calledOnce, 'should receive no more actions after unsubscribe'); $.subscribe(spy); t.ok(spy.calledTwice, 'should receive another actions after re-subscribe from the fresh'); }); (0, tstest_1.test)('registerPuppetInRegistry() with store & wechaty', async (t) => { const spy = tstest_1.sinon.spy(); const puppet = new wechaty_puppet_mock_1.PuppetMock(); const pool = new Map(); const wechaty = wechaty_1.WechatyBuilder.build({ puppet }); const store = { dispatch: spy, }; const $ = new rxjs_1.Subject().pipe((0, register_puppet_js_1.registerPuppetInRegistry)(pool)(puppet, { store, wechaty, })); t.ok(spy.notCalled, 'should be a clean storeSpy'); const sub1 = $.subscribe(store.dispatch); t.ok(spy.calledTwice, 'should receive two action'); t.same(spy.args[0][0], duck.actions.REGISTER_PUPPET_COMMAND(puppet.id), 'should emit puppet register action after subscribe once'); t.same(spy.args[1][0], duck.actions.BIND_WECHATY_PUPPET_COMMAND({ puppetId: puppet.id, wechatyId: wechaty.id }), 'should emit bind wechaty puppet action after subscribe once'); spy.resetHistory(); spy.resetHistory(); const sub2 = $.subscribe(spy); t.ok(spy.notCalled, 'should receive no more actions after the 2+ subscription'); sub1.unsubscribe(); t.ok(spy.notCalled, 'should receive no more actions after 1 unsubscribe'); sub2.unsubscribe(); t.equal(spy.callCount, 2, 'should receive two action after unsubscribe all'); t.same(spy.args[0][0], duck.actions.UNBIND_WECHATY_PUPPET_COMMAND({ puppetId: puppet.id, wechatyId: wechaty.id }), 'should emit unbind wechaty puppet action after unsubscribed all'); t.same(spy.args[1][0], duck.actions.DEREGISTER_PUPPET_COMMAND(puppet.id), 'should emit deregister puppet action after subscribe all'); }); //# sourceMappingURL=register-puppet.spec.js.map