tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Actions.test.js (7713B)


      1 import {
      2  actionCreators as ac,
      3  actionTypes as at,
      4  actionUtils as au,
      5  BACKGROUND_PROCESS,
      6  CONTENT_MESSAGE_TYPE,
      7  globalImportContext,
      8  MAIN_MESSAGE_TYPE,
      9  PRELOAD_MESSAGE_TYPE,
     10  UI_CODE,
     11 } from "common/Actions.mjs";
     12 
     13 describe("Actions", () => {
     14  it("should set globalImportContext to UI_CODE", () => {
     15    assert.equal(globalImportContext, UI_CODE);
     16  });
     17 });
     18 
     19 describe("ActionTypes", () => {
     20  it("should be in alpha order", () => {
     21    assert.equal(Object.keys(at).join(", "), Object.keys(at).sort().join(", "));
     22  });
     23 });
     24 
     25 describe("ActionCreators", () => {
     26  describe("_RouteMessage", () => {
     27    it("should throw if options are not passed as the second param", () => {
     28      assert.throws(() => {
     29        au._RouteMessage({ type: "FOO" });
     30      });
     31    });
     32    it("should set all defined options on the .meta property of the new action", () => {
     33      assert.deepEqual(
     34        au._RouteMessage(
     35          { type: "FOO", meta: { hello: "world" } },
     36          { from: "foo", to: "bar" }
     37        ),
     38        { type: "FOO", meta: { hello: "world", from: "foo", to: "bar" } }
     39      );
     40    });
     41    it("should remove any undefined options related to message routing", () => {
     42      const action = au._RouteMessage(
     43        { type: "FOO", meta: { fromTarget: "bar" } },
     44        { from: "foo", to: "bar" }
     45      );
     46      assert.isUndefined(action.meta.fromTarget);
     47    });
     48  });
     49  describe("AlsoToMain", () => {
     50    it("should create the right action", () => {
     51      const action = { type: "FOO", data: "BAR" };
     52      const newAction = ac.AlsoToMain(action);
     53      assert.deepEqual(newAction, {
     54        type: "FOO",
     55        data: "BAR",
     56        meta: { from: CONTENT_MESSAGE_TYPE, to: MAIN_MESSAGE_TYPE },
     57      });
     58    });
     59    it("should add the fromTarget if it was supplied", () => {
     60      const action = { type: "FOO", data: "BAR" };
     61      const newAction = ac.AlsoToMain(action, "port123");
     62      assert.equal(newAction.meta.fromTarget, "port123");
     63    });
     64    describe("isSendToMain", () => {
     65      it("should return true if action is AlsoToMain", () => {
     66        const newAction = ac.AlsoToMain({ type: "FOO" });
     67        assert.isTrue(au.isSendToMain(newAction));
     68      });
     69      it("should return false if action is not AlsoToMain", () => {
     70        assert.isFalse(au.isSendToMain({ type: "FOO" }));
     71      });
     72    });
     73  });
     74  describe("AlsoToOneContent", () => {
     75    it("should create the right action", () => {
     76      const action = { type: "FOO", data: "BAR" };
     77      const targetId = "abc123";
     78      const newAction = ac.AlsoToOneContent(action, targetId);
     79      assert.deepEqual(newAction, {
     80        type: "FOO",
     81        data: "BAR",
     82        meta: {
     83          from: MAIN_MESSAGE_TYPE,
     84          to: CONTENT_MESSAGE_TYPE,
     85          toTarget: targetId,
     86        },
     87      });
     88    });
     89    it("should throw if no targetId is provided", () => {
     90      assert.throws(() => {
     91        ac.AlsoToOneContent({ type: "FOO" });
     92      });
     93    });
     94    describe("isSendToOneContent", () => {
     95      it("should return true if action is AlsoToOneContent", () => {
     96        const newAction = ac.AlsoToOneContent({ type: "FOO" }, "foo123");
     97        assert.isTrue(au.isSendToOneContent(newAction));
     98      });
     99      it("should return false if action is not AlsoToMain", () => {
    100        assert.isFalse(au.isSendToOneContent({ type: "FOO" }));
    101        assert.isFalse(
    102          au.isSendToOneContent(ac.BroadcastToContent({ type: "FOO" }))
    103        );
    104      });
    105    });
    106    describe("isFromMain", () => {
    107      it("should return true if action is AlsoToOneContent", () => {
    108        const newAction = ac.AlsoToOneContent({ type: "FOO" }, "foo123");
    109        assert.isTrue(au.isFromMain(newAction));
    110      });
    111      it("should return true if action is BroadcastToContent", () => {
    112        const newAction = ac.BroadcastToContent({ type: "FOO" });
    113        assert.isTrue(au.isFromMain(newAction));
    114      });
    115      it("should return false if action is AlsoToMain", () => {
    116        const newAction = ac.AlsoToMain({ type: "FOO" });
    117        assert.isFalse(au.isFromMain(newAction));
    118      });
    119    });
    120  });
    121  describe("BroadcastToContent", () => {
    122    it("should create the right action", () => {
    123      const action = { type: "FOO", data: "BAR" };
    124      const newAction = ac.BroadcastToContent(action);
    125      assert.deepEqual(newAction, {
    126        type: "FOO",
    127        data: "BAR",
    128        meta: { from: MAIN_MESSAGE_TYPE, to: CONTENT_MESSAGE_TYPE },
    129      });
    130    });
    131    describe("isBroadcastToContent", () => {
    132      it("should return true if action is BroadcastToContent", () => {
    133        assert.isTrue(
    134          au.isBroadcastToContent(ac.BroadcastToContent({ type: "FOO" }))
    135        );
    136      });
    137      it("should return false if action is not BroadcastToContent", () => {
    138        assert.isFalse(au.isBroadcastToContent({ type: "FOO" }));
    139        assert.isFalse(
    140          au.isBroadcastToContent(
    141            ac.AlsoToOneContent({ type: "FOO" }, "foo123")
    142          )
    143        );
    144      });
    145    });
    146  });
    147  describe("AlsoToPreloaded", () => {
    148    it("should create the right action", () => {
    149      const action = { type: "FOO", data: "BAR" };
    150      const newAction = ac.AlsoToPreloaded(action);
    151      assert.deepEqual(newAction, {
    152        type: "FOO",
    153        data: "BAR",
    154        meta: { from: MAIN_MESSAGE_TYPE, to: PRELOAD_MESSAGE_TYPE },
    155      });
    156    });
    157  });
    158  describe("isSendToPreloaded", () => {
    159    it("should return true if action is AlsoToPreloaded", () => {
    160      assert.isTrue(au.isSendToPreloaded(ac.AlsoToPreloaded({ type: "FOO" })));
    161    });
    162    it("should return false if action is not AlsoToPreloaded", () => {
    163      assert.isFalse(au.isSendToPreloaded({ type: "FOO" }));
    164      assert.isFalse(
    165        au.isSendToPreloaded(ac.BroadcastToContent({ type: "FOO" }))
    166      );
    167    });
    168  });
    169  describe("UserEvent", () => {
    170    it("should include the given data", () => {
    171      const data = { action: "foo" };
    172      assert.equal(ac.UserEvent(data).data, data);
    173    });
    174    it("should wrap with AlsoToMain", () => {
    175      const action = ac.UserEvent({ action: "foo" });
    176      assert.isTrue(au.isSendToMain(action), "isSendToMain");
    177    });
    178  });
    179  describe("ImpressionStats", () => {
    180    it("should include the right data", () => {
    181      const data = { action: "foo" };
    182      assert.equal(ac.ImpressionStats(data).data, data);
    183    });
    184    it("should wrap with AlsoToMain if in UI code", () => {
    185      assert.isTrue(
    186        au.isSendToMain(ac.ImpressionStats({ action: "foo" })),
    187        "isSendToMain"
    188      );
    189    });
    190    it("should not wrap with AlsoToMain if not in UI code", () => {
    191      const action = ac.ImpressionStats({ action: "foo" }, BACKGROUND_PROCESS);
    192      assert.isFalse(au.isSendToMain(action), "isSendToMain");
    193    });
    194  });
    195  describe("WebExtEvent", () => {
    196    it("should set the provided type", () => {
    197      const action = ac.WebExtEvent(at.WEBEXT_CLICK, {
    198        source: "MyExtension",
    199        url: "foo.com",
    200      });
    201      assert.equal(action.type, at.WEBEXT_CLICK);
    202    });
    203    it("should set the provided data", () => {
    204      const data = { source: "MyExtension", url: "foo.com" };
    205      const action = ac.WebExtEvent(at.WEBEXT_CLICK, data);
    206      assert.equal(action.data, data);
    207    });
    208    it("should throw if the 'source' property is missing", () => {
    209      assert.throws(() => {
    210        ac.WebExtEvent(at.WEBEXT_CLICK, {});
    211      });
    212    });
    213  });
    214 });
    215 
    216 describe("ActionUtils", () => {
    217  describe("getPortIdOfSender", () => {
    218    it("should return the PortID from a AlsoToMain action", () => {
    219      const portID = "foo123";
    220      const result = au.getPortIdOfSender(
    221        ac.AlsoToMain({ type: "FOO" }, portID)
    222      );
    223      assert.equal(result, portID);
    224    });
    225  });
    226 });