summaryrefslogtreecommitdiffstats
path: root/browser/components/asrouter/tests/unit/ASRouterChild.test.js
blob: 41fdd79ea26fa430ad3960735f9e31e5978860e7 (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
/*eslint max-nested-callbacks: ["error", 10]*/
import { ASRouterChild } from "actors/ASRouterChild.sys.mjs";
import { MESSAGE_TYPE_HASH as msg } from "modules/ActorConstants.sys.mjs";
import { GlobalOverrider } from "test/unit/utils";

describe("ASRouterChild", () => {
  let asRouterChild = null;
  let globals = null;
  let overrider = null;
  let sandbox = null;
  beforeEach(() => {
    sandbox = sinon.createSandbox();
    globals = {
      Cu: {
        cloneInto: sandbox.stub().returns(Promise.resolve()),
      },
    };
    overrider = new GlobalOverrider();
    overrider.set(globals);
    asRouterChild = new ASRouterChild();
    asRouterChild.telemetry = {
      sendTelemetry: sandbox.stub(),
    };
    sandbox.stub(asRouterChild, "sendAsyncMessage");
    sandbox.stub(asRouterChild, "sendQuery").returns(Promise.resolve());
  });
  afterEach(() => {
    sandbox.restore();
    overrider.restore();
    asRouterChild = null;
  });
  describe("asRouterMessage", () => {
    describe("uses sendAsyncMessage for types that don't need an async response", () => {
      [
        msg.DISABLE_PROVIDER,
        msg.ENABLE_PROVIDER,
        msg.EXPIRE_QUERY_CACHE,
        msg.FORCE_WHATSNEW_PANEL,
        msg.IMPRESSION,
        msg.RESET_PROVIDER_PREF,
        msg.SET_PROVIDER_USER_PREF,
        msg.USER_ACTION,
      ].forEach(type => {
        it(`type ${type}`, () => {
          asRouterChild.asRouterMessage({
            type,
            data: {
              something: 1,
            },
          });
          sandbox.assert.calledOnce(asRouterChild.sendAsyncMessage);
          sandbox.assert.calledWith(asRouterChild.sendAsyncMessage, type, {
            something: 1,
          });
        });
      });
    });
    // Some legacy privileged extensions still send this legacy NEWTAB_MESSAGE_REQUEST
    // action type. We simply
    it("can accept the legacy NEWTAB_MESSAGE_REQUEST message without throwing", async () => {
      assert.doesNotThrow(async () => {
        let result = await asRouterChild.asRouterMessage({
          type: "NEWTAB_MESSAGE_REQUEST",
          data: {},
        });
        sandbox.assert.deepEqual(result, {});
        sandbox.assert.notCalled(asRouterChild.sendAsyncMessage);
      });
    });
  });
});