summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/lib/NewTabInit.test.js
blob: 0def9293f09840a88084b6638bc22a516b1d6709 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { actionCreators as ac, actionTypes as at } from "common/Actions.mjs";
import { NewTabInit } from "lib/NewTabInit.sys.mjs";

describe("NewTabInit", () => {
  let instance;
  let store;
  let STATE;
  const requestFromTab = portID =>
    instance.onAction(
      ac.AlsoToMain({ type: at.NEW_TAB_STATE_REQUEST }, portID)
    );
  beforeEach(() => {
    STATE = {};
    store = { getState: sinon.stub().returns(STATE), dispatch: sinon.stub() };
    instance = new NewTabInit();
    instance.store = store;
  });
  it("should reply with a copy of the state immediately", () => {
    requestFromTab(123);

    const resp = ac.AlsoToOneContent(
      { type: at.NEW_TAB_INITIAL_STATE, data: STATE },
      123
    );
    assert.calledWith(store.dispatch, resp);
  });
  describe("early / simulated new tabs", () => {
    const simulateTabInit = portID =>
      instance.onAction({
        type: at.NEW_TAB_INIT,
        data: { portID, simulated: true },
      });
    beforeEach(() => {
      simulateTabInit("foo");
    });
    it("should dispatch if not replied yet", () => {
      requestFromTab("foo");

      assert.calledWith(
        store.dispatch,
        ac.AlsoToOneContent(
          { type: at.NEW_TAB_INITIAL_STATE, data: STATE },
          "foo"
        )
      );
    });
    it("should dispatch once for multiple requests", () => {
      requestFromTab("foo");
      requestFromTab("foo");
      requestFromTab("foo");

      assert.calledOnce(store.dispatch);
    });
    describe("multiple tabs", () => {
      beforeEach(() => {
        simulateTabInit("bar");
      });
      it("should dispatch once to each tab", () => {
        requestFromTab("foo");
        requestFromTab("bar");
        assert.calledTwice(store.dispatch);
        requestFromTab("foo");
        requestFromTab("bar");

        assert.calledTwice(store.dispatch);
      });
      it("should clean up when tabs close", () => {
        assert.propertyVal(instance._repliedEarlyTabs, "size", 2);
        instance.onAction(ac.AlsoToMain({ type: at.NEW_TAB_UNLOAD }, "foo"));
        assert.propertyVal(instance._repliedEarlyTabs, "size", 1);
        instance.onAction(ac.AlsoToMain({ type: at.NEW_TAB_UNLOAD }, "foo"));
        assert.propertyVal(instance._repliedEarlyTabs, "size", 1);
        instance.onAction(ac.AlsoToMain({ type: at.NEW_TAB_UNLOAD }, "bar"));
        assert.propertyVal(instance._repliedEarlyTabs, "size", 0);
      });
    });
  });
});