Commit ea13fa22 authored by Andrei Oprea's avatar Andrei Oprea
Browse files

Bug 1619702 - Use Sinon.jsm for about:welcome mochitests and xpcshell r=pdahiya

Differential Revision: https://phabricator.services.mozilla.com/D66624

--HG--
extra : moz-landing-system : lando
parent 8ca3ed1a
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -2,6 +2,10 @@

const SEPARATE_ABOUT_WELCOME_PREF = "browser.aboutwelcome.enabled";

const { FxAccounts } = ChromeUtils.import(
  "resource://gre/modules/FxAccounts.jsm"
);

/**
 * Sets the aboutwelcome pref to enabled simplified welcome UI
 */
@@ -116,3 +120,38 @@ test_newtab(
  },
  "about:welcome"
);

add_task(function setup() {
  const sandbox = sinon.createSandbox();
  sandbox.stub(FxAccounts.config, "promiseMetricsFlowURI").resolves("");

  registerCleanupFunction(() => {
    sandbox.restore();
  });
});

test_newtab(
  {
    async before({ pushPrefs }) {
      await pushPrefs(["browser.aboutwelcome.enabled", true]);
    },
    test: async function test_startBrowsing() {
      await ContentTaskUtils.waitForCondition(
        () => content.document.querySelector(".start-button"),
        "Wait for start browsing button to load"
      );
    },
    after() {
      ok(
        FxAccounts.config.promiseMetricsFlowURI.callCount === 1,
        "Stub was called"
      );
      Assert.equal(
        FxAccounts.config.promiseMetricsFlowURI.firstCall.args[0],
        "aboutwelcome",
        "Called by AboutWelcomeParent"
      );
    },
  },
  "about:welcome"
);
+4 −0
Original line number Diff line number Diff line
@@ -11,6 +11,10 @@ ChromeUtils.defineModuleGetter(
  "resource://activity-stream/lib/ASRouterTargeting.jsm"
);

// We import sinon here to make it available across all mochitest test files
// eslint-disable-next-line no-unused-vars
const { sinon } = ChromeUtils.import("resource://testing-common/Sinon.jsm");

function popPrefs() {
  return SpecialPowers.popPrefEnv();
}
+24 −1
Original line number Diff line number Diff line
@@ -8,9 +8,10 @@ const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { AboutWelcomeTelemetry } = ChromeUtils.import(
  "resource://activity-stream/aboutwelcome/lib/AboutWelcomeTelemetry.jsm"
);
const { sinon } = ChromeUtils.import("resource://testing-common/Sinon.jsm");
const TELEMETRY_PREF = "browser.newtabpage.activity-stream.telemetry";

add_task(async function test_enabled() {
add_task(function test_enabled() {
  registerCleanupFunction(() => {
    Services.prefs.clearUserPref(TELEMETRY_PREF);
  });
@@ -24,3 +25,25 @@ add_task(async function test_enabled() {

  equal(AWTelemetry.telemetryEnabled, false, "Telemetry should be off");
});

add_task(async function test_pingPayload() {
  registerCleanupFunction(() => {
    Services.prefs.clearUserPref(TELEMETRY_PREF);
  });
  Services.prefs.setBoolPref(TELEMETRY_PREF, true);
  const AWTelemetry = new AboutWelcomeTelemetry();
  const stub = sinon.stub(
    AWTelemetry.pingCentre,
    "sendStructuredIngestionPing"
  );
  sinon.stub(AWTelemetry, "_createPing").resolves({ event: "MOCHITEST" });

  await AWTelemetry.sendTelemetry();

  equal(stub.callCount, 1, "Call was made");
  // check the endpoint
  ok(
    stub.firstCall.args[1].includes("/messaging-system/onboarding"),
    "Endpoint is correct"
  );
});