tor-browser

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

AWScreenUtils.test.jsx (4422B)


      1 import { AWScreenUtils } from "modules/AWScreenUtils.sys.mjs";
      2 import { GlobalOverrider } from "asrouter/tests/unit/utils";
      3 import { ASRouter } from "asrouter/modules/ASRouter.sys.mjs";
      4 
      5 describe("AWScreenUtils", () => {
      6  let sandbox;
      7  let globals;
      8 
      9  beforeEach(() => {
     10    globals = new GlobalOverrider();
     11    globals.set({
     12      ASRouter,
     13      ASRouterTargeting: {
     14        Environment: {},
     15      },
     16    });
     17 
     18    sandbox = sinon.createSandbox();
     19  });
     20  afterEach(() => {
     21    sandbox.restore();
     22    globals.restore();
     23  });
     24  describe("removeScreens", () => {
     25    it("should run callback function once for each array element", async () => {
     26      const callback = sandbox.stub().resolves(false);
     27      const arr = ["foo", "bar"];
     28      await AWScreenUtils.removeScreens(arr, callback);
     29      assert.calledTwice(callback);
     30    });
     31    it("should remove screen when passed function evaluates true", async () => {
     32      const callback = sandbox.stub().resolves(true);
     33      const arr = ["foo", "bar"];
     34      await AWScreenUtils.removeScreens(arr, callback);
     35      assert.deepEqual(arr, []);
     36    });
     37  });
     38  describe("evaluateScreenTargeting", () => {
     39    it("should return the eval result if the eval succeeds", async () => {
     40      const evalStub = sandbox.stub(ASRouter, "evaluateExpression").resolves({
     41        evaluationStatus: {
     42          success: true,
     43          result: false,
     44        },
     45      });
     46      const result =
     47        await AWScreenUtils.evaluateScreenTargeting("test expression");
     48      assert.calledOnce(evalStub);
     49      assert.equal(result, false);
     50    });
     51    it("should return true if the targeting eval fails", async () => {
     52      const evalStub = sandbox.stub(ASRouter, "evaluateExpression").resolves({
     53        evaluationStatus: {
     54          success: false,
     55          result: false,
     56        },
     57      });
     58      const result =
     59        await AWScreenUtils.evaluateScreenTargeting("test expression");
     60      assert.calledOnce(evalStub);
     61      assert.equal(result, true);
     62    });
     63  });
     64  describe("evaluateTargetingAndRemoveScreens", () => {
     65    it("should manipulate an array of screens", async () => {
     66      const screens = [
     67        {
     68          id: "first",
     69          targeting: true,
     70        },
     71        {
     72          id: "second",
     73          targeting: false,
     74        },
     75      ];
     76 
     77      const expectedScreens = [
     78        {
     79          id: "first",
     80          targeting: true,
     81        },
     82      ];
     83      sandbox.stub(ASRouter, "evaluateExpression").callsFake(targeting => {
     84        return {
     85          evaluationStatus: {
     86            success: true,
     87            result: targeting.expression,
     88          },
     89        };
     90      });
     91      const evaluatedStrings =
     92        await AWScreenUtils.evaluateTargetingAndRemoveScreens(screens);
     93      assert.deepEqual(evaluatedStrings, expectedScreens);
     94    });
     95    it("should not remove screens with no targeting", async () => {
     96      const screens = [
     97        {
     98          id: "first",
     99        },
    100        {
    101          id: "second",
    102          targeting: false,
    103        },
    104      ];
    105 
    106      const expectedScreens = [
    107        {
    108          id: "first",
    109        },
    110      ];
    111      sandbox
    112        .stub(AWScreenUtils, "evaluateScreenTargeting")
    113        .callsFake(targeting => {
    114          if (targeting === undefined) {
    115            return true;
    116          }
    117          return targeting;
    118        });
    119      const evaluatedStrings =
    120        await AWScreenUtils.evaluateTargetingAndRemoveScreens(screens);
    121      assert.deepEqual(evaluatedStrings, expectedScreens);
    122    });
    123  });
    124 
    125  describe("addScreenImpression", () => {
    126    it("Should call addScreenImpression with provided screen ID", () => {
    127      const addScreenImpressionStub = sandbox.stub(
    128        ASRouter,
    129        "addScreenImpression"
    130      );
    131      const testScreen = { id: "test" };
    132      AWScreenUtils.addScreenImpression(testScreen);
    133 
    134      assert.calledOnce(addScreenImpressionStub);
    135      assert.equal(addScreenImpressionStub.firstCall.args[0].id, testScreen.id);
    136    });
    137  });
    138  describe("getUnhandledCampaignAction", () => {
    139    it("Should call evaluateExpression", () => {
    140      const evaluateExpressionStub = sandbox.stub(
    141        ASRouter,
    142        "evaluateExpression"
    143      );
    144      AWScreenUtils.getUnhandledCampaignAction();
    145 
    146      assert.calledOnce(evaluateExpressionStub);
    147      assert.equal(
    148        evaluateExpressionStub.firstCall.args[0].expression,
    149        "unhandledCampaignAction"
    150      );
    151    });
    152  });
    153 });