NewTabInit.test.js (2452B)
1 import { actionCreators as ac, actionTypes as at } from "common/Actions.mjs"; 2 import { NewTabInit } from "lib/NewTabInit.sys.mjs"; 3 4 describe("NewTabInit", () => { 5 let instance; 6 let store; 7 let STATE; 8 const requestFromTab = portID => 9 instance.onAction( 10 ac.AlsoToMain({ type: at.NEW_TAB_STATE_REQUEST }, portID) 11 ); 12 beforeEach(() => { 13 STATE = {}; 14 store = { getState: sinon.stub().returns(STATE), dispatch: sinon.stub() }; 15 instance = new NewTabInit(); 16 instance.store = store; 17 }); 18 it("should reply with a copy of the state immediately", () => { 19 requestFromTab(123); 20 21 const resp = ac.AlsoToOneContent( 22 { type: at.NEW_TAB_INITIAL_STATE, data: STATE }, 23 123 24 ); 25 assert.calledWith(store.dispatch, resp); 26 }); 27 describe("early / simulated new tabs", () => { 28 const simulateTabInit = portID => 29 instance.onAction({ 30 type: at.NEW_TAB_INIT, 31 data: { portID, simulated: true }, 32 }); 33 beforeEach(() => { 34 simulateTabInit("foo"); 35 }); 36 it("should dispatch if not replied yet", () => { 37 requestFromTab("foo"); 38 39 assert.calledWith( 40 store.dispatch, 41 ac.AlsoToOneContent( 42 { type: at.NEW_TAB_INITIAL_STATE, data: STATE }, 43 "foo" 44 ) 45 ); 46 }); 47 it("should dispatch once for multiple requests", () => { 48 requestFromTab("foo"); 49 requestFromTab("foo"); 50 requestFromTab("foo"); 51 52 assert.calledOnce(store.dispatch); 53 }); 54 describe("multiple tabs", () => { 55 beforeEach(() => { 56 simulateTabInit("bar"); 57 }); 58 it("should dispatch once to each tab", () => { 59 requestFromTab("foo"); 60 requestFromTab("bar"); 61 assert.calledTwice(store.dispatch); 62 requestFromTab("foo"); 63 requestFromTab("bar"); 64 65 assert.calledTwice(store.dispatch); 66 }); 67 it("should clean up when tabs close", () => { 68 assert.propertyVal(instance._repliedEarlyTabs, "size", 2); 69 instance.onAction(ac.AlsoToMain({ type: at.NEW_TAB_UNLOAD }, "foo")); 70 assert.propertyVal(instance._repliedEarlyTabs, "size", 1); 71 instance.onAction(ac.AlsoToMain({ type: at.NEW_TAB_UNLOAD }, "foo")); 72 assert.propertyVal(instance._repliedEarlyTabs, "size", 1); 73 instance.onAction(ac.AlsoToMain({ type: at.NEW_TAB_UNLOAD }, "bar")); 74 assert.propertyVal(instance._repliedEarlyTabs, "size", 0); 75 }); 76 }); 77 }); 78 });