summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutwelcome/tests/xpcshell/test_AboutWelcomeAttribution.js
blob: 3d83f473d56e92dc2e3a2351a94e5245ba559066 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

const { AboutWelcomeDefaults } = ChromeUtils.importESModule(
  "resource:///modules/aboutwelcome/AboutWelcomeDefaults.sys.mjs"
);
const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);
const { AttributionCode } = ChromeUtils.importESModule(
  "resource:///modules/AttributionCode.sys.mjs"
);
const { AddonRepository } = ChromeUtils.importESModule(
  "resource://gre/modules/addons/AddonRepository.sys.mjs"
);

const TEST_ATTRIBUTION_DATA = {
  source: "addons.mozilla.org",
  medium: "referral",
  campaign: "non-fx-button",
  content: "rta:iridium%40particlecore.github.io",
};

add_task(async function test_handleAddonInfoNotFound() {
  let sandbox = sinon.createSandbox();
  const stub = sandbox.stub(AttributionCode, "getAttrDataAsync").resolves(null);
  let result = await AboutWelcomeDefaults.getAttributionContent();
  equal(stub.callCount, 1, "Call was made");
  equal(result, null, "No data is returned");

  sandbox.restore();
});

add_task(async function test_UAAttribution() {
  let sandbox = sinon.createSandbox();
  const stub = sandbox
    .stub(AttributionCode, "getAttrDataAsync")
    .resolves({ ua: "test" });
  let result = await AboutWelcomeDefaults.getAttributionContent();
  equal(stub.callCount, 1, "Call was made");
  equal(result.template, undefined, "Template was not returned");
  equal(result.ua, "test", "UA was returned");

  sandbox.restore();
});

add_task(async function test_formatAttributionData() {
  let sandbox = sinon.createSandbox();
  const TEST_ADDON_INFO = {
    sourceURI: { scheme: "https", spec: "https://test.xpi" },
    name: "Test Add-on",
    icons: { 64: "http://test.svg" },
  };
  sandbox
    .stub(AttributionCode, "getAttrDataAsync")
    .resolves(TEST_ATTRIBUTION_DATA);
  sandbox.stub(AddonRepository, "getAddonsByIDs").resolves([TEST_ADDON_INFO]);
  let result = await AboutWelcomeDefaults.getAttributionContent(
    TEST_ATTRIBUTION_DATA
  );
  equal(AddonRepository.getAddonsByIDs.callCount, 1, "Retrieve addon content");
  equal(result.template, "return_to_amo", "RTAMO template returned");
  equal(result.name, TEST_ADDON_INFO.name, "AddonInfo returned");

  sandbox.restore();
});