summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/asrouter/ASRouterParent.test.js
blob: 1b494bbe0e3a5e68e3f48448e470453fbfa6131f (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { ASRouterParent } from "actors/ASRouterParent.sys.mjs";
import { MESSAGE_TYPE_HASH as msg } from "common/ActorConstants.sys.mjs";

describe("ASRouterParent", () => {
  let asRouterParent = null;
  let sandbox = null;
  let handleMessage = null;
  let tabs = null;
  beforeEach(() => {
    sandbox = sinon.createSandbox();
    handleMessage = sandbox.stub().resolves("handle-message-result");
    ASRouterParent.nextTabId = 1;
    const methods = {
      destroy: sandbox.stub(),
      size: 1,
      messageAll: sandbox.stub().resolves(),
      registerActor: sandbox.stub(),
      unregisterActor: sandbox.stub(),
      loadingMessageHandler: Promise.resolve({
        handleMessage,
      }),
    };
    tabs = {
      methods,
      factory: sandbox.stub().returns(methods),
    };
    asRouterParent = new ASRouterParent({ tabsFactory: tabs.factory });
    ASRouterParent.tabs = tabs.methods;
    asRouterParent.browsingContext = {
      embedderElement: {
        getAttribute: () => true,
      },
    };
    asRouterParent.tabId = ASRouterParent.nextTabId;
  });
  afterEach(() => {
    sandbox.restore();
    asRouterParent = null;
  });
  describe("actorCreated", () => {
    it("after ASRouterTabs is instanced", () => {
      asRouterParent.actorCreated();
      assert.equal(asRouterParent.tabId, 2);
      assert.notCalled(tabs.factory);
      assert.calledOnce(tabs.methods.registerActor);
    });
    it("before ASRouterTabs is instanced", () => {
      ASRouterParent.tabs = null;
      ASRouterParent.nextTabId = 0;
      asRouterParent.actorCreated();
      assert.calledOnce(tabs.factory);
      assert.isNotNull(ASRouterParent.tabs);
      assert.equal(asRouterParent.tabId, 1);
    });
  });
  describe("didDestroy", () => {
    it("one still remains", () => {
      ASRouterParent.tabs.size = 1;
      asRouterParent.didDestroy();
      assert.isNotNull(ASRouterParent.tabs);
      assert.calledOnce(ASRouterParent.tabs.unregisterActor);
      assert.notCalled(ASRouterParent.tabs.destroy);
    });
    it("none remain", () => {
      ASRouterParent.tabs.size = 0;
      const tabsCopy = ASRouterParent.tabs;
      asRouterParent.didDestroy();
      assert.isNull(ASRouterParent.tabs);
      assert.calledOnce(tabsCopy.unregisterActor);
      assert.calledOnce(tabsCopy.destroy);
    });
  });
  describe("receiveMessage", async () => {
    it("passes call to parentProcessMessageHandler and returns the result from handler", async () => {
      const result = await asRouterParent.receiveMessage({
        name: msg.BLOCK_MESSAGE_BY_ID,
        data: { id: 1 },
      });
      assert.calledOnce(handleMessage);
      assert.equal(result, "handle-message-result");
    });
    it("it messages all actors on BLOCK_MESSAGE_BY_ID messages", async () => {
      const MESSAGE_ID = 1;
      const result = await asRouterParent.receiveMessage({
        name: msg.BLOCK_MESSAGE_BY_ID,
        data: { id: MESSAGE_ID, campaign: "message-campaign" },
      });
      assert.calledOnce(handleMessage);
      // Check that we correctly pass the tabId
      assert.calledWithExactly(
        handleMessage,
        sinon.match.any,
        sinon.match.any,
        { id: sinon.match.number, browser: sinon.match.any }
      );
      assert.calledWithExactly(
        ASRouterParent.tabs.messageAll,
        "ClearMessages",
        // When blocking an id the entire campaign is blocked
        // and all other snippets become invalid
        ["message-campaign"]
      );
      assert.equal(result, "handle-message-result");
    });
  });
});