ASRouterParent.test.js (2711B)
1 import { ASRouterParent } from "actors/ASRouterParent.sys.mjs"; 2 import { MESSAGE_TYPE_HASH as msg } from "modules/ActorConstants.mjs"; 3 4 describe("ASRouterParent", () => { 5 let asRouterParent = null; 6 let sandbox = null; 7 let handleMessage = null; 8 let tabs = null; 9 beforeEach(() => { 10 sandbox = sinon.createSandbox(); 11 handleMessage = sandbox.stub().resolves("handle-message-result"); 12 ASRouterParent.nextTabId = 1; 13 const methods = { 14 destroy: sandbox.stub(), 15 size: 1, 16 messageAll: sandbox.stub().resolves(), 17 registerActor: sandbox.stub(), 18 unregisterActor: sandbox.stub(), 19 loadingMessageHandler: Promise.resolve({ 20 handleMessage, 21 }), 22 }; 23 tabs = { 24 methods, 25 factory: sandbox.stub().returns(methods), 26 }; 27 asRouterParent = new ASRouterParent({ tabsFactory: tabs.factory }); 28 ASRouterParent.tabs = tabs.methods; 29 asRouterParent.browsingContext = { 30 embedderElement: { 31 getAttribute: () => true, 32 }, 33 }; 34 asRouterParent.tabId = ASRouterParent.nextTabId; 35 }); 36 afterEach(() => { 37 sandbox.restore(); 38 asRouterParent = null; 39 }); 40 describe("actorCreated", () => { 41 it("after ASRouterTabs is instanced", () => { 42 asRouterParent.actorCreated(); 43 assert.equal(asRouterParent.tabId, 2); 44 assert.notCalled(tabs.factory); 45 assert.calledOnce(tabs.methods.registerActor); 46 }); 47 it("before ASRouterTabs is instanced", () => { 48 ASRouterParent.tabs = null; 49 ASRouterParent.nextTabId = 0; 50 asRouterParent.actorCreated(); 51 assert.calledOnce(tabs.factory); 52 assert.isNotNull(ASRouterParent.tabs); 53 assert.equal(asRouterParent.tabId, 1); 54 }); 55 }); 56 describe("didDestroy", () => { 57 it("one still remains", () => { 58 ASRouterParent.tabs.size = 1; 59 asRouterParent.didDestroy(); 60 assert.isNotNull(ASRouterParent.tabs); 61 assert.calledOnce(ASRouterParent.tabs.unregisterActor); 62 assert.notCalled(ASRouterParent.tabs.destroy); 63 }); 64 it("none remain", () => { 65 ASRouterParent.tabs.size = 0; 66 const tabsCopy = ASRouterParent.tabs; 67 asRouterParent.didDestroy(); 68 assert.isNull(ASRouterParent.tabs); 69 assert.calledOnce(tabsCopy.unregisterActor); 70 assert.calledOnce(tabsCopy.destroy); 71 }); 72 }); 73 describe("receiveMessage", async () => { 74 it("passes call to parentProcessMessageHandler and returns the result from handler", async () => { 75 const result = await asRouterParent.receiveMessage({ 76 name: msg.BLOCK_MESSAGE_BY_ID, 77 data: { id: 1 }, 78 }); 79 assert.calledOnce(handleMessage); 80 assert.equal(result, "handle-message-result"); 81 }); 82 }); 83 });