asrouter-utils.test.js (3127B)
1 import { ASRouterUtils } from "../../content-src/asrouter-utils.mjs"; 2 3 describe("ASRouterUtils", () => { 4 let sandbox = null; 5 beforeEach(() => { 6 sandbox = sinon.createSandbox(); 7 globalThis.ASRouterMessage = sandbox.stub().resolves({}); 8 }); 9 afterEach(() => { 10 sandbox.restore(); 11 }); 12 describe("sendMessage", () => { 13 it("default", () => { 14 ASRouterUtils.sendMessage({ foo: "bar" }); 15 assert.calledOnce(globalThis.ASRouterMessage); 16 assert.calledWith(globalThis.ASRouterMessage, { foo: "bar" }); 17 }); 18 it("throws if ASRouterMessage is not defined", () => { 19 globalThis.ASRouterMessage = null; 20 assert.throws(() => ASRouterUtils.sendMessage({ foo: "bar" })); 21 }); 22 it("can accept the legacy NEWTAB_MESSAGE_REQUEST message without throwing", async () => { 23 assert.doesNotThrow(async () => { 24 let result = await ASRouterUtils.sendMessage({ 25 type: "NEWTAB_MESSAGE_REQUEST", 26 data: {}, 27 }); 28 sandbox.assert.deepEqual(result, {}); 29 }); 30 }); 31 }); 32 describe("blockById", () => { 33 it("default", () => { 34 ASRouterUtils.blockById(1, { foo: "bar" }); 35 assert.calledWith( 36 globalThis.ASRouterMessage, 37 sinon.match({ data: { foo: "bar", id: 1 } }) 38 ); 39 }); 40 }); 41 describe("modifyMessageJson", () => { 42 it("default", () => { 43 ASRouterUtils.modifyMessageJson({ foo: "bar" }); 44 assert.calledWith( 45 globalThis.ASRouterMessage, 46 sinon.match({ data: { content: { foo: "bar" } } }) 47 ); 48 }); 49 }); 50 describe("executeAction", () => { 51 it("default", () => { 52 ASRouterUtils.executeAction({ foo: "bar" }); 53 assert.calledWith( 54 globalThis.ASRouterMessage, 55 sinon.match({ data: { foo: "bar" } }) 56 ); 57 }); 58 }); 59 describe("unblockById", () => { 60 it("default", () => { 61 ASRouterUtils.unblockById(2); 62 assert.calledWith( 63 globalThis.ASRouterMessage, 64 sinon.match({ data: { id: 2 } }) 65 ); 66 }); 67 }); 68 describe("blockBundle", () => { 69 it("default", () => { 70 ASRouterUtils.blockBundle(2); 71 assert.calledWith( 72 globalThis.ASRouterMessage, 73 sinon.match({ data: { bundle: 2 } }) 74 ); 75 }); 76 }); 77 describe("unblockBundle", () => { 78 it("default", () => { 79 ASRouterUtils.unblockBundle(2); 80 assert.calledWith( 81 globalThis.ASRouterMessage, 82 sinon.match({ data: { bundle: 2 } }) 83 ); 84 }); 85 }); 86 describe("overrideMessage", () => { 87 it("default", () => { 88 ASRouterUtils.overrideMessage(12); 89 assert.calledWith( 90 globalThis.ASRouterMessage, 91 sinon.match({ data: { id: 12 } }) 92 ); 93 }); 94 }); 95 describe("editState", () => { 96 it("default", () => { 97 ASRouterUtils.editState("foo", "bar"); 98 assert.calledWith( 99 globalThis.ASRouterMessage, 100 sinon.match({ data: { foo: "bar" } }) 101 ); 102 }); 103 }); 104 describe("sendTelemetry", () => { 105 it("default", () => { 106 ASRouterUtils.sendTelemetry({ foo: "bar" }); 107 assert.calledOnce(globalThis.ASRouterMessage); 108 }); 109 }); 110 });