summaryrefslogtreecommitdiffstats
path: root/browser/components/pocket/test/unit
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /browser/components/pocket/test/unit
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/components/pocket/test/unit')
-rw-r--r--browser/components/pocket/test/unit/browser.ini6
-rw-r--r--browser/components/pocket/test/unit/browser_pocket_main.js82
-rw-r--r--browser/components/pocket/test/unit/browser_pocket_pktTelemetry.js56
-rw-r--r--browser/components/pocket/test/unit/head.js12
4 files changed, 156 insertions, 0 deletions
diff --git a/browser/components/pocket/test/unit/browser.ini b/browser/components/pocket/test/unit/browser.ini
new file mode 100644
index 0000000000..9d1fd0583c
--- /dev/null
+++ b/browser/components/pocket/test/unit/browser.ini
@@ -0,0 +1,6 @@
+[DEFAULT]
+support-files =
+ head.js
+
+[browser_pocket_main.js]
+[browser_pocket_pktTelemetry.js]
diff --git a/browser/components/pocket/test/unit/browser_pocket_main.js b/browser/components/pocket/test/unit/browser_pocket_main.js
new file mode 100644
index 0000000000..c6955c09f7
--- /dev/null
+++ b/browser/components/pocket/test/unit/browser_pocket_main.js
@@ -0,0 +1,82 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+"use strict";
+
+function test_runner(test) {
+ let testTask = async () => {
+ // Before each
+ pktUI.initPrefs();
+ const sandbox = sinon.createSandbox();
+ try {
+ await test({ sandbox });
+ } finally {
+ // After each
+ sandbox.restore();
+ }
+ };
+
+ // Copy the name of the test function to identify the test
+ Object.defineProperty(testTask, "name", { value: test.name });
+ add_task(testTask);
+}
+
+test_runner(async function test_main_getAndShowRecsForItem_on({ sandbox }) {
+ await SpecialPowers.pushPrefEnv({
+ set: [["extensions.pocket.onSaveRecs", true]],
+ });
+ pktUI.initPrefs();
+ const getRecsForItemStub = sandbox.stub(pktApi, "getRecsForItem");
+
+ pktUI.getAndShowRecsForItem(
+ {
+ resolved_id: "1234",
+ },
+ {
+ success() {},
+ }
+ );
+
+ Assert.ok(getRecsForItemStub.calledOnce);
+ Assert.equal(getRecsForItemStub.getCall(0).args[0], "1234");
+});
+
+test_runner(async function test_main_getAndShowRecsForItem_off({ sandbox }) {
+ await SpecialPowers.pushPrefEnv({
+ set: [["extensions.pocket.onSaveRecs", false]],
+ });
+ pktUI.initPrefs();
+ const getRecsForItemStub = sandbox.stub(pktApi, "getRecsForItem");
+
+ pktUI.getAndShowRecsForItem(
+ {
+ resolved_id: "1234",
+ },
+ {
+ success() {},
+ }
+ );
+
+ Assert.ok(getRecsForItemStub.notCalled);
+});
+
+test_runner(async function test_main_getAndShowRecsForItem_locale({ sandbox }) {
+ await SpecialPowers.pushPrefEnv({
+ set: [
+ ["extensions.pocket.onSaveRecs", true],
+ ["extensions.pocket.onSaveRecs.locales", "de"],
+ ],
+ });
+ pktUI.initPrefs();
+ const getRecsForItemStub = sandbox.stub(pktApi, "getRecsForItem");
+
+ pktUI.getAndShowRecsForItem(
+ {
+ resolved_id: "1234",
+ },
+ {
+ success() {},
+ }
+ );
+
+ Assert.ok(getRecsForItemStub.notCalled);
+});
diff --git a/browser/components/pocket/test/unit/browser_pocket_pktTelemetry.js b/browser/components/pocket/test/unit/browser_pocket_pktTelemetry.js
new file mode 100644
index 0000000000..5346f6c158
--- /dev/null
+++ b/browser/components/pocket/test/unit/browser_pocket_pktTelemetry.js
@@ -0,0 +1,56 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+"use strict";
+
+ChromeUtils.defineModuleGetter(
+ this,
+ "pktTelemetry",
+ "chrome://pocket/content/pktTelemetry.jsm"
+);
+
+function test_runner(test) {
+ let testTask = async () => {
+ // Before each
+ const sandbox = sinon.createSandbox();
+ try {
+ await test({ sandbox });
+ } finally {
+ // After each
+ sandbox.restore();
+ }
+ };
+
+ // Copy the name of the test function to identify the test
+ Object.defineProperty(testTask, "name", { value: test.name });
+ add_task(testTask);
+}
+
+test_runner(async function test_createPingPayload({ sandbox }) {
+ const impressionId = "{7fd5a1ac-6089-4212-91a7-fcdec1d2f533}";
+ const creationDate = "18578";
+ await SpecialPowers.pushPrefEnv({
+ set: [["browser.newtabpage.activity-stream.impressionId", impressionId]],
+ });
+ sandbox.stub(pktTelemetry, "_profileCreationDate").returns(creationDate);
+ const result = pktTelemetry.createPingPayload({ test: "test" });
+
+ Assert.deepEqual(result, {
+ test: "test",
+ pocket_logged_in_status: false,
+ profile_creation_date: creationDate,
+ impression_id: impressionId,
+ });
+});
+
+test_runner(async function test_generateStructuredIngestionEndpoint({
+ sandbox,
+}) {
+ sandbox
+ .stub(pktTelemetry, "_generateUUID")
+ .returns("{7fd5a1ac-6089-4212-91a7-fcdec1d2f533}");
+ const endpoint = pktTelemetry._generateStructuredIngestionEndpoint();
+ Assert.equal(
+ endpoint,
+ "https://incoming.telemetry.mozilla.org/submit/activity-stream/pocket-button/1/7fd5a1ac-6089-4212-91a7-fcdec1d2f533"
+ );
+});
diff --git a/browser/components/pocket/test/unit/head.js b/browser/components/pocket/test/unit/head.js
new file mode 100644
index 0000000000..6fc5edb7bd
--- /dev/null
+++ b/browser/components/pocket/test/unit/head.js
@@ -0,0 +1,12 @@
+ChromeUtils.defineModuleGetter(
+ this,
+ "pktApi",
+ "chrome://pocket/content/pktApi.jsm"
+);
+XPCOMUtils.defineLazyScriptGetter(
+ this,
+ "pktUI",
+ "chrome://pocket/content/main.js"
+);
+
+const { sinon } = ChromeUtils.import("resource://testing-common/Sinon.jsm");