summaryrefslogtreecommitdiffstats
path: root/browser/components/asrouter/tests/unit/asrouter-utils.test.js
blob: 81252b4d504638af9de58beb8cf3d16c08c18999 (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
107
108
109
110
import { ASRouterUtils } from "../../content-src/asrouter-utils.mjs";

describe("ASRouterUtils", () => {
  let sandbox = null;
  beforeEach(() => {
    sandbox = sinon.createSandbox();
    globalThis.ASRouterMessage = sandbox.stub().resolves({});
  });
  afterEach(() => {
    sandbox.restore();
  });
  describe("sendMessage", () => {
    it("default", () => {
      ASRouterUtils.sendMessage({ foo: "bar" });
      assert.calledOnce(globalThis.ASRouterMessage);
      assert.calledWith(globalThis.ASRouterMessage, { foo: "bar" });
    });
    it("throws if ASRouterMessage is not defined", () => {
      globalThis.ASRouterMessage = null;
      assert.throws(() => ASRouterUtils.sendMessage({ foo: "bar" }));
    });
    it("can accept the legacy NEWTAB_MESSAGE_REQUEST message without throwing", async () => {
      assert.doesNotThrow(async () => {
        let result = await ASRouterUtils.sendMessage({
          type: "NEWTAB_MESSAGE_REQUEST",
          data: {},
        });
        sandbox.assert.deepEqual(result, {});
      });
    });
  });
  describe("blockById", () => {
    it("default", () => {
      ASRouterUtils.blockById(1, { foo: "bar" });
      assert.calledWith(
        globalThis.ASRouterMessage,
        sinon.match({ data: { foo: "bar", id: 1 } })
      );
    });
  });
  describe("modifyMessageJson", () => {
    it("default", () => {
      ASRouterUtils.modifyMessageJson({ foo: "bar" });
      assert.calledWith(
        globalThis.ASRouterMessage,
        sinon.match({ data: { content: { foo: "bar" } } })
      );
    });
  });
  describe("executeAction", () => {
    it("default", () => {
      ASRouterUtils.executeAction({ foo: "bar" });
      assert.calledWith(
        globalThis.ASRouterMessage,
        sinon.match({ data: { foo: "bar" } })
      );
    });
  });
  describe("unblockById", () => {
    it("default", () => {
      ASRouterUtils.unblockById(2);
      assert.calledWith(
        globalThis.ASRouterMessage,
        sinon.match({ data: { id: 2 } })
      );
    });
  });
  describe("blockBundle", () => {
    it("default", () => {
      ASRouterUtils.blockBundle(2);
      assert.calledWith(
        globalThis.ASRouterMessage,
        sinon.match({ data: { bundle: 2 } })
      );
    });
  });
  describe("unblockBundle", () => {
    it("default", () => {
      ASRouterUtils.unblockBundle(2);
      assert.calledWith(
        globalThis.ASRouterMessage,
        sinon.match({ data: { bundle: 2 } })
      );
    });
  });
  describe("overrideMessage", () => {
    it("default", () => {
      ASRouterUtils.overrideMessage(12);
      assert.calledWith(
        globalThis.ASRouterMessage,
        sinon.match({ data: { id: 12 } })
      );
    });
  });
  describe("editState", () => {
    it("default", () => {
      ASRouterUtils.editState("foo", "bar");
      assert.calledWith(
        globalThis.ASRouterMessage,
        sinon.match({ data: { foo: "bar" } })
      );
    });
  });
  describe("sendTelemetry", () => {
    it("default", () => {
      ASRouterUtils.sendTelemetry({ foo: "bar" });
      assert.calledOnce(globalThis.ASRouterMessage);
    });
  });
});