diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /browser/components/newtab/test/unit | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
77 files changed, 24559 insertions, 0 deletions
diff --git a/browser/components/newtab/test/unit/common/Actions.test.js b/browser/components/newtab/test/unit/common/Actions.test.js new file mode 100644 index 0000000000..32e417ea3f --- /dev/null +++ b/browser/components/newtab/test/unit/common/Actions.test.js @@ -0,0 +1,236 @@ +import { + actionCreators as ac, + actionTypes as at, + actionUtils as au, + BACKGROUND_PROCESS, + CONTENT_MESSAGE_TYPE, + globalImportContext, + MAIN_MESSAGE_TYPE, + PRELOAD_MESSAGE_TYPE, + UI_CODE, +} from "common/Actions.sys.mjs"; + +describe("Actions", () => { + it("should set globalImportContext to UI_CODE", () => { + assert.equal(globalImportContext, UI_CODE); + }); +}); + +describe("ActionTypes", () => { + it("should be in alpha order", () => { + assert.equal(Object.keys(at).join(", "), Object.keys(at).sort().join(", ")); + }); +}); + +describe("ActionCreators", () => { + describe("_RouteMessage", () => { + it("should throw if options are not passed as the second param", () => { + assert.throws(() => { + au._RouteMessage({ type: "FOO" }); + }); + }); + it("should set all defined options on the .meta property of the new action", () => { + assert.deepEqual( + au._RouteMessage( + { type: "FOO", meta: { hello: "world" } }, + { from: "foo", to: "bar" } + ), + { type: "FOO", meta: { hello: "world", from: "foo", to: "bar" } } + ); + }); + it("should remove any undefined options related to message routing", () => { + const action = au._RouteMessage( + { type: "FOO", meta: { fromTarget: "bar" } }, + { from: "foo", to: "bar" } + ); + assert.isUndefined(action.meta.fromTarget); + }); + }); + describe("AlsoToMain", () => { + it("should create the right action", () => { + const action = { type: "FOO", data: "BAR" }; + const newAction = ac.AlsoToMain(action); + assert.deepEqual(newAction, { + type: "FOO", + data: "BAR", + meta: { from: CONTENT_MESSAGE_TYPE, to: MAIN_MESSAGE_TYPE }, + }); + }); + it("should add the fromTarget if it was supplied", () => { + const action = { type: "FOO", data: "BAR" }; + const newAction = ac.AlsoToMain(action, "port123"); + assert.equal(newAction.meta.fromTarget, "port123"); + }); + describe("isSendToMain", () => { + it("should return true if action is AlsoToMain", () => { + const newAction = ac.AlsoToMain({ type: "FOO" }); + assert.isTrue(au.isSendToMain(newAction)); + }); + it("should return false if action is not AlsoToMain", () => { + assert.isFalse(au.isSendToMain({ type: "FOO" })); + }); + }); + }); + describe("AlsoToOneContent", () => { + it("should create the right action", () => { + const action = { type: "FOO", data: "BAR" }; + const targetId = "abc123"; + const newAction = ac.AlsoToOneContent(action, targetId); + assert.deepEqual(newAction, { + type: "FOO", + data: "BAR", + meta: { + from: MAIN_MESSAGE_TYPE, + to: CONTENT_MESSAGE_TYPE, + toTarget: targetId, + }, + }); + }); + it("should throw if no targetId is provided", () => { + assert.throws(() => { + ac.AlsoToOneContent({ type: "FOO" }); + }); + }); + describe("isSendToOneContent", () => { + it("should return true if action is AlsoToOneContent", () => { + const newAction = ac.AlsoToOneContent({ type: "FOO" }, "foo123"); + assert.isTrue(au.isSendToOneContent(newAction)); + }); + it("should return false if action is not AlsoToMain", () => { + assert.isFalse(au.isSendToOneContent({ type: "FOO" })); + assert.isFalse( + au.isSendToOneContent(ac.BroadcastToContent({ type: "FOO" })) + ); + }); + }); + describe("isFromMain", () => { + it("should return true if action is AlsoToOneContent", () => { + const newAction = ac.AlsoToOneContent({ type: "FOO" }, "foo123"); + assert.isTrue(au.isFromMain(newAction)); + }); + it("should return true if action is BroadcastToContent", () => { + const newAction = ac.BroadcastToContent({ type: "FOO" }); + assert.isTrue(au.isFromMain(newAction)); + }); + it("should return false if action is AlsoToMain", () => { + const newAction = ac.AlsoToMain({ type: "FOO" }); + assert.isFalse(au.isFromMain(newAction)); + }); + }); + }); + describe("BroadcastToContent", () => { + it("should create the right action", () => { + const action = { type: "FOO", data: "BAR" }; + const newAction = ac.BroadcastToContent(action); + assert.deepEqual(newAction, { + type: "FOO", + data: "BAR", + meta: { from: MAIN_MESSAGE_TYPE, to: CONTENT_MESSAGE_TYPE }, + }); + }); + describe("isBroadcastToContent", () => { + it("should return true if action is BroadcastToContent", () => { + assert.isTrue( + au.isBroadcastToContent(ac.BroadcastToContent({ type: "FOO" })) + ); + }); + it("should return false if action is not BroadcastToContent", () => { + assert.isFalse(au.isBroadcastToContent({ type: "FOO" })); + assert.isFalse( + au.isBroadcastToContent( + ac.AlsoToOneContent({ type: "FOO" }, "foo123") + ) + ); + }); + }); + }); + describe("AlsoToPreloaded", () => { + it("should create the right action", () => { + const action = { type: "FOO", data: "BAR" }; + const newAction = ac.AlsoToPreloaded(action); + assert.deepEqual(newAction, { + type: "FOO", + data: "BAR", + meta: { from: MAIN_MESSAGE_TYPE, to: PRELOAD_MESSAGE_TYPE }, + }); + }); + }); + describe("isSendToPreloaded", () => { + it("should return true if action is AlsoToPreloaded", () => { + assert.isTrue(au.isSendToPreloaded(ac.AlsoToPreloaded({ type: "FOO" }))); + }); + it("should return false if action is not AlsoToPreloaded", () => { + assert.isFalse(au.isSendToPreloaded({ type: "FOO" })); + assert.isFalse( + au.isSendToPreloaded(ac.BroadcastToContent({ type: "FOO" })) + ); + }); + }); + describe("UserEvent", () => { + it("should include the given data", () => { + const data = { action: "foo" }; + assert.equal(ac.UserEvent(data).data, data); + }); + it("should wrap with AlsoToMain", () => { + const action = ac.UserEvent({ action: "foo" }); + assert.isTrue(au.isSendToMain(action), "isSendToMain"); + }); + }); + describe("ASRouterUserEvent", () => { + it("should include the given data", () => { + const data = { action: "foo" }; + assert.equal(ac.ASRouterUserEvent(data).data, data); + }); + it("should wrap with AlsoToMain", () => { + const action = ac.ASRouterUserEvent({ action: "foo" }); + assert.isTrue(au.isSendToMain(action), "isSendToMain"); + }); + }); + describe("ImpressionStats", () => { + it("should include the right data", () => { + const data = { action: "foo" }; + assert.equal(ac.ImpressionStats(data).data, data); + }); + it("should wrap with AlsoToMain if in UI code", () => { + assert.isTrue( + au.isSendToMain(ac.ImpressionStats({ action: "foo" })), + "isSendToMain" + ); + }); + it("should not wrap with AlsoToMain if not in UI code", () => { + const action = ac.ImpressionStats({ action: "foo" }, BACKGROUND_PROCESS); + assert.isFalse(au.isSendToMain(action), "isSendToMain"); + }); + }); + describe("WebExtEvent", () => { + it("should set the provided type", () => { + const action = ac.WebExtEvent(at.WEBEXT_CLICK, { + source: "MyExtension", + url: "foo.com", + }); + assert.equal(action.type, at.WEBEXT_CLICK); + }); + it("should set the provided data", () => { + const data = { source: "MyExtension", url: "foo.com" }; + const action = ac.WebExtEvent(at.WEBEXT_CLICK, data); + assert.equal(action.data, data); + }); + it("should throw if the 'source' property is missing", () => { + assert.throws(() => { + ac.WebExtEvent(at.WEBEXT_CLICK, {}); + }); + }); + }); +}); + +describe("ActionUtils", () => { + describe("getPortIdOfSender", () => { + it("should return the PortID from a AlsoToMain action", () => { + const portID = "foo123"; + const result = au.getPortIdOfSender( + ac.AlsoToMain({ type: "FOO" }, portID) + ); + assert.equal(result, portID); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/common/Dedupe.test.js b/browser/components/newtab/test/unit/common/Dedupe.test.js new file mode 100644 index 0000000000..1c85eafa50 --- /dev/null +++ b/browser/components/newtab/test/unit/common/Dedupe.test.js @@ -0,0 +1,38 @@ +import { Dedupe } from "common/Dedupe.sys.mjs"; + +describe("Dedupe", () => { + let instance; + beforeEach(() => { + instance = new Dedupe(); + }); + describe("group", () => { + it("should remove duplicates inside the groups", () => { + const beforeItems = [ + [1, 1, 1], + [2, 2, 2], + [3, 3, 3], + ]; + const afterItems = [[1], [2], [3]]; + assert.deepEqual(instance.group(...beforeItems), afterItems); + }); + it("should remove duplicates between groups, favouring earlier groups", () => { + const beforeItems = [ + [1, 2, 3], + [2, 3, 4], + [3, 4, 5], + ]; + const afterItems = [[1, 2, 3], [4], [5]]; + assert.deepEqual(instance.group(...beforeItems), afterItems); + }); + it("should remove duplicates from groups of objects", () => { + instance = new Dedupe(item => item.id); + const beforeItems = [ + [{ id: 1 }, { id: 1 }, { id: 2 }], + [{ id: 1 }, { id: 3 }, { id: 2 }], + [{ id: 1 }, { id: 2 }, { id: 5 }], + ]; + const afterItems = [[{ id: 1 }, { id: 2 }], [{ id: 3 }], [{ id: 5 }]]; + assert.deepEqual(instance.group(...beforeItems), afterItems); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/common/Reducers.test.js b/browser/components/newtab/test/unit/common/Reducers.test.js new file mode 100644 index 0000000000..7343fc6224 --- /dev/null +++ b/browser/components/newtab/test/unit/common/Reducers.test.js @@ -0,0 +1,1525 @@ +import { INITIAL_STATE, insertPinned, reducers } from "common/Reducers.sys.mjs"; +const { + TopSites, + App, + Prefs, + Dialog, + Sections, + Pocket, + Personalization, + DiscoveryStream, + Search, + ASRouter, +} = reducers; +import { actionTypes as at } from "common/Actions.sys.mjs"; + +describe("Reducers", () => { + describe("App", () => { + it("should return the initial state", () => { + const nextState = App(undefined, { type: "FOO" }); + assert.equal(nextState, INITIAL_STATE.App); + }); + it("should set initialized to true on INIT", () => { + const nextState = App(undefined, { type: "INIT" }); + + assert.propertyVal(nextState, "initialized", true); + }); + }); + describe("TopSites", () => { + it("should return the initial state", () => { + const nextState = TopSites(undefined, { type: "FOO" }); + assert.equal(nextState, INITIAL_STATE.TopSites); + }); + it("should add top sites on TOP_SITES_UPDATED", () => { + const newRows = [{ url: "foo.com" }, { url: "bar.com" }]; + const nextState = TopSites(undefined, { + type: at.TOP_SITES_UPDATED, + data: { links: newRows }, + }); + assert.equal(nextState.rows, newRows); + }); + it("should not update state for empty action.data on TOP_SITES_UPDATED", () => { + const nextState = TopSites(undefined, { type: at.TOP_SITES_UPDATED }); + assert.equal(nextState, INITIAL_STATE.TopSites); + }); + it("should initialize prefs on TOP_SITES_UPDATED", () => { + const nextState = TopSites(undefined, { + type: at.TOP_SITES_UPDATED, + data: { links: [], pref: "foo" }, + }); + + assert.equal(nextState.pref, "foo"); + }); + it("should pass prevState.prefs if not present in TOP_SITES_UPDATED", () => { + const nextState = TopSites( + { prefs: "foo" }, + { type: at.TOP_SITES_UPDATED, data: { links: [] } } + ); + + assert.equal(nextState.prefs, "foo"); + }); + it("should set editForm.site to action.data on TOP_SITES_EDIT", () => { + const data = { index: 7 }; + const nextState = TopSites(undefined, { type: at.TOP_SITES_EDIT, data }); + assert.equal(nextState.editForm.index, data.index); + }); + it("should set editForm to null on TOP_SITES_CANCEL_EDIT", () => { + const nextState = TopSites(undefined, { type: at.TOP_SITES_CANCEL_EDIT }); + assert.isNull(nextState.editForm); + }); + it("should preserve the editForm.index", () => { + const actionTypes = [ + at.PREVIEW_RESPONSE, + at.PREVIEW_REQUEST, + at.PREVIEW_REQUEST_CANCEL, + ]; + actionTypes.forEach(type => { + const oldState = { editForm: { index: 0, previewUrl: "foo" } }; + const action = { type, data: { url: "foo" } }; + const nextState = TopSites(oldState, action); + assert.equal(nextState.editForm.index, 0); + }); + }); + it("should set previewResponse on PREVIEW_RESPONSE", () => { + const oldState = { editForm: { previewUrl: "url" } }; + const action = { + type: at.PREVIEW_RESPONSE, + data: { preview: "data:123", url: "url" }, + }; + const nextState = TopSites(oldState, action); + assert.propertyVal(nextState.editForm, "previewResponse", "data:123"); + }); + it("should return previous state if action url does not match expected", () => { + const oldState = { editForm: { previewUrl: "foo" } }; + const action = { type: at.PREVIEW_RESPONSE, data: { url: "bar" } }; + const nextState = TopSites(oldState, action); + assert.equal(nextState, oldState); + }); + it("should return previous state if editForm is not set", () => { + const actionTypes = [ + at.PREVIEW_RESPONSE, + at.PREVIEW_REQUEST, + at.PREVIEW_REQUEST_CANCEL, + ]; + actionTypes.forEach(type => { + const oldState = { editForm: null }; + const action = { type, data: { url: "bar" } }; + const nextState = TopSites(oldState, action); + assert.equal(nextState, oldState, type); + }); + }); + it("should set previewResponse to null on PREVIEW_REQUEST", () => { + const oldState = { editForm: { previewResponse: "foo" } }; + const action = { type: at.PREVIEW_REQUEST, data: {} }; + const nextState = TopSites(oldState, action); + assert.propertyVal(nextState.editForm, "previewResponse", null); + }); + it("should set previewUrl on PREVIEW_REQUEST", () => { + const oldState = { editForm: {} }; + const action = { type: at.PREVIEW_REQUEST, data: { url: "bar" } }; + const nextState = TopSites(oldState, action); + assert.propertyVal(nextState.editForm, "previewUrl", "bar"); + }); + it("should add screenshots for SCREENSHOT_UPDATED", () => { + const oldState = { rows: [{ url: "foo.com" }, { url: "bar.com" }] }; + const action = { + type: at.SCREENSHOT_UPDATED, + data: { url: "bar.com", screenshot: "data:123" }, + }; + const nextState = TopSites(oldState, action); + assert.deepEqual(nextState.rows, [ + { url: "foo.com" }, + { url: "bar.com", screenshot: "data:123" }, + ]); + }); + it("should not modify rows if nothing matches the url for SCREENSHOT_UPDATED", () => { + const oldState = { rows: [{ url: "foo.com" }, { url: "bar.com" }] }; + const action = { + type: at.SCREENSHOT_UPDATED, + data: { url: "baz.com", screenshot: "data:123" }, + }; + const nextState = TopSites(oldState, action); + assert.deepEqual(nextState, oldState); + }); + it("should bookmark an item on PLACES_BOOKMARK_ADDED", () => { + const oldState = { rows: [{ url: "foo.com" }, { url: "bar.com" }] }; + const action = { + type: at.PLACES_BOOKMARK_ADDED, + data: { + url: "bar.com", + bookmarkGuid: "bookmark123", + bookmarkTitle: "Title for bar.com", + dateAdded: 1234567, + }, + }; + const nextState = TopSites(oldState, action); + const [, newRow] = nextState.rows; + // new row has bookmark data + assert.equal(newRow.url, action.data.url); + assert.equal(newRow.bookmarkGuid, action.data.bookmarkGuid); + assert.equal(newRow.bookmarkTitle, action.data.bookmarkTitle); + assert.equal(newRow.bookmarkDateCreated, action.data.dateAdded); + + // old row is unchanged + assert.equal(nextState.rows[0], oldState.rows[0]); + }); + it("should not update state for empty action.data on PLACES_BOOKMARK_ADDED", () => { + const nextState = TopSites(undefined, { type: at.PLACES_BOOKMARK_ADDED }); + assert.equal(nextState, INITIAL_STATE.TopSites); + }); + it("should remove a bookmark on PLACES_BOOKMARKS_REMOVED", () => { + const oldState = { + rows: [ + { url: "foo.com" }, + { + url: "bar.com", + bookmarkGuid: "bookmark123", + bookmarkTitle: "Title for bar.com", + dateAdded: 123456, + }, + ], + }; + const action = { + type: at.PLACES_BOOKMARKS_REMOVED, + data: { urls: ["bar.com"] }, + }; + const nextState = TopSites(oldState, action); + const [, newRow] = nextState.rows; + // new row no longer has bookmark data + assert.equal(newRow.url, oldState.rows[1].url); + assert.isUndefined(newRow.bookmarkGuid); + assert.isUndefined(newRow.bookmarkTitle); + assert.isUndefined(newRow.bookmarkDateCreated); + + // old row is unchanged + assert.deepEqual(nextState.rows[0], oldState.rows[0]); + }); + it("should not update state for empty action.data on PLACES_BOOKMARKS_REMOVED", () => { + const nextState = TopSites(undefined, { + type: at.PLACES_BOOKMARKS_REMOVED, + }); + assert.equal(nextState, INITIAL_STATE.TopSites); + }); + it("should update prefs on TOP_SITES_PREFS_UPDATED", () => { + const state = TopSites( + {}, + { type: at.TOP_SITES_PREFS_UPDATED, data: { pref: "foo" } } + ); + + assert.equal(state.pref, "foo"); + }); + it("should not update state for empty action.data on PLACES_LINKS_DELETED", () => { + const nextState = TopSites(undefined, { type: at.PLACES_LINKS_DELETED }); + assert.equal(nextState, INITIAL_STATE.TopSites); + }); + it("should remove the site on PLACES_LINKS_DELETED", () => { + const oldState = { rows: [{ url: "foo.com" }, { url: "bar.com" }] }; + const deleteAction = { + type: at.PLACES_LINKS_DELETED, + data: { urls: ["foo.com"] }, + }; + const nextState = TopSites(oldState, deleteAction); + assert.deepEqual(nextState.rows, [{ url: "bar.com" }]); + }); + it("should set showSearchShortcutsForm to true on TOP_SITES_OPEN_SEARCH_SHORTCUTS_MODAL", () => { + const data = { index: 7 }; + const nextState = TopSites(undefined, { + type: at.TOP_SITES_OPEN_SEARCH_SHORTCUTS_MODAL, + data, + }); + assert.isTrue(nextState.showSearchShortcutsForm); + }); + it("should set showSearchShortcutsForm to false on TOP_SITES_CLOSE_SEARCH_SHORTCUTS_MODAL", () => { + const nextState = TopSites(undefined, { + type: at.TOP_SITES_CLOSE_SEARCH_SHORTCUTS_MODAL, + }); + assert.isFalse(nextState.showSearchShortcutsForm); + }); + it("should update searchShortcuts on UPDATE_SEARCH_SHORTCUTS", () => { + const shortcuts = [ + { + keyword: "@google", + shortURL: "google", + url: "https://google.com", + searchIdentifier: /^google/, + }, + { + keyword: "@baidu", + shortURL: "baidu", + url: "https://baidu.com", + searchIdentifier: /^baidu/, + }, + ]; + const nextState = TopSites(undefined, { + type: at.UPDATE_SEARCH_SHORTCUTS, + data: { searchShortcuts: shortcuts }, + }); + assert.deepEqual(shortcuts, nextState.searchShortcuts); + }); + it("should set sov positions and state", () => { + const positions = [ + { position: 0, assignedPartner: "amp" }, + { position: 1, assignedPartner: "moz-sales" }, + ]; + const nextState = TopSites(undefined, { + type: at.SOV_UPDATED, + data: { ready: true, positions }, + }); + assert.equal(nextState.sov.ready, true); + assert.equal(nextState.sov.positions, positions); + }); + }); + describe("Prefs", () => { + function prevState(custom = {}) { + return Object.assign({}, INITIAL_STATE.Prefs, custom); + } + it("should have the correct initial state", () => { + const state = Prefs(undefined, {}); + assert.deepEqual(state, INITIAL_STATE.Prefs); + }); + describe("PREFS_INITIAL_VALUES", () => { + it("should return a new object", () => { + const state = Prefs(undefined, { + type: at.PREFS_INITIAL_VALUES, + data: {}, + }); + assert.notEqual( + INITIAL_STATE.Prefs, + state, + "should not modify INITIAL_STATE" + ); + }); + it("should set initalized to true", () => { + const state = Prefs(undefined, { + type: at.PREFS_INITIAL_VALUES, + data: {}, + }); + assert.isTrue(state.initialized); + }); + it("should set .values", () => { + const newValues = { foo: 1, bar: 2 }; + const state = Prefs(undefined, { + type: at.PREFS_INITIAL_VALUES, + data: newValues, + }); + assert.equal(state.values, newValues); + }); + }); + describe("PREF_CHANGED", () => { + it("should return a new Prefs object", () => { + const state = Prefs(undefined, { + type: at.PREF_CHANGED, + data: { name: "foo", value: 2 }, + }); + assert.notEqual( + INITIAL_STATE.Prefs, + state, + "should not modify INITIAL_STATE" + ); + }); + it("should set the changed pref", () => { + const state = Prefs(prevState({ foo: 1 }), { + type: at.PREF_CHANGED, + data: { name: "foo", value: 2 }, + }); + assert.equal(state.values.foo, 2); + }); + it("should return a new .pref object instead of mutating", () => { + const oldState = prevState({ foo: 1 }); + const state = Prefs(oldState, { + type: at.PREF_CHANGED, + data: { name: "foo", value: 2 }, + }); + assert.notEqual(oldState.values, state.values); + }); + }); + }); + describe("Dialog", () => { + it("should return INITIAL_STATE by default", () => { + assert.equal( + INITIAL_STATE.Dialog, + Dialog(undefined, { type: "non_existent" }) + ); + }); + it("should toggle visible to true on DIALOG_OPEN", () => { + const action = { type: at.DIALOG_OPEN }; + const nextState = Dialog(INITIAL_STATE.Dialog, action); + assert.isTrue(nextState.visible); + }); + it("should pass url data on DIALOG_OPEN", () => { + const action = { type: at.DIALOG_OPEN, data: "some url" }; + const nextState = Dialog(INITIAL_STATE.Dialog, action); + assert.equal(nextState.data, action.data); + }); + it("should toggle visible to false on DIALOG_CANCEL", () => { + const action = { type: at.DIALOG_CANCEL, data: "some url" }; + const nextState = Dialog(INITIAL_STATE.Dialog, action); + assert.isFalse(nextState.visible); + }); + it("should return inital state on DELETE_HISTORY_URL", () => { + const action = { type: at.DELETE_HISTORY_URL }; + const nextState = Dialog(INITIAL_STATE.Dialog, action); + + assert.deepEqual(INITIAL_STATE.Dialog, nextState); + }); + }); + describe("Sections", () => { + let oldState; + + beforeEach(() => { + oldState = new Array(5).fill(null).map((v, i) => ({ + id: `foo_bar_${i}`, + title: `Foo Bar ${i}`, + initialized: false, + rows: [ + { url: "www.foo.bar", pocket_id: 123 }, + { url: "www.other.url" }, + ], + order: i, + type: "history", + })); + }); + + it("should return INITIAL_STATE by default", () => { + assert.equal( + INITIAL_STATE.Sections, + Sections(undefined, { type: "non_existent" }) + ); + }); + it("should remove the correct section on SECTION_DEREGISTER", () => { + const newState = Sections(oldState, { + type: at.SECTION_DEREGISTER, + data: "foo_bar_2", + }); + assert.lengthOf(newState, 4); + const expectedNewState = oldState.splice(2, 1) && oldState; + assert.deepEqual(newState, expectedNewState); + }); + it("should add a section on SECTION_REGISTER if it doesn't already exist", () => { + const action = { + type: at.SECTION_REGISTER, + data: { id: "foo_bar_5", title: "Foo Bar 5" }, + }; + const newState = Sections(oldState, action); + assert.lengthOf(newState, 6); + const insertedSection = newState.find( + section => section.id === "foo_bar_5" + ); + assert.propertyVal(insertedSection, "title", action.data.title); + }); + it("should set newSection.rows === [] if no rows are provided on SECTION_REGISTER", () => { + const action = { + type: at.SECTION_REGISTER, + data: { id: "foo_bar_5", title: "Foo Bar 5" }, + }; + const newState = Sections(oldState, action); + const insertedSection = newState.find( + section => section.id === "foo_bar_5" + ); + assert.deepEqual(insertedSection.rows, []); + }); + it("should update a section on SECTION_REGISTER if it already exists", () => { + const NEW_TITLE = "New Title"; + const action = { + type: at.SECTION_REGISTER, + data: { id: "foo_bar_2", title: NEW_TITLE }, + }; + const newState = Sections(oldState, action); + assert.lengthOf(newState, 5); + const updatedSection = newState.find( + section => section.id === "foo_bar_2" + ); + assert.ok(updatedSection && updatedSection.title === NEW_TITLE); + }); + it("should set initialized to false on SECTION_REGISTER if there are no rows", () => { + const NEW_TITLE = "New Title"; + const action = { + type: at.SECTION_REGISTER, + data: { id: "bloop", title: NEW_TITLE }, + }; + const newState = Sections(oldState, action); + const updatedSection = newState.find(section => section.id === "bloop"); + assert.propertyVal(updatedSection, "initialized", false); + }); + it("should set initialized to true on SECTION_REGISTER if there are rows", () => { + const NEW_TITLE = "New Title"; + const action = { + type: at.SECTION_REGISTER, + data: { id: "bloop", title: NEW_TITLE, rows: [{}, {}] }, + }; + const newState = Sections(oldState, action); + const updatedSection = newState.find(section => section.id === "bloop"); + assert.propertyVal(updatedSection, "initialized", true); + }); + it("should have no effect on SECTION_UPDATE if the id doesn't exist", () => { + const action = { + type: at.SECTION_UPDATE, + data: { id: "fake_id", data: "fake_data" }, + }; + const newState = Sections(oldState, action); + assert.deepEqual(oldState, newState); + }); + it("should update the section with the correct data on SECTION_UPDATE", () => { + const FAKE_DATA = { rows: ["some", "fake", "data"], foo: "bar" }; + const action = { + type: at.SECTION_UPDATE, + data: Object.assign(FAKE_DATA, { id: "foo_bar_2" }), + }; + const newState = Sections(oldState, action); + const updatedSection = newState.find( + section => section.id === "foo_bar_2" + ); + assert.include(updatedSection, FAKE_DATA); + }); + it("should set initialized to true on SECTION_UPDATE if rows is defined on action.data", () => { + const data = { rows: [], id: "foo_bar_2" }; + const action = { type: at.SECTION_UPDATE, data }; + const newState = Sections(oldState, action); + const updatedSection = newState.find( + section => section.id === "foo_bar_2" + ); + assert.propertyVal(updatedSection, "initialized", true); + }); + it("should retain pinned cards on SECTION_UPDATE", () => { + const ROW = { id: "row" }; + let newState = Sections(oldState, { + type: at.SECTION_UPDATE, + data: Object.assign({ rows: [ROW] }, { id: "foo_bar_2" }), + }); + let updatedSection = newState.find(section => section.id === "foo_bar_2"); + assert.deepEqual(updatedSection.rows, [ROW]); + + const PINNED_ROW = { id: "pinned", pinned: true, guid: "pinned" }; + newState = Sections(newState, { + type: at.SECTION_UPDATE, + data: Object.assign({ rows: [PINNED_ROW] }, { id: "foo_bar_2" }), + }); + updatedSection = newState.find(section => section.id === "foo_bar_2"); + assert.deepEqual(updatedSection.rows, [PINNED_ROW]); + + // Updating the section again should not duplicate pinned cards + newState = Sections(newState, { + type: at.SECTION_UPDATE, + data: Object.assign({ rows: [PINNED_ROW] }, { id: "foo_bar_2" }), + }); + updatedSection = newState.find(section => section.id === "foo_bar_2"); + assert.deepEqual(updatedSection.rows, [PINNED_ROW]); + + // Updating the section should retain pinned card at its index + newState = Sections(newState, { + type: at.SECTION_UPDATE, + data: Object.assign({ rows: [ROW] }, { id: "foo_bar_2" }), + }); + updatedSection = newState.find(section => section.id === "foo_bar_2"); + assert.deepEqual(updatedSection.rows, [PINNED_ROW, ROW]); + + // Clearing/Resetting the section should clear pinned cards + newState = Sections(newState, { + type: at.SECTION_UPDATE, + data: Object.assign({ rows: [] }, { id: "foo_bar_2" }), + }); + updatedSection = newState.find(section => section.id === "foo_bar_2"); + assert.deepEqual(updatedSection.rows, []); + }); + it("should have no effect on SECTION_UPDATE_CARD if the id or url doesn't exist", () => { + const noIdAction = { + type: at.SECTION_UPDATE_CARD, + data: { + id: "non-existent", + url: "www.foo.bar", + options: { title: "New title" }, + }, + }; + const noIdState = Sections(oldState, noIdAction); + const noUrlAction = { + type: at.SECTION_UPDATE_CARD, + data: { + id: "foo_bar_2", + url: "www.non-existent.url", + options: { title: "New title" }, + }, + }; + const noUrlState = Sections(oldState, noUrlAction); + assert.deepEqual(noIdState, oldState); + assert.deepEqual(noUrlState, oldState); + }); + it("should update the card with the correct data on SECTION_UPDATE_CARD", () => { + const action = { + type: at.SECTION_UPDATE_CARD, + data: { + id: "foo_bar_2", + url: "www.other.url", + options: { title: "Fake new title" }, + }, + }; + const newState = Sections(oldState, action); + const updatedSection = newState.find( + section => section.id === "foo_bar_2" + ); + const updatedCard = updatedSection.rows.find( + card => card.url === "www.other.url" + ); + assert.propertyVal(updatedCard, "title", "Fake new title"); + }); + it("should only update the cards belonging to the right section on SECTION_UPDATE_CARD", () => { + const action = { + type: at.SECTION_UPDATE_CARD, + data: { + id: "foo_bar_2", + url: "www.other.url", + options: { title: "Fake new title" }, + }, + }; + const newState = Sections(oldState, action); + newState.forEach((section, i) => { + if (section.id !== "foo_bar_2") { + assert.deepEqual(section, oldState[i]); + } + }); + }); + it("should allow action.data to set .initialized", () => { + const data = { rows: [], initialized: false, id: "foo_bar_2" }; + const action = { type: at.SECTION_UPDATE, data }; + const newState = Sections(oldState, action); + const updatedSection = newState.find( + section => section.id === "foo_bar_2" + ); + assert.propertyVal(updatedSection, "initialized", false); + }); + it("should dedupe based on dedupeConfigurations", () => { + const site = { url: "foo.com" }; + const highlights = { rows: [site], id: "highlights" }; + const topstories = { rows: [site], id: "topstories" }; + const dedupeConfigurations = [ + { id: "topstories", dedupeFrom: ["highlights"] }, + ]; + const action = { data: { dedupeConfigurations }, type: "SECTION_UPDATE" }; + const state = [highlights, topstories]; + + const nextState = Sections(state, action); + + assert.equal(nextState.find(s => s.id === "highlights").rows.length, 1); + assert.equal(nextState.find(s => s.id === "topstories").rows.length, 0); + }); + it("should remove blocked and deleted urls from all rows in all sections", () => { + const blockAction = { + type: at.PLACES_LINK_BLOCKED, + data: { url: "www.foo.bar" }, + }; + const deleteAction = { + type: at.PLACES_LINKS_DELETED, + data: { urls: ["www.foo.bar"] }, + }; + const newBlockState = Sections(oldState, blockAction); + const newDeleteState = Sections(oldState, deleteAction); + newBlockState.concat(newDeleteState).forEach(section => { + assert.deepEqual(section.rows, [{ url: "www.other.url" }]); + }); + }); + it("should not update state for empty action.data on PLACES_LINK_BLOCKED", () => { + const nextState = Sections(undefined, { type: at.PLACES_LINK_BLOCKED }); + assert.equal(nextState, INITIAL_STATE.Sections); + }); + it("should not update state for empty action.data on PLACES_LINKS_DELETED", () => { + const nextState = Sections(undefined, { type: at.PLACES_LINKS_DELETED }); + assert.equal(nextState, INITIAL_STATE.Sections); + }); + it("should remove all removed pocket urls", () => { + const removeAction = { + type: at.DELETE_FROM_POCKET, + data: { pocket_id: 123 }, + }; + const newBlockState = Sections(oldState, removeAction); + newBlockState.forEach(section => { + assert.deepEqual(section.rows, [{ url: "www.other.url" }]); + }); + }); + it("should archive all archived pocket urls", () => { + const removeAction = { + type: at.ARCHIVE_FROM_POCKET, + data: { pocket_id: 123 }, + }; + const newBlockState = Sections(oldState, removeAction); + newBlockState.forEach(section => { + assert.deepEqual(section.rows, [{ url: "www.other.url" }]); + }); + }); + it("should not update state for empty action.data on PLACES_BOOKMARK_ADDED", () => { + const nextState = Sections(undefined, { type: at.PLACES_BOOKMARK_ADDED }); + assert.equal(nextState, INITIAL_STATE.Sections); + }); + it("should bookmark an item when PLACES_BOOKMARK_ADDED is received", () => { + const action = { + type: at.PLACES_BOOKMARK_ADDED, + data: { + url: "www.foo.bar", + bookmarkGuid: "bookmark123", + bookmarkTitle: "Title for bar.com", + dateAdded: 1234567, + }, + }; + const nextState = Sections(oldState, action); + // check a section to ensure the correct url was bookmarked + const [newRow, oldRow] = nextState[0].rows; + + // new row has bookmark data + assert.equal(newRow.url, action.data.url); + assert.equal(newRow.type, "bookmark"); + assert.equal(newRow.bookmarkGuid, action.data.bookmarkGuid); + assert.equal(newRow.bookmarkTitle, action.data.bookmarkTitle); + assert.equal(newRow.bookmarkDateCreated, action.data.dateAdded); + + // old row is unchanged + assert.equal(oldRow, oldState[0].rows[1]); + }); + it("should not update state for empty action.data on PLACES_BOOKMARKS_REMOVED", () => { + const nextState = Sections(undefined, { + type: at.PLACES_BOOKMARKS_REMOVED, + }); + assert.equal(nextState, INITIAL_STATE.Sections); + }); + it("should remove the bookmark when PLACES_BOOKMARKS_REMOVED is received", () => { + const action = { + type: at.PLACES_BOOKMARKS_REMOVED, + data: { + urls: ["www.foo.bar"], + bookmarkGuid: "bookmark123", + }, + }; + // add some bookmark data for the first url in rows + oldState.forEach(item => { + item.rows[0].bookmarkGuid = "bookmark123"; + item.rows[0].bookmarkTitle = "Title for bar.com"; + item.rows[0].bookmarkDateCreated = 1234567; + item.rows[0].type = "bookmark"; + }); + const nextState = Sections(oldState, action); + // check a section to ensure the correct bookmark was removed + const [newRow, oldRow] = nextState[0].rows; + + // new row isn't a bookmark + assert.equal(newRow.url, action.data.urls[0]); + assert.equal(newRow.type, "history"); + assert.isUndefined(newRow.bookmarkGuid); + assert.isUndefined(newRow.bookmarkTitle); + assert.isUndefined(newRow.bookmarkDateCreated); + + // old row is unchanged + assert.equal(oldRow, oldState[0].rows[1]); + }); + it("should not update state for empty action.data on PLACES_SAVED_TO_POCKET", () => { + const nextState = Sections(undefined, { + type: at.PLACES_SAVED_TO_POCKET, + }); + assert.equal(nextState, INITIAL_STATE.Sections); + }); + it("should add a pocked item on PLACES_SAVED_TO_POCKET", () => { + const action = { + type: at.PLACES_SAVED_TO_POCKET, + data: { + url: "www.foo.bar", + pocket_id: 1234, + title: "Title for bar.com", + }, + }; + const nextState = Sections(oldState, action); + // check a section to ensure the correct url was saved to pocket + const [newRow, oldRow] = nextState[0].rows; + + // new row has pocket data + assert.equal(newRow.url, action.data.url); + assert.equal(newRow.type, "pocket"); + assert.equal(newRow.pocket_id, action.data.pocket_id); + assert.equal(newRow.title, action.data.title); + + // old row is unchanged + assert.equal(oldRow, oldState[0].rows[1]); + }); + }); + describe("#insertPinned", () => { + let links; + + beforeEach(() => { + links = new Array(12).fill(null).map((v, i) => ({ url: `site${i}.com` })); + }); + + it("should place pinned links where they belong", () => { + const pinned = [ + { url: "http://github.com/mozilla/activity-stream", title: "moz/a-s" }, + { url: "http://example.com", title: "example" }, + ]; + const result = insertPinned(links, pinned); + for (let index of [0, 1]) { + assert.equal(result[index].url, pinned[index].url); + assert.ok(result[index].isPinned); + assert.equal(result[index].pinIndex, index); + } + assert.deepEqual(result.slice(2), links); + }); + it("should handle empty slots in the pinned list", () => { + const pinned = [ + null, + { url: "http://github.com/mozilla/activity-stream", title: "moz/a-s" }, + null, + null, + { url: "http://example.com", title: "example" }, + ]; + const result = insertPinned(links, pinned); + for (let index of [1, 4]) { + assert.equal(result[index].url, pinned[index].url); + assert.ok(result[index].isPinned); + assert.equal(result[index].pinIndex, index); + } + result.splice(4, 1); + result.splice(1, 1); + assert.deepEqual(result, links); + }); + it("should handle a pinned site past the end of the list of links", () => { + const pinned = []; + pinned[11] = { + url: "http://github.com/mozilla/activity-stream", + title: "moz/a-s", + }; + const result = insertPinned([], pinned); + assert.equal(result[11].url, pinned[11].url); + assert.isTrue(result[11].isPinned); + assert.equal(result[11].pinIndex, 11); + }); + it("should unpin previously pinned links no longer in the pinned list", () => { + const pinned = []; + links[2].isPinned = true; + links[2].pinIndex = 2; + const result = insertPinned(links, pinned); + assert.notProperty(result[2], "isPinned"); + assert.notProperty(result[2], "pinIndex"); + }); + it("should handle a link present in both the links and pinned list", () => { + const pinned = [links[7]]; + const result = insertPinned(links, pinned); + assert.equal(links.length, result.length); + }); + it("should not modify the original data", () => { + const pinned = [{ url: "http://example.com" }]; + + insertPinned(links, pinned); + + assert.equal(typeof pinned[0].isPinned, "undefined"); + }); + }); + describe("Pocket", () => { + it("should return INITIAL_STATE by default", () => { + assert.equal( + Pocket(undefined, { type: "some_action" }), + INITIAL_STATE.Pocket + ); + }); + it("should set waitingForSpoc on a POCKET_WAITING_FOR_SPOC action", () => { + const state = Pocket(undefined, { + type: at.POCKET_WAITING_FOR_SPOC, + data: false, + }); + assert.isFalse(state.waitingForSpoc); + }); + it("should have undefined for initial isUserLoggedIn state", () => { + assert.isNull(Pocket(undefined, { type: "some_action" }).isUserLoggedIn); + }); + it("should set isUserLoggedIn to false on a POCKET_LOGGED_IN with null", () => { + const state = Pocket(undefined, { + type: at.POCKET_LOGGED_IN, + data: null, + }); + assert.isFalse(state.isUserLoggedIn); + }); + it("should set isUserLoggedIn to false on a POCKET_LOGGED_IN with false", () => { + const state = Pocket(undefined, { + type: at.POCKET_LOGGED_IN, + data: false, + }); + assert.isFalse(state.isUserLoggedIn); + }); + it("should set isUserLoggedIn to true on a POCKET_LOGGED_IN with true", () => { + const state = Pocket(undefined, { + type: at.POCKET_LOGGED_IN, + data: true, + }); + assert.isTrue(state.isUserLoggedIn); + }); + it("should set pocketCta with correct object on a POCKET_CTA", () => { + const data = { + cta_button: "cta button", + cta_text: "cta text", + cta_url: "https://cta-url.com", + use_cta: true, + }; + const state = Pocket(undefined, { type: at.POCKET_CTA, data }); + assert.equal(state.pocketCta.ctaButton, data.cta_button); + assert.equal(state.pocketCta.ctaText, data.cta_text); + assert.equal(state.pocketCta.ctaUrl, data.cta_url); + assert.equal(state.pocketCta.useCta, data.use_cta); + }); + }); + describe("Personalization", () => { + it("should return INITIAL_STATE by default", () => { + assert.equal( + Personalization(undefined, { type: "some_action" }), + INITIAL_STATE.Personalization + ); + }); + it("should set lastUpdated with DISCOVERY_STREAM_PERSONALIZATION_LAST_UPDATED", () => { + const state = Personalization(undefined, { + type: at.DISCOVERY_STREAM_PERSONALIZATION_LAST_UPDATED, + data: { + lastUpdated: 123, + }, + }); + assert.equal(state.lastUpdated, 123); + }); + it("should set initialized to true with DISCOVERY_STREAM_PERSONALIZATION_INIT", () => { + const state = Personalization(undefined, { + type: at.DISCOVERY_STREAM_PERSONALIZATION_INIT, + }); + assert.equal(state.initialized, true); + }); + }); + describe("DiscoveryStream", () => { + it("should return INITIAL_STATE by default", () => { + assert.equal( + DiscoveryStream(undefined, { type: "some_action" }), + INITIAL_STATE.DiscoveryStream + ); + }); + it("should set isPrivacyInfoModalVisible to true with SHOW_PRIVACY_INFO", () => { + const state = DiscoveryStream(undefined, { + type: at.SHOW_PRIVACY_INFO, + }); + assert.equal(state.isPrivacyInfoModalVisible, true); + }); + it("should set isPrivacyInfoModalVisible to false with HIDE_PRIVACY_INFO", () => { + const state = DiscoveryStream(undefined, { + type: at.HIDE_PRIVACY_INFO, + }); + assert.equal(state.isPrivacyInfoModalVisible, false); + }); + it("should set layout data with DISCOVERY_STREAM_LAYOUT_UPDATE", () => { + const state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: ["test"] }, + }); + assert.equal(state.layout[0], "test"); + }); + it("should reset layout data with DISCOVERY_STREAM_LAYOUT_RESET", () => { + const layoutData = { layout: ["test"], lastUpdated: 123 }; + const feedsData = { + "https://foo.com/feed1": { lastUpdated: 123, data: [1, 2, 3] }, + }; + const spocsData = { + lastUpdated: 123, + spocs: [1, 2, 3], + }; + let state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: layoutData, + }); + state = DiscoveryStream(state, { + type: at.DISCOVERY_STREAM_FEEDS_UPDATE, + data: feedsData, + }); + state = DiscoveryStream(state, { + type: at.DISCOVERY_STREAM_SPOCS_UPDATE, + data: spocsData, + }); + state = DiscoveryStream(state, { + type: at.DISCOVERY_STREAM_LAYOUT_RESET, + }); + + assert.deepEqual(state, INITIAL_STATE.DiscoveryStream); + }); + it("should set config data with DISCOVERY_STREAM_CONFIG_CHANGE", () => { + const state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_CONFIG_CHANGE, + data: { enabled: true }, + }); + assert.deepEqual(state.config, { enabled: true }); + }); + it("should set recentSavesEnabled with DISCOVERY_STREAM_PREFS_SETUP", () => { + const state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_PREFS_SETUP, + data: { recentSavesEnabled: true }, + }); + assert.isTrue(state.recentSavesEnabled); + }); + it("should set recentSavesData with DISCOVERY_STREAM_RECENT_SAVES", () => { + const state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_RECENT_SAVES, + data: { recentSaves: [1, 2, 3] }, + }); + assert.deepEqual(state.recentSavesData, [1, 2, 3]); + }); + it("should set isUserLoggedIn with DISCOVERY_STREAM_POCKET_STATE_SET", () => { + const state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_POCKET_STATE_SET, + data: { isUserLoggedIn: true }, + }); + assert.isTrue(state.isUserLoggedIn); + }); + it("should set feeds as loaded with DISCOVERY_STREAM_FEEDS_UPDATE", () => { + const state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_FEEDS_UPDATE, + }); + assert.isTrue(state.feeds.loaded); + }); + it("should set spoc_endpoint with DISCOVERY_STREAM_SPOCS_ENDPOINT", () => { + const state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_SPOCS_ENDPOINT, + data: { url: "foo.com" }, + }); + assert.equal(state.spocs.spocs_endpoint, "foo.com"); + }); + it("should use initial state with DISCOVERY_STREAM_SPOCS_PLACEMENTS", () => { + const state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_SPOCS_PLACEMENTS, + data: {}, + }); + assert.deepEqual(state.spocs.placements, []); + }); + it("should set placements with DISCOVERY_STREAM_SPOCS_PLACEMENTS", () => { + const state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_SPOCS_PLACEMENTS, + data: { + placements: [1, 2, 3], + }, + }); + assert.deepEqual(state.spocs.placements, [1, 2, 3]); + }); + it("should set spocs with DISCOVERY_STREAM_SPOCS_UPDATE", () => { + const data = { + lastUpdated: 123, + spocs: [1, 2, 3], + }; + const state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_SPOCS_UPDATE, + data, + }); + assert.deepEqual(state.spocs, { + spocs_endpoint: "", + data: [1, 2, 3], + lastUpdated: 123, + loaded: true, + frequency_caps: [], + blocked: [], + placements: [], + }); + }); + it("should default to a single spoc placement", () => { + const deleteAction = { + type: at.DISCOVERY_STREAM_LINK_BLOCKED, + data: { url: "https://foo.com" }, + }; + const oldState = { + spocs: { + data: { + spocs: { + items: [ + { + url: "test-spoc.com", + }, + ], + }, + }, + loaded: true, + }, + feeds: { + data: {}, + loaded: true, + }, + }; + + const newState = DiscoveryStream(oldState, deleteAction); + + assert.equal(newState.spocs.data.spocs.items.length, 1); + }); + it("should handle no data from DISCOVERY_STREAM_SPOCS_UPDATE", () => { + const data = null; + const state = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_SPOCS_UPDATE, + data, + }); + assert.deepEqual(state.spocs, INITIAL_STATE.DiscoveryStream.spocs); + }); + it("should add blocked spocs to blocked array with DISCOVERY_STREAM_SPOC_BLOCKED", () => { + const firstState = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_SPOC_BLOCKED, + data: { url: "https://foo.com" }, + }); + const secondState = DiscoveryStream(firstState, { + type: at.DISCOVERY_STREAM_SPOC_BLOCKED, + data: { url: "https://bar.com" }, + }); + assert.deepEqual(firstState.spocs.blocked, ["https://foo.com"]); + assert.deepEqual(secondState.spocs.blocked, [ + "https://foo.com", + "https://bar.com", + ]); + }); + it("should not update state for empty action.data on DISCOVERY_STREAM_LINK_BLOCKED", () => { + const newState = DiscoveryStream(undefined, { + type: at.DISCOVERY_STREAM_LINK_BLOCKED, + }); + assert.equal(newState, INITIAL_STATE.DiscoveryStream); + }); + it("should not update state if feeds are not loaded", () => { + const deleteAction = { + type: at.DISCOVERY_STREAM_LINK_BLOCKED, + data: { url: "foo.com" }, + }; + const newState = DiscoveryStream(undefined, deleteAction); + assert.equal(newState, INITIAL_STATE.DiscoveryStream); + }); + it("should not update state if spocs and feeds data is undefined", () => { + const deleteAction = { + type: at.DISCOVERY_STREAM_LINK_BLOCKED, + data: { url: "foo.com" }, + }; + const oldState = { + spocs: { + data: {}, + loaded: true, + placements: [{ name: "spocs" }], + }, + feeds: { + data: {}, + loaded: true, + }, + }; + const newState = DiscoveryStream(oldState, deleteAction); + assert.deepEqual(newState, oldState); + }); + it("should remove the site on DISCOVERY_STREAM_LINK_BLOCKED from spocs if feeds data is empty", () => { + const deleteAction = { + type: at.DISCOVERY_STREAM_LINK_BLOCKED, + data: { url: "https://foo.com" }, + }; + const oldState = { + spocs: { + data: { + spocs: { + items: [{ url: "https://foo.com" }, { url: "test-spoc.com" }], + }, + }, + loaded: true, + placements: [{ name: "spocs" }], + }, + feeds: { + data: {}, + loaded: true, + }, + }; + const newState = DiscoveryStream(oldState, deleteAction); + assert.deepEqual(newState.spocs.data.spocs.items, [ + { url: "test-spoc.com" }, + ]); + }); + it("should remove the site on DISCOVERY_STREAM_LINK_BLOCKED from feeds if spocs data is empty", () => { + const deleteAction = { + type: at.DISCOVERY_STREAM_LINK_BLOCKED, + data: { url: "https://foo.com" }, + }; + const oldState = { + spocs: { + data: {}, + loaded: true, + placements: [{ name: "spocs" }], + }, + feeds: { + data: { + "https://foo.com/feed1": { + data: { + recommendations: [ + { url: "https://foo.com" }, + { url: "test.com" }, + ], + }, + }, + }, + loaded: true, + }, + }; + const newState = DiscoveryStream(oldState, deleteAction); + assert.deepEqual( + newState.feeds.data["https://foo.com/feed1"].data.recommendations, + [{ url: "test.com" }] + ); + }); + it("should remove the site on DISCOVERY_STREAM_LINK_BLOCKED from both feeds and spocs", () => { + const oldState = { + feeds: { + data: { + "https://foo.com/feed1": { + data: { + recommendations: [ + { url: "https://foo.com" }, + { url: "test.com" }, + ], + }, + }, + }, + loaded: true, + }, + spocs: { + data: { + spocs: { + items: [{ url: "https://foo.com" }, { url: "test-spoc.com" }], + }, + }, + loaded: true, + placements: [{ name: "spocs" }], + }, + }; + const deleteAction = { + type: at.DISCOVERY_STREAM_LINK_BLOCKED, + data: { url: "https://foo.com" }, + }; + const newState = DiscoveryStream(oldState, deleteAction); + assert.deepEqual(newState.spocs.data.spocs.items, [ + { url: "test-spoc.com" }, + ]); + assert.deepEqual( + newState.feeds.data["https://foo.com/feed1"].data.recommendations, + [{ url: "test.com" }] + ); + }); + it("should not update state for empty action.data on PLACES_SAVED_TO_POCKET", () => { + const newState = DiscoveryStream(undefined, { + type: at.PLACES_SAVED_TO_POCKET, + }); + assert.equal(newState, INITIAL_STATE.DiscoveryStream); + }); + it("should add pocket_id on PLACES_SAVED_TO_POCKET in both feeds and spocs", () => { + const oldState = { + feeds: { + data: { + "https://foo.com/feed1": { + data: { + recommendations: [ + { url: "https://foo.com" }, + { url: "test.com" }, + ], + }, + }, + }, + loaded: true, + }, + spocs: { + data: { + spocs: { + items: [{ url: "https://foo.com" }, { url: "test-spoc.com" }], + }, + }, + placements: [{ name: "spocs" }], + loaded: true, + }, + }; + const action = { + type: at.PLACES_SAVED_TO_POCKET, + data: { + url: "https://foo.com", + pocket_id: 1234, + open_url: "https://foo-1234", + }, + }; + + const newState = DiscoveryStream(oldState, action); + + assert.lengthOf(newState.spocs.data.spocs.items, 2); + assert.equal( + newState.spocs.data.spocs.items[0].pocket_id, + action.data.pocket_id + ); + assert.equal( + newState.spocs.data.spocs.items[0].open_url, + action.data.open_url + ); + assert.isUndefined(newState.spocs.data.spocs.items[1].pocket_id); + + assert.lengthOf( + newState.feeds.data["https://foo.com/feed1"].data.recommendations, + 2 + ); + assert.equal( + newState.feeds.data["https://foo.com/feed1"].data.recommendations[0] + .pocket_id, + action.data.pocket_id + ); + assert.equal( + newState.feeds.data["https://foo.com/feed1"].data.recommendations[0] + .open_url, + action.data.open_url + ); + assert.isUndefined( + newState.feeds.data["https://foo.com/feed1"].data.recommendations[1] + .pocket_id + ); + }); + it("should not update state for empty action.data on DELETE_FROM_POCKET", () => { + const newState = DiscoveryStream(undefined, { + type: at.DELETE_FROM_POCKET, + }); + assert.equal(newState, INITIAL_STATE.DiscoveryStream); + }); + it("should remove site on DELETE_FROM_POCKET in both feeds and spocs", () => { + const oldState = { + feeds: { + data: { + "https://foo.com/feed1": { + data: { + recommendations: [ + { url: "https://foo.com", pocket_id: 1234 }, + { url: "test.com" }, + ], + }, + }, + }, + loaded: true, + }, + spocs: { + data: { + spocs: { + items: [ + { url: "https://foo.com", pocket_id: 1234 }, + { url: "test-spoc.com" }, + ], + }, + }, + loaded: true, + placements: [{ name: "spocs" }], + }, + }; + const deleteAction = { + type: at.DELETE_FROM_POCKET, + data: { + pocket_id: 1234, + }, + }; + + const newState = DiscoveryStream(oldState, deleteAction); + assert.deepEqual(newState.spocs.data.spocs.items, [ + { url: "test-spoc.com" }, + ]); + assert.deepEqual( + newState.feeds.data["https://foo.com/feed1"].data.recommendations, + [{ url: "test.com" }] + ); + }); + it("should remove site on ARCHIVE_FROM_POCKET in both feeds and spocs", () => { + const oldState = { + feeds: { + data: { + "https://foo.com/feed1": { + data: { + recommendations: [ + { url: "https://foo.com", pocket_id: 1234 }, + { url: "test.com" }, + ], + }, + }, + }, + loaded: true, + }, + spocs: { + data: { + spocs: { + items: [ + { url: "https://foo.com", pocket_id: 1234 }, + { url: "test-spoc.com" }, + ], + }, + }, + loaded: true, + placements: [{ name: "spocs" }], + }, + }; + const deleteAction = { + type: at.ARCHIVE_FROM_POCKET, + data: { + pocket_id: 1234, + }, + }; + + const newState = DiscoveryStream(oldState, deleteAction); + assert.deepEqual(newState.spocs.data.spocs.items, [ + { url: "test-spoc.com" }, + ]); + assert.deepEqual( + newState.feeds.data["https://foo.com/feed1"].data.recommendations, + [{ url: "test.com" }] + ); + }); + it("should add boookmark details on PLACES_BOOKMARK_ADDED in both feeds and spocs", () => { + const oldState = { + feeds: { + data: { + "https://foo.com/feed1": { + data: { + recommendations: [ + { url: "https://foo.com" }, + { url: "test.com" }, + ], + }, + }, + }, + loaded: true, + }, + spocs: { + data: { + spocs: { + items: [{ url: "https://foo.com" }, { url: "test-spoc.com" }], + }, + }, + loaded: true, + placements: [{ name: "spocs" }], + }, + }; + const bookmarkAction = { + type: at.PLACES_BOOKMARK_ADDED, + data: { + url: "https://foo.com", + bookmarkGuid: "bookmark123", + bookmarkTitle: "Title for bar.com", + dateAdded: 1234567, + }, + }; + + const newState = DiscoveryStream(oldState, bookmarkAction); + + assert.lengthOf(newState.spocs.data.spocs.items, 2); + assert.equal( + newState.spocs.data.spocs.items[0].bookmarkGuid, + bookmarkAction.data.bookmarkGuid + ); + assert.equal( + newState.spocs.data.spocs.items[0].bookmarkTitle, + bookmarkAction.data.bookmarkTitle + ); + assert.isUndefined(newState.spocs.data.spocs.items[1].bookmarkGuid); + + assert.lengthOf( + newState.feeds.data["https://foo.com/feed1"].data.recommendations, + 2 + ); + assert.equal( + newState.feeds.data["https://foo.com/feed1"].data.recommendations[0] + .bookmarkGuid, + bookmarkAction.data.bookmarkGuid + ); + assert.equal( + newState.feeds.data["https://foo.com/feed1"].data.recommendations[0] + .bookmarkTitle, + bookmarkAction.data.bookmarkTitle + ); + assert.isUndefined( + newState.feeds.data["https://foo.com/feed1"].data.recommendations[1] + .bookmarkGuid + ); + }); + + it("should remove boookmark details on PLACES_BOOKMARKS_REMOVED in both feeds and spocs", () => { + const oldState = { + feeds: { + data: { + "https://foo.com/feed1": { + data: { + recommendations: [ + { + url: "https://foo.com", + bookmarkGuid: "bookmark123", + bookmarkTitle: "Title for bar.com", + }, + { url: "test.com" }, + ], + }, + }, + }, + loaded: true, + }, + spocs: { + data: { + spocs: { + items: [ + { + url: "https://foo.com", + bookmarkGuid: "bookmark123", + bookmarkTitle: "Title for bar.com", + }, + { url: "test-spoc.com" }, + ], + }, + }, + loaded: true, + placements: [{ name: "spocs" }], + }, + }; + const action = { + type: at.PLACES_BOOKMARKS_REMOVED, + data: { + urls: ["https://foo.com"], + }, + }; + + const newState = DiscoveryStream(oldState, action); + + assert.lengthOf(newState.spocs.data.spocs.items, 2); + assert.isUndefined(newState.spocs.data.spocs.items[0].bookmarkGuid); + assert.isUndefined(newState.spocs.data.spocs.items[0].bookmarkTitle); + + assert.lengthOf( + newState.feeds.data["https://foo.com/feed1"].data.recommendations, + 2 + ); + assert.isUndefined( + newState.feeds.data["https://foo.com/feed1"].data.recommendations[0] + .bookmarkGuid + ); + assert.isUndefined( + newState.feeds.data["https://foo.com/feed1"].data.recommendations[0] + .bookmarkTitle + ); + }); + describe("PREF_CHANGED", () => { + it("should set isCollectionDismissible", () => { + const state = DiscoveryStream(undefined, { + type: at.PREF_CHANGED, + data: { + name: "discoverystream.isCollectionDismissible", + value: true, + }, + }); + assert.equal(state.isCollectionDismissible, true); + }); + }); + }); + describe("Search", () => { + it("should return INITIAL_STATE by default", () => { + assert.equal( + Search(undefined, { type: "some_action" }), + INITIAL_STATE.Search + ); + }); + it("should set disable to true on DISABLE_SEARCH", () => { + const nextState = Search(undefined, { type: "DISABLE_SEARCH" }); + assert.propertyVal(nextState, "disable", true); + }); + it("should set focus to true on FAKE_FOCUS_SEARCH", () => { + const nextState = Search(undefined, { type: "FAKE_FOCUS_SEARCH" }); + assert.propertyVal(nextState, "fakeFocus", true); + }); + it("should set focus and disable to false on SHOW_SEARCH", () => { + const nextState = Search(undefined, { type: "SHOW_SEARCH" }); + assert.propertyVal(nextState, "fakeFocus", false); + assert.propertyVal(nextState, "disable", false); + }); + }); + it("should set initialized to true on AS_ROUTER_INITIALIZED", () => { + const nextState = ASRouter(undefined, { type: "AS_ROUTER_INITIALIZED" }); + assert.propertyVal(nextState, "initialized", true); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/Base.test.jsx b/browser/components/newtab/test/unit/content-src/components/Base.test.jsx new file mode 100644 index 0000000000..c764348006 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/Base.test.jsx @@ -0,0 +1,130 @@ +import { + _Base as Base, + BaseContent, + PrefsButton, +} from "content-src/components/Base/Base"; +import { DiscoveryStreamAdmin } from "content-src/components/DiscoveryStreamAdmin/DiscoveryStreamAdmin"; +import { ErrorBoundary } from "content-src/components/ErrorBoundary/ErrorBoundary"; +import React from "react"; +import { Search } from "content-src/components/Search/Search"; +import { shallow } from "enzyme"; +import { actionCreators as ac } from "common/Actions.sys.mjs"; + +describe("<Base>", () => { + let DEFAULT_PROPS = { + store: { getState: () => {} }, + App: { initialized: true }, + Prefs: { values: {} }, + Sections: [], + DiscoveryStream: { config: { enabled: false } }, + dispatch: () => {}, + adminContent: { + message: {}, + }, + }; + + it("should render Base component", () => { + const wrapper = shallow(<Base {...DEFAULT_PROPS} />); + assert.ok(wrapper.exists()); + }); + + it("should render the BaseContent component, passing through all props", () => { + const wrapper = shallow(<Base {...DEFAULT_PROPS} />); + const props = wrapper.find(BaseContent).props(); + assert.deepEqual( + props, + DEFAULT_PROPS, + JSON.stringify([props, DEFAULT_PROPS], null, 3) + ); + }); + + it("should render an ErrorBoundary with class base-content-fallback", () => { + const wrapper = shallow(<Base {...DEFAULT_PROPS} />); + + assert.equal( + wrapper.find(ErrorBoundary).first().prop("className"), + "base-content-fallback" + ); + }); + + it("should render an DiscoveryStreamAdmin if the devtools pref is true", () => { + const wrapper = shallow( + <Base + {...DEFAULT_PROPS} + Prefs={{ values: { "asrouter.devtoolsEnabled": true } }} + /> + ); + assert.lengthOf(wrapper.find(DiscoveryStreamAdmin), 1); + }); + + it("should not render an DiscoveryStreamAdmin if the devtools pref is false", () => { + const wrapper = shallow( + <Base + {...DEFAULT_PROPS} + Prefs={{ values: { "asrouter.devtoolsEnabled": false } }} + /> + ); + assert.lengthOf(wrapper.find(DiscoveryStreamAdmin), 0); + }); +}); + +describe("<BaseContent>", () => { + let DEFAULT_PROPS = { + store: { getState: () => {} }, + App: { initialized: true }, + Prefs: { values: {} }, + Sections: [], + DiscoveryStream: { config: { enabled: false } }, + dispatch: () => {}, + }; + + it("should render an ErrorBoundary with a Search child", () => { + const searchEnabledProps = Object.assign({}, DEFAULT_PROPS, { + Prefs: { values: { showSearch: true } }, + }); + + const wrapper = shallow(<BaseContent {...searchEnabledProps} />); + + assert.isTrue(wrapper.find(Search).parent().is(ErrorBoundary)); + }); + + it("should dispatch a user event when the customize menu is opened or closed", () => { + const dispatch = sinon.stub(); + const wrapper = shallow( + <BaseContent + {...DEFAULT_PROPS} + dispatch={dispatch} + App={{ customizeMenuVisible: true }} + /> + ); + wrapper.instance().openCustomizationMenu(); + assert.calledWith(dispatch, { type: "SHOW_PERSONALIZE" }); + assert.calledWith(dispatch, ac.UserEvent({ event: "SHOW_PERSONALIZE" })); + wrapper.instance().closeCustomizationMenu(); + assert.calledWith(dispatch, { type: "HIDE_PERSONALIZE" }); + assert.calledWith(dispatch, ac.UserEvent({ event: "HIDE_PERSONALIZE" })); + }); + + it("should render only search if no Sections are enabled", () => { + const onlySearchProps = Object.assign({}, DEFAULT_PROPS, { + Sections: [{ id: "highlights", enabled: false }], + Prefs: { values: { showSearch: true } }, + }); + + const wrapper = shallow(<BaseContent {...onlySearchProps} />); + assert.lengthOf(wrapper.find(".only-search"), 1); + }); +}); + +describe("<PrefsButton>", () => { + it("should render icon-settings if props.icon is empty", () => { + const wrapper = shallow(<PrefsButton icon="" />); + + assert.isTrue(wrapper.find("button").hasClass("icon-settings")); + }); + it("should render props.icon as a className", () => { + const wrapper = shallow(<PrefsButton icon="icon-happy" />); + + assert.isTrue(wrapper.find("button").hasClass("icon-happy")); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/Card.test.jsx b/browser/components/newtab/test/unit/content-src/components/Card.test.jsx new file mode 100644 index 0000000000..5f07570b2e --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/Card.test.jsx @@ -0,0 +1,510 @@ +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { + _Card as Card, + PlaceholderCard, +} from "content-src/components/Card/Card"; +import { combineReducers, createStore } from "redux"; +import { GlobalOverrider } from "test/unit/utils"; +import { INITIAL_STATE, reducers } from "common/Reducers.sys.mjs"; +import { cardContextTypes } from "content-src/components/Card/types"; +import { ContextMenuButton } from "content-src/components/ContextMenu/ContextMenuButton"; +import { LinkMenu } from "content-src/components/LinkMenu/LinkMenu"; +import { Provider } from "react-redux"; +import React from "react"; +import { shallow, mount } from "enzyme"; + +let DEFAULT_PROPS = { + dispatch: sinon.stub(), + index: 0, + link: { + hostname: "foo", + title: "A title for foo", + url: "http://www.foo.com", + type: "history", + description: "A description for foo", + image: "http://www.foo.com/img.png", + guid: 1, + }, + eventSource: "TOP_STORIES", + shouldSendImpressionStats: true, + contextMenuOptions: ["Separator"], +}; + +let DEFAULT_BLOB_IMAGE = { + path: "/testpath", + data: new Blob([0]), +}; + +function mountCardWithProps(props) { + const store = createStore(combineReducers(reducers), INITIAL_STATE); + return mount( + <Provider store={store}> + <Card {...props} /> + </Provider> + ); +} + +describe("<Card>", () => { + let globals; + let wrapper; + beforeEach(() => { + globals = new GlobalOverrider(); + wrapper = mountCardWithProps(DEFAULT_PROPS); + }); + afterEach(() => { + DEFAULT_PROPS.dispatch.reset(); + globals.restore(); + }); + it("should render a Card component", () => assert.ok(wrapper.exists())); + it("should add the right url", () => { + assert.propertyVal( + wrapper.find("a").props(), + "href", + DEFAULT_PROPS.link.url + ); + + // test that pocket cards get a special open_url href + const pocketLink = Object.assign({}, DEFAULT_PROPS.link, { + open_url: "getpocket.com/foo", + type: "pocket", + }); + wrapper = mount( + <Card {...Object.assign({}, DEFAULT_PROPS, { link: pocketLink })} /> + ); + assert.propertyVal(wrapper.find("a").props(), "href", pocketLink.open_url); + }); + it("should display a title", () => + assert.equal(wrapper.find(".card-title").text(), DEFAULT_PROPS.link.title)); + it("should display a description", () => + assert.equal( + wrapper.find(".card-description").text(), + DEFAULT_PROPS.link.description + )); + it("should display a host name", () => + assert.equal(wrapper.find(".card-host-name").text(), "foo")); + it("should have a link menu button", () => + assert.ok(wrapper.find(".context-menu-button").exists())); + it("should render a link menu when button is clicked", () => { + const button = wrapper.find(".context-menu-button"); + assert.equal(wrapper.find(LinkMenu).length, 0); + button.simulate("click", { preventDefault: () => {} }); + assert.equal(wrapper.find(LinkMenu).length, 1); + }); + it("should pass dispatch, source, onUpdate, site, options, and index to LinkMenu", () => { + wrapper + .find(".context-menu-button") + .simulate("click", { preventDefault: () => {} }); + const { dispatch, source, onUpdate, site, options, index } = wrapper + .find(LinkMenu) + .props(); + assert.equal(dispatch, DEFAULT_PROPS.dispatch); + assert.equal(source, DEFAULT_PROPS.eventSource); + assert.ok(onUpdate); + assert.equal(site, DEFAULT_PROPS.link); + assert.equal(options, DEFAULT_PROPS.contextMenuOptions); + assert.equal(index, DEFAULT_PROPS.index); + }); + it("should pass through the correct menu options to LinkMenu if overridden by individual card", () => { + const link = Object.assign({}, DEFAULT_PROPS.link); + link.contextMenuOptions = ["CheckBookmark"]; + + wrapper = mountCardWithProps(Object.assign({}, DEFAULT_PROPS, { link })); + wrapper + .find(".context-menu-button") + .simulate("click", { preventDefault: () => {} }); + const { options } = wrapper.find(LinkMenu).props(); + assert.equal(options, link.contextMenuOptions); + }); + it("should have a context based on type", () => { + wrapper = shallow(<Card {...DEFAULT_PROPS} />); + const context = wrapper.find(".card-context"); + const { icon, fluentID } = cardContextTypes[DEFAULT_PROPS.link.type]; + assert.isTrue(context.childAt(0).hasClass(`icon-${icon}`)); + assert.isTrue(context.childAt(1).hasClass("card-context-label")); + assert.equal(context.childAt(1).prop("data-l10n-id"), fluentID); + }); + it("should support setting custom context", () => { + const linkWithCustomContext = { + type: "history", + context: "Custom", + icon: "icon-url", + }; + + wrapper = shallow( + <Card + {...Object.assign({}, DEFAULT_PROPS, { link: linkWithCustomContext })} + /> + ); + const context = wrapper.find(".card-context"); + const { icon } = cardContextTypes[DEFAULT_PROPS.link.type]; + assert.isFalse(context.childAt(0).hasClass(`icon-${icon}`)); + assert.equal( + context.childAt(0).props().style.backgroundImage, + "url('icon-url')" + ); + + assert.isTrue(context.childAt(1).hasClass("card-context-label")); + assert.equal(context.childAt(1).text(), linkWithCustomContext.context); + }); + it("should parse args for fluent correctly", () => { + const title = '"fluent"'; + const link = { ...DEFAULT_PROPS.link, title }; + + wrapper = mountCardWithProps({ ...DEFAULT_PROPS, link }); + let button = wrapper.find(ContextMenuButton).find("button"); + + assert.equal(button.prop("data-l10n-args"), JSON.stringify({ title })); + }); + it("should have .active class, on card-outer if context menu is open", () => { + const button = wrapper.find(ContextMenuButton); + assert.isFalse( + wrapper.find(".card-outer").hasClass("active"), + "does not have active class" + ); + button.simulate("click", { preventDefault: () => {} }); + assert.isTrue( + wrapper.find(".card-outer").hasClass("active"), + "has active class" + ); + }); + it("should send OPEN_DOWNLOAD_FILE if we clicked on a download", () => { + const downloadLink = { + type: "download", + url: "download.mov", + }; + wrapper = mountCardWithProps( + Object.assign({}, DEFAULT_PROPS, { link: downloadLink }) + ); + const card = wrapper.find(".card"); + card.simulate("click", { preventDefault: () => {} }); + assert.calledThrice(DEFAULT_PROPS.dispatch); + + assert.equal( + DEFAULT_PROPS.dispatch.firstCall.args[0].type, + at.OPEN_DOWNLOAD_FILE + ); + assert.deepEqual( + DEFAULT_PROPS.dispatch.firstCall.args[0].data, + downloadLink + ); + }); + it("should send OPEN_LINK if we clicked on anything other than a download", () => { + const nonDownloadLink = { + type: "history", + url: "download.mov", + }; + wrapper = mountCardWithProps( + Object.assign({}, DEFAULT_PROPS, { link: nonDownloadLink }) + ); + const card = wrapper.find(".card"); + const event = { + altKey: "1", + button: "2", + ctrlKey: "3", + metaKey: "4", + shiftKey: "5", + }; + card.simulate( + "click", + Object.assign({}, event, { preventDefault: () => {} }) + ); + assert.calledThrice(DEFAULT_PROPS.dispatch); + + assert.equal(DEFAULT_PROPS.dispatch.firstCall.args[0].type, at.OPEN_LINK); + }); + describe("card image display", () => { + const DEFAULT_BLOB_URL = "blob://test"; + let url; + beforeEach(() => { + url = { + createObjectURL: globals.sandbox.stub().returns(DEFAULT_BLOB_URL), + revokeObjectURL: globals.sandbox.spy(), + }; + globals.set("URL", url); + }); + afterEach(() => { + globals.restore(); + }); + it("should display a regular image correctly and not call revokeObjectURL when unmounted", () => { + wrapper = shallow(<Card {...DEFAULT_PROPS} />); + + assert.isUndefined(wrapper.state("cardImage").path); + assert.equal(wrapper.state("cardImage").url, DEFAULT_PROPS.link.image); + assert.equal( + wrapper.find(".card-preview-image").props().style.backgroundImage, + `url(${wrapper.state("cardImage").url})` + ); + + wrapper.unmount(); + assert.notCalled(url.revokeObjectURL); + }); + it("should display a blob image correctly and revoke blob url when unmounted", () => { + const link = Object.assign({}, DEFAULT_PROPS.link, { + image: DEFAULT_BLOB_IMAGE, + }); + wrapper = shallow(<Card {...DEFAULT_PROPS} link={link} />); + + assert.equal(wrapper.state("cardImage").path, DEFAULT_BLOB_IMAGE.path); + assert.equal(wrapper.state("cardImage").url, DEFAULT_BLOB_URL); + assert.equal( + wrapper.find(".card-preview-image").props().style.backgroundImage, + `url(${wrapper.state("cardImage").url})` + ); + + wrapper.unmount(); + assert.calledOnce(url.revokeObjectURL); + }); + it("should not show an image if there isn't one and not call revokeObjectURL when unmounted", () => { + const link = Object.assign({}, DEFAULT_PROPS.link); + delete link.image; + + wrapper = shallow(<Card {...DEFAULT_PROPS} link={link} />); + + assert.isNull(wrapper.state("cardImage")); + assert.lengthOf(wrapper.find(".card-preview-image"), 0); + + wrapper.unmount(); + assert.notCalled(url.revokeObjectURL); + }); + it("should remove current card image if new image is not present", () => { + wrapper = shallow(<Card {...DEFAULT_PROPS} />); + + const otherLink = Object.assign({}, DEFAULT_PROPS.link); + delete otherLink.image; + wrapper.setProps(Object.assign({}, DEFAULT_PROPS, { link: otherLink })); + + assert.isNull(wrapper.state("cardImage")); + }); + it("should not create or revoke urls if normal image is already in state", () => { + wrapper = shallow(<Card {...DEFAULT_PROPS} />); + + wrapper.setProps(DEFAULT_PROPS); + + assert.notCalled(url.createObjectURL); + assert.notCalled(url.revokeObjectURL); + }); + it("should not create or revoke more urls if blob image is already in state", () => { + const link = Object.assign({}, DEFAULT_PROPS.link, { + image: DEFAULT_BLOB_IMAGE, + }); + wrapper = shallow(<Card {...DEFAULT_PROPS} link={link} />); + + assert.calledOnce(url.createObjectURL); + assert.notCalled(url.revokeObjectURL); + + wrapper.setProps(Object.assign({}, DEFAULT_PROPS, { link })); + + assert.calledOnce(url.createObjectURL); + assert.notCalled(url.revokeObjectURL); + }); + it("should create blob urls for new blobs and revoke existing ones", () => { + const link = Object.assign({}, DEFAULT_PROPS.link, { + image: DEFAULT_BLOB_IMAGE, + }); + wrapper = shallow(<Card {...DEFAULT_PROPS} link={link} />); + + assert.calledOnce(url.createObjectURL); + assert.notCalled(url.revokeObjectURL); + + const otherLink = Object.assign({}, DEFAULT_PROPS.link, { + image: { path: "/newpath", data: new Blob([0]) }, + }); + wrapper.setProps(Object.assign({}, DEFAULT_PROPS, { link: otherLink })); + + assert.calledTwice(url.createObjectURL); + assert.calledOnce(url.revokeObjectURL); + }); + it("should not call createObjectURL and revokeObjectURL for normal images", () => { + wrapper = shallow(<Card {...DEFAULT_PROPS} />); + + assert.notCalled(url.createObjectURL); + assert.notCalled(url.revokeObjectURL); + + const otherLink = Object.assign({}, DEFAULT_PROPS.link, { + image: "https://other/image", + }); + wrapper.setProps(Object.assign({}, DEFAULT_PROPS, { link: otherLink })); + + assert.notCalled(url.createObjectURL); + assert.notCalled(url.revokeObjectURL); + }); + }); + describe("image loading", () => { + let link; + let triggerImage = {}; + let uniqueLink = 0; + beforeEach(() => { + global.Image.prototype = { + addEventListener(event, callback) { + triggerImage[event] = () => Promise.resolve(callback()); + }, + }; + + link = Object.assign({}, DEFAULT_PROPS.link); + link.image += uniqueLink++; + wrapper = shallow(<Card {...DEFAULT_PROPS} link={link} />); + }); + it("should have a loaded preview image when the image is loaded", () => { + assert.isFalse(wrapper.find(".card-preview-image").hasClass("loaded")); + + wrapper.setState({ imageLoaded: true }); + + assert.isTrue(wrapper.find(".card-preview-image").hasClass("loaded")); + }); + it("should start not loaded", () => { + assert.isFalse(wrapper.state("imageLoaded")); + }); + it("should be loaded after load", async () => { + await triggerImage.load(); + + assert.isTrue(wrapper.state("imageLoaded")); + }); + it("should be not be loaded after error ", async () => { + await triggerImage.error(); + + assert.isFalse(wrapper.state("imageLoaded")); + }); + it("should be not be loaded if image changes", async () => { + await triggerImage.load(); + const otherLink = Object.assign({}, link, { + image: "https://other/image", + }); + + wrapper.setProps(Object.assign({}, DEFAULT_PROPS, { link: otherLink })); + + assert.isFalse(wrapper.state("imageLoaded")); + }); + }); + describe("placeholder=true", () => { + beforeEach(() => { + wrapper = mount(<Card placeholder={true} />); + }); + it("should render when placeholder=true", () => { + assert.ok(wrapper.exists()); + }); + it("should add a placeholder class to the outer element", () => { + assert.isTrue(wrapper.find(".card-outer").hasClass("placeholder")); + }); + it("should not have a context menu button or LinkMenu", () => { + assert.isFalse( + wrapper.find(ContextMenuButton).exists(), + "context menu button" + ); + assert.isFalse(wrapper.find(LinkMenu).exists(), "LinkMenu"); + }); + it("should not call onLinkClick when the link is clicked", () => { + const spy = sinon.spy(wrapper.instance(), "onLinkClick"); + const card = wrapper.find(".card"); + card.simulate("click"); + assert.notCalled(spy); + }); + }); + describe("#trackClick", () => { + it("should call dispatch when the link is clicked with the right data", () => { + const card = wrapper.find(".card"); + const event = { + altKey: "1", + button: "2", + ctrlKey: "3", + metaKey: "4", + shiftKey: "5", + }; + card.simulate( + "click", + Object.assign({}, event, { preventDefault: () => {} }) + ); + assert.calledThrice(DEFAULT_PROPS.dispatch); + + // first dispatch call is the AlsoToMain message which will open a link in a window, and send some event data + assert.equal(DEFAULT_PROPS.dispatch.firstCall.args[0].type, at.OPEN_LINK); + assert.deepEqual( + DEFAULT_PROPS.dispatch.firstCall.args[0].data.event, + event + ); + + // second dispatch call is a UserEvent action for telemetry + assert.isUserEventAction(DEFAULT_PROPS.dispatch.secondCall.args[0]); + assert.calledWith( + DEFAULT_PROPS.dispatch.secondCall, + ac.UserEvent({ + event: "CLICK", + source: DEFAULT_PROPS.eventSource, + action_position: DEFAULT_PROPS.index, + }) + ); + + // third dispatch call is to send impression stats + assert.calledWith( + DEFAULT_PROPS.dispatch.thirdCall, + ac.ImpressionStats({ + source: DEFAULT_PROPS.eventSource, + click: 0, + tiles: [{ id: DEFAULT_PROPS.link.guid, pos: DEFAULT_PROPS.index }], + }) + ); + }); + it("should provide card_type to telemetry info if type is not history", () => { + const link = Object.assign({}, DEFAULT_PROPS.link); + link.type = "bookmark"; + wrapper = mount(<Card {...Object.assign({}, DEFAULT_PROPS, { link })} />); + const card = wrapper.find(".card"); + const event = { + altKey: "1", + button: "2", + ctrlKey: "3", + metaKey: "4", + shiftKey: "5", + }; + + card.simulate( + "click", + Object.assign({}, event, { preventDefault: () => {} }) + ); + + assert.isUserEventAction(DEFAULT_PROPS.dispatch.secondCall.args[0]); + assert.calledWith( + DEFAULT_PROPS.dispatch.secondCall, + ac.UserEvent({ + event: "CLICK", + source: DEFAULT_PROPS.eventSource, + action_position: DEFAULT_PROPS.index, + value: { card_type: link.type }, + }) + ); + }); + it("should notify Web Extensions with WEBEXT_CLICK if props.isWebExtension is true", () => { + wrapper = mountCardWithProps( + Object.assign({}, DEFAULT_PROPS, { + isWebExtension: true, + eventSource: "MyExtension", + index: 3, + }) + ); + const card = wrapper.find(".card"); + const event = { preventDefault() {} }; + card.simulate("click", event); + assert.calledWith( + DEFAULT_PROPS.dispatch, + ac.WebExtEvent(at.WEBEXT_CLICK, { + source: "MyExtension", + url: DEFAULT_PROPS.link.url, + action_position: 3, + }) + ); + }); + }); +}); + +describe("<PlaceholderCard />", () => { + it("should render a Card with placeholder=true", () => { + const wrapper = mount( + <Provider store={createStore(combineReducers(reducers), INITIAL_STATE)}> + <PlaceholderCard /> + </Provider> + ); + assert.isTrue(wrapper.find(Card).props().placeholder); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/CollapsibleSection.test.jsx b/browser/components/newtab/test/unit/content-src/components/CollapsibleSection.test.jsx new file mode 100644 index 0000000000..f2a8e276b4 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/CollapsibleSection.test.jsx @@ -0,0 +1,67 @@ +import { _CollapsibleSection as CollapsibleSection } from "content-src/components/CollapsibleSection/CollapsibleSection"; +import { ErrorBoundary } from "content-src/components/ErrorBoundary/ErrorBoundary"; +import { mount } from "enzyme"; +import React from "react"; + +const DEFAULT_PROPS = { + id: "cool", + className: "cool-section", + title: "Cool Section", + prefName: "collapseSection", + collapsed: false, + eventSource: "foo", + document: { + addEventListener: () => {}, + removeEventListener: () => {}, + visibilityState: "visible", + }, + dispatch: () => {}, + Prefs: { values: { featureConfig: {} } }, +}; + +describe("CollapsibleSection", () => { + let wrapper; + + function setup(props = {}) { + const customProps = Object.assign({}, DEFAULT_PROPS, props); + wrapper = mount( + <CollapsibleSection {...customProps}>foo</CollapsibleSection> + ); + } + + beforeEach(() => setup()); + + it("should render the component", () => { + assert.ok(wrapper.exists()); + }); + + it("should render an ErrorBoundary with class section-body-fallback", () => { + assert.equal( + wrapper.find(ErrorBoundary).first().prop("className"), + "section-body-fallback" + ); + }); + + describe("without collapsible pref", () => { + let dispatch; + beforeEach(() => { + dispatch = sinon.stub(); + setup({ collapsed: undefined, dispatch }); + }); + it("should render the section uncollapsed", () => { + assert.isFalse( + wrapper.find(".collapsible-section").first().hasClass("collapsed") + ); + }); + + it("should not render the arrow if no collapsible pref exists for the section", () => { + assert.lengthOf(wrapper.find(".click-target .collapsible-arrow"), 0); + }); + }); + + describe("icon", () => { + it("no icon should be shown", () => { + assert.lengthOf(wrapper.find(".icon"), 0); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/ComponentPerfTimer.test.jsx b/browser/components/newtab/test/unit/content-src/components/ComponentPerfTimer.test.jsx new file mode 100644 index 0000000000..baf203947e --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/ComponentPerfTimer.test.jsx @@ -0,0 +1,447 @@ +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { ComponentPerfTimer } from "content-src/components/ComponentPerfTimer/ComponentPerfTimer"; +import createMockRaf from "mock-raf"; +import React from "react"; + +import { shallow } from "enzyme"; + +const perfSvc = { + mark() {}, + getMostRecentAbsMarkStartByName() {}, +}; + +let DEFAULT_PROPS = { + initialized: true, + rows: [], + id: "highlights", + dispatch() {}, + perfSvc, +}; + +describe("<ComponentPerfTimer>", () => { + let mockRaf; + let sandbox; + let wrapper; + + const InnerEl = () => <div>Inner Element</div>; + + beforeEach(() => { + mockRaf = createMockRaf(); + sandbox = sinon.createSandbox(); + sandbox.stub(window, "requestAnimationFrame").callsFake(mockRaf.raf); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + }); + afterEach(() => { + sandbox.restore(); + }); + + it("should render props.children", () => { + assert.ok(wrapper.contains(<InnerEl />)); + }); + + describe("#constructor", () => { + beforeEach(() => { + sandbox.stub(ComponentPerfTimer.prototype, "_maybeSendBadStateEvent"); + sandbox.stub( + ComponentPerfTimer.prototype, + "_ensureFirstRenderTsRecorded" + ); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer>, + { disableLifecycleMethods: true } + ); + }); + + it("should have the correct defaults", () => { + const instance = wrapper.instance(); + + assert.isFalse(instance._reportMissingData); + assert.isFalse(instance._timestampHandled); + assert.isFalse(instance._recordedFirstRender); + }); + }); + + describe("#render", () => { + beforeEach(() => { + sandbox.stub(DEFAULT_PROPS, "id").value("fake_section"); + sandbox.stub(ComponentPerfTimer.prototype, "_maybeSendBadStateEvent"); + sandbox.stub( + ComponentPerfTimer.prototype, + "_ensureFirstRenderTsRecorded" + ); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + }); + + it("should not call telemetry on sections that we don't want to record", () => { + const instance = wrapper.instance(); + + assert.notCalled(instance._maybeSendBadStateEvent); + assert.notCalled(instance._ensureFirstRenderTsRecorded); + }); + }); + + describe("#_componentDidMount", () => { + it("should call _maybeSendPaintedEvent", () => { + const instance = wrapper.instance(); + const stub = sandbox.stub(instance, "_maybeSendPaintedEvent"); + + instance.componentDidMount(); + + assert.calledOnce(stub); + }); + + it("should not call _maybeSendPaintedEvent if id not in RECORDED_SECTIONS", () => { + sandbox.stub(DEFAULT_PROPS, "id").value("topstories"); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + const instance = wrapper.instance(); + const stub = sandbox.stub(instance, "_maybeSendPaintedEvent"); + + instance.componentDidMount(); + + assert.notCalled(stub); + }); + }); + + describe("#_componentDidUpdate", () => { + it("should call _maybeSendPaintedEvent", () => { + const instance = wrapper.instance(); + const maybeSendPaintStub = sandbox.stub( + instance, + "_maybeSendPaintedEvent" + ); + + instance.componentDidUpdate(); + + assert.calledOnce(maybeSendPaintStub); + }); + + it("should not call _maybeSendPaintedEvent if id not in RECORDED_SECTIONS", () => { + sandbox.stub(DEFAULT_PROPS, "id").value("topstories"); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + const instance = wrapper.instance(); + const stub = sandbox.stub(instance, "_maybeSendPaintedEvent"); + + instance.componentDidUpdate(); + + assert.notCalled(stub); + }); + }); + + describe("_ensureFirstRenderTsRecorded", () => { + let recordFirstRenderStub; + beforeEach(() => { + sandbox.stub(ComponentPerfTimer.prototype, "_maybeSendBadStateEvent"); + recordFirstRenderStub = sandbox.stub( + ComponentPerfTimer.prototype, + "_ensureFirstRenderTsRecorded" + ); + }); + + it("should set _recordedFirstRender", () => { + sandbox.stub(DEFAULT_PROPS, "initialized").value(false); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + const instance = wrapper.instance(); + + assert.isFalse(instance._recordedFirstRender); + + recordFirstRenderStub.callThrough(); + instance._ensureFirstRenderTsRecorded(); + + assert.isTrue(instance._recordedFirstRender); + }); + + it("should mark first_render_ts", () => { + sandbox.stub(DEFAULT_PROPS, "initialized").value(false); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + const instance = wrapper.instance(); + const stub = sandbox.stub(perfSvc, "mark"); + + recordFirstRenderStub.callThrough(); + instance._ensureFirstRenderTsRecorded(); + + assert.calledOnce(stub); + assert.calledWithExactly(stub, `${DEFAULT_PROPS.id}_first_render_ts`); + }); + }); + + describe("#_maybeSendBadStateEvent", () => { + let sendBadStateStub; + beforeEach(() => { + sendBadStateStub = sandbox.stub( + ComponentPerfTimer.prototype, + "_maybeSendBadStateEvent" + ); + sandbox.stub( + ComponentPerfTimer.prototype, + "_ensureFirstRenderTsRecorded" + ); + }); + + it("should set this._reportMissingData=true when called with initialized === false", () => { + sandbox.stub(DEFAULT_PROPS, "initialized").value(false); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + const instance = wrapper.instance(); + + assert.isFalse(instance._reportMissingData); + + sendBadStateStub.callThrough(); + instance._maybeSendBadStateEvent(); + + assert.isTrue(instance._reportMissingData); + }); + + it("should call _sendBadStateEvent if initialized & other metrics have been recorded", () => { + const instance = wrapper.instance(); + const stub = sandbox.stub(instance, "_sendBadStateEvent"); + instance._reportMissingData = true; + instance._timestampHandled = true; + instance._recordedFirstRender = true; + + sendBadStateStub.callThrough(); + instance._maybeSendBadStateEvent(); + + assert.calledOnce(stub); + assert.isFalse(instance._reportMissingData); + }); + }); + + describe("#_maybeSendPaintedEvent", () => { + it("should call _sendPaintedEvent if props.initialized is true", () => { + sandbox.stub(DEFAULT_PROPS, "initialized").value(true); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer>, + { disableLifecycleMethods: true } + ); + const instance = wrapper.instance(); + const stub = sandbox.stub(instance, "_afterFramePaint"); + + assert.isFalse(instance._timestampHandled); + + instance._maybeSendPaintedEvent(); + + assert.calledOnce(stub); + assert.calledWithExactly(stub, instance._sendPaintedEvent); + assert.isTrue(wrapper.instance()._timestampHandled); + }); + it("should not call _sendPaintedEvent if this._timestampHandled is true", () => { + const instance = wrapper.instance(); + const spy = sinon.spy(instance, "_afterFramePaint"); + instance._timestampHandled = true; + + instance._maybeSendPaintedEvent(); + spy.neverCalledWith(instance._sendPaintedEvent); + }); + it("should not call _sendPaintedEvent if component not initialized", () => { + sandbox.stub(DEFAULT_PROPS, "initialized").value(false); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + const instance = wrapper.instance(); + const spy = sinon.spy(instance, "_afterFramePaint"); + + instance._maybeSendPaintedEvent(); + + spy.neverCalledWith(instance._sendPaintedEvent); + }); + }); + + describe("#_afterFramePaint", () => { + it("should call callback after the requestAnimationFrame callback returns", () => + new Promise(resolve => { + // Setting the callback to resolve is the test that it does finally get + // called at the correct time, after the event loop ticks again. + // If it doesn't get called, this test will time out. + const callback = sandbox.spy(resolve); + + const instance = wrapper.instance(); + + instance._afterFramePaint(callback); + + assert.notCalled(callback); + mockRaf.step({ count: 1 }); + })); + }); + + describe("#_sendBadStateEvent", () => { + it("should call perfSvc.mark", () => { + sandbox.spy(perfSvc, "mark"); + const key = `${DEFAULT_PROPS.id}_data_ready_ts`; + + wrapper.instance()._sendBadStateEvent(); + + assert.calledOnce(perfSvc.mark); + assert.calledWithExactly(perfSvc.mark, key); + }); + + it("should call compute the delta from first render to data ready", () => { + sandbox.stub(perfSvc, "getMostRecentAbsMarkStartByName"); + + wrapper + .instance() + ._sendBadStateEvent(`${DEFAULT_PROPS.id}_data_ready_ts`); + + assert.calledTwice(perfSvc.getMostRecentAbsMarkStartByName); + assert.calledWithExactly( + perfSvc.getMostRecentAbsMarkStartByName, + `${DEFAULT_PROPS.id}_data_ready_ts` + ); + assert.calledWithExactly( + perfSvc.getMostRecentAbsMarkStartByName, + `${DEFAULT_PROPS.id}_first_render_ts` + ); + }); + + it("should call dispatch SAVE_SESSION_PERF_DATA", () => { + sandbox + .stub(perfSvc, "getMostRecentAbsMarkStartByName") + .withArgs("highlights_first_render_ts") + .returns(0.5) + .withArgs("highlights_data_ready_ts") + .returns(3.2); + + const dispatch = sandbox.spy(DEFAULT_PROPS, "dispatch"); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + + wrapper.instance()._sendBadStateEvent(); + + assert.calledOnce(dispatch); + assert.calledWithExactly( + dispatch, + ac.OnlyToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { [`${DEFAULT_PROPS.id}_data_late_by_ms`]: 2 }, + }) + ); + }); + }); + + describe("#_sendPaintedEvent", () => { + beforeEach(() => { + sandbox.stub(ComponentPerfTimer.prototype, "_maybeSendBadStateEvent"); + sandbox.stub( + ComponentPerfTimer.prototype, + "_ensureFirstRenderTsRecorded" + ); + }); + + it("should not call mark with the wrong id", () => { + sandbox.stub(perfSvc, "mark"); + sandbox.stub(DEFAULT_PROPS, "id").value("fake_id"); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + + wrapper.instance()._sendPaintedEvent(); + + assert.notCalled(perfSvc.mark); + }); + it("should call mark with the correct topsites", () => { + sandbox.stub(perfSvc, "mark"); + sandbox.stub(DEFAULT_PROPS, "id").value("topsites"); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + + wrapper.instance()._sendPaintedEvent(); + + assert.calledOnce(perfSvc.mark); + assert.calledWithExactly(perfSvc.mark, "topsites_first_painted_ts"); + }); + it("should not call getMostRecentAbsMarkStartByName if id!=topsites", () => { + sandbox.stub(perfSvc, "getMostRecentAbsMarkStartByName"); + sandbox.stub(DEFAULT_PROPS, "id").value("fake_id"); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + + wrapper.instance()._sendPaintedEvent(); + + assert.notCalled(perfSvc.getMostRecentAbsMarkStartByName); + }); + it("should call getMostRecentAbsMarkStartByName for topsites", () => { + sandbox.stub(perfSvc, "getMostRecentAbsMarkStartByName"); + sandbox.stub(DEFAULT_PROPS, "id").value("topsites"); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + + wrapper.instance()._sendPaintedEvent(); + + assert.calledOnce(perfSvc.getMostRecentAbsMarkStartByName); + assert.calledWithExactly( + perfSvc.getMostRecentAbsMarkStartByName, + "topsites_first_painted_ts" + ); + }); + it("should dispatch SAVE_SESSION_PERF_DATA", () => { + sandbox.stub(perfSvc, "getMostRecentAbsMarkStartByName").returns(42); + sandbox.stub(DEFAULT_PROPS, "id").value("topsites"); + const dispatch = sandbox.spy(DEFAULT_PROPS, "dispatch"); + wrapper = shallow( + <ComponentPerfTimer {...DEFAULT_PROPS}> + <InnerEl /> + </ComponentPerfTimer> + ); + + wrapper.instance()._sendPaintedEvent(); + + assert.calledOnce(dispatch); + assert.calledWithExactly( + dispatch, + ac.OnlyToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { topsites_first_painted_ts: 42 }, + }) + ); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/ConfirmDialog.test.jsx b/browser/components/newtab/test/unit/content-src/components/ConfirmDialog.test.jsx new file mode 100644 index 0000000000..a471c09e66 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/ConfirmDialog.test.jsx @@ -0,0 +1,182 @@ +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { _ConfirmDialog as ConfirmDialog } from "content-src/components/ConfirmDialog/ConfirmDialog"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<ConfirmDialog>", () => { + let wrapper; + let dispatch; + let ConfirmDialogProps; + beforeEach(() => { + dispatch = sinon.stub(); + ConfirmDialogProps = { + visible: true, + data: { + onConfirm: [], + cancel_button_string_id: "newtab-topsites-delete-history-button", + confirm_button_string_id: "newtab-topsites-cancel-button", + eventSource: "HIGHLIGHTS", + }, + }; + wrapper = shallow( + <ConfirmDialog dispatch={dispatch} {...ConfirmDialogProps} /> + ); + }); + it("should render an overlay", () => { + assert.ok(wrapper.find(".modal-overlay").exists()); + }); + it("should render a modal", () => { + assert.ok(wrapper.find(".confirmation-dialog").exists()); + }); + it("should not render if visible is false", () => { + ConfirmDialogProps.visible = false; + wrapper = shallow( + <ConfirmDialog dispatch={dispatch} {...ConfirmDialogProps} /> + ); + + assert.lengthOf(wrapper.find(".confirmation-dialog"), 0); + }); + it("should display an icon if we provide one in props", () => { + const iconName = "modal-icon"; + // If there is no icon in the props, we shouldn't display an icon + assert.lengthOf(wrapper.find(`.icon-${iconName}`), 0); + + ConfirmDialogProps.data.icon = iconName; + wrapper = shallow( + <ConfirmDialog dispatch={dispatch} {...ConfirmDialogProps} /> + ); + + // But if we do provide an icon - we should show it + assert.lengthOf(wrapper.find(`.icon-${iconName}`), 1); + }); + describe("fluent message check", () => { + it("should render the message body sent via props", () => { + Object.assign(ConfirmDialogProps.data, { + body_string_id: ["foo", "bar"], + }); + wrapper = shallow( + <ConfirmDialog dispatch={dispatch} {...ConfirmDialogProps} /> + ); + let msgs = wrapper.find(".modal-message").find("p"); + assert.equal(msgs.length, ConfirmDialogProps.data.body_string_id.length); + msgs.forEach((fm, i) => + assert.equal( + fm.prop("data-l10n-id"), + ConfirmDialogProps.data.body_string_id[i] + ) + ); + }); + it("should render the correct primary button text", () => { + Object.assign(ConfirmDialogProps.data, { + confirm_button_string_id: "primary_foo", + }); + wrapper = shallow( + <ConfirmDialog dispatch={dispatch} {...ConfirmDialogProps} /> + ); + + let doneLabel = wrapper.find(".actions").childAt(1); + assert.ok(doneLabel.exists()); + assert.equal( + doneLabel.prop("data-l10n-id"), + ConfirmDialogProps.data.confirm_button_string_id + ); + }); + }); + describe("click events", () => { + it("should emit AlsoToMain DIALOG_CANCEL when you click the overlay", () => { + let overlay = wrapper.find(".modal-overlay"); + + assert.ok(overlay.exists()); + overlay.simulate("click"); + + // Two events are emitted: UserEvent+AlsoToMain. + assert.calledTwice(dispatch); + assert.propertyVal(dispatch.firstCall.args[0], "type", at.DIALOG_CANCEL); + assert.calledWith(dispatch, { type: at.DIALOG_CANCEL }); + }); + it("should emit UserEvent DIALOG_CANCEL when you click the overlay", () => { + let overlay = wrapper.find(".modal-overlay"); + + assert.ok(overlay); + overlay.simulate("click"); + + // Two events are emitted: UserEvent+AlsoToMain. + assert.calledTwice(dispatch); + assert.isUserEventAction(dispatch.secondCall.args[0]); + assert.calledWith( + dispatch, + ac.UserEvent({ event: at.DIALOG_CANCEL, source: "HIGHLIGHTS" }) + ); + }); + it("should emit AlsoToMain DIALOG_CANCEL on cancel", () => { + let cancelButton = wrapper.find(".actions").childAt(0); + + assert.ok(cancelButton); + cancelButton.simulate("click"); + + // Two events are emitted: UserEvent+AlsoToMain. + assert.calledTwice(dispatch); + assert.propertyVal(dispatch.firstCall.args[0], "type", at.DIALOG_CANCEL); + assert.calledWith(dispatch, { type: at.DIALOG_CANCEL }); + }); + it("should emit UserEvent DIALOG_CANCEL on cancel", () => { + let cancelButton = wrapper.find(".actions").childAt(0); + + assert.ok(cancelButton); + cancelButton.simulate("click"); + + // Two events are emitted: UserEvent+AlsoToMain. + assert.calledTwice(dispatch); + assert.isUserEventAction(dispatch.secondCall.args[0]); + assert.calledWith( + dispatch, + ac.UserEvent({ event: at.DIALOG_CANCEL, source: "HIGHLIGHTS" }) + ); + }); + it("should emit UserEvent on primary button", () => { + Object.assign(ConfirmDialogProps.data, { + body_string_id: ["foo", "bar"], + onConfirm: [ + ac.AlsoToMain({ type: at.DELETE_URL, data: "foo.bar" }), + ac.UserEvent({ event: "DELETE" }), + ], + }); + wrapper = shallow( + <ConfirmDialog dispatch={dispatch} {...ConfirmDialogProps} /> + ); + let doneButton = wrapper.find(".actions").childAt(1); + + assert.ok(doneButton); + doneButton.simulate("click"); + + // Two events are emitted: UserEvent+AlsoToMain. + assert.isUserEventAction(dispatch.secondCall.args[0]); + + assert.calledTwice(dispatch); + assert.calledWith(dispatch, ConfirmDialogProps.data.onConfirm[1]); + }); + it("should emit AlsoToMain on primary button", () => { + Object.assign(ConfirmDialogProps.data, { + body_string_id: ["foo", "bar"], + onConfirm: [ + ac.AlsoToMain({ type: at.DELETE_URL, data: "foo.bar" }), + ac.UserEvent({ event: "DELETE" }), + ], + }); + wrapper = shallow( + <ConfirmDialog dispatch={dispatch} {...ConfirmDialogProps} /> + ); + let doneButton = wrapper.find(".actions").childAt(1); + + assert.ok(doneButton); + doneButton.simulate("click"); + + // Two events are emitted: UserEvent+AlsoToMain. + assert.calledTwice(dispatch); + assert.calledWith(dispatch, ConfirmDialogProps.data.onConfirm[0]); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/ContextMenu.test.jsx b/browser/components/newtab/test/unit/content-src/components/ContextMenu.test.jsx new file mode 100644 index 0000000000..4f7edadc41 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/ContextMenu.test.jsx @@ -0,0 +1,227 @@ +import { + ContextMenu, + ContextMenuItem, + _ContextMenuItem, +} from "content-src/components/ContextMenu/ContextMenu"; +import { ContextMenuButton } from "content-src/components/ContextMenu/ContextMenuButton"; +import { mount, shallow } from "enzyme"; +import React from "react"; +import { INITIAL_STATE, reducers } from "common/Reducers.sys.mjs"; +import { Provider } from "react-redux"; +import { combineReducers, createStore } from "redux"; + +const DEFAULT_PROPS = { + onUpdate: () => {}, + options: [], + tabbableOptionsLength: 0, +}; + +const DEFAULT_MENU_OPTIONS = [ + "MoveUp", + "MoveDown", + "Separator", + "ManageSection", +]; + +const FakeMenu = props => { + return <div>{props.children}</div>; +}; + +describe("<ContextMenuButton>", () => { + function mountWithProps(options) { + const store = createStore(combineReducers(reducers), INITIAL_STATE); + return mount( + <Provider store={store}> + <ContextMenuButton> + <ContextMenu options={options} /> + </ContextMenuButton> + </Provider> + ); + } + + let sandbox; + beforeEach(() => { + sandbox = sinon.createSandbox(); + }); + afterEach(() => { + sandbox.restore(); + }); + it("should call onUpdate when clicked", () => { + const onUpdate = sandbox.spy(); + const wrapper = mount( + <ContextMenuButton onUpdate={onUpdate}> + <FakeMenu /> + </ContextMenuButton> + ); + wrapper.find(".context-menu-button").simulate("click"); + assert.calledOnce(onUpdate); + }); + it("should call onUpdate when activated with Enter", () => { + const onUpdate = sandbox.spy(); + const wrapper = mount( + <ContextMenuButton onUpdate={onUpdate}> + <FakeMenu /> + </ContextMenuButton> + ); + wrapper.find(".context-menu-button").simulate("keydown", { key: "Enter" }); + assert.calledOnce(onUpdate); + }); + it("should call onClick", () => { + const onClick = sandbox.spy(ContextMenuButton.prototype, "onClick"); + const wrapper = mount( + <ContextMenuButton> + <FakeMenu /> + </ContextMenuButton> + ); + wrapper.find("button").simulate("click"); + assert.calledOnce(onClick); + }); + it("should have a default keyboardAccess prop of false", () => { + const wrapper = mountWithProps(DEFAULT_MENU_OPTIONS); + wrapper.find(ContextMenuButton).setState({ showContextMenu: true }); + assert.equal(wrapper.find(ContextMenu).prop("keyboardAccess"), false); + }); + it("should pass the keyboardAccess prop down to ContextMenu", () => { + const wrapper = mountWithProps(DEFAULT_MENU_OPTIONS); + wrapper + .find(ContextMenuButton) + .setState({ showContextMenu: true, contextMenuKeyboard: true }); + assert.equal(wrapper.find(ContextMenu).prop("keyboardAccess"), true); + }); + it("should call focusFirst when keyboardAccess is true", () => { + const options = [{ label: "item1", first: true }]; + const wrapper = mountWithProps(options); + const focusFirst = sandbox.spy(_ContextMenuItem.prototype, "focusFirst"); + wrapper + .find(ContextMenuButton) + .setState({ showContextMenu: true, contextMenuKeyboard: true }); + assert.calledOnce(focusFirst); + }); +}); + +describe("<ContextMenu>", () => { + function mountWithProps(props) { + const store = createStore(combineReducers(reducers), INITIAL_STATE); + return mount( + <Provider store={store}> + <ContextMenu {...props} /> + </Provider> + ); + } + + it("should render all the options provided", () => { + const options = [ + { label: "item1" }, + { type: "separator" }, + { label: "item2" }, + ]; + const wrapper = shallow( + <ContextMenu {...DEFAULT_PROPS} options={options} /> + ); + assert.lengthOf(wrapper.find(".context-menu-list").children(), 3); + }); + it("should not add a link for a separator", () => { + const options = [{ label: "item1" }, { type: "separator" }]; + const wrapper = shallow( + <ContextMenu {...DEFAULT_PROPS} options={options} /> + ); + assert.lengthOf(wrapper.find(".separator"), 1); + }); + it("should add a link for all types that are not separators", () => { + const options = [{ label: "item1" }, { type: "separator" }]; + const wrapper = shallow( + <ContextMenu {...DEFAULT_PROPS} options={options} /> + ); + assert.lengthOf(wrapper.find(ContextMenuItem), 1); + }); + it("should not add an icon to any items", () => { + const props = Object.assign({}, DEFAULT_PROPS, { + options: [{ label: "item1", icon: "icon1" }, { type: "separator" }], + }); + const wrapper = mountWithProps(props); + assert.lengthOf(wrapper.find(".icon-icon1"), 0); + }); + it("should be tabbable", () => { + const props = { + options: [{ label: "item1", icon: "icon1" }, { type: "separator" }], + }; + const wrapper = mountWithProps(props); + assert.equal( + wrapper.find(".context-menu-item").props().role, + "presentation" + ); + }); + it("should call onUpdate with false when an option is clicked", () => { + const onUpdate = sinon.spy(); + const onClick = sinon.spy(); + const props = Object.assign({}, DEFAULT_PROPS, { + onUpdate, + options: [{ label: "item1", onClick }], + }); + const wrapper = mountWithProps(props); + wrapper.find(".context-menu-item button").simulate("click"); + assert.calledOnce(onUpdate); + assert.calledOnce(onClick); + }); + it("should not have disabled className by default", () => { + const props = Object.assign({}, DEFAULT_PROPS, { + options: [{ label: "item1", icon: "icon1" }, { type: "separator" }], + }); + const wrapper = mountWithProps(props); + assert.lengthOf(wrapper.find(".context-menu-item a.disabled"), 0); + }); + it("should add disabled className to any disabled options", () => { + const options = [ + { label: "item1", icon: "icon1", disabled: true }, + { type: "separator" }, + ]; + const props = Object.assign({}, DEFAULT_PROPS, { options }); + const wrapper = mountWithProps(props); + assert.lengthOf(wrapper.find(".context-menu-item button.disabled"), 1); + }); + it("should have the context-menu-item class", () => { + const options = [{ label: "item1", icon: "icon1" }]; + const props = Object.assign({}, DEFAULT_PROPS, { options }); + const wrapper = mountWithProps(props); + assert.lengthOf(wrapper.find(".context-menu-item"), 1); + }); + it("should call onClick when onKeyDown is called with Enter", () => { + const onClick = sinon.spy(); + const props = Object.assign({}, DEFAULT_PROPS, { + options: [{ label: "item1", onClick }], + }); + const wrapper = mountWithProps(props); + wrapper + .find(".context-menu-item button") + .simulate("keydown", { key: "Enter" }); + assert.calledOnce(onClick); + }); + it("should call focusSibling when onKeyDown is called with ArrowUp", () => { + const props = Object.assign({}, DEFAULT_PROPS, { + options: [{ label: "item1" }], + }); + const wrapper = mountWithProps(props); + const focusSibling = sinon.stub( + wrapper.find(_ContextMenuItem).instance(), + "focusSibling" + ); + wrapper + .find(".context-menu-item button") + .simulate("keydown", { key: "ArrowUp" }); + assert.calledOnce(focusSibling); + }); + it("should call focusSibling when onKeyDown is called with ArrowDown", () => { + const props = Object.assign({}, DEFAULT_PROPS, { + options: [{ label: "item1" }], + }); + const wrapper = mountWithProps(props); + const focusSibling = sinon.stub( + wrapper.find(_ContextMenuItem).instance(), + "focusSibling" + ); + wrapper + .find(".context-menu-item button") + .simulate("keydown", { key: "ArrowDown" }); + assert.calledOnce(focusSibling); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/CustomiseMenu.test.jsx b/browser/components/newtab/test/unit/content-src/components/CustomiseMenu.test.jsx new file mode 100644 index 0000000000..e1f84f7d84 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/CustomiseMenu.test.jsx @@ -0,0 +1,72 @@ +import { actionCreators as ac } from "common/Actions.sys.mjs"; +import { ContentSection } from "content-src/components/CustomizeMenu/ContentSection/ContentSection"; +import { mount } from "enzyme"; +import React from "react"; + +const DEFAULT_PROPS = { + enabledSections: { + pocketEnabled: true, + topSitesEnabled: true, + }, + mayHaveSponsoredTopSites: true, + mayHaveSponsoredStories: true, + pocketRegion: true, + dispatch: sinon.stub(), + setPref: sinon.stub(), +}; + +describe("ContentSection", () => { + let wrapper; + beforeEach(() => { + wrapper = mount(<ContentSection {...DEFAULT_PROPS} />); + }); + + it("should render the component", () => { + assert.ok(wrapper.exists()); + }); + + it("should look for a data-eventSource attribute and dispatch an event for INPUT", () => { + wrapper.instance().onPreferenceSelect({ + target: { + nodeName: "INPUT", + checked: true, + dataset: { preference: "foo", eventSource: "bar" }, + }, + }); + + assert.calledWith( + DEFAULT_PROPS.dispatch, + ac.UserEvent({ + event: "PREF_CHANGED", + source: "bar", + value: { status: true, menu_source: "CUSTOMIZE_MENU" }, + }) + ); + assert.calledWith(DEFAULT_PROPS.setPref, "foo", true); + wrapper.unmount(); + }); + + it("should have data-eventSource attributes on relevent pref changing inputs", () => { + wrapper = mount(<ContentSection {...DEFAULT_PROPS} />); + assert.equal( + wrapper.find("#shortcuts-toggle").prop("data-eventSource"), + "TOP_SITES" + ); + assert.equal( + wrapper.find("#sponsored-shortcuts").prop("data-eventSource"), + "SPONSORED_TOP_SITES" + ); + assert.equal( + wrapper.find("#pocket-toggle").prop("data-eventSource"), + "TOP_STORIES" + ); + assert.equal( + wrapper.find("#sponsored-pocket").prop("data-eventSource"), + "POCKET_SPOCS" + ); + assert.equal( + wrapper.find("#highlights-toggle").prop("data-eventSource"), + "HIGHLIGHTS" + ); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamAdmin.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamAdmin.test.jsx new file mode 100644 index 0000000000..41849fba3e --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamAdmin.test.jsx @@ -0,0 +1,267 @@ +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { + DiscoveryStreamAdminInner, + CollapseToggle, + DiscoveryStreamAdminUI, + Personalization, + ToggleStoryButton, +} from "content-src/components/DiscoveryStreamAdmin/DiscoveryStreamAdmin"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("DiscoveryStreamAdmin", () => { + let sandbox; + let wrapper; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + wrapper = shallow( + <DiscoveryStreamAdminInner + collapsed={false} + location={{ routes: [""] }} + Prefs={{}} + /> + ); + }); + afterEach(() => { + sandbox.restore(); + }); + it("should render DiscoveryStreamAdmin component", () => { + assert.ok(wrapper.exists()); + }); + it("should set a .collapsed class on the outer div if props.collapsed is true", () => { + wrapper.setProps({ collapsed: true }); + assert.isTrue(wrapper.find(".discoverystream-admin").hasClass("collapsed")); + }); + it("should set a .expanded class on the outer div if props.collapsed is false", () => { + wrapper.setProps({ collapsed: false }); + assert.isTrue(wrapper.find(".discoverystream-admin").hasClass("expanded")); + assert.isFalse( + wrapper.find(".discoverystream-admin").hasClass("collapsed") + ); + }); + it("should render a DS section", () => { + assert.equal(wrapper.find("h1").at(0).text(), "Discovery Stream Admin"); + }); + + describe("#DiscoveryStream", () => { + let state = {}; + let dispatch; + beforeEach(() => { + dispatch = sandbox.stub(); + state = { + config: { + enabled: true, + }, + layout: [], + spocs: { + frequency_caps: [], + }, + feeds: { + data: {}, + }, + }; + wrapper = shallow( + <DiscoveryStreamAdminUI + dispatch={dispatch} + otherPrefs={{}} + state={{ + DiscoveryStream: state, + }} + /> + ); + }); + it("should render a DiscoveryStreamAdminUI component", () => { + assert.equal(wrapper.find("h3").at(0).text(), "Layout"); + }); + it("should render a spoc in DiscoveryStreamAdminUI component", () => { + state.spocs = { + frequency_caps: [], + data: { + spocs: { + items: [ + { + id: 12345, + }, + ], + }, + }, + }; + wrapper = shallow( + <DiscoveryStreamAdminUI + otherPrefs={{}} + state={{ DiscoveryStream: state }} + /> + ); + wrapper.instance().onStoryToggle({ id: 12345 }); + const messageSummary = wrapper.find(".message-summary").at(0); + const pre = messageSummary.find("pre").at(0); + const spocText = pre.text(); + assert.equal(spocText, '{\n "id": 12345\n}'); + }); + it("should fire restorePrefDefaults with DISCOVERY_STREAM_CONFIG_RESET_DEFAULTS", () => { + wrapper.find("button").at(0).simulate("click"); + assert.calledWith( + dispatch, + ac.OnlyToMain({ + type: at.DISCOVERY_STREAM_CONFIG_RESET_DEFAULTS, + }) + ); + }); + it("should fire config change with DISCOVERY_STREAM_CONFIG_CHANGE", () => { + wrapper.find("button").at(1).simulate("click"); + assert.calledWith( + dispatch, + ac.OnlyToMain({ + type: at.DISCOVERY_STREAM_CONFIG_CHANGE, + data: { enabled: true }, + }) + ); + }); + it("should fire expireCache with DISCOVERY_STREAM_DEV_EXPIRE_CACHE", () => { + wrapper.find("button").at(2).simulate("click"); + assert.calledWith( + dispatch, + ac.OnlyToMain({ + type: at.DISCOVERY_STREAM_DEV_EXPIRE_CACHE, + }) + ); + }); + it("should fire systemTick with DISCOVERY_STREAM_DEV_SYSTEM_TICK", () => { + wrapper.find("button").at(3).simulate("click"); + assert.calledWith( + dispatch, + ac.OnlyToMain({ + type: at.DISCOVERY_STREAM_DEV_SYSTEM_TICK, + }) + ); + }); + it("should fire idleDaily with DISCOVERY_STREAM_DEV_IDLE_DAILY", () => { + wrapper.find("button").at(4).simulate("click"); + assert.calledWith( + dispatch, + ac.OnlyToMain({ + type: at.DISCOVERY_STREAM_DEV_IDLE_DAILY, + }) + ); + }); + it("should fire syncRemoteSettings with DISCOVERY_STREAM_DEV_SYNC_RS", () => { + wrapper.find("button").at(5).simulate("click"); + assert.calledWith( + dispatch, + ac.OnlyToMain({ + type: at.DISCOVERY_STREAM_DEV_SYNC_RS, + }) + ); + }); + it("should fire setConfigValue with DISCOVERY_STREAM_CONFIG_SET_VALUE", () => { + const name = "name"; + const value = "value"; + wrapper.instance().setConfigValue(name, value); + assert.calledWith( + dispatch, + ac.OnlyToMain({ + type: at.DISCOVERY_STREAM_CONFIG_SET_VALUE, + data: { name, value }, + }) + ); + }); + }); + + describe("#Personalization", () => { + let dispatch; + beforeEach(() => { + dispatch = sandbox.stub(); + wrapper = shallow( + <Personalization + dispatch={dispatch} + state={{ + Personalization: { + lastUpdated: 1000, + initialized: true, + }, + }} + /> + ); + }); + it("should render with pref checkbox, lastUpdated, and initialized", () => { + assert.lengthOf(wrapper.find("TogglePrefCheckbox"), 1); + assert.equal( + wrapper.find("td").at(1).text(), + "Personalization Last Updated" + ); + assert.equal( + wrapper.find("td").at(2).text(), + new Date(1000).toLocaleString() + ); + assert.equal( + wrapper.find("td").at(3).text(), + "Personalization Initialized" + ); + assert.equal(wrapper.find("td").at(4).text(), "true"); + }); + it("should render with no data with no last updated", () => { + wrapper = shallow( + <Personalization + dispatch={dispatch} + state={{ + Personalization: { + version: 2, + lastUpdated: 0, + initialized: true, + }, + }} + /> + ); + assert.equal(wrapper.find("td").at(2).text(), "(no data)"); + }); + it("should dispatch DISCOVERY_STREAM_PERSONALIZATION_TOGGLE", () => { + wrapper.instance().togglePersonalization(); + assert.calledWith( + dispatch, + ac.OnlyToMain({ + type: at.DISCOVERY_STREAM_PERSONALIZATION_TOGGLE, + }) + ); + }); + }); + + describe("#ToggleStoryButton", () => { + it("should fire onClick in toggle button", async () => { + let result = ""; + function onClick(spoc) { + result = spoc; + } + + wrapper = shallow(<ToggleStoryButton story="spoc" onClick={onClick} />); + wrapper.find("button").simulate("click"); + + assert.equal(result, "spoc"); + }); + }); +}); + +describe("CollapseToggle", () => { + let wrapper; + beforeEach(() => { + wrapper = shallow(<CollapseToggle location={{ routes: [""] }} />); + }); + + describe("rendering inner content", () => { + it("should not render DiscoveryStreamAdminInner for about:newtab (no hash)", () => { + wrapper.setProps({ location: { hash: "", routes: [""] } }); + assert.lengthOf(wrapper.find(DiscoveryStreamAdminInner), 0); + }); + + it("should render DiscoveryStreamAdminInner for about:newtab#devtools and subroutes", () => { + wrapper.setProps({ location: { hash: "#devtools", routes: [""] } }); + assert.lengthOf(wrapper.find(DiscoveryStreamAdminInner), 1); + + wrapper.setProps({ location: { hash: "#devtools-foo", routes: [""] } }); + assert.lengthOf(wrapper.find(DiscoveryStreamAdminInner), 1); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamBase.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamBase.test.jsx new file mode 100644 index 0000000000..7720e07327 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamBase.test.jsx @@ -0,0 +1,313 @@ +import { + _DiscoveryStreamBase as DiscoveryStreamBase, + isAllowedCSS, +} from "content-src/components/DiscoveryStreamBase/DiscoveryStreamBase"; +import { GlobalOverrider } from "test/unit/utils"; +import { CardGrid } from "content-src/components/DiscoveryStreamComponents/CardGrid/CardGrid"; +import { CollapsibleSection } from "content-src/components/CollapsibleSection/CollapsibleSection"; +import { DSMessage } from "content-src/components/DiscoveryStreamComponents/DSMessage/DSMessage"; +import { HorizontalRule } from "content-src/components/DiscoveryStreamComponents/HorizontalRule/HorizontalRule"; +import { Navigation } from "content-src/components/DiscoveryStreamComponents/Navigation/Navigation"; +import React from "react"; +import { shallow } from "enzyme"; +import { SectionTitle } from "content-src/components/DiscoveryStreamComponents/SectionTitle/SectionTitle"; +import { TopSites } from "content-src/components/TopSites/TopSites"; + +describe("<isAllowedCSS>", () => { + it("should allow colors", () => { + assert.isTrue(isAllowedCSS("color", "red")); + }); + + it("should allow chrome urls", () => { + assert.isTrue( + isAllowedCSS( + "background-image", + `url("chrome://global/skin/icons/info.svg")` + ) + ); + }); + + it("should allow chrome urls", () => { + assert.isTrue( + isAllowedCSS( + "background-image", + `url("chrome://browser/skin/history.svg")` + ) + ); + }); + + it("should allow allowed https urls", () => { + assert.isTrue( + isAllowedCSS( + "background-image", + `url("https://img-getpocket.cdn.mozilla.net/media/image.png")` + ) + ); + }); + + it("should disallow other https urls", () => { + assert.isFalse( + isAllowedCSS( + "background-image", + `url("https://mozilla.org/media/image.png")` + ) + ); + }); + + it("should disallow other protocols", () => { + assert.isFalse( + isAllowedCSS( + "background-image", + `url("ftp://mozilla.org/media/image.png")` + ) + ); + }); + + it("should allow allowed multiple valid urls", () => { + assert.isTrue( + isAllowedCSS( + "background-image", + `url("https://img-getpocket.cdn.mozilla.net/media/image.png"), url("chrome://browser/skin/history.svg")` + ) + ); + }); + + it("should disallow if any invaild", () => { + assert.isFalse( + isAllowedCSS( + "background-image", + `url("chrome://browser/skin/history.svg"), url("ftp://mozilla.org/media/image.png")` + ) + ); + }); +}); + +describe("<DiscoveryStreamBase>", () => { + let wrapper; + let globals; + let sandbox; + + function mountComponent(props = {}) { + const defaultProps = { + config: { collapsible: true }, + layout: [], + feeds: { loaded: true }, + spocs: { + loaded: true, + data: { spocs: null }, + }, + ...props, + }; + return shallow( + <DiscoveryStreamBase + locale="en-US" + DiscoveryStream={defaultProps} + Prefs={{ + values: { + "feeds.section.topstories": true, + "feeds.system.topstories": true, + "feeds.topsites": true, + }, + }} + App={{ + locale: "en-US", + }} + document={{ + documentElement: { lang: "en-US" }, + }} + Sections={[ + { + id: "topstories", + learnMore: { link: {} }, + pref: {}, + }, + ]} + /> + ); + } + + beforeEach(() => { + globals = new GlobalOverrider(); + sandbox = sinon.createSandbox(); + wrapper = mountComponent(); + }); + + afterEach(() => { + sandbox.restore(); + globals.restore(); + }); + + it("should render something if spocs are not loaded", () => { + wrapper = mountComponent({ + spocs: { loaded: false, data: { spocs: null } }, + }); + + assert.notEqual(wrapper.type(), null); + }); + + it("should render something if feeds are not loaded", () => { + wrapper = mountComponent({ feeds: { loaded: false } }); + + assert.notEqual(wrapper.type(), null); + }); + + it("should render nothing with no layout", () => { + assert.ok(wrapper.exists()); + assert.isEmpty(wrapper.children()); + }); + + it("should render a HorizontalRule component", () => { + wrapper = mountComponent({ + layout: [{ components: [{ type: "HorizontalRule" }] }], + }); + + assert.equal( + wrapper.find(".ds-column-grid div").children().at(0).type(), + HorizontalRule + ); + }); + + it("should render a CardGrid component", () => { + wrapper = mountComponent({ + layout: [{ components: [{ properties: {}, type: "CardGrid" }] }], + }); + + assert.equal( + wrapper.find(".ds-column-grid div").children().at(0).type(), + CardGrid + ); + }); + + it("should render a Navigation component", () => { + wrapper = mountComponent({ + layout: [{ components: [{ properties: {}, type: "Navigation" }] }], + }); + + assert.equal( + wrapper.find(".ds-column-grid div").children().at(0).type(), + Navigation + ); + }); + + it("should render nothing if there was only a Message", () => { + wrapper = mountComponent({ + layout: [ + { components: [{ header: {}, properties: {}, type: "Message" }] }, + ], + }); + + assert.isEmpty(wrapper.children()); + }); + + it("should render a regular Message when not collapsible", () => { + wrapper = mountComponent({ + config: { collapsible: false }, + layout: [ + { components: [{ header: {}, properties: {}, type: "Message" }] }, + ], + }); + + assert.equal( + wrapper.find(".ds-column-grid div").children().at(0).type(), + DSMessage + ); + }); + + it("should convert first Message component to CollapsibleSection", () => { + wrapper = mountComponent({ + layout: [ + { + components: [ + { header: {}, properties: {}, type: "Message" }, + { type: "HorizontalRule" }, + ], + }, + ], + }); + + assert.equal(wrapper.children().at(0).type(), CollapsibleSection); + assert.equal(wrapper.children().at(0).props().eventSource, "CARDGRID"); + }); + + it("should render a Message component", () => { + wrapper = mountComponent({ + layout: [ + { + components: [ + { header: {}, type: "Message" }, + { properties: {}, type: "Message" }, + ], + }, + ], + }); + + assert.equal( + wrapper.find(".ds-column-grid div").children().at(0).type(), + DSMessage + ); + }); + + it("should render a SectionTitle component", () => { + wrapper = mountComponent({ + layout: [{ components: [{ properties: {}, type: "SectionTitle" }] }], + }); + + assert.equal( + wrapper.find(".ds-column-grid div").children().at(0).type(), + SectionTitle + ); + }); + + it("should render TopSites", () => { + wrapper = mountComponent({ + layout: [{ components: [{ properties: {}, type: "TopSites" }] }], + }); + + assert.equal( + wrapper + .find(".ds-column-grid div") + .find(".ds-top-sites") + .children() + .at(0) + .type(), + TopSites + ); + }); + + describe("#onStyleMount", () => { + let parseStub; + + beforeEach(() => { + parseStub = sandbox.stub(); + globals.set("JSON", { parse: parseStub }); + }); + + afterEach(() => { + sandbox.restore(); + globals.restore(); + }); + + it("should return if no style", () => { + assert.isUndefined(wrapper.instance().onStyleMount()); + assert.notCalled(parseStub); + }); + + it("should insert rules", () => { + const sheetStub = { insertRule: sandbox.stub(), cssRules: [{}] }; + parseStub.returns([ + [ + null, + { + ".ds-message": "margin-bottom: -20px", + }, + null, + null, + ], + ]); + wrapper.instance().onStyleMount({ sheet: sheetStub, dataset: {} }); + + assert.calledOnce(sheetStub.insertRule); + assert.calledWithExactly(sheetStub.insertRule, "DUMMY#CSS.SELECTOR {}"); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/CardGrid.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/CardGrid.test.jsx new file mode 100644 index 0000000000..418a731ba1 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/CardGrid.test.jsx @@ -0,0 +1,354 @@ +import { + _CardGrid as CardGrid, + IntersectionObserver, + RecentSavesContainer, + OnboardingExperience, + DSSubHeader, +} from "content-src/components/DiscoveryStreamComponents/CardGrid/CardGrid"; +import { combineReducers, createStore } from "redux"; +import { INITIAL_STATE, reducers } from "common/Reducers.sys.mjs"; +import { Provider } from "react-redux"; +import { + DSCard, + PlaceholderDSCard, +} from "content-src/components/DiscoveryStreamComponents/DSCard/DSCard"; +import { TopicsWidget } from "content-src/components/DiscoveryStreamComponents/TopicsWidget/TopicsWidget"; +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import React from "react"; +import { shallow, mount } from "enzyme"; + +// Wrap this around any component that uses useSelector, +// or any mount that uses a child that uses redux. +function WrapWithProvider({ children, state = INITIAL_STATE }) { + let store = createStore(combineReducers(reducers), state); + return <Provider store={store}>{children}</Provider>; +} + +describe("<CardGrid>", () => { + let wrapper; + + beforeEach(() => { + wrapper = shallow( + <CardGrid + Prefs={INITIAL_STATE.Prefs} + DiscoveryStream={INITIAL_STATE.DiscoveryStream} + /> + ); + }); + + it("should render an empty div", () => { + assert.ok(wrapper.exists()); + assert.lengthOf(wrapper.children(), 0); + }); + + it("should render DSCards", () => { + wrapper.setProps({ items: 2, data: { recommendations: [{}, {}] } }); + + assert.lengthOf(wrapper.find(".ds-card-grid").children(), 2); + assert.equal(wrapper.find(".ds-card-grid").children().at(0).type(), DSCard); + }); + + it("should add 4 card classname to card grid", () => { + wrapper.setProps({ + fourCardLayout: true, + data: { recommendations: [{}, {}] }, + }); + + assert.ok(wrapper.find(".ds-card-grid-four-card-variant").exists()); + }); + + it("should add no description classname to card grid", () => { + wrapper.setProps({ + hideCardBackground: true, + data: { recommendations: [{}, {}] }, + }); + + assert.ok(wrapper.find(".ds-card-grid-hide-background").exists()); + }); + + it("should render sub header in the middle of the card grid for both regular and compact", () => { + const commonProps = { + essentialReadsHeader: true, + editorsPicksHeader: true, + items: 12, + data: { + recommendations: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}], + }, + Prefs: INITIAL_STATE.Prefs, + DiscoveryStream: INITIAL_STATE.DiscoveryStream, + }; + wrapper = mount( + <WrapWithProvider> + <CardGrid {...commonProps} /> + </WrapWithProvider> + ); + + assert.ok(wrapper.find(DSSubHeader).exists()); + + wrapper.setProps({ + compact: true, + }); + wrapper = mount( + <WrapWithProvider> + <CardGrid {...commonProps} compact={true} /> + </WrapWithProvider> + ); + + assert.ok(wrapper.find(DSSubHeader).exists()); + }); + + it("should add/hide description classname to card grid", () => { + wrapper.setProps({ + data: { recommendations: [{}, {}] }, + }); + + assert.ok(wrapper.find(".ds-card-grid-include-descriptions").exists()); + + wrapper.setProps({ + hideDescriptions: true, + data: { recommendations: [{}, {}] }, + }); + + assert.ok(!wrapper.find(".ds-card-grid-include-descriptions").exists()); + }); + + it("should create a widget card", () => { + wrapper.setProps({ + widgets: { + positions: [{ index: 1 }], + data: [{ type: "TopicsWidget" }], + }, + data: { + recommendations: [{}, {}, {}], + }, + }); + + assert.ok(wrapper.find(TopicsWidget).exists()); + }); +}); + +// Build IntersectionObserver class with the arg `entries` for the intersect callback. +function buildIntersectionObserver(entries) { + return class { + constructor(callback) { + this.callback = callback; + } + + observe() { + this.callback(entries); + } + + unobserve() {} + + disconnect() {} + }; +} + +describe("<IntersectionObserver>", () => { + let wrapper; + let fakeWindow; + let intersectEntries; + + beforeEach(() => { + intersectEntries = [{ isIntersecting: true }]; + fakeWindow = { + IntersectionObserver: buildIntersectionObserver(intersectEntries), + }; + wrapper = mount(<IntersectionObserver windowObj={fakeWindow} />); + }); + + it("should render an empty div", () => { + assert.ok(wrapper.exists()); + assert.equal(wrapper.children().at(0).type(), "div"); + }); + + it("should fire onIntersecting", () => { + const onIntersecting = sinon.stub(); + wrapper = mount( + <IntersectionObserver + windowObj={fakeWindow} + onIntersecting={onIntersecting} + /> + ); + assert.calledOnce(onIntersecting); + }); +}); + +describe("<RecentSavesContainer>", () => { + let wrapper; + let fakeWindow; + let intersectEntries; + let dispatch; + + beforeEach(() => { + dispatch = sinon.stub(); + intersectEntries = [{ isIntersecting: true }]; + fakeWindow = { + IntersectionObserver: buildIntersectionObserver(intersectEntries), + }; + wrapper = mount( + <WrapWithProvider + state={{ + DiscoveryStream: { + isUserLoggedIn: true, + recentSavesData: [ + { + resolved_id: "resolved_id", + top_image_url: "top_image_url", + title: "title", + resolved_url: "https://resolved_url", + domain: "domain", + excerpt: "excerpt", + }, + ], + experimentData: { + utmSource: "utmSource", + utmContent: "utmContent", + utmCampaign: "utmCampaign", + }, + }, + }} + > + <RecentSavesContainer + gridClassName="ds-card-grid" + windowObj={fakeWindow} + dispatch={dispatch} + /> + </WrapWithProvider> + ).find(RecentSavesContainer); + }); + + it("should render an IntersectionObserver when not visible", () => { + intersectEntries = [{ isIntersecting: false }]; + fakeWindow = { + IntersectionObserver: buildIntersectionObserver(intersectEntries), + }; + wrapper = mount( + <WrapWithProvider> + <RecentSavesContainer windowObj={fakeWindow} dispatch={dispatch} /> + </WrapWithProvider> + ).find(RecentSavesContainer); + + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(IntersectionObserver).exists()); + }); + + it("should render nothing if visible until we log in", () => { + assert.ok(!wrapper.find(IntersectionObserver).exists()); + assert.calledOnce(dispatch); + assert.calledWith( + dispatch, + ac.AlsoToMain({ + type: at.DISCOVERY_STREAM_POCKET_STATE_INIT, + }) + ); + }); + + it("should render a grid if visible and logged in", () => { + assert.lengthOf(wrapper.find(".ds-card-grid"), 1); + assert.lengthOf(wrapper.find(DSSubHeader), 1); + assert.lengthOf(wrapper.find(PlaceholderDSCard), 2); + assert.lengthOf(wrapper.find(DSCard), 3); + }); + + it("should render a my list link with proper utm params", () => { + assert.equal( + wrapper.find(".section-sub-link").at(0).prop("url"), + "https://getpocket.com/a?utm_source=utmSource&utm_content=utmContent&utm_campaign=utmCampaign" + ); + }); + + it("should fire a UserEvent for my list clicks", () => { + wrapper.find(".section-sub-link").at(0).simulate("click"); + assert.calledWith( + dispatch, + ac.DiscoveryStreamUserEvent({ + event: "CLICK", + source: `CARDGRID_RECENT_SAVES_VIEW_LIST`, + }) + ); + }); +}); + +describe("<OnboardingExperience>", () => { + let wrapper; + let fakeWindow; + let intersectEntries; + let dispatch; + let resizeCallback; + + let fakeResizeObserver = class { + constructor(callback) { + resizeCallback = callback; + } + + observe() {} + + unobserve() {} + + disconnect() {} + }; + + beforeEach(() => { + dispatch = sinon.stub(); + intersectEntries = [{ isIntersecting: true, intersectionRatio: 1 }]; + fakeWindow = { + ResizeObserver: fakeResizeObserver, + IntersectionObserver: buildIntersectionObserver(intersectEntries), + document: { + visibilityState: "visible", + addEventListener: () => {}, + removeEventListener: () => {}, + }, + }; + wrapper = mount( + <WrapWithProvider state={{}}> + <OnboardingExperience windowObj={fakeWindow} dispatch={dispatch} /> + </WrapWithProvider> + ).find(OnboardingExperience); + }); + + it("should render a ds-onboarding", () => { + assert.ok(wrapper.exists()); + assert.lengthOf(wrapper.find(".ds-onboarding"), 1); + }); + + it("should dismiss on dismiss click", () => { + wrapper.find(".ds-dismiss-button").simulate("click"); + + assert.calledWith( + dispatch, + ac.DiscoveryStreamUserEvent({ + event: "BLOCK", + source: "POCKET_ONBOARDING", + }) + ); + assert.calledWith( + dispatch, + ac.SetPref("discoverystream.onboardingExperience.dismissed", true) + ); + assert.equal(wrapper.getDOMNode().style["max-height"], "0px"); + assert.equal(wrapper.getDOMNode().style.opacity, "0"); + }); + + it("should update max-height on resize", () => { + sinon + .stub(wrapper.find(".ds-onboarding-ref").getDOMNode(), "offsetHeight") + .get(() => 123); + resizeCallback(); + assert.equal(wrapper.getDOMNode().style["max-height"], "123px"); + }); + + it("should fire intersection events", () => { + assert.calledWith( + dispatch, + ac.DiscoveryStreamUserEvent({ + event: "IMPRESSION", + source: "POCKET_ONBOARDING", + }) + ); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/CollectionCardGrid.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/CollectionCardGrid.test.jsx new file mode 100644 index 0000000000..10b06cab9d --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/CollectionCardGrid.test.jsx @@ -0,0 +1,138 @@ +import { CollectionCardGrid } from "content-src/components/DiscoveryStreamComponents/CollectionCardGrid/CollectionCardGrid"; +import { CardGrid } from "content-src/components/DiscoveryStreamComponents/CardGrid/CardGrid"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<CollectionCardGrid>", () => { + let wrapper; + let sandbox; + let dispatchStub; + const initialSpocs = [ + { id: 123, url: "123" }, + { id: 456, url: "456" }, + { id: 789, url: "789" }, + ]; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + dispatchStub = sandbox.stub(); + wrapper = shallow( + <CollectionCardGrid + dispatch={dispatchStub} + type="COLLECTIONCARDGRID" + placement={{ + name: "spocs", + }} + data={{ + spocs: initialSpocs, + }} + spocs={{ + data: { + spocs: { + title: "title", + context: "context", + items: initialSpocs, + }, + }, + }} + /> + ); + }); + + it("should render an empty div", () => { + wrapper = shallow(<CollectionCardGrid />); + assert.ok(wrapper.exists()); + assert.ok(!wrapper.exists(".ds-collection-card-grid")); + }); + + it("should render a CardGrid", () => { + assert.lengthOf(wrapper.find(".ds-collection-card-grid").children(), 1); + assert.equal( + wrapper.find(".ds-collection-card-grid").children().at(0).type(), + CardGrid + ); + }); + + it("should inject spocs in every CardGrid rec position", () => { + assert.lengthOf( + wrapper.find(".ds-collection-card-grid").children().at(0).props().data + .recommendations, + 3 + ); + }); + + it("should pass along title and context to CardGrid", () => { + assert.equal( + wrapper.find(".ds-collection-card-grid").children().at(0).props().title, + "title" + ); + + assert.equal( + wrapper.find(".ds-collection-card-grid").children().at(0).props().context, + "context" + ); + }); + + it("should render nothing without a title", () => { + wrapper = shallow( + <CollectionCardGrid + dispatch={dispatchStub} + placement={{ + name: "spocs", + }} + data={{ + spocs: initialSpocs, + }} + spocs={{ + data: { + spocs: { + title: "", + context: "context", + items: initialSpocs, + }, + }, + }} + /> + ); + + assert.ok(wrapper.exists()); + assert.ok(!wrapper.exists(".ds-collection-card-grid")); + }); + + it("should dispatch telemety events on dismiss", () => { + wrapper.instance().onDismissClick(); + + const firstCall = dispatchStub.getCall(0); + const secondCall = dispatchStub.getCall(1); + const thirdCall = dispatchStub.getCall(2); + + assert.equal(firstCall.args[0].type, "BLOCK_URL"); + let expected = ["123", "456", "789"].map(url => ({ + url, + pocket_id: undefined, + isSponsoredTopSite: undefined, + position: 0, + is_pocket_card: false, + })); + + assert.deepEqual(firstCall.args[0].data, expected); + + assert.equal(secondCall.args[0].type, "DISCOVERY_STREAM_USER_EVENT"); + assert.deepEqual(secondCall.args[0].data, { + event: "BLOCK", + source: "COLLECTIONCARDGRID", + action_position: 0, + }); + + assert.equal(thirdCall.args[0].type, "TELEMETRY_IMPRESSION_STATS"); + assert.deepEqual(thirdCall.args[0].data, { + source: "COLLECTIONCARDGRID", + block: 0, + tiles: [ + { id: 123, pos: 0 }, + { id: 456, pos: 1 }, + { id: 789, pos: 2 }, + ], + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSCard.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSCard.test.jsx new file mode 100644 index 0000000000..dad4d19fa5 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSCard.test.jsx @@ -0,0 +1,582 @@ +import { + _DSCard as DSCard, + readTimeFromWordCount, + DSSource, + DefaultMeta, + PlaceholderDSCard, +} from "content-src/components/DiscoveryStreamComponents/DSCard/DSCard"; +import { + DSContextFooter, + StatusMessage, + SponsorLabel, +} from "content-src/components/DiscoveryStreamComponents/DSContextFooter/DSContextFooter"; +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { DSLinkMenu } from "content-src/components/DiscoveryStreamComponents/DSLinkMenu/DSLinkMenu"; +import React from "react"; +import { INITIAL_STATE } from "common/Reducers.sys.mjs"; +import { SafeAnchor } from "content-src/components/DiscoveryStreamComponents/SafeAnchor/SafeAnchor"; +import { shallow, mount } from "enzyme"; +import { FluentOrText } from "content-src/components/FluentOrText/FluentOrText"; + +const DEFAULT_PROPS = { + url: "about:robots", + title: "title", + App: { + isForStartupCache: false, + }, + DiscoveryStream: INITIAL_STATE.DiscoveryStream, +}; + +describe("<DSCard>", () => { + let wrapper; + let sandbox; + let dispatch; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + dispatch = sandbox.stub(); + wrapper = shallow(<DSCard dispatch={dispatch} {...DEFAULT_PROPS} />); + wrapper.setState({ isSeen: true }); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(".ds-card")); + }); + + it("should render a SafeAnchor", () => { + wrapper.setProps({ url: "https://foo.com" }); + + assert.equal(wrapper.children().at(0).type(), SafeAnchor); + assert.propertyVal( + wrapper.children().at(0).props(), + "url", + "https://foo.com" + ); + }); + + it("should pass onLinkClick prop", () => { + assert.propertyVal( + wrapper.children().at(0).props(), + "onLinkClick", + wrapper.instance().onLinkClick + ); + }); + + it("should render DSLinkMenu", () => { + assert.equal(wrapper.children().at(1).type(), DSLinkMenu); + }); + + it("should start with no .active class", () => { + assert.equal(wrapper.find(".active").length, 0); + }); + + it("should render badges for pocket, bookmark when not a spoc element ", () => { + wrapper = mount(<DSCard context_type="bookmark" {...DEFAULT_PROPS} />); + wrapper.setState({ isSeen: true }); + const contextFooter = wrapper.find(DSContextFooter); + + assert.lengthOf(contextFooter.find(StatusMessage), 1); + }); + + it("should render Sponsored Context for a spoc element", () => { + const context = "Sponsored by Foo"; + wrapper = mount( + <DSCard context_type="bookmark" context={context} {...DEFAULT_PROPS} /> + ); + wrapper.setState({ isSeen: true }); + const contextFooter = wrapper.find(DSContextFooter); + + assert.lengthOf(contextFooter.find(StatusMessage), 0); + assert.equal(contextFooter.find(".story-sponsored-label").text(), context); + }); + + it("should render time to read", () => { + const discoveryStream = { + ...INITIAL_STATE.DiscoveryStream, + readTime: true, + }; + wrapper = mount( + <DSCard + time_to_read={4} + {...DEFAULT_PROPS} + DiscoveryStream={discoveryStream} + /> + ); + wrapper.setState({ isSeen: true }); + const defaultMeta = wrapper.find(DefaultMeta); + assert.lengthOf(defaultMeta, 1); + assert.equal(defaultMeta.props().timeToRead, 4); + }); + + it("should not show save to pocket button for spocs", () => { + wrapper.setProps({ + id: "fooidx", + pos: 1, + type: "foo", + flightId: 12345, + saveToPocketCard: true, + }); + + let stpButton = wrapper.find(".card-stp-button"); + + assert.lengthOf(stpButton, 0); + }); + + it("should show save to pocket button for non-spocs", () => { + wrapper.setProps({ + id: "fooidx", + pos: 1, + type: "foo", + saveToPocketCard: true, + }); + + let stpButton = wrapper.find(".card-stp-button"); + + assert.lengthOf(stpButton, 1); + }); + + describe("onLinkClick", () => { + let fakeWindow; + + beforeEach(() => { + fakeWindow = { + requestIdleCallback: sinon.stub().returns(1), + cancelIdleCallback: sinon.stub(), + innerWidth: 1000, + innerHeight: 900, + }; + wrapper = mount( + <DSCard {...DEFAULT_PROPS} dispatch={dispatch} windowObj={fakeWindow} /> + ); + }); + + it("should call dispatch with the correct events", () => { + wrapper.setProps({ id: "fooidx", pos: 1, type: "foo" }); + + wrapper.instance().onLinkClick(); + + assert.calledTwice(dispatch); + assert.calledWith( + dispatch, + ac.DiscoveryStreamUserEvent({ + event: "CLICK", + source: "FOO", + action_position: 1, + value: { + card_type: "organic", + recommendation_id: undefined, + tile_id: "fooidx", + }, + }) + ); + assert.calledWith( + dispatch, + ac.ImpressionStats({ + click: 0, + source: "FOO", + tiles: [ + { + id: "fooidx", + pos: 1, + type: "organic", + recommendation_id: undefined, + }, + ], + window_inner_width: 1000, + window_inner_height: 900, + }) + ); + }); + + it("should set the right card_type on spocs", () => { + wrapper.setProps({ id: "fooidx", pos: 1, type: "foo", flightId: 12345 }); + + wrapper.instance().onLinkClick(); + + assert.calledTwice(dispatch); + assert.calledWith( + dispatch, + ac.DiscoveryStreamUserEvent({ + event: "CLICK", + source: "FOO", + action_position: 1, + value: { + card_type: "spoc", + recommendation_id: undefined, + tile_id: "fooidx", + }, + }) + ); + assert.calledWith( + dispatch, + ac.ImpressionStats({ + click: 0, + source: "FOO", + tiles: [ + { + id: "fooidx", + pos: 1, + type: "spoc", + recommendation_id: undefined, + }, + ], + window_inner_width: 1000, + window_inner_height: 900, + }) + ); + }); + + it("should call dispatch with a shim", () => { + wrapper.setProps({ + id: "fooidx", + pos: 1, + type: "foo", + shim: { + click: "click shim", + }, + }); + + wrapper.instance().onLinkClick(); + + assert.calledTwice(dispatch); + assert.calledWith( + dispatch, + ac.DiscoveryStreamUserEvent({ + event: "CLICK", + source: "FOO", + action_position: 1, + value: { + card_type: "organic", + recommendation_id: undefined, + tile_id: "fooidx", + shim: "click shim", + }, + }) + ); + assert.calledWith( + dispatch, + ac.ImpressionStats({ + click: 0, + source: "FOO", + tiles: [ + { + id: "fooidx", + pos: 1, + shim: "click shim", + type: "organic", + recommendation_id: undefined, + }, + ], + window_inner_width: 1000, + window_inner_height: 900, + }) + ); + }); + }); + + describe("DSCard with CTA", () => { + beforeEach(() => { + wrapper = mount(<DSCard {...DEFAULT_PROPS} />); + wrapper.setState({ isSeen: true }); + }); + + it("should render Default Meta", () => { + const default_meta = wrapper.find(DefaultMeta); + assert.ok(default_meta.exists()); + }); + }); + + describe("DSCard with Intersection Observer", () => { + beforeEach(() => { + wrapper = shallow(<DSCard {...DEFAULT_PROPS} />); + }); + + it("should render card when seen", () => { + let card = wrapper.find("div.ds-card.placeholder"); + assert.lengthOf(card, 1); + + wrapper.instance().observer = { + unobserve: sandbox.stub(), + }; + wrapper.instance().placeholderElement = "element"; + + wrapper.instance().onSeen([ + { + isIntersecting: true, + }, + ]); + + assert.isTrue(wrapper.instance().state.isSeen); + card = wrapper.find("div.ds-card.placeholder"); + assert.lengthOf(card, 0); + assert.lengthOf(wrapper.find(SafeAnchor), 1); + assert.calledOnce(wrapper.instance().observer.unobserve); + assert.calledWith(wrapper.instance().observer.unobserve, "element"); + }); + + it("should setup proper placholder ref for isSeen", () => { + wrapper.instance().setPlaceholderRef("element"); + assert.equal(wrapper.instance().placeholderElement, "element"); + }); + + it("should setup observer on componentDidMount", () => { + wrapper = mount(<DSCard {...DEFAULT_PROPS} />); + assert.isTrue(!!wrapper.instance().observer); + }); + }); + + describe("DSCard with Idle Callback", () => { + let windowStub = { + requestIdleCallback: sinon.stub().returns(1), + cancelIdleCallback: sinon.stub(), + }; + beforeEach(() => { + wrapper = shallow(<DSCard windowObj={windowStub} {...DEFAULT_PROPS} />); + }); + + it("should call requestIdleCallback on componentDidMount", () => { + assert.calledOnce(windowStub.requestIdleCallback); + }); + + it("should call cancelIdleCallback on componentWillUnmount", () => { + wrapper.instance().componentWillUnmount(); + assert.calledOnce(windowStub.cancelIdleCallback); + }); + }); + + describe("DSCard when rendered for about:home startup cache", () => { + beforeEach(() => { + const props = { + App: { + isForStartupCache: true, + }, + DiscoveryStream: INITIAL_STATE.DiscoveryStream, + }; + wrapper = mount(<DSCard {...props} />); + }); + + it("should be set as isSeen automatically", () => { + assert.isTrue(wrapper.instance().state.isSeen); + }); + }); + + describe("DSCard onSaveClick", () => { + it("should fire telemetry for onSaveClick", () => { + wrapper.setProps({ id: "fooidx", pos: 1, type: "foo" }); + wrapper.instance().onSaveClick(); + + assert.calledThrice(dispatch); + assert.calledWith( + dispatch, + ac.AlsoToMain({ + type: at.SAVE_TO_POCKET, + data: { site: { url: "about:robots", title: "title" } }, + }) + ); + assert.calledWith( + dispatch, + ac.DiscoveryStreamUserEvent({ + event: "SAVE_TO_POCKET", + source: "CARDGRID_HOVER", + action_position: 1, + value: { + card_type: "organic", + recommendation_id: undefined, + tile_id: "fooidx", + }, + }) + ); + assert.calledWith( + dispatch, + ac.ImpressionStats({ + source: "CARDGRID_HOVER", + pocket: 0, + tiles: [ + { + id: "fooidx", + pos: 1, + recommendation_id: undefined, + }, + ], + }) + ); + }); + }); + + describe("DSCard menu open states", () => { + let cardNode; + let fakeDocument; + let fakeWindow; + + beforeEach(() => { + fakeDocument = { l10n: { translateFragment: sinon.stub() } }; + fakeWindow = { + document: fakeDocument, + requestIdleCallback: sinon.stub().returns(1), + cancelIdleCallback: sinon.stub(), + }; + wrapper = mount(<DSCard {...DEFAULT_PROPS} windowObj={fakeWindow} />); + wrapper.setState({ isSeen: true }); + cardNode = wrapper.getDOMNode(); + }); + + it("Should remove active on Menu Update", () => { + // Add active class name to DSCard wrapper + // to simulate menu open state + cardNode.classList.add("active"); + assert.equal( + cardNode.className, + "ds-card ds-card-title-lines-3 ds-card-desc-lines-3 active" + ); + + wrapper.instance().onMenuUpdate(false); + wrapper.update(); + + assert.equal( + cardNode.className, + "ds-card ds-card-title-lines-3 ds-card-desc-lines-3" + ); + }); + + it("Should add active on Menu Show", async () => { + await wrapper.instance().onMenuShow(); + wrapper.update(); + assert.equal( + cardNode.className, + "ds-card ds-card-title-lines-3 ds-card-desc-lines-3 active" + ); + }); + + it("Should add last-item to support resized window", async () => { + fakeWindow.scrollMaxX = 20; + await wrapper.instance().onMenuShow(); + wrapper.update(); + assert.equal( + cardNode.className, + "ds-card ds-card-title-lines-3 ds-card-desc-lines-3 last-item active" + ); + }); + + it("should remove .active and .last-item classes", () => { + const instance = wrapper.instance(); + const remove = sinon.stub(); + instance.contextMenuButtonHostElement = { + classList: { remove }, + }; + instance.onMenuUpdate(); + assert.calledOnce(remove); + }); + + it("should add .active and .last-item classes", async () => { + const instance = wrapper.instance(); + const add = sinon.stub(); + instance.contextMenuButtonHostElement = { + classList: { add }, + }; + await instance.onMenuShow(); + assert.calledOnce(add); + }); + }); +}); + +describe("<PlaceholderDSCard> component", () => { + it("should have placeholder prop", () => { + const wrapper = shallow(<PlaceholderDSCard />); + const placeholder = wrapper.prop("placeholder"); + assert.isTrue(placeholder); + }); + + it("should contain placeholder div", () => { + const wrapper = shallow(<DSCard placeholder={true} {...DEFAULT_PROPS} />); + wrapper.setState({ isSeen: true }); + const card = wrapper.find("div.ds-card.placeholder"); + assert.lengthOf(card, 1); + }); + + it("should not be clickable", () => { + const wrapper = shallow(<DSCard placeholder={true} {...DEFAULT_PROPS} />); + wrapper.setState({ isSeen: true }); + const anchor = wrapper.find("SafeAnchor.ds-card-link"); + assert.lengthOf(anchor, 0); + }); + + it("should not have context menu", () => { + const wrapper = shallow(<DSCard placeholder={true} {...DEFAULT_PROPS} />); + wrapper.setState({ isSeen: true }); + const linkMenu = wrapper.find(DSLinkMenu); + assert.lengthOf(linkMenu, 0); + }); +}); + +describe("<DSSource> component", () => { + it("should return a default source without compact", () => { + const wrapper = shallow(<DSSource source="Mozilla" />); + + let sourceElement = wrapper.find(".source"); + assert.equal(sourceElement.text(), "Mozilla"); + }); + it("should return a default source with compact without a sponsor or time to read", () => { + const wrapper = shallow(<DSSource compact={true} source="Mozilla" />); + + let sourceElement = wrapper.find(".source"); + assert.equal(sourceElement.text(), "Mozilla"); + }); + it("should return a SponsorLabel with compact and a sponsor", () => { + const wrapper = shallow( + <DSSource newSponsoredLabel={true} sponsor="Mozilla" /> + ); + const sponsorLabel = wrapper.find(SponsorLabel); + assert.lengthOf(sponsorLabel, 1); + }); + it("should return a time to read with compact and without a sponsor but with a time to read", () => { + const wrapper = shallow( + <DSSource compact={true} source="Mozilla" timeToRead="2000" /> + ); + + let timeToRead = wrapper.find(".time-to-read"); + assert.lengthOf(timeToRead, 1); + + // Weirdly, we can test for the pressence of fluent, because time to read needs to be translated. + // This is also because we did a shallow render, that th contents of fluent would be empty anyway. + const fluentOrText = wrapper.find(FluentOrText); + assert.lengthOf(fluentOrText, 1); + }); + it("should prioritize a SponsorLabel if for some reason it gets everything", () => { + const wrapper = shallow( + <DSSource + newSponsoredLabel={true} + sponsor="Mozilla" + source="Mozilla" + timeToRead="2000" + /> + ); + const sponsorLabel = wrapper.find(SponsorLabel); + assert.lengthOf(sponsorLabel, 1); + }); +}); + +describe("readTimeFromWordCount function", () => { + it("should return proper read time", () => { + const result = readTimeFromWordCount(2000); + assert.equal(result, 10); + }); + it("should return false with falsey word count", () => { + assert.isFalse(readTimeFromWordCount()); + assert.isFalse(readTimeFromWordCount(0)); + assert.isFalse(readTimeFromWordCount("")); + assert.isFalse(readTimeFromWordCount(null)); + assert.isFalse(readTimeFromWordCount(undefined)); + }); + it("should return NaN with invalid word count", () => { + assert.isNaN(readTimeFromWordCount("zero")); + assert.isNaN(readTimeFromWordCount({})); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSContextFooter.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSContextFooter.test.jsx new file mode 100644 index 0000000000..08ac7868ce --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSContextFooter.test.jsx @@ -0,0 +1,138 @@ +import { + DSContextFooter, + StatusMessage, + DSMessageFooter, +} from "content-src/components/DiscoveryStreamComponents/DSContextFooter/DSContextFooter"; +import React from "react"; +import { mount } from "enzyme"; +import { cardContextTypes } from "content-src/components/Card/types.js"; +import { FluentOrText } from "content-src/components/FluentOrText/FluentOrText.jsx"; + +describe("<DSContextFooter>", () => { + let wrapper; + let sandbox; + const bookmarkBadge = "bookmark"; + const removeBookmarkBadge = "removedBookmark"; + const context = "Sponsored by Babel"; + const sponsored_by_override = "Sponsored override"; + const engagement = "Popular"; + + beforeEach(() => { + wrapper = mount(<DSContextFooter />); + sandbox = sinon.createSandbox(); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should render", () => assert.isTrue(wrapper.exists())); + it("should not render an engagement status if display_engagement_labels is false", () => { + wrapper = mount( + <DSContextFooter + display_engagement_labels={false} + engagement={engagement} + /> + ); + + const engagementLabel = wrapper.find(".story-view-count"); + assert.equal(engagementLabel.length, 0); + }); + it("should render a badge if a proper badge prop is passed", () => { + wrapper = mount( + <DSContextFooter context_type={bookmarkBadge} engagement={engagement} /> + ); + const { fluentID } = cardContextTypes[bookmarkBadge]; + + assert.lengthOf(wrapper.find(".story-view-count"), 0); + const statusLabel = wrapper.find(".story-context-label"); + assert.equal(statusLabel.prop("data-l10n-id"), fluentID); + }); + it("should only render a sponsored context if pass a sponsored context", async () => { + wrapper = mount( + <DSContextFooter + context_type={bookmarkBadge} + context={context} + engagement={engagement} + /> + ); + + assert.lengthOf(wrapper.find(".story-view-count"), 0); + assert.lengthOf(wrapper.find(StatusMessage), 0); + assert.equal(wrapper.find(".story-sponsored-label").text(), context); + }); + it("should render a sponsored_by_override if passed a sponsored_by_override", async () => { + wrapper = mount( + <DSContextFooter + context_type={bookmarkBadge} + context={context} + sponsored_by_override={sponsored_by_override} + engagement={engagement} + /> + ); + + assert.equal( + wrapper.find(".story-sponsored-label").text(), + sponsored_by_override + ); + }); + it("should render nothing with a sponsored_by_override empty string", async () => { + wrapper = mount( + <DSContextFooter + context_type={bookmarkBadge} + context={context} + sponsored_by_override="" + engagement={engagement} + /> + ); + + assert.isFalse(wrapper.find(".story-sponsored-label").exists()); + }); + it("should render localized string with sponsor with no sponsored_by_override", async () => { + wrapper = mount( + <DSContextFooter + context_type={bookmarkBadge} + context={context} + sponsor="Nimoy" + engagement={engagement} + /> + ); + + assert.equal( + wrapper.find(".story-sponsored-label").children().at(0).type(), + FluentOrText + ); + }); + it("should render a new badge if props change from an old badge to a new one", async () => { + wrapper = mount(<DSContextFooter context_type={bookmarkBadge} />); + + const { fluentID: bookmarkFluentID } = cardContextTypes[bookmarkBadge]; + const bookmarkStatusMessage = wrapper.find( + `div[data-l10n-id='${bookmarkFluentID}']` + ); + assert.isTrue(bookmarkStatusMessage.exists()); + + const { fluentID: removeBookmarkFluentID } = + cardContextTypes[removeBookmarkBadge]; + + wrapper.setProps({ context_type: removeBookmarkBadge }); + await wrapper.update(); + + assert.isEmpty(bookmarkStatusMessage); + const removedBookmarkStatusMessage = wrapper.find( + `div[data-l10n-id='${removeBookmarkFluentID}']` + ); + assert.isTrue(removedBookmarkStatusMessage.exists()); + }); + it("should render a story footer", () => { + wrapper = mount( + <DSMessageFooter + context_type={bookmarkBadge} + engagement={engagement} + display_engagement_labels={true} + /> + ); + + assert.lengthOf(wrapper.find(".story-footer"), 1); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSDismiss.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSDismiss.test.jsx new file mode 100644 index 0000000000..2f7e206b4f --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSDismiss.test.jsx @@ -0,0 +1,51 @@ +import { DSDismiss } from "content-src/components/DiscoveryStreamComponents/DSDismiss/DSDismiss"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<DSDismiss>", () => { + const fakeSpoc = { + url: "https://foo.com", + guid: "1234", + }; + let wrapper; + let sandbox; + let onDismissClickStub; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + onDismissClickStub = sandbox.stub(); + wrapper = shallow( + <DSDismiss + data={fakeSpoc} + onDismissClick={onDismissClickStub} + shouldSendImpressionStats={true} + /> + ); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(".ds-dismiss").exists()); + }); + + it("should render proper hover state", () => { + wrapper.instance().onHover(); + assert.ok(wrapper.find(".hovering").exists()); + wrapper.instance().offHover(); + assert.ok(!wrapper.find(".hovering").exists()); + }); + + it("should dispatch call onDismissClick", () => { + wrapper.instance().onDismissClick(); + assert.calledOnce(onDismissClickStub); + }); + + it("should add extra classes", () => { + wrapper = shallow(<DSDismiss extraClasses="extra-class" />); + assert.ok(wrapper.find(".extra-class").exists()); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSEmptyState.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSEmptyState.test.jsx new file mode 100644 index 0000000000..6aa8045299 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSEmptyState.test.jsx @@ -0,0 +1,73 @@ +import { DSEmptyState } from "content-src/components/DiscoveryStreamComponents/DSEmptyState/DSEmptyState"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<DSEmptyState>", () => { + let wrapper; + + beforeEach(() => { + wrapper = shallow(<DSEmptyState />); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(".section-empty-state").exists()); + }); + + it("should render defaultempty state message", () => { + assert.ok(wrapper.find(".empty-state-message").exists()); + const header = wrapper.find( + "h2[data-l10n-id='newtab-discovery-empty-section-topstories-header']" + ); + const paragraph = wrapper.find( + "p[data-l10n-id='newtab-discovery-empty-section-topstories-content']" + ); + + assert.ok(header.exists()); + assert.ok(paragraph.exists()); + }); + + it("should render failed state message", () => { + wrapper = shallow(<DSEmptyState status="failed" />); + const button = wrapper.find( + "button[data-l10n-id='newtab-discovery-empty-section-topstories-try-again-button']" + ); + + assert.ok(button.exists()); + }); + + it("should render waiting state message", () => { + wrapper = shallow(<DSEmptyState status="waiting" />); + const button = wrapper.find( + "button[data-l10n-id='newtab-discovery-empty-section-topstories-loading']" + ); + + assert.ok(button.exists()); + }); + + it("should dispatch DISCOVERY_STREAM_RETRY_FEED on failed state button click", () => { + const dispatch = sinon.spy(); + + wrapper = shallow( + <DSEmptyState + status="failed" + dispatch={dispatch} + feed={{ url: "https://foo.com", data: {} }} + /> + ); + wrapper.find("button.try-again-button").simulate("click"); + + assert.calledTwice(dispatch); + let [action] = dispatch.firstCall.args; + assert.equal(action.type, "DISCOVERY_STREAM_FEED_UPDATE"); + assert.deepEqual(action.data.feed, { + url: "https://foo.com", + data: { status: "waiting" }, + }); + + [action] = dispatch.secondCall.args; + + assert.equal(action.type, "DISCOVERY_STREAM_RETRY_FEED"); + assert.deepEqual(action.data.feed, { url: "https://foo.com", data: {} }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSImage.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSImage.test.jsx new file mode 100644 index 0000000000..bb2ce3b0b3 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSImage.test.jsx @@ -0,0 +1,146 @@ +import { DSImage } from "content-src/components/DiscoveryStreamComponents/DSImage/DSImage"; +import { mount } from "enzyme"; +import React from "react"; + +describe("Discovery Stream <DSImage>", () => { + it("should have a child with class ds-image", () => { + const img = mount(<DSImage />); + const child = img.find(".ds-image"); + + assert.lengthOf(child, 1); + }); + + it("should set proper sources if only `source` is available", () => { + const img = mount(<DSImage source="https://placekitten.com/g/640/480" />); + + assert.equal( + img.find("img").prop("src"), + "https://placekitten.com/g/640/480" + ); + }); + + it("should set proper sources if `rawSource` is available", () => { + const testSizes = [ + { + mediaMatcher: "(min-width: 1122px)", + width: 296, + height: 148, + }, + + { + mediaMatcher: "(min-width: 866px)", + width: 218, + height: 109, + }, + + { + mediaMatcher: "(max-width: 610px)", + width: 202, + height: 101, + }, + ]; + + const img = mount( + <DSImage + rawSource="https://placekitten.com/g/640/480" + sizes={testSizes} + /> + ); + + assert.equal( + img.find("img").prop("src"), + "https://placekitten.com/g/640/480" + ); + assert.equal( + img.find("img").prop("srcSet"), + [ + "https://img-getpocket.cdn.mozilla.net/296x148/filters:format(jpeg):quality(60):no_upscale():strip_exif()/https%3A%2F%2Fplacekitten.com%2Fg%2F640%2F480 296w", + "https://img-getpocket.cdn.mozilla.net/592x296/filters:format(jpeg):quality(60):no_upscale():strip_exif()/https%3A%2F%2Fplacekitten.com%2Fg%2F640%2F480 592w", + "https://img-getpocket.cdn.mozilla.net/218x109/filters:format(jpeg):quality(60):no_upscale():strip_exif()/https%3A%2F%2Fplacekitten.com%2Fg%2F640%2F480 218w", + "https://img-getpocket.cdn.mozilla.net/436x218/filters:format(jpeg):quality(60):no_upscale():strip_exif()/https%3A%2F%2Fplacekitten.com%2Fg%2F640%2F480 436w", + "https://img-getpocket.cdn.mozilla.net/202x101/filters:format(jpeg):quality(60):no_upscale():strip_exif()/https%3A%2F%2Fplacekitten.com%2Fg%2F640%2F480 202w", + "https://img-getpocket.cdn.mozilla.net/404x202/filters:format(jpeg):quality(60):no_upscale():strip_exif()/https%3A%2F%2Fplacekitten.com%2Fg%2F640%2F480 404w", + ].join(",") + ); + }); + + it("should fall back to unoptimized when optimized failed", () => { + const img = mount( + <DSImage + source="https://placekitten.com/g/640/480" + rawSource="https://placekitten.com/g/640/480" + /> + ); + img.setState({ + isSeen: true, + containerWidth: 640, + containerHeight: 480, + }); + + img.instance().onOptimizedImageError(); + img.update(); + + assert.equal( + img.find("img").prop("src"), + "https://placekitten.com/g/640/480" + ); + }); + + it("should render a placeholder image with no source and recent save", () => { + const img = mount(<DSImage isRecentSave={true} url="foo" title="bar" />); + img.setState({ isSeen: true }); + + img.update(); + + assert.equal(img.find("div").prop("className"), "placeholder-image"); + }); + + it("should render a broken image with a source and a recent save", () => { + const img = mount(<DSImage isRecentSave={true} source="foo" />); + img.setState({ isSeen: true }); + + img.instance().onNonOptimizedImageError(); + img.update(); + + assert.equal(img.find("div").prop("className"), "broken-image"); + }); + + it("should render a broken image without a source and not a recent save", () => { + const img = mount(<DSImage isRecentSave={false} />); + img.setState({ isSeen: true }); + + img.instance().onNonOptimizedImageError(); + img.update(); + + assert.equal(img.find("div").prop("className"), "broken-image"); + }); + + it("should update loaded state when seen", () => { + const img = mount( + <DSImage rawSource="https://placekitten.com/g/640/480" /> + ); + + img.instance().onLoad(); + assert.propertyVal(img.state(), "isLoaded", true); + }); + + describe("DSImage with Idle Callback", () => { + let wrapper; + let windowStub = { + requestIdleCallback: sinon.stub().returns(1), + cancelIdleCallback: sinon.stub(), + }; + beforeEach(() => { + wrapper = mount(<DSImage windowObj={windowStub} />); + }); + + it("should call requestIdleCallback on componentDidMount", () => { + assert.calledOnce(windowStub.requestIdleCallback); + }); + + it("should call cancelIdleCallback on componentWillUnmount", () => { + wrapper.instance().componentWillUnmount(); + assert.calledOnce(windowStub.cancelIdleCallback); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSLinkMenu.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSLinkMenu.test.jsx new file mode 100644 index 0000000000..3aa128a32a --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSLinkMenu.test.jsx @@ -0,0 +1,151 @@ +import { mount, shallow } from "enzyme"; +import { DSLinkMenu } from "content-src/components/DiscoveryStreamComponents/DSLinkMenu/DSLinkMenu"; +import { ContextMenuButton } from "content-src/components/ContextMenu/ContextMenuButton"; +import { LinkMenu } from "content-src/components/LinkMenu/LinkMenu"; +import React from "react"; + +describe("<DSLinkMenu>", () => { + let wrapper; + + describe("DS link menu actions", () => { + beforeEach(() => { + wrapper = mount(<DSLinkMenu />); + }); + + afterEach(() => { + wrapper.unmount(); + }); + + it("should parse args for fluent correctly ", () => { + const title = '"fluent"'; + wrapper = mount(<DSLinkMenu title={title} />); + + const button = wrapper.find( + "button[data-l10n-id='newtab-menu-content-tooltip']" + ); + assert.equal(button.prop("data-l10n-args"), JSON.stringify({ title })); + }); + }); + + describe("DS context menu options", () => { + const ValidDSLinkMenuProps = { + site: {}, + pocket_button_enabled: true, + }; + + beforeEach(() => { + wrapper = shallow(<DSLinkMenu {...ValidDSLinkMenuProps} />); + }); + + it("should render a context menu button", () => { + assert.ok(wrapper.exists()); + assert.ok( + wrapper.find(ContextMenuButton).exists(), + "context menu button exists" + ); + }); + + it("should render LinkMenu when context menu button is clicked", () => { + let button = wrapper.find(ContextMenuButton); + button.simulate("click", { preventDefault: () => {} }); + assert.equal(wrapper.find(LinkMenu).length, 1); + }); + + it("should pass dispatch, onShow, site, options, shouldSendImpressionStats, source and index to LinkMenu", () => { + wrapper + .find(ContextMenuButton) + .simulate("click", { preventDefault: () => {} }); + const linkMenuProps = wrapper.find(LinkMenu).props(); + [ + "dispatch", + "onShow", + "site", + "index", + "options", + "source", + "shouldSendImpressionStats", + ].forEach(prop => assert.property(linkMenuProps, prop)); + }); + + it("should pass through the correct menu options to LinkMenu", () => { + wrapper + .find(ContextMenuButton) + .simulate("click", { preventDefault: () => {} }); + const linkMenuProps = wrapper.find(LinkMenu).props(); + assert.deepEqual(linkMenuProps.options, [ + "CheckBookmark", + "CheckArchiveFromPocket", + "CheckSavedToPocket", + "Separator", + "OpenInNewWindow", + "OpenInPrivateWindow", + "Separator", + "BlockUrl", + ]); + }); + + it("should pass through the correct menu options to LinkMenu for spocs", () => { + wrapper = shallow( + <DSLinkMenu + {...ValidDSLinkMenuProps} + flightId="1234" + showPrivacyInfo={true} + /> + ); + wrapper + .find(ContextMenuButton) + .simulate("click", { preventDefault: () => {} }); + const linkMenuProps = wrapper.find(LinkMenu).props(); + assert.deepEqual(linkMenuProps.options, [ + "CheckBookmark", + "CheckArchiveFromPocket", + "CheckSavedToPocket", + "Separator", + "OpenInNewWindow", + "OpenInPrivateWindow", + "Separator", + "BlockUrl", + "ShowPrivacyInfo", + ]); + }); + + it("should pass through the correct menu options to LinkMenu for save to Pocket button", () => { + wrapper = shallow( + <DSLinkMenu {...ValidDSLinkMenuProps} saveToPocketCard={true} /> + ); + wrapper + .find(ContextMenuButton) + .simulate("click", { preventDefault: () => {} }); + const linkMenuProps = wrapper.find(LinkMenu).props(); + assert.deepEqual(linkMenuProps.options, [ + "CheckBookmark", + "CheckArchiveFromPocket", + "CheckDeleteFromPocket", + "Separator", + "OpenInNewWindow", + "OpenInPrivateWindow", + "Separator", + "BlockUrl", + ]); + }); + + it("should pass through the correct menu options to LinkMenu if Pocket is disabled", () => { + wrapper = shallow( + <DSLinkMenu {...ValidDSLinkMenuProps} pocket_button_enabled={false} /> + ); + wrapper + .find(ContextMenuButton) + .simulate("click", { preventDefault: () => {} }); + const linkMenuProps = wrapper.find(LinkMenu).props(); + assert.deepEqual(linkMenuProps.options, [ + "CheckBookmark", + "CheckArchiveFromPocket", + "Separator", + "OpenInNewWindow", + "OpenInPrivateWindow", + "Separator", + "BlockUrl", + ]); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSMessage.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSMessage.test.jsx new file mode 100644 index 0000000000..7d9f13cc8a --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSMessage.test.jsx @@ -0,0 +1,57 @@ +import { DSMessage } from "content-src/components/DiscoveryStreamComponents/DSMessage/DSMessage"; +import React from "react"; +import { SafeAnchor } from "content-src/components/DiscoveryStreamComponents/SafeAnchor/SafeAnchor"; +import { FluentOrText } from "content-src/components/FluentOrText/FluentOrText"; +import { mount } from "enzyme"; + +describe("<DSMessage>", () => { + let wrapper; + + beforeEach(() => { + wrapper = mount(<DSMessage />); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(".ds-message").exists()); + }); + + it("should render an icon", () => { + wrapper.setProps({ icon: "foo" }); + + assert.ok(wrapper.find(".glyph").exists()); + assert.propertyVal( + wrapper.find(".glyph").props().style, + "backgroundImage", + `url(foo)` + ); + }); + + it("should render a title", () => { + wrapper.setProps({ title: "foo" }); + + assert.ok(wrapper.find(".title-text").exists()); + assert.equal(wrapper.find(".title-text").text(), "foo"); + }); + + it("should render a SafeAnchor", () => { + wrapper.setProps({ link_text: "foo", link_url: "https://foo.com" }); + + assert.equal(wrapper.find(".title").children().at(0).type(), SafeAnchor); + }); + + it("should render a FluentOrText", () => { + wrapper.setProps({ + link_text: "link_text", + title: "title", + link_url: "https://link_url.com", + }); + + assert.equal( + wrapper.find(".title-text").children().at(0).type(), + FluentOrText + ); + + assert.equal(wrapper.find(".link a").children().at(0).type(), FluentOrText); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSPrivacyModal.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSPrivacyModal.test.jsx new file mode 100644 index 0000000000..b4b743c7ff --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSPrivacyModal.test.jsx @@ -0,0 +1,50 @@ +import { DSPrivacyModal } from "content-src/components/DiscoveryStreamComponents/DSPrivacyModal/DSPrivacyModal"; +import { shallow, mount } from "enzyme"; +import { actionCreators as ac } from "common/Actions.sys.mjs"; +import React from "react"; + +describe("Discovery Stream <DSPrivacyModal>", () => { + let sandbox; + let dispatch; + let wrapper; + beforeEach(() => { + sandbox = sinon.createSandbox(); + dispatch = sandbox.stub(); + wrapper = shallow(<DSPrivacyModal dispatch={dispatch} />); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should contain a privacy notice", () => { + const modal = mount(<DSPrivacyModal />); + const child = modal.find(".privacy-notice"); + + assert.lengthOf(child, 1); + }); + + it("should call dispatch when modal is closed", () => { + wrapper.instance().closeModal(); + assert.calledOnce(dispatch); + }); + + it("should call dispatch with the correct events for onLearnLinkClick", () => { + wrapper.instance().onLearnLinkClick(); + + assert.calledOnce(dispatch); + assert.calledWith( + dispatch, + ac.DiscoveryStreamUserEvent({ + event: "CLICK_PRIVACY_INFO", + source: "DS_PRIVACY_MODAL", + }) + ); + }); + + it("should call dispatch with the correct events for onManageLinkClick", () => { + wrapper.instance().onManageLinkClick(); + + assert.calledOnce(dispatch); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSSignup.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSSignup.test.jsx new file mode 100644 index 0000000000..904f98e439 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSSignup.test.jsx @@ -0,0 +1,92 @@ +import { DSSignup } from "content-src/components/DiscoveryStreamComponents/DSSignup/DSSignup"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<DSSignup>", () => { + let wrapper; + let sandbox; + let dispatchStub; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + dispatchStub = sandbox.stub(); + wrapper = shallow( + <DSSignup + data={{ + spocs: [ + { + shim: { impression: "1234" }, + id: "1234", + }, + ], + }} + type="SIGNUP" + dispatch={dispatchStub} + /> + ); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(".ds-signup").exists()); + }); + + it("should dispatch a click event on click", () => { + wrapper.instance().onLinkClick(); + + assert.calledTwice(dispatchStub); + assert.deepEqual(dispatchStub.firstCall.args[0].data, { + event: "CLICK", + source: "SIGNUP", + action_position: 0, + }); + assert.deepEqual(dispatchStub.secondCall.args[0].data, { + source: "SIGNUP", + click: 0, + tiles: [{ id: "1234", pos: 0 }], + }); + }); + + it("Should remove active on Menu Update", () => { + wrapper.setState = sandbox.stub(); + wrapper.instance().onMenuButtonUpdate(false); + assert.calledWith(wrapper.setState, { active: false, lastItem: false }); + }); + + it("Should add active on Menu Show", async () => { + wrapper.setState = sandbox.stub(); + wrapper.instance().nextAnimationFrame = () => {}; + await wrapper.instance().onMenuShow(); + assert.calledWith(wrapper.setState, { active: true, lastItem: false }); + }); + + it("Should add last-item to support resized window", async () => { + const fakeWindow = { scrollMaxX: "20" }; + wrapper = shallow(<DSSignup windowObj={fakeWindow} />); + wrapper.setState = sandbox.stub(); + wrapper.instance().nextAnimationFrame = () => {}; + await wrapper.instance().onMenuShow(); + assert.calledWith(wrapper.setState, { active: true, lastItem: true }); + }); + + it("Should add last-item and active classes", () => { + wrapper.setState({ + active: true, + lastItem: true, + }); + assert.ok(wrapper.find(".last-item").exists()); + assert.ok(wrapper.find(".active").exists()); + }); + + it("Should call rAF from nextAnimationFrame", () => { + const fakeWindow = { requestAnimationFrame: sinon.stub() }; + wrapper = shallow(<DSSignup windowObj={fakeWindow} />); + + wrapper.instance().nextAnimationFrame(); + assert.calledOnce(fakeWindow.requestAnimationFrame); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSTextPromo.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSTextPromo.test.jsx new file mode 100644 index 0000000000..0748ff701a --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSTextPromo.test.jsx @@ -0,0 +1,96 @@ +import { DSTextPromo } from "content-src/components/DiscoveryStreamComponents/DSTextPromo/DSTextPromo"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<DSTextPromo>", () => { + let wrapper; + let sandbox; + let dispatchStub; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + dispatchStub = sandbox.stub(); + wrapper = shallow( + <DSTextPromo + data={{ + spocs: [ + { + shim: { impression: "1234" }, + id: "1234", + }, + ], + }} + type="TEXTPROMO" + dispatch={dispatchStub} + /> + ); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(".ds-text-promo").exists()); + }); + + it("should render a header", () => { + wrapper.setProps({ header: "foo" }); + assert.ok(wrapper.find(".text").exists()); + }); + + it("should render a subtitle", () => { + wrapper.setProps({ subtitle: "foo" }); + assert.ok(wrapper.find(".subtitle").exists()); + }); + + it("should dispatch a click event on click", () => { + wrapper.instance().onLinkClick(); + + assert.calledTwice(dispatchStub); + assert.deepEqual(dispatchStub.firstCall.args[0].data, { + event: "CLICK", + source: "TEXTPROMO", + action_position: 0, + }); + assert.deepEqual(dispatchStub.secondCall.args[0].data, { + source: "TEXTPROMO", + click: 0, + tiles: [{ id: "1234", pos: 0 }], + }); + }); + + it("should dispath telemety events on dismiss", () => { + wrapper.instance().onDismissClick(); + + const firstCall = dispatchStub.getCall(0); + const secondCall = dispatchStub.getCall(1); + const thirdCall = dispatchStub.getCall(2); + + assert.equal(firstCall.args[0].type, "BLOCK_URL"); + assert.deepEqual(firstCall.args[0].data, [ + { + url: undefined, + pocket_id: undefined, + isSponsoredTopSite: undefined, + position: 0, + is_pocket_card: false, + }, + ]); + + assert.equal(secondCall.args[0].type, "DISCOVERY_STREAM_USER_EVENT"); + assert.deepEqual(secondCall.args[0].data, { + event: "BLOCK", + source: "TEXTPROMO", + action_position: 0, + }); + + assert.equal(thirdCall.args[0].type, "TELEMETRY_IMPRESSION_STATS"); + assert.deepEqual(thirdCall.args[0].data, { + source: "TEXTPROMO", + block: 0, + tiles: [{ id: "1234", pos: 0 }], + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/Highlights.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/Highlights.test.jsx new file mode 100644 index 0000000000..d8c16d8e71 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/Highlights.test.jsx @@ -0,0 +1,41 @@ +import { combineReducers, createStore } from "redux"; +import { INITIAL_STATE, reducers } from "common/Reducers.sys.mjs"; +import { Highlights } from "content-src/components/DiscoveryStreamComponents/Highlights/Highlights"; +import { mount } from "enzyme"; +import { Provider } from "react-redux"; +import React from "react"; + +describe("Discovery Stream <Highlights>", () => { + let wrapper; + + afterEach(() => { + wrapper.unmount(); + }); + + it("should render nothing with no highlights data", () => { + const store = createStore(combineReducers(reducers), { ...INITIAL_STATE }); + + wrapper = mount( + <Provider store={store}> + <Highlights /> + </Provider> + ); + + assert.ok(wrapper.isEmptyRender()); + }); + + it("should render highlights", () => { + const store = createStore(combineReducers(reducers), { + ...INITIAL_STATE, + Sections: [{ id: "highlights", enabled: true }], + }); + + wrapper = mount( + <Provider store={store}> + <Highlights /> + </Provider> + ); + + assert.lengthOf(wrapper.find(".ds-highlights"), 1); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/HorizontalRule.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/HorizontalRule.test.jsx new file mode 100644 index 0000000000..03538df6f2 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/HorizontalRule.test.jsx @@ -0,0 +1,16 @@ +import { HorizontalRule } from "content-src/components/DiscoveryStreamComponents/HorizontalRule/HorizontalRule"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<HorizontalRule>", () => { + let wrapper; + + beforeEach(() => { + wrapper = shallow(<HorizontalRule />); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(".ds-hr").exists()); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/ImpressionStats.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/ImpressionStats.test.jsx new file mode 100644 index 0000000000..4926cc6c70 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/ImpressionStats.test.jsx @@ -0,0 +1,276 @@ +import { + ImpressionStats, + INTERSECTION_RATIO, +} from "content-src/components/DiscoveryStreamImpressionStats/ImpressionStats"; +import { actionTypes as at } from "common/Actions.sys.mjs"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<ImpressionStats>", () => { + const SOURCE = "TEST_SOURCE"; + const FullIntersectEntries = [ + { isIntersecting: true, intersectionRatio: INTERSECTION_RATIO }, + ]; + const ZeroIntersectEntries = [ + { isIntersecting: false, intersectionRatio: 0 }, + ]; + const PartialIntersectEntries = [ + { isIntersecting: true, intersectionRatio: INTERSECTION_RATIO / 2 }, + ]; + + // Build IntersectionObserver class with the arg `entries` for the intersect callback. + function buildIntersectionObserver(entries) { + return class { + constructor(callback) { + this.callback = callback; + } + + observe() { + this.callback(entries); + } + + unobserve() {} + }; + } + + const DEFAULT_PROPS = { + rows: [ + { id: 1, pos: 0 }, + { id: 2, pos: 1 }, + { id: 3, pos: 2 }, + ], + source: SOURCE, + IntersectionObserver: buildIntersectionObserver(FullIntersectEntries), + document: { + visibilityState: "visible", + addEventListener: sinon.stub(), + removeEventListener: sinon.stub(), + }, + }; + + const InnerEl = () => <div>Inner Element</div>; + + function renderImpressionStats(props = {}) { + return shallow( + <ImpressionStats {...DEFAULT_PROPS} {...props}> + <InnerEl /> + </ImpressionStats> + ); + } + + it("should render props.children", () => { + const wrapper = renderImpressionStats(); + assert.ok(wrapper.contains(<InnerEl />)); + }); + it("should not send loaded content nor impression when the page is not visible", () => { + const dispatch = sinon.spy(); + const props = { + dispatch, + document: { + visibilityState: "hidden", + addEventListener: sinon.spy(), + removeEventListener: sinon.spy(), + }, + }; + renderImpressionStats(props); + + assert.notCalled(dispatch); + }); + it("should noly send loaded content but not impression when the wrapped item is not visbible", () => { + const dispatch = sinon.spy(); + const props = { + dispatch, + IntersectionObserver: buildIntersectionObserver(ZeroIntersectEntries), + }; + renderImpressionStats(props); + + // This one is for loaded content. + assert.calledOnce(dispatch); + const [action] = dispatch.firstCall.args; + assert.equal(action.type, at.DISCOVERY_STREAM_LOADED_CONTENT); + assert.equal(action.data.source, SOURCE); + assert.deepEqual(action.data.tiles, [ + { id: 1, pos: 0 }, + { id: 2, pos: 1 }, + { id: 3, pos: 2 }, + ]); + }); + it("should not send impression when the wrapped item is visbible but below the ratio", () => { + const dispatch = sinon.spy(); + const props = { + dispatch, + IntersectionObserver: buildIntersectionObserver(PartialIntersectEntries), + }; + renderImpressionStats(props); + + // This one is for loaded content. + assert.calledOnce(dispatch); + }); + it("should send a loaded content and an impression when the page is visible and the wrapped item meets the visibility ratio", () => { + const dispatch = sinon.spy(); + const props = { + dispatch, + IntersectionObserver: buildIntersectionObserver(FullIntersectEntries), + }; + renderImpressionStats(props); + + assert.calledTwice(dispatch); + + let [action] = dispatch.firstCall.args; + assert.equal(action.type, at.DISCOVERY_STREAM_LOADED_CONTENT); + assert.equal(action.data.source, SOURCE); + assert.deepEqual(action.data.tiles, [ + { id: 1, pos: 0 }, + { id: 2, pos: 1 }, + { id: 3, pos: 2 }, + ]); + + [action] = dispatch.secondCall.args; + assert.equal(action.type, at.DISCOVERY_STREAM_IMPRESSION_STATS); + assert.equal(action.data.source, SOURCE); + assert.deepEqual(action.data.tiles, [ + { id: 1, pos: 0, type: "organic", recommendation_id: undefined }, + { id: 2, pos: 1, type: "organic", recommendation_id: undefined }, + { id: 3, pos: 2, type: "organic", recommendation_id: undefined }, + ]); + }); + it("should send a DISCOVERY_STREAM_SPOC_IMPRESSION when the wrapped item has a flightId", () => { + const dispatch = sinon.spy(); + const flightId = "a_flight_id"; + const props = { + dispatch, + flightId, + rows: [{ id: 1, pos: 1, advertiser: "test advertiser" }], + source: "TOP_SITES", + IntersectionObserver: buildIntersectionObserver(FullIntersectEntries), + }; + renderImpressionStats(props); + + // Loaded content + DISCOVERY_STREAM_SPOC_IMPRESSION + TOP_SITES_SPONSORED_IMPRESSION_STATS + impression + assert.callCount(dispatch, 4); + + const [action] = dispatch.secondCall.args; + assert.equal(action.type, at.DISCOVERY_STREAM_SPOC_IMPRESSION); + assert.deepEqual(action.data, { flightId }); + }); + it("should send a TOP_SITES_SPONSORED_IMPRESSION_STATS when the wrapped item has a flightId", () => { + const dispatch = sinon.spy(); + const flightId = "a_flight_id"; + const props = { + dispatch, + flightId, + rows: [{ id: 1, pos: 1, advertiser: "test advertiser" }], + source: "TOP_SITES", + IntersectionObserver: buildIntersectionObserver(FullIntersectEntries), + }; + renderImpressionStats(props); + + // Loaded content + DISCOVERY_STREAM_SPOC_IMPRESSION + TOP_SITES_SPONSORED_IMPRESSION_STATS + impression + assert.callCount(dispatch, 4); + + const [action] = dispatch.getCall(2).args; + assert.equal(action.type, at.TOP_SITES_SPONSORED_IMPRESSION_STATS); + assert.deepEqual(action.data, { + type: "impression", + tile_id: 1, + source: "newtab", + advertiser: "test advertiser", + position: 1, + }); + }); + it("should send an impression when the wrapped item transiting from invisible to visible", () => { + const dispatch = sinon.spy(); + const props = { + dispatch, + IntersectionObserver: buildIntersectionObserver(ZeroIntersectEntries), + }; + const wrapper = renderImpressionStats(props); + + // For the loaded content + assert.calledOnce(dispatch); + + let [action] = dispatch.firstCall.args; + assert.equal(action.type, at.DISCOVERY_STREAM_LOADED_CONTENT); + assert.equal(action.data.source, SOURCE); + assert.deepEqual(action.data.tiles, [ + { id: 1, pos: 0 }, + { id: 2, pos: 1 }, + { id: 3, pos: 2 }, + ]); + + dispatch.resetHistory(); + wrapper.instance().impressionObserver.callback(FullIntersectEntries); + + // For the impression + assert.calledOnce(dispatch); + + [action] = dispatch.firstCall.args; + assert.equal(action.type, at.DISCOVERY_STREAM_IMPRESSION_STATS); + assert.deepEqual(action.data.tiles, [ + { id: 1, pos: 0, type: "organic", recommendation_id: undefined }, + { id: 2, pos: 1, type: "organic", recommendation_id: undefined }, + { id: 3, pos: 2, type: "organic", recommendation_id: undefined }, + ]); + }); + it("should remove visibility change listener when the wrapper is removed", () => { + const props = { + dispatch: sinon.spy(), + document: { + visibilityState: "hidden", + addEventListener: sinon.spy(), + removeEventListener: sinon.spy(), + }, + IntersectionObserver, + }; + + const wrapper = renderImpressionStats(props); + assert.calledWith(props.document.addEventListener, "visibilitychange"); + const [, listener] = props.document.addEventListener.firstCall.args; + + wrapper.unmount(); + assert.calledWith( + props.document.removeEventListener, + "visibilitychange", + listener + ); + }); + it("should unobserve the intersection observer when the wrapper is removed", () => { + const IntersectionObserver = + buildIntersectionObserver(ZeroIntersectEntries); + const spy = sinon.spy(IntersectionObserver.prototype, "unobserve"); + const props = { dispatch: sinon.spy(), IntersectionObserver }; + + const wrapper = renderImpressionStats(props); + wrapper.unmount(); + + assert.calledOnce(spy); + }); + it("should only send the latest impression on a visibility change", () => { + const listeners = new Set(); + const props = { + dispatch: sinon.spy(), + document: { + visibilityState: "hidden", + addEventListener: (ev, cb) => listeners.add(cb), + removeEventListener: (ev, cb) => listeners.delete(cb), + }, + }; + + const wrapper = renderImpressionStats(props); + + // Update twice + wrapper.setProps({ ...props, ...{ rows: [{ id: 123, pos: 4 }] } }); + wrapper.setProps({ ...props, ...{ rows: [{ id: 2432, pos: 5 }] } }); + + assert.notCalled(props.dispatch); + + // Simulate listeners getting called + props.document.visibilityState = "visible"; + listeners.forEach(l => l()); + + // Make sure we only sent the latest event + assert.calledTwice(props.dispatch); + const [action] = props.dispatch.firstCall.args; + assert.deepEqual(action.data.tiles, [{ id: 2432, pos: 5 }]); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/Navigation.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/Navigation.test.jsx new file mode 100644 index 0000000000..ef5baf50c1 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/Navigation.test.jsx @@ -0,0 +1,131 @@ +import { + Navigation, + Topic, +} from "content-src/components/DiscoveryStreamComponents/Navigation/Navigation"; +import React from "react"; +import { SafeAnchor } from "content-src/components/DiscoveryStreamComponents/SafeAnchor/SafeAnchor"; +import { FluentOrText } from "content-src/components/FluentOrText/FluentOrText"; +import { shallow, mount } from "enzyme"; + +const DEFAULT_PROPS = { + App: { + isForStartupCache: false, + }, +}; + +describe("<Navigation>", () => { + let wrapper; + + beforeEach(() => { + wrapper = mount(<Navigation header={{}} locale="en-US" />); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + }); + + it("should render a title", () => { + wrapper.setProps({ header: { title: "Foo" } }); + + assert.equal(wrapper.find(".ds-navigation-header").text(), "Foo"); + }); + + it("should not render a title", () => { + wrapper.setProps({ header: null }); + + assert.lengthOf(wrapper.find(".ds-navigation-header"), 0); + }); + + it("should set default alignment", () => { + assert.lengthOf(wrapper.find(".ds-navigation-centered"), 1); + }); + + it("should set custom alignment", () => { + wrapper.setProps({ alignment: "left-align" }); + + assert.lengthOf(wrapper.find(".ds-navigation-left-align"), 1); + }); + + it("should set default of no links", () => { + assert.lengthOf(wrapper.find("ul").children(), 0); + }); + + it("should render a FluentOrText", () => { + wrapper.setProps({ header: { title: "Foo" } }); + + assert.equal( + wrapper.find(".ds-navigation").children().at(0).type(), + FluentOrText + ); + }); + + it("should render 2 Topics", () => { + wrapper.setProps({ + links: [ + { url: "https://foo.com", name: "foo" }, + { url: "https://bar.com", name: "bar" }, + ], + }); + + assert.lengthOf(wrapper.find("ul").children(), 2); + }); + + it("should render 2 extra Topics", () => { + wrapper.setProps({ + newFooterSection: true, + links: [ + { url: "https://foo.com", name: "foo" }, + { url: "https://bar.com", name: "bar" }, + ], + extraLinks: [ + { url: "https://foo.com", name: "foo" }, + { url: "https://bar.com", name: "bar" }, + ], + }); + + assert.lengthOf(wrapper.find("ul").children(), 4); + }); +}); + +describe("<Topic>", () => { + let wrapper; + let sandbox; + + beforeEach(() => { + wrapper = shallow(<Topic url="https://foo.com" name="foo" />); + sandbox = sinon.createSandbox(); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should pass onLinkClick prop", () => { + assert.propertyVal( + wrapper.at(0).props(), + "onLinkClick", + wrapper.instance().onLinkClick + ); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.equal(wrapper.type(), SafeAnchor); + }); + + describe("onLinkClick", () => { + let dispatch; + + beforeEach(() => { + dispatch = sandbox.stub(); + wrapper = shallow(<Topic dispatch={dispatch} {...DEFAULT_PROPS} />); + wrapper.setState({ isSeen: true }); + }); + + it("should call dispatch", () => { + wrapper.instance().onLinkClick({ target: { text: `Must Reads` } }); + + assert.calledOnce(dispatch); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/PrivacyLink.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/PrivacyLink.test.jsx new file mode 100644 index 0000000000..285cc16c0e --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/PrivacyLink.test.jsx @@ -0,0 +1,29 @@ +import { PrivacyLink } from "content-src/components/DiscoveryStreamComponents/PrivacyLink/PrivacyLink"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<PrivacyLink>", () => { + let wrapper; + let sandbox; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + wrapper = shallow( + <PrivacyLink + properties={{ + url: "url", + title: "Privacy Link", + }} + /> + ); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(".ds-privacy-link").exists()); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/SafeAnchor.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/SafeAnchor.test.jsx new file mode 100644 index 0000000000..5d643869b8 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/SafeAnchor.test.jsx @@ -0,0 +1,56 @@ +import React from "react"; +import { SafeAnchor } from "content-src/components/DiscoveryStreamComponents/SafeAnchor/SafeAnchor"; +import { shallow } from "enzyme"; + +describe("Discovery Stream <SafeAnchor>", () => { + let warnStub; + let sandbox; + beforeEach(() => { + warnStub = sinon.stub(console, "warn"); + sandbox = sinon.createSandbox(); + }); + afterEach(() => { + warnStub.restore(); + sandbox.restore(); + }); + it("should render with anchor", () => { + const wrapper = shallow(<SafeAnchor />); + assert.lengthOf(wrapper.find("a"), 1); + }); + it("should render with anchor target for http", () => { + const wrapper = shallow(<SafeAnchor url="http://example.com" />); + assert.equal(wrapper.find("a").prop("href"), "http://example.com"); + }); + it("should render with anchor target for https", () => { + const wrapper = shallow(<SafeAnchor url="https://example.com" />); + assert.equal(wrapper.find("a").prop("href"), "https://example.com"); + }); + it("should not allow javascript: URIs", () => { + const wrapper = shallow(<SafeAnchor url="javascript:foo()" />); // eslint-disable-line no-script-url + assert.equal(wrapper.find("a").prop("href"), ""); + assert.calledOnce(warnStub); + }); + it("should not warn if the URL is falsey ", () => { + const wrapper = shallow(<SafeAnchor url="" />); + assert.equal(wrapper.find("a").prop("href"), ""); + assert.notCalled(warnStub); + }); + it("should dispatch an event on click", () => { + const dispatchStub = sandbox.stub(); + const fakeEvent = { preventDefault: sandbox.stub(), currentTarget: {} }; + const wrapper = shallow(<SafeAnchor dispatch={dispatchStub} />); + + wrapper.find("a").simulate("click", fakeEvent); + + assert.calledOnce(dispatchStub); + assert.calledOnce(fakeEvent.preventDefault); + }); + it("should call onLinkClick if provided", () => { + const onLinkClickStub = sandbox.stub(); + const wrapper = shallow(<SafeAnchor onLinkClick={onLinkClickStub} />); + + wrapper.find("a").simulate("click"); + + assert.calledOnce(onLinkClickStub); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/SectionTitle.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/SectionTitle.test.jsx new file mode 100644 index 0000000000..b5ea007022 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/SectionTitle.test.jsx @@ -0,0 +1,22 @@ +import React from "react"; +import { SectionTitle } from "content-src/components/DiscoveryStreamComponents/SectionTitle/SectionTitle"; +import { shallow } from "enzyme"; + +describe("<SectionTitle>", () => { + let wrapper; + + beforeEach(() => { + wrapper = shallow(<SectionTitle header={{}} />); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(".ds-section-title").exists()); + }); + + it("should render a subtitle", () => { + wrapper.setProps({ header: { title: "Foo", subtitle: "Bar" } }); + + assert.equal(wrapper.find(".subtitle").text(), "Bar"); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/TopicsWidget.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/TopicsWidget.test.jsx new file mode 100644 index 0000000000..f879600a8f --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/TopicsWidget.test.jsx @@ -0,0 +1,238 @@ +import { combineReducers, createStore } from "redux"; +import { INITIAL_STATE, reducers } from "common/Reducers.sys.mjs"; +import { Provider } from "react-redux"; +import { + _TopicsWidget as TopicsWidgetBase, + TopicsWidget, +} from "content-src/components/DiscoveryStreamComponents/TopicsWidget/TopicsWidget"; +import { SafeAnchor } from "content-src/components/DiscoveryStreamComponents/SafeAnchor/SafeAnchor"; +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { mount } from "enzyme"; +import React from "react"; + +describe("Discovery Stream <TopicsWidget>", () => { + let sandbox; + let wrapper; + let dispatch; + let fakeWindow; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + dispatch = sandbox.stub(); + fakeWindow = { + innerWidth: 1000, + innerHeight: 900, + }; + + wrapper = mount( + <TopicsWidgetBase + dispatch={dispatch} + source="CARDGRID_WIDGET" + position={2} + id={1} + windowObj={fakeWindow} + DiscoveryStream={{ + experimentData: { + utmCampaign: "utmCampaign", + utmContent: "utmContent", + utmSource: "utmSource", + }, + }} + /> + ); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(".ds-topics-widget").exists()); + }); + + it("should connect with DiscoveryStream store", () => { + let store = createStore(combineReducers(reducers), INITIAL_STATE); + wrapper = mount( + <Provider store={store}> + <TopicsWidget /> + </Provider> + ); + + const topicsWidget = wrapper.find(TopicsWidgetBase); + assert.ok(topicsWidget.exists()); + assert.lengthOf(topicsWidget, 1); + assert.deepEqual( + topicsWidget.props().DiscoveryStream.experimentData, + INITIAL_STATE.DiscoveryStream.experimentData + ); + }); + + describe("dispatch", () => { + it("should dispatch loaded event", () => { + assert.callCount(dispatch, 1); + const [first] = dispatch.getCalls(); + assert.calledWith( + first, + ac.DiscoveryStreamLoadedContent({ + source: "CARDGRID_WIDGET", + tiles: [ + { + id: 1, + pos: 2, + }, + ], + }) + ); + }); + + it("should dispatch click event for technology", () => { + // Click technology topic. + wrapper.find(SafeAnchor).at(0).simulate("click"); + + // First call is DiscoveryStreamLoadedContent, which is already tested. + const [second, third, fourth] = dispatch.getCalls().slice(1, 4); + + assert.callCount(dispatch, 4); + assert.calledWith( + second, + ac.OnlyToMain({ + type: at.OPEN_LINK, + data: { + event: { + altKey: undefined, + button: undefined, + ctrlKey: undefined, + metaKey: undefined, + shiftKey: undefined, + }, + referrer: "https://getpocket.com/recommendations", + url: "https://getpocket.com/explore/technology?utm_source=utmSource&utm_content=utmContent&utm_campaign=utmCampaign", + }, + }) + ); + assert.calledWith( + third, + ac.DiscoveryStreamUserEvent({ + event: "CLICK", + source: "CARDGRID_WIDGET", + action_position: 2, + value: { + card_type: "topics_widget", + topic: "technology", + position_in_card: 0, + }, + }) + ); + assert.calledWith( + fourth, + ac.ImpressionStats({ + click: 0, + source: "CARDGRID_WIDGET", + tiles: [{ id: 1, pos: 2 }], + window_inner_width: 1000, + window_inner_height: 900, + }) + ); + }); + + it("should dispatch click event for must reads", () => { + // Click must reads topic. + wrapper.find(SafeAnchor).at(8).simulate("click"); + + // First call is DiscoveryStreamLoadedContent, which is already tested. + const [second, third, fourth] = dispatch.getCalls().slice(1, 4); + + assert.callCount(dispatch, 4); + assert.calledWith( + second, + ac.OnlyToMain({ + type: at.OPEN_LINK, + data: { + event: { + altKey: undefined, + button: undefined, + ctrlKey: undefined, + metaKey: undefined, + shiftKey: undefined, + }, + referrer: "https://getpocket.com/recommendations", + url: "https://getpocket.com/collections?utm_source=utmSource&utm_content=utmContent&utm_campaign=utmCampaign", + }, + }) + ); + assert.calledWith( + third, + ac.DiscoveryStreamUserEvent({ + event: "CLICK", + source: "CARDGRID_WIDGET", + action_position: 2, + value: { + card_type: "topics_widget", + topic: "must-reads", + position_in_card: 8, + }, + }) + ); + assert.calledWith( + fourth, + ac.ImpressionStats({ + click: 0, + source: "CARDGRID_WIDGET", + tiles: [{ id: 1, pos: 2 }], + window_inner_width: 1000, + window_inner_height: 900, + }) + ); + }); + + it("should dispatch click event for more topics", () => { + // Click more-topics. + wrapper.find(SafeAnchor).at(9).simulate("click"); + + // First call is DiscoveryStreamLoadedContent, which is already tested. + const [second, third, fourth] = dispatch.getCalls().slice(1, 4); + + assert.callCount(dispatch, 4); + assert.calledWith( + second, + ac.OnlyToMain({ + type: at.OPEN_LINK, + data: { + event: { + altKey: undefined, + button: undefined, + ctrlKey: undefined, + metaKey: undefined, + shiftKey: undefined, + }, + referrer: "https://getpocket.com/recommendations", + url: "https://getpocket.com/?utm_source=utmSource&utm_content=utmContent&utm_campaign=utmCampaign", + }, + }) + ); + assert.calledWith( + third, + ac.DiscoveryStreamUserEvent({ + event: "CLICK", + source: "CARDGRID_WIDGET", + action_position: 2, + value: { card_type: "topics_widget", topic: "more-topics" }, + }) + ); + assert.calledWith( + fourth, + ac.ImpressionStats({ + click: 0, + source: "CARDGRID_WIDGET", + tiles: [{ id: 1, pos: 2 }], + window_inner_width: 1000, + window_inner_height: 900, + }) + ); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/ErrorBoundary.test.jsx b/browser/components/newtab/test/unit/content-src/components/ErrorBoundary.test.jsx new file mode 100644 index 0000000000..99cc8b0ca7 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/ErrorBoundary.test.jsx @@ -0,0 +1,110 @@ +import { A11yLinkButton } from "content-src/components/A11yLinkButton/A11yLinkButton"; +import { + ErrorBoundary, + ErrorBoundaryFallback, +} from "content-src/components/ErrorBoundary/ErrorBoundary"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<ErrorBoundary>", () => { + it("should render its children if componentDidCatch wasn't called", () => { + const wrapper = shallow( + <ErrorBoundary> + <div className="kids" /> + </ErrorBoundary> + ); + + assert.lengthOf(wrapper.find(".kids"), 1); + }); + + it("should render ErrorBoundaryFallback if componentDidCatch called", () => { + const wrapper = shallow(<ErrorBoundary />); + + wrapper.instance().componentDidCatch(); + // since shallow wrappers don't automatically manage lifecycle semantics: + wrapper.update(); + + assert.lengthOf(wrapper.find(ErrorBoundaryFallback), 1); + }); + + it("should render the given FallbackComponent if componentDidCatch called", () => { + class TestFallback extends React.PureComponent { + render() { + return <div className="my-fallback">doh!</div>; + } + } + + const wrapper = shallow(<ErrorBoundary FallbackComponent={TestFallback} />); + wrapper.instance().componentDidCatch(); + // since shallow wrappers don't automatically manage lifecycle semantics: + wrapper.update(); + + assert.lengthOf(wrapper.find(TestFallback), 1); + }); + + it("should pass the given className prop to the FallbackComponent", () => { + class TestFallback extends React.PureComponent { + render() { + return <div className={this.props.className}>doh!</div>; + } + } + + const wrapper = shallow( + <ErrorBoundary FallbackComponent={TestFallback} className="sheep" /> + ); + wrapper.instance().componentDidCatch(); + // since shallow wrappers don't automatically manage lifecycle semantics: + wrapper.update(); + + assert.lengthOf(wrapper.find(".sheep"), 1); + }); +}); + +describe("ErrorBoundaryFallback", () => { + it("should render a <div> with a class of as-error-fallback", () => { + const wrapper = shallow(<ErrorBoundaryFallback />); + + assert.lengthOf(wrapper.find("div.as-error-fallback"), 1); + }); + + it("should render a <div> with the props.className and .as-error-fallback", () => { + const wrapper = shallow(<ErrorBoundaryFallback className="monkeys" />); + + assert.lengthOf(wrapper.find("div.monkeys.as-error-fallback"), 1); + }); + + it("should call window.location.reload(true) if .reload-button clicked", () => { + const fakeWindow = { location: { reload: sinon.spy() } }; + const wrapper = shallow(<ErrorBoundaryFallback windowObj={fakeWindow} />); + + wrapper.find(".reload-button").simulate("click"); + + assert.calledOnce(fakeWindow.location.reload); + assert.calledWithExactly(fakeWindow.location.reload, true); + }); + + it("should render .reload-button as an <A11yLinkButton>", () => { + const wrapper = shallow(<ErrorBoundaryFallback />); + + assert.lengthOf(wrapper.find("A11yLinkButton.reload-button"), 1); + }); + + it("should render newtab-error-fallback-refresh-link node", () => { + const wrapper = shallow(<ErrorBoundaryFallback />); + + const msgWrapper = wrapper.find( + '[data-l10n-id="newtab-error-fallback-refresh-link"]' + ); + assert.lengthOf(msgWrapper, 1); + assert.isTrue(msgWrapper.is(A11yLinkButton)); + }); + + it("should render newtab-error-fallback-info node", () => { + const wrapper = shallow(<ErrorBoundaryFallback />); + + const msgWrapper = wrapper.find( + '[data-l10n-id="newtab-error-fallback-info"]' + ); + assert.lengthOf(msgWrapper, 1); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/FluentOrText.test.jsx b/browser/components/newtab/test/unit/content-src/components/FluentOrText.test.jsx new file mode 100644 index 0000000000..165f2a6dcf --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/FluentOrText.test.jsx @@ -0,0 +1,68 @@ +import { FluentOrText } from "content-src/components/FluentOrText/FluentOrText"; +import React from "react"; +import { shallow, mount } from "enzyme"; + +describe("<FluentOrText>", () => { + it("should create span with no children", () => { + const wrapper = shallow(<FluentOrText />); + + assert.ok(wrapper.find("span").exists()); + }); + it("should set plain text", () => { + const wrapper = shallow(<FluentOrText message={"hello"} />); + + assert.equal(wrapper.text(), "hello"); + }); + it("should use fluent id on automatic span", () => { + const wrapper = shallow(<FluentOrText message={{ id: "fluent" }} />); + + assert.ok(wrapper.find("span[data-l10n-id='fluent']").exists()); + }); + it("should also allow string_id", () => { + const wrapper = shallow(<FluentOrText message={{ string_id: "fluent" }} />); + + assert.ok(wrapper.find("span[data-l10n-id='fluent']").exists()); + }); + it("should use fluent id on child", () => { + const wrapper = shallow( + <FluentOrText message={{ id: "fluent" }}> + <p /> + </FluentOrText> + ); + + assert.ok(wrapper.find("p[data-l10n-id='fluent']").exists()); + }); + it("should set args for fluent", () => { + const wrapper = mount(<FluentOrText message={{ args: { num: 5 } }} />); + const { attributes } = wrapper.getDOMNode(); + const args = attributes.getNamedItem("data-l10n-args").value; + assert.equal(JSON.parse(args).num, 5); + }); + it("should also allow values", () => { + const wrapper = mount(<FluentOrText message={{ values: { num: 5 } }} />); + const { attributes } = wrapper.getDOMNode(); + const args = attributes.getNamedItem("data-l10n-args").value; + assert.equal(JSON.parse(args).num, 5); + }); + it("should preserve original children with fluent", () => { + const wrapper = shallow( + <FluentOrText message={{ id: "fluent" }}> + <p> + <b data-l10n-name="bold" /> + </p> + </FluentOrText> + ); + + assert.ok(wrapper.find("b[data-l10n-name='bold']").exists()); + }); + it("should only allow a single child", () => { + assert.throws(() => + shallow( + <FluentOrText> + <p /> + <p /> + </FluentOrText> + ) + ); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/LinkMenu.test.jsx b/browser/components/newtab/test/unit/content-src/components/LinkMenu.test.jsx new file mode 100644 index 0000000000..be7ac219d6 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/LinkMenu.test.jsx @@ -0,0 +1,667 @@ +import { ContextMenu } from "content-src/components/ContextMenu/ContextMenu"; +import { _LinkMenu as LinkMenu } from "content-src/components/LinkMenu/LinkMenu"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<LinkMenu>", () => { + let wrapper; + beforeEach(() => { + wrapper = shallow( + <LinkMenu + site={{ url: "" }} + options={["CheckPinTopSite", "CheckBookmark", "OpenInNewWindow"]} + dispatch={() => {}} + /> + ); + }); + it("should render a ContextMenu element", () => { + assert.ok(wrapper.find(ContextMenu).exists()); + }); + it("should pass onUpdate, and options to ContextMenu", () => { + assert.ok(wrapper.find(ContextMenu).exists()); + const contextMenuProps = wrapper.find(ContextMenu).props(); + ["onUpdate", "options"].forEach(prop => + assert.property(contextMenuProps, prop) + ); + }); + it("should give ContextMenu the correct tabbable options length for a11y", () => { + const { options } = wrapper.find(ContextMenu).props(); + const [firstItem] = options; + const lastItem = options[options.length - 1]; + + // first item should have {first: true} + assert.isTrue(firstItem.first); + assert.ok(!firstItem.last); + + // last item should have {last: true} + assert.isTrue(lastItem.last); + assert.ok(!lastItem.first); + + // middle items should have neither + for (let i = 1; i < options.length - 1; i++) { + assert.ok(!options[i].first && !options[i].last); + } + }); + it("should show the correct options for default sites", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", isDefault: true }} + options={["CheckBookmark"]} + source={"TOP_SITES"} + isPrivateBrowsingEnabled={true} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + let i = 0; + assert.propertyVal(options[i++], "id", "newtab-menu-pin"); + assert.propertyVal(options[i++], "id", "newtab-menu-edit-topsites"); + assert.propertyVal(options[i++], "type", "separator"); + assert.propertyVal(options[i++], "id", "newtab-menu-open-new-window"); + assert.propertyVal( + options[i++], + "id", + "newtab-menu-open-new-private-window" + ); + assert.propertyVal(options[i++], "type", "separator"); + assert.propertyVal(options[i++], "id", "newtab-menu-dismiss"); + assert.propertyVal(options, "length", i); + // Double check that delete options are not included for default top sites + options + .filter(o => o.type !== "separator") + .forEach(o => { + assert.notInclude(["newtab-menu-delete-history"], o.id); + }); + }); + it("should show Unpin option for a pinned site if CheckPinTopSite in options list", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", isPinned: true }} + source={"TOP_SITES"} + options={["CheckPinTopSite"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined(options.find(o => o.id && o.id === "newtab-menu-unpin")); + }); + it("should show Pin option for an unpinned site if CheckPinTopSite in options list", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", isPinned: false }} + source={"TOP_SITES"} + options={["CheckPinTopSite"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined(options.find(o => o.id && o.id === "newtab-menu-pin")); + }); + it("should show Unbookmark option for a bookmarked site if CheckBookmark in options list", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", bookmarkGuid: 1234 }} + source={"TOP_SITES"} + options={["CheckBookmark"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-remove-bookmark") + ); + }); + it("should show Bookmark option for an unbookmarked site if CheckBookmark in options list", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", bookmarkGuid: 0 }} + source={"TOP_SITES"} + options={["CheckBookmark"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-bookmark") + ); + }); + it("should show Save to Pocket option for an unsaved Pocket item if CheckSavedToPocket in options list", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", bookmarkGuid: 0 }} + source={"HIGHLIGHTS"} + options={["CheckSavedToPocket"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-save-to-pocket") + ); + }); + it("should show Delete from Pocket option for a saved Pocket item if CheckSavedToPocket in options list", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", pocket_id: 1234 }} + source={"HIGHLIGHTS"} + options={["CheckSavedToPocket"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-delete-pocket") + ); + }); + it("should show Archive from Pocket option for a saved Pocket item if CheckBookmarkOrArchive", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", pocket_id: 1234 }} + source={"HIGHLIGHTS"} + options={["CheckBookmarkOrArchive"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-archive-pocket") + ); + }); + it("should show Bookmark option for an unbookmarked site if CheckBookmarkOrArchive in options list and no pocket_id", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "" }} + source={"HIGHLIGHTS"} + options={["CheckBookmarkOrArchive"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-bookmark") + ); + }); + it("should show Unbookmark option for a bookmarked site if CheckBookmarkOrArchive in options list and no pocket_id", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", bookmarkGuid: 1234 }} + source={"HIGHLIGHTS"} + options={["CheckBookmarkOrArchive"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-remove-bookmark") + ); + }); + it("should show Archive from Pocket option for a saved Pocket item if CheckArchiveFromPocket", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", pocket_id: 1234 }} + source={"TOP_STORIES"} + options={["CheckArchiveFromPocket"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-archive-pocket") + ); + }); + it("should show empty from no Pocket option for no saved Pocket item if CheckArchiveFromPocket", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "" }} + source={"TOP_STORIES"} + options={["CheckArchiveFromPocket"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isUndefined( + options.find(o => o.id && o.id === "newtab-menu-archive-pocket") + ); + }); + it("should show Delete from Pocket option for a saved Pocket item if CheckDeleteFromPocket", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", pocket_id: 1234 }} + source={"TOP_STORIES"} + options={["CheckDeleteFromPocket"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-delete-pocket") + ); + }); + it("should show empty from Pocket option for no saved Pocket item if CheckDeleteFromPocket", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "" }} + source={"TOP_STORIES"} + options={["CheckDeleteFromPocket"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isUndefined( + options.find(o => o.id && o.id === "newtab-menu-archive-pocket") + ); + }); + it("should show Open File option for a downloaded item", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", type: "download", path: "foo" }} + source={"HIGHLIGHTS"} + options={["OpenFile"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-open-file") + ); + }); + it("should show Show File option for a downloaded item on a default platform", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", type: "download", path: "foo" }} + source={"HIGHLIGHTS"} + options={["ShowFile"]} + platform={"default"} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-show-file") + ); + }); + it("should show Copy Downlad Link option for a downloaded item when CopyDownloadLink", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", type: "download" }} + source={"HIGHLIGHTS"} + options={["CopyDownloadLink"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-copy-download-link") + ); + }); + it("should show Go To Download Page option for a downloaded item when GoToDownloadPage", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", type: "download", referrer: "foo" }} + source={"HIGHLIGHTS"} + options={["GoToDownloadPage"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-go-to-download-page") + ); + assert.isFalse(options[0].disabled); + }); + it("should show Go To Download Page option as disabled for a downloaded item when GoToDownloadPage if no referrer exists", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", type: "download", referrer: null }} + source={"HIGHLIGHTS"} + options={["GoToDownloadPage"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-go-to-download-page") + ); + assert.isTrue(options[0].disabled); + }); + it("should show Remove Download Link option for a downloaded item when RemoveDownload", () => { + wrapper = shallow( + <LinkMenu + site={{ url: "", type: "download" }} + source={"HIGHLIGHTS"} + options={["RemoveDownload"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + assert.isDefined( + options.find(o => o.id && o.id === "newtab-menu-remove-download") + ); + }); + it("should show Edit option", () => { + const props = { url: "foo", label: "label" }; + const index = 5; + wrapper = shallow( + <LinkMenu + site={props} + index={5} + source={"TOP_SITES"} + options={["EditTopSite"]} + dispatch={() => {}} + /> + ); + const { options } = wrapper.find(ContextMenu).props(); + const option = options.find( + o => o.id && o.id === "newtab-menu-edit-topsites" + ); + assert.isDefined(option); + assert.equal(option.action.data.index, index); + }); + describe(".onClick", () => { + const FAKE_EVENT = {}; + const FAKE_INDEX = 3; + const FAKE_SOURCE = "TOP_SITES"; + const FAKE_SITE = { + bookmarkGuid: 1234, + hostname: "foo", + path: "foo", + pocket_id: "1234", + referrer: "https://foo.com/ref", + title: "bar", + type: "bookmark", + typedBonus: true, + url: "https://foo.com", + sponsored_tile_id: 12345, + }; + const dispatch = sinon.stub(); + const propOptions = [ + "ShowFile", + "CopyDownloadLink", + "GoToDownloadPage", + "RemoveDownload", + "Separator", + "ShowPrivacyInfo", + "RemoveBookmark", + "AddBookmark", + "OpenInNewWindow", + "OpenInPrivateWindow", + "BlockUrl", + "DeleteUrl", + "PinTopSite", + "UnpinTopSite", + "SaveToPocket", + "DeleteFromPocket", + "ArchiveFromPocket", + "WebExtDismiss", + ]; + const expectedActionData = { + "newtab-menu-remove-bookmark": FAKE_SITE.bookmarkGuid, + "newtab-menu-bookmark": { + url: FAKE_SITE.url, + title: FAKE_SITE.title, + type: FAKE_SITE.type, + }, + "newtab-menu-open-new-window": { + url: FAKE_SITE.url, + referrer: FAKE_SITE.referrer, + typedBonus: FAKE_SITE.typedBonus, + sponsored_tile_id: FAKE_SITE.sponsored_tile_id, + }, + "newtab-menu-open-new-private-window": { + url: FAKE_SITE.url, + referrer: FAKE_SITE.referrer, + }, + "newtab-menu-dismiss": [ + { + url: FAKE_SITE.url, + pocket_id: FAKE_SITE.pocket_id, + isSponsoredTopSite: undefined, + position: 3, + tile_id: 12345, + is_pocket_card: false, + }, + ], + menu_action_webext_dismiss: { + source: "TOP_SITES", + url: FAKE_SITE.url, + action_position: 3, + }, + "newtab-menu-delete-history": { + url: FAKE_SITE.url, + pocket_id: FAKE_SITE.pocket_id, + forceBlock: FAKE_SITE.bookmarkGuid, + }, + "newtab-menu-pin": { site: FAKE_SITE, index: FAKE_INDEX }, + "newtab-menu-unpin": { site: { url: FAKE_SITE.url } }, + "newtab-menu-save-to-pocket": { + site: { url: FAKE_SITE.url, title: FAKE_SITE.title }, + }, + "newtab-menu-delete-pocket": { pocket_id: "1234" }, + "newtab-menu-archive-pocket": { pocket_id: "1234" }, + "newtab-menu-show-file": { url: FAKE_SITE.url }, + "newtab-menu-copy-download-link": { url: FAKE_SITE.url }, + "newtab-menu-go-to-download-page": { url: FAKE_SITE.referrer }, + "newtab-menu-remove-download": { url: FAKE_SITE.url }, + }; + const { options } = shallow( + <LinkMenu + site={FAKE_SITE} + siteInfo={{ value: { card_type: FAKE_SITE.type } }} + dispatch={dispatch} + index={FAKE_INDEX} + isPrivateBrowsingEnabled={true} + platform={"default"} + options={propOptions} + source={FAKE_SOURCE} + shouldSendImpressionStats={true} + /> + ) + .find(ContextMenu) + .props(); + afterEach(() => dispatch.reset()); + options + .filter(o => o.type !== "separator") + .forEach(option => { + it(`should fire a ${option.action.type} action for ${option.id} with the expected data`, () => { + option.onClick(FAKE_EVENT); + + if (option.impression && option.userEvent) { + assert.calledThrice(dispatch); + } else if (option.impression || option.userEvent) { + assert.calledTwice(dispatch); + } else { + assert.calledOnce(dispatch); + } + + // option.action is dispatched + assert.ok(dispatch.firstCall.calledWith(option.action)); + + // option.action has correct data + // (delete is a special case as it dispatches a nested DIALOG_OPEN-type action) + // in the case of this FAKE_SITE, we send a bookmarkGuid therefore we also want + // to block this if we delete it + if (option.id === "newtab-menu-delete-history") { + assert.deepEqual( + option.action.data.onConfirm[0].data, + expectedActionData[option.id] + ); + // Test UserEvent send correct meta about item deleted + assert.propertyVal( + option.action.data.onConfirm[1].data, + "action_position", + FAKE_INDEX + ); + assert.propertyVal( + option.action.data.onConfirm[1].data, + "source", + FAKE_SOURCE + ); + } else { + assert.deepEqual(option.action.data, expectedActionData[option.id]); + } + }); + it(`should fire a UserEvent action for ${option.id} if configured`, () => { + if (option.userEvent) { + option.onClick(FAKE_EVENT); + const [action] = dispatch.secondCall.args; + assert.isUserEventAction(action); + assert.propertyVal(action.data, "source", FAKE_SOURCE); + assert.propertyVal(action.data, "action_position", FAKE_INDEX); + assert.propertyVal(action.data.value, "card_type", FAKE_SITE.type); + } + }); + it(`should send impression stats for ${option.id}`, () => { + if (option.impression) { + option.onClick(FAKE_EVENT); + const [action] = dispatch.thirdCall.args; + assert.deepEqual(action, option.impression); + } + }); + }); + it(`should not send impression stats if not configured`, () => { + const fakeOptions = shallow( + <LinkMenu + site={FAKE_SITE} + dispatch={dispatch} + index={FAKE_INDEX} + options={propOptions} + source={FAKE_SOURCE} + shouldSendImpressionStats={false} + /> + ) + .find(ContextMenu) + .props().options; + + fakeOptions + .filter(o => o.type !== "separator") + .forEach(option => { + if (option.impression) { + option.onClick(FAKE_EVENT); + assert.calledTwice(dispatch); + assert.notEqual(dispatch.firstCall.args[0], option.impression); + assert.notEqual(dispatch.secondCall.args[0], option.impression); + dispatch.reset(); + } + }); + }); + it(`should pin a SPOC with all of the site details sent`, () => { + const pinSpocTopSite = "PinTopSite"; + const { options: spocOptions } = shallow( + <LinkMenu + site={FAKE_SITE} + siteInfo={{ value: { card_type: FAKE_SITE.type } }} + dispatch={dispatch} + index={FAKE_INDEX} + isPrivateBrowsingEnabled={true} + platform={"default"} + options={[pinSpocTopSite]} + source={FAKE_SOURCE} + shouldSendImpressionStats={true} + /> + ) + .find(ContextMenu) + .props(); + + const [pinSpocOption] = spocOptions; + pinSpocOption.onClick(FAKE_EVENT); + + if (pinSpocOption.impression && pinSpocOption.userEvent) { + assert.calledThrice(dispatch); + } else if (pinSpocOption.impression || pinSpocOption.userEvent) { + assert.calledTwice(dispatch); + } else { + assert.calledOnce(dispatch); + } + + // option.action is dispatched + assert.ok(dispatch.firstCall.calledWith(pinSpocOption.action)); + + assert.deepEqual(pinSpocOption.action.data, { + site: FAKE_SITE, + index: FAKE_INDEX, + }); + }); + it(`should create a proper BLOCK_URL action for a sponsored tile`, () => { + const site = { + hostname: "foo", + path: "foo", + referrer: "https://foo.com/ref", + title: "bar", + type: "bookmark", + typedBonus: true, + url: "https://foo.com", + sponsored_position: 1, + }; + const { options: blockOptions } = shallow( + <LinkMenu + site={site} + siteInfo={{ value: { card_type: site.type } }} + dispatch={dispatch} + index={FAKE_INDEX} + isPrivateBrowsingEnabled={true} + platform={"default"} + options={["BlockUrl"]} + source={FAKE_SOURCE} + shouldSendImpressionStats={true} + /> + ) + .find(ContextMenu) + .props(); + const [blockUrlOption] = blockOptions; + + blockUrlOption.onClick(FAKE_EVENT); + + assert.calledThrice(dispatch); + assert.ok(dispatch.firstCall.calledWith(blockUrlOption.action)); + const expected = { + url: site.url, + pocket_id: undefined, + advertiser_name: site.hostname, + isSponsoredTopSite: 1, + position: 3, + is_pocket_card: false, + }; + assert.deepEqual(blockUrlOption.action.data[0], expected); + }); + it(`should create a proper BLOCK_URL action for a pocket item`, () => { + const site = { + hostname: "foo", + path: "foo", + referrer: "https://foo.com/ref", + title: "bar", + type: "CardGrid", + typedBonus: true, + url: "https://foo.com", + }; + const { options: blockOptions } = shallow( + <LinkMenu + site={site} + siteInfo={{ value: { card_type: site.type } }} + dispatch={dispatch} + index={FAKE_INDEX} + isPrivateBrowsingEnabled={true} + platform={"default"} + options={["BlockUrl"]} + source={FAKE_SOURCE} + shouldSendImpressionStats={true} + /> + ) + .find(ContextMenu) + .props(); + const [blockUrlOption] = blockOptions; + + blockUrlOption.onClick(FAKE_EVENT); + + assert.calledThrice(dispatch); + assert.ok(dispatch.firstCall.calledWith(blockUrlOption.action)); + const expected = { + url: site.url, + pocket_id: undefined, + isSponsoredTopSite: undefined, + position: 3, + is_pocket_card: true, + }; + assert.deepEqual(blockUrlOption.action.data[0], expected); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/MoreRecommendations.test.jsx b/browser/components/newtab/test/unit/content-src/components/MoreRecommendations.test.jsx new file mode 100644 index 0000000000..2b3c06b6bf --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/MoreRecommendations.test.jsx @@ -0,0 +1,24 @@ +import { MoreRecommendations } from "content-src/components/MoreRecommendations/MoreRecommendations"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<MoreRecommendations>", () => { + it("should render a MoreRecommendations element", () => { + const wrapper = shallow(<MoreRecommendations />); + assert.ok(wrapper.exists()); + }); + it("should render a link when provided with read_more_endpoint prop", () => { + const wrapper = shallow( + <MoreRecommendations read_more_endpoint="https://endpoint.com" /> + ); + + const link = wrapper.find(".more-recommendations"); + assert.lengthOf(link, 1); + }); + it("should not render a link when provided with read_more_endpoint prop", () => { + const wrapper = shallow(<MoreRecommendations read_more_endpoint="" />); + + const link = wrapper.find(".more-recommendations"); + assert.lengthOf(link, 0); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/PocketLoggedInCta.test.jsx b/browser/components/newtab/test/unit/content-src/components/PocketLoggedInCta.test.jsx new file mode 100644 index 0000000000..31a5e7be4d --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/PocketLoggedInCta.test.jsx @@ -0,0 +1,46 @@ +import { combineReducers, createStore } from "redux"; +import { INITIAL_STATE, reducers } from "common/Reducers.sys.mjs"; +import { mount, shallow } from "enzyme"; +import { + PocketLoggedInCta, + _PocketLoggedInCta as PocketLoggedInCtaRaw, +} from "content-src/components/PocketLoggedInCta/PocketLoggedInCta"; +import { Provider } from "react-redux"; +import React from "react"; + +function mountSectionWithProps(props) { + const store = createStore(combineReducers(reducers), INITIAL_STATE); + return mount( + <Provider store={store}> + <PocketLoggedInCta {...props} /> + </Provider> + ); +} + +describe("<PocketLoggedInCta>", () => { + it("should render a PocketLoggedInCta element", () => { + const wrapper = mountSectionWithProps({}); + assert.ok(wrapper.exists()); + }); + it("should render Fluent spans when rendered without props", () => { + const wrapper = mountSectionWithProps({}); + + const message = wrapper.find("span[data-l10n-id]"); + assert.lengthOf(message, 2); + }); + it("should not render Fluent spans when rendered with props", () => { + const wrapper = shallow( + <PocketLoggedInCtaRaw + Pocket={{ + pocketCta: { + ctaButton: "button", + ctaText: "text", + }, + }} + /> + ); + + const message = wrapper.find("span[data-l10n-id]"); + assert.lengthOf(message, 0); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/Search.test.jsx b/browser/components/newtab/test/unit/content-src/components/Search.test.jsx new file mode 100644 index 0000000000..54a3b611cc --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/Search.test.jsx @@ -0,0 +1,179 @@ +import { GlobalOverrider } from "test/unit/utils"; +import { mount, shallow } from "enzyme"; +import React from "react"; +import { _Search as Search } from "content-src/components/Search/Search"; + +const DEFAULT_PROPS = { + dispatch() {}, + Prefs: { values: { featureConfig: {} } }, +}; + +describe("<Search>", () => { + let globals; + let sandbox; + beforeEach(() => { + globals = new GlobalOverrider(); + sandbox = globals.sandbox; + + global.ContentSearchUIController.prototype = { search: sandbox.spy() }; + }); + afterEach(() => { + globals.restore(); + }); + + it("should render a Search element", () => { + const wrapper = shallow(<Search {...DEFAULT_PROPS} />); + assert.ok(wrapper.exists()); + }); + it("should not use a <form> element", () => { + const wrapper = mount(<Search {...DEFAULT_PROPS} />); + + assert.equal(wrapper.find("form").length, 0); + }); + it("should listen for ContentSearchClient on render", () => { + const spy = globals.set("addEventListener", sandbox.spy()); + + const wrapper = mount(<Search {...DEFAULT_PROPS} />); + + assert.calledOnce(spy.withArgs("ContentSearchClient", wrapper.instance())); + }); + it("should stop listening for ContentSearchClient on unmount", () => { + const spy = globals.set("removeEventListener", sandbox.spy()); + const wrapper = mount(<Search {...DEFAULT_PROPS} />); + // cache the instance as we can't call this method after unmount is called + const instance = wrapper.instance(); + + wrapper.unmount(); + + assert.calledOnce(spy.withArgs("ContentSearchClient", instance)); + }); + it("should add gContentSearchController as a global", () => { + // current about:home tests need gContentSearchController to exist as a global + // so let's test it here too to ensure we don't break this behaviour + mount(<Search {...DEFAULT_PROPS} />); + assert.property(window, "gContentSearchController"); + assert.ok(window.gContentSearchController); + }); + it("should pass along search when clicking the search button", () => { + const wrapper = mount(<Search {...DEFAULT_PROPS} />); + + wrapper.find(".search-button").simulate("click"); + + const { search } = window.gContentSearchController; + assert.calledOnce(search); + assert.propertyVal(search.firstCall.args[0], "type", "click"); + }); + it("should send a UserEvent action", () => { + global.ContentSearchUIController.prototype.search = () => { + dispatchEvent( + new CustomEvent("ContentSearchClient", { detail: { type: "Search" } }) + ); + }; + const dispatch = sinon.spy(); + const wrapper = mount(<Search {...DEFAULT_PROPS} dispatch={dispatch} />); + + wrapper.find(".search-button").simulate("click"); + + assert.calledOnce(dispatch); + const [action] = dispatch.firstCall.args; + assert.isUserEventAction(action); + assert.propertyVal(action.data, "event", "SEARCH"); + }); + it("should show our logo when the prop exists.", () => { + const showLogoProps = Object.assign({}, DEFAULT_PROPS, { showLogo: true }); + + const wrapper = shallow(<Search {...showLogoProps} />); + assert.lengthOf(wrapper.find(".logo-and-wordmark"), 1); + }); + it("should not show our logo when the prop does not exist.", () => { + const hideLogoProps = Object.assign({}, DEFAULT_PROPS, { showLogo: false }); + + const wrapper = shallow(<Search {...hideLogoProps} />); + assert.lengthOf(wrapper.find(".logo-and-wordmark"), 0); + }); + + describe("Search Hand-off", () => { + it("should render a Search element when hand-off is enabled", () => { + const wrapper = shallow( + <Search {...DEFAULT_PROPS} handoffEnabled={true} /> + ); + assert.ok(wrapper.exists()); + assert.equal(wrapper.find(".search-handoff-button").length, 1); + }); + it("should hand-off search when button is clicked", () => { + const dispatch = sinon.spy(); + const wrapper = shallow( + <Search {...DEFAULT_PROPS} handoffEnabled={true} dispatch={dispatch} /> + ); + wrapper + .find(".search-handoff-button") + .simulate("click", { preventDefault: () => {} }); + assert.calledThrice(dispatch); + assert.calledWith(dispatch, { + data: { text: undefined }, + meta: { + from: "ActivityStream:Content", + skipLocal: true, + to: "ActivityStream:Main", + }, + type: "HANDOFF_SEARCH_TO_AWESOMEBAR", + }); + assert.calledWith(dispatch, { type: "FAKE_FOCUS_SEARCH" }); + const [action] = dispatch.thirdCall.args; + assert.isUserEventAction(action); + assert.propertyVal(action.data, "event", "SEARCH_HANDOFF"); + }); + it("should hand-off search on paste", () => { + const dispatch = sinon.spy(); + const wrapper = mount( + <Search {...DEFAULT_PROPS} handoffEnabled={true} dispatch={dispatch} /> + ); + wrapper.instance()._searchHandoffButton = { contains: () => true }; + wrapper.instance().onSearchHandoffPaste({ + clipboardData: { + getData: () => "some copied text", + }, + preventDefault: () => {}, + }); + assert.equal(dispatch.callCount, 4); + assert.calledWith(dispatch, { + data: { text: "some copied text" }, + meta: { + from: "ActivityStream:Content", + skipLocal: true, + to: "ActivityStream:Main", + }, + type: "HANDOFF_SEARCH_TO_AWESOMEBAR", + }); + assert.calledWith(dispatch, { type: "DISABLE_SEARCH" }); + const [action] = dispatch.thirdCall.args; + assert.isUserEventAction(action); + assert.propertyVal(action.data, "event", "SEARCH_HANDOFF"); + }); + it("should properly handle drop events", () => { + const dispatch = sinon.spy(); + const wrapper = mount( + <Search {...DEFAULT_PROPS} handoffEnabled={true} dispatch={dispatch} /> + ); + const preventDefault = sinon.spy(); + wrapper.find(".fake-editable").simulate("drop", { + dataTransfer: { getData: () => "dropped text" }, + preventDefault, + }); + assert.equal(dispatch.callCount, 4); + assert.calledWith(dispatch, { + data: { text: "dropped text" }, + meta: { + from: "ActivityStream:Content", + skipLocal: true, + to: "ActivityStream:Main", + }, + type: "HANDOFF_SEARCH_TO_AWESOMEBAR", + }); + assert.calledWith(dispatch, { type: "DISABLE_SEARCH" }); + const [action] = dispatch.thirdCall.args; + assert.isUserEventAction(action); + assert.propertyVal(action.data, "event", "SEARCH_HANDOFF"); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/Sections.test.jsx b/browser/components/newtab/test/unit/content-src/components/Sections.test.jsx new file mode 100644 index 0000000000..9f4008369a --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/Sections.test.jsx @@ -0,0 +1,600 @@ +import { combineReducers, createStore } from "redux"; +import { INITIAL_STATE, reducers } from "common/Reducers.sys.mjs"; +import { + Section, + SectionIntl, + _Sections as Sections, +} from "content-src/components/Sections/Sections"; +import { actionTypes as at } from "common/Actions.sys.mjs"; +import { mount, shallow } from "enzyme"; +import { PlaceholderCard } from "content-src/components/Card/Card"; +import { PocketLoggedInCta } from "content-src/components/PocketLoggedInCta/PocketLoggedInCta"; +import { Provider } from "react-redux"; +import React from "react"; +import { Topics } from "content-src/components/Topics/Topics"; +import { TopSites } from "content-src/components/TopSites/TopSites"; + +function mountSectionWithProps(props) { + const store = createStore(combineReducers(reducers), INITIAL_STATE); + return mount( + <Provider store={store}> + <Section {...props} /> + </Provider> + ); +} + +function mountSectionIntlWithProps(props) { + const store = createStore(combineReducers(reducers), INITIAL_STATE); + return mount( + <Provider store={store}> + <SectionIntl {...props} /> + </Provider> + ); +} + +describe("<Sections>", () => { + let wrapper; + let FAKE_SECTIONS; + beforeEach(() => { + FAKE_SECTIONS = new Array(5).fill(null).map((v, i) => ({ + id: `foo_bar_${i}`, + title: `Foo Bar ${i}`, + enabled: !!(i % 2), + rows: [], + })); + wrapper = shallow( + <Sections + Sections={FAKE_SECTIONS} + Prefs={{ + values: { sectionOrder: FAKE_SECTIONS.map(i => i.id).join(",") }, + }} + /> + ); + }); + it("should render a Sections element", () => { + assert.ok(wrapper.exists()); + }); + it("should render a Section for each one passed in props.Sections with .enabled === true", () => { + const sectionElems = wrapper.find(SectionIntl); + assert.lengthOf(sectionElems, 2); + sectionElems.forEach((section, i) => { + assert.equal(section.props().id, FAKE_SECTIONS[2 * i + 1].id); + assert.equal(section.props().enabled, true); + }); + }); + it("should render Top Sites if feeds.topsites pref is true", () => { + wrapper = shallow( + <Sections + Sections={FAKE_SECTIONS} + Prefs={{ + values: { + "feeds.topsites": true, + sectionOrder: "topsites,topstories,highlights", + }, + }} + /> + ); + assert.equal(wrapper.find(TopSites).length, 1); + }); + it("should NOT render Top Sites if feeds.topsites pref is false", () => { + wrapper = shallow( + <Sections + Sections={FAKE_SECTIONS} + Prefs={{ + values: { + "feeds.topsites": false, + sectionOrder: "topsites,topstories,highlights", + }, + }} + /> + ); + assert.equal(wrapper.find(TopSites).length, 0); + }); + it("should render the sections in the order specifed by sectionOrder pref", () => { + wrapper = shallow( + <Sections + Sections={FAKE_SECTIONS} + Prefs={{ values: { sectionOrder: "foo_bar_1,foo_bar_3" } }} + /> + ); + let sections = wrapper.find(SectionIntl); + assert.lengthOf(sections, 2); + assert.equal(sections.first().props().id, "foo_bar_1"); + assert.equal(sections.last().props().id, "foo_bar_3"); + wrapper = shallow( + <Sections + Sections={FAKE_SECTIONS} + Prefs={{ values: { sectionOrder: "foo_bar_3,foo_bar_1" } }} + /> + ); + sections = wrapper.find(SectionIntl); + assert.lengthOf(sections, 2); + assert.equal(sections.first().props().id, "foo_bar_3"); + assert.equal(sections.last().props().id, "foo_bar_1"); + }); +}); + +describe("<Section>", () => { + let wrapper; + let FAKE_SECTION; + + beforeEach(() => { + FAKE_SECTION = { + id: `foo_bar_1`, + pref: { collapsed: false }, + title: `Foo Bar 1`, + rows: [{ link: "http://localhost", index: 0 }], + emptyState: { + icon: "check", + message: "Some message", + }, + rowsPref: "section.rows", + maxRows: 4, + Prefs: { values: { "section.rows": 2 } }, + }; + wrapper = mountSectionIntlWithProps(FAKE_SECTION); + }); + + describe("placeholders", () => { + const CARDS_PER_ROW = 3; + const fakeSite = { link: "http://localhost" }; + function renderWithSites(rows) { + const store = createStore(combineReducers(reducers), INITIAL_STATE); + return mount( + <Provider store={store}> + <Section {...FAKE_SECTION} rows={rows} /> + </Provider> + ); + } + + it("should return 2 row of placeholders if realRows is 0", () => { + wrapper = renderWithSites([]); + assert.lengthOf(wrapper.find(PlaceholderCard), 6); + }); + it("should fill in the rest of the rows", () => { + wrapper = renderWithSites(new Array(CARDS_PER_ROW).fill(fakeSite)); + assert.lengthOf( + wrapper.find(PlaceholderCard), + CARDS_PER_ROW, + "CARDS_PER_ROW" + ); + + wrapper = renderWithSites(new Array(CARDS_PER_ROW + 1).fill(fakeSite)); + assert.lengthOf(wrapper.find(PlaceholderCard), 2, "CARDS_PER_ROW + 1"); + + wrapper = renderWithSites(new Array(CARDS_PER_ROW + 2).fill(fakeSite)); + assert.lengthOf(wrapper.find(PlaceholderCard), 1, "CARDS_PER_ROW + 2"); + + wrapper = renderWithSites( + new Array(2 * CARDS_PER_ROW - 1).fill(fakeSite) + ); + assert.lengthOf(wrapper.find(PlaceholderCard), 1, "CARDS_PER_ROW - 1"); + }); + it("should not add placeholders all the rows are full", () => { + wrapper = renderWithSites(new Array(2 * CARDS_PER_ROW).fill(fakeSite)); + assert.lengthOf(wrapper.find(PlaceholderCard), 0, "2 rows"); + }); + }); + + describe("empty state", () => { + beforeEach(() => { + Object.assign(FAKE_SECTION, { + initialized: true, + dispatch: () => {}, + rows: [], + emptyState: { + message: "Some message", + }, + }); + wrapper = shallow(<Section {...FAKE_SECTION} />); + }); + it("should be shown when rows is empty and initialized is true", () => { + assert.ok(wrapper.find(".empty-state").exists()); + }); + it("should not be shown in initialized is false", () => { + Object.assign(FAKE_SECTION, { + initialized: false, + rows: [], + emptyState: { + message: "Some message", + }, + }); + wrapper = shallow(<Section {...FAKE_SECTION} />); + assert.isFalse(wrapper.find(".empty-state").exists()); + }); + it("no icon should be shown", () => { + assert.lengthOf(wrapper.find(".icon"), 0); + }); + }); + + describe("topics component", () => { + let TOP_STORIES_SECTION; + beforeEach(() => { + TOP_STORIES_SECTION = { + id: "topstories", + title: "TopStories", + pref: { collapsed: false }, + rows: [{ guid: 1, link: "http://localhost", isDefault: true }], + topics: [], + read_more_endpoint: "http://localhost/read-more", + maxRows: 1, + eventSource: "TOP_STORIES", + }; + }); + it("should not render for empty topics", () => { + wrapper = mountSectionIntlWithProps(TOP_STORIES_SECTION); + + assert.lengthOf(wrapper.find(".topic"), 0); + }); + it("should render for non-empty topics", () => { + TOP_STORIES_SECTION.topics = [{ name: "topic1", url: "topic-url1" }]; + wrapper = shallow( + <Section + Pocket={{ pocketCta: { useCta: true }, isUserLoggedIn: true }} + {...TOP_STORIES_SECTION} + /> + ); + + assert.lengthOf(wrapper.find(Topics), 1); + assert.lengthOf(wrapper.find(PocketLoggedInCta), 0); + }); + it("should delay render of third rec to give time for potential spoc", async () => { + TOP_STORIES_SECTION.rows = [ + { guid: 1, link: "http://localhost" }, + { guid: 2, link: "http://localhost" }, + { guid: 3, link: "http://localhost" }, + ]; + wrapper = shallow( + <Section + Pocket={{ waitingForSpoc: true, pocketCta: {} }} + {...TOP_STORIES_SECTION} + /> + ); + assert.lengthOf(wrapper.find(PlaceholderCard), 1); + + wrapper.setProps({ + Pocket: { + waitingForSpoc: false, + pocketCta: {}, + }, + }); + assert.lengthOf(wrapper.find(PlaceholderCard), 0); + }); + it("should render container for uninitialized topics to ensure content doesn't shift", () => { + delete TOP_STORIES_SECTION.topics; + + wrapper = mountSectionIntlWithProps(TOP_STORIES_SECTION); + + assert.lengthOf(wrapper.find(".top-stories-bottom-container"), 1); + assert.lengthOf(wrapper.find(Topics), 0); + assert.lengthOf(wrapper.find(PocketLoggedInCta), 0); + }); + + it("should render a pocket cta if not logged in and set to display cta", () => { + TOP_STORIES_SECTION.topics = [{ name: "topic1", url: "topic-url1" }]; + wrapper = shallow( + <Section + Pocket={{ pocketCta: { useCta: true }, isUserLoggedIn: false }} + {...TOP_STORIES_SECTION} + /> + ); + + assert.lengthOf(wrapper.find(Topics), 0); + assert.lengthOf(wrapper.find(PocketLoggedInCta), 1); + }); + it("should render nothing while loading to avoid a flicker of log in state", () => { + TOP_STORIES_SECTION.topics = [{ name: "topic1", url: "topic-url1" }]; + wrapper = shallow( + <Section + Pocket={{ pocketCta: { useCta: false } }} + {...TOP_STORIES_SECTION} + /> + ); + + assert.lengthOf(wrapper.find(Topics), 0); + assert.lengthOf(wrapper.find(PocketLoggedInCta), 0); + }); + it("should render a topics list if set to not display cta with either logged or out", () => { + TOP_STORIES_SECTION.topics = [{ name: "topic1", url: "topic-url1" }]; + wrapper = shallow( + <Section + Pocket={{ pocketCta: { useCta: false }, isUserLoggedIn: false }} + {...TOP_STORIES_SECTION} + /> + ); + + assert.lengthOf(wrapper.find(Topics), 1); + assert.lengthOf(wrapper.find(PocketLoggedInCta), 0); + + wrapper = shallow( + <Section + Pocket={{ pocketCta: { useCta: false }, isUserLoggedIn: true }} + {...TOP_STORIES_SECTION} + /> + ); + + assert.lengthOf(wrapper.find(Topics), 1); + assert.lengthOf(wrapper.find(PocketLoggedInCta), 0); + }); + it("should render nothing if set to display a cta and not logged in or out (waiting for state)", () => { + TOP_STORIES_SECTION.topics = [{ name: "topic1", url: "topic-url1" }]; + wrapper = shallow( + <Section + Pocket={{ pocketCta: { useCta: true } }} + {...TOP_STORIES_SECTION} + /> + ); + + assert.lengthOf(wrapper.find(Topics), 0); + assert.lengthOf(wrapper.find(PocketLoggedInCta), 0); + }); + }); + + describe("impression stats", () => { + const FAKE_TOPSTORIES_SECTION_PROPS = { + id: "TopStories", + title: "Foo Bar 1", + pref: { collapsed: false }, + maxRows: 1, + rows: [{ guid: 1 }, { guid: 2 }], + shouldSendImpressionStats: true, + + document: { + visibilityState: "visible", + addEventListener: sinon.stub(), + removeEventListener: sinon.stub(), + }, + eventSource: "TOP_STORIES", + options: { personalized: false }, + }; + + function renderSection(props = {}) { + return shallow(<Section {...FAKE_TOPSTORIES_SECTION_PROPS} {...props} />); + } + + it("should send impression with the right stats when the page loads", () => { + const dispatch = sinon.spy(); + renderSection({ dispatch }); + + assert.calledOnce(dispatch); + + const [action] = dispatch.firstCall.args; + assert.equal(action.type, at.TELEMETRY_IMPRESSION_STATS); + assert.equal(action.data.source, "TOP_STORIES"); + assert.deepEqual(action.data.tiles, [{ id: 1 }, { id: 2 }]); + }); + it("should not send impression stats if not configured", () => { + const dispatch = sinon.spy(); + const props = Object.assign({}, FAKE_TOPSTORIES_SECTION_PROPS, { + shouldSendImpressionStats: false, + dispatch, + }); + renderSection(props); + assert.notCalled(dispatch); + }); + it("should not send impression stats if the section is collapsed", () => { + const dispatch = sinon.spy(); + const props = Object.assign({}, FAKE_TOPSTORIES_SECTION_PROPS, { + pref: { collapsed: true }, + }); + renderSection(props); + assert.notCalled(dispatch); + }); + it("should send 1 impression when the page becomes visibile after loading", () => { + const props = { + dispatch: sinon.spy(), + document: { + visibilityState: "hidden", + addEventListener: sinon.spy(), + removeEventListener: sinon.spy(), + }, + }; + + renderSection(props); + + // Was the event listener added? + assert.calledWith(props.document.addEventListener, "visibilitychange"); + + // Make sure dispatch wasn't called yet + assert.notCalled(props.dispatch); + + // Simulate a visibilityChange event + const [, listener] = props.document.addEventListener.firstCall.args; + props.document.visibilityState = "visible"; + listener(); + + // Did we actually dispatch an event? + assert.calledOnce(props.dispatch); + assert.equal( + props.dispatch.firstCall.args[0].type, + at.TELEMETRY_IMPRESSION_STATS + ); + + // Did we remove the event listener? + assert.calledWith( + props.document.removeEventListener, + "visibilitychange", + listener + ); + }); + it("should remove visibility change listener when section is removed", () => { + const props = { + dispatch: sinon.spy(), + document: { + visibilityState: "hidden", + addEventListener: sinon.spy(), + removeEventListener: sinon.spy(), + }, + }; + + const section = renderSection(props); + assert.calledWith(props.document.addEventListener, "visibilitychange"); + const [, listener] = props.document.addEventListener.firstCall.args; + + section.unmount(); + assert.calledWith( + props.document.removeEventListener, + "visibilitychange", + listener + ); + }); + it("should send an impression if props are updated and props.rows are different", () => { + const props = { dispatch: sinon.spy() }; + wrapper = renderSection(props); + props.dispatch.resetHistory(); + + // New rows + wrapper.setProps( + Object.assign({}, FAKE_TOPSTORIES_SECTION_PROPS, { + rows: [{ guid: 123 }], + }) + ); + + assert.calledOnce(props.dispatch); + }); + it("should not send an impression if props are updated but props.rows are the same", () => { + const props = { dispatch: sinon.spy() }; + wrapper = renderSection(props); + props.dispatch.resetHistory(); + + // Only update the disclaimer prop + wrapper.setProps( + Object.assign({}, FAKE_TOPSTORIES_SECTION_PROPS, { + disclaimer: { id: "bar" }, + }) + ); + + assert.notCalled(props.dispatch); + }); + it("should not send an impression if props are updated and props.rows are the same but section is collapsed", () => { + const props = { dispatch: sinon.spy() }; + wrapper = renderSection(props); + props.dispatch.resetHistory(); + + // New rows and collapsed + wrapper.setProps( + Object.assign({}, FAKE_TOPSTORIES_SECTION_PROPS, { + rows: [{ guid: 123 }], + pref: { collapsed: true }, + }) + ); + + assert.notCalled(props.dispatch); + + // Expand the section. Now the impression stats should be sent + wrapper.setProps( + Object.assign({}, FAKE_TOPSTORIES_SECTION_PROPS, { + rows: [{ guid: 123 }], + pref: { collapsed: false }, + }) + ); + + assert.calledOnce(props.dispatch); + }); + it("should not send an impression if props are updated but GUIDs are the same", () => { + const props = { dispatch: sinon.spy() }; + wrapper = renderSection(props); + props.dispatch.resetHistory(); + + wrapper.setProps( + Object.assign({}, FAKE_TOPSTORIES_SECTION_PROPS, { + rows: [{ guid: 1 }, { guid: 2 }], + }) + ); + + assert.notCalled(props.dispatch); + }); + it("should only send the latest impression on a visibility change", () => { + const listeners = new Set(); + const props = { + dispatch: sinon.spy(), + document: { + visibilityState: "hidden", + addEventListener: (ev, cb) => listeners.add(cb), + removeEventListener: (ev, cb) => listeners.delete(cb), + }, + }; + + wrapper = renderSection(props); + + // Update twice + wrapper.setProps(Object.assign({}, props, { rows: [{ guid: 123 }] })); + wrapper.setProps(Object.assign({}, props, { rows: [{ guid: 2432 }] })); + + assert.notCalled(props.dispatch); + + // Simulate listeners getting called + props.document.visibilityState = "visible"; + listeners.forEach(l => l()); + + // Make sure we only sent the latest event + assert.calledOnce(props.dispatch); + const [action] = props.dispatch.firstCall.args; + assert.deepEqual(action.data.tiles, [{ id: 2432 }]); + }); + }); + + describe("tab rehydrated", () => { + it("should fire NEW_TAB_REHYDRATED event", () => { + const dispatch = sinon.spy(); + const TOP_STORIES_SECTION = { + id: "topstories", + title: "TopStories", + pref: { collapsed: false }, + initialized: false, + rows: [{ guid: 1, link: "http://localhost", isDefault: true }], + topics: [], + read_more_endpoint: "http://localhost/read-more", + maxRows: 1, + eventSource: "TOP_STORIES", + }; + wrapper = shallow( + <Section + Pocket={{ waitingForSpoc: true, pocketCta: {} }} + {...TOP_STORIES_SECTION} + dispatch={dispatch} + /> + ); + assert.notCalled(dispatch); + + wrapper.setProps({ initialized: true }); + + assert.calledOnce(dispatch); + const [action] = dispatch.firstCall.args; + assert.equal("NEW_TAB_REHYDRATED", action.type); + }); + }); + + describe("#numRows", () => { + it("should return maxRows if there is no rowsPref set", () => { + delete FAKE_SECTION.rowsPref; + wrapper = mountSectionIntlWithProps(FAKE_SECTION); + assert.equal( + wrapper.find(Section).instance().numRows, + FAKE_SECTION.maxRows + ); + }); + + it("should return number of rows set in Pref if rowsPref is set", () => { + const numRows = 2; + Object.assign(FAKE_SECTION, { + rowsPref: "section.rows", + maxRows: 4, + Prefs: { values: { "section.rows": numRows } }, + }); + wrapper = mountSectionWithProps(FAKE_SECTION); + assert.equal(wrapper.find(Section).instance().numRows, numRows); + }); + + it("should return number of rows set in Pref even if higher than maxRows value", () => { + const numRows = 10; + Object.assign(FAKE_SECTION, { + rowsPref: "section.rows", + maxRows: 4, + Prefs: { values: { "section.rows": numRows } }, + }); + wrapper = mountSectionWithProps(FAKE_SECTION); + assert.equal(wrapper.find(Section).instance().numRows, numRows); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/TopSites.test.jsx b/browser/components/newtab/test/unit/content-src/components/TopSites.test.jsx new file mode 100644 index 0000000000..1977066f0d --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/TopSites.test.jsx @@ -0,0 +1,1930 @@ +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { GlobalOverrider } from "test/unit/utils"; +import { MIN_RICH_FAVICON_SIZE } from "content-src/components/TopSites/TopSitesConstants"; +import { + TOP_SITES_DEFAULT_ROWS, + TOP_SITES_MAX_SITES_PER_ROW, +} from "common/Reducers.sys.mjs"; +import { + TopSite, + TopSiteLink, + _TopSiteList as TopSiteList, + TopSitePlaceholder, +} from "content-src/components/TopSites/TopSite"; +import { + INTERSECTION_RATIO, + TopSiteImpressionWrapper, +} from "content-src/components/TopSites/TopSiteImpressionWrapper"; +import { A11yLinkButton } from "content-src/components/A11yLinkButton/A11yLinkButton"; +import { LinkMenu } from "content-src/components/LinkMenu/LinkMenu"; +import React from "react"; +import { mount, shallow } from "enzyme"; +import { TopSiteForm } from "content-src/components/TopSites/TopSiteForm"; +import { TopSiteFormInput } from "content-src/components/TopSites/TopSiteFormInput"; +import { _TopSites as TopSites } from "content-src/components/TopSites/TopSites"; +import { ContextMenuButton } from "content-src/components/ContextMenu/ContextMenuButton"; + +const perfSvc = { + mark() {}, + getMostRecentAbsMarkStartByName() {}, +}; + +const DEFAULT_PROPS = { + Prefs: { values: { featureConfig: {} } }, + TopSites: { initialized: true, rows: [] }, + TopSitesRows: TOP_SITES_DEFAULT_ROWS, + topSiteIconType: () => "no_image", + dispatch() {}, + perfSvc, +}; + +const DEFAULT_BLOB_URL = "blob://test"; + +describe("<TopSites>", () => { + let sandbox; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should render a TopSites element", () => { + const wrapper = shallow(<TopSites {...DEFAULT_PROPS} />); + assert.ok(wrapper.exists()); + }); + describe("#_dispatchTopSitesStats", () => { + let globals; + let wrapper; + let dispatchStatsSpy; + + beforeEach(() => { + globals = new GlobalOverrider(); + sandbox.stub(DEFAULT_PROPS, "dispatch"); + wrapper = shallow(<TopSites {...DEFAULT_PROPS} />, { + disableLifecycleMethods: true, + }); + dispatchStatsSpy = sandbox.spy( + wrapper.instance(), + "_dispatchTopSitesStats" + ); + }); + afterEach(() => { + globals.restore(); + sandbox.restore(); + }); + it("should call _dispatchTopSitesStats on componentDidMount", () => { + wrapper.instance().componentDidMount(); + + assert.calledOnce(dispatchStatsSpy); + }); + it("should call _dispatchTopSitesStats on componentDidUpdate", () => { + wrapper.instance().componentDidUpdate(); + + assert.calledOnce(dispatchStatsSpy); + }); + it("should dispatch SAVE_SESSION_PERF_DATA", () => { + wrapper.instance()._dispatchTopSitesStats(); + + assert.calledOnce(DEFAULT_PROPS.dispatch); + assert.calledWithExactly( + DEFAULT_PROPS.dispatch, + ac.AlsoToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { + topsites_icon_stats: { + custom_screenshot: 0, + screenshot: 0, + tippytop: 0, + rich_icon: 0, + no_image: 0, + }, + topsites_pinned: 0, + topsites_search_shortcuts: 0, + }, + }) + ); + }); + it("should correctly count TopSite images - just screenshot", () => { + const rows = [{ screenshot: true }]; + sandbox.stub(DEFAULT_PROPS.TopSites, "rows").value(rows); + wrapper.instance()._dispatchTopSitesStats(); + + assert.calledOnce(DEFAULT_PROPS.dispatch); + assert.calledWithExactly( + DEFAULT_PROPS.dispatch, + ac.AlsoToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { + topsites_icon_stats: { + custom_screenshot: 0, + screenshot: 1, + tippytop: 0, + rich_icon: 0, + no_image: 0, + }, + topsites_pinned: 0, + topsites_search_shortcuts: 0, + }, + }) + ); + }); + it("should correctly count TopSite images - custom_screenshot", () => { + const rows = [{ customScreenshotURL: true }]; + sandbox.stub(DEFAULT_PROPS.TopSites, "rows").value(rows); + wrapper.instance()._dispatchTopSitesStats(); + + assert.calledOnce(DEFAULT_PROPS.dispatch); + assert.calledWithExactly( + DEFAULT_PROPS.dispatch, + ac.AlsoToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { + topsites_icon_stats: { + custom_screenshot: 1, + screenshot: 0, + tippytop: 0, + rich_icon: 0, + no_image: 0, + }, + topsites_pinned: 0, + topsites_search_shortcuts: 0, + }, + }) + ); + }); + it("should correctly count TopSite images - rich_icon", () => { + const rows = [{ faviconSize: MIN_RICH_FAVICON_SIZE }]; + sandbox.stub(DEFAULT_PROPS.TopSites, "rows").value(rows); + wrapper.instance()._dispatchTopSitesStats(); + + assert.calledOnce(DEFAULT_PROPS.dispatch); + assert.calledWithExactly( + DEFAULT_PROPS.dispatch, + ac.AlsoToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { + topsites_icon_stats: { + custom_screenshot: 0, + screenshot: 0, + tippytop: 0, + rich_icon: 1, + no_image: 0, + }, + topsites_pinned: 0, + topsites_search_shortcuts: 0, + }, + }) + ); + }); + it("should correctly count TopSite images - tippytop", () => { + const rows = [ + { tippyTopIcon: "foo" }, + { faviconRef: "tippytop" }, + { faviconRef: "foobar" }, + ]; + sandbox.stub(DEFAULT_PROPS.TopSites, "rows").value(rows); + wrapper.instance()._dispatchTopSitesStats(); + + assert.calledOnce(DEFAULT_PROPS.dispatch); + assert.calledWithExactly( + DEFAULT_PROPS.dispatch, + ac.AlsoToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { + topsites_icon_stats: { + custom_screenshot: 0, + screenshot: 0, + tippytop: 2, + rich_icon: 0, + no_image: 1, + }, + topsites_pinned: 0, + topsites_search_shortcuts: 0, + }, + }) + ); + }); + it("should correctly count TopSite images - no image", () => { + const rows = [{}]; + sandbox.stub(DEFAULT_PROPS.TopSites, "rows").value(rows); + wrapper.instance()._dispatchTopSitesStats(); + + assert.calledOnce(DEFAULT_PROPS.dispatch); + assert.calledWithExactly( + DEFAULT_PROPS.dispatch, + ac.AlsoToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { + topsites_icon_stats: { + custom_screenshot: 0, + screenshot: 0, + tippytop: 0, + rich_icon: 0, + no_image: 1, + }, + topsites_pinned: 0, + topsites_search_shortcuts: 0, + }, + }) + ); + }); + it("should correctly count pinned Top Sites", () => { + const rows = [ + { isPinned: true }, + { isPinned: false }, + { isPinned: true }, + ]; + sandbox.stub(DEFAULT_PROPS.TopSites, "rows").value(rows); + wrapper.instance()._dispatchTopSitesStats(); + + assert.calledOnce(DEFAULT_PROPS.dispatch); + assert.calledWithExactly( + DEFAULT_PROPS.dispatch, + ac.AlsoToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { + topsites_icon_stats: { + custom_screenshot: 0, + screenshot: 0, + tippytop: 0, + rich_icon: 0, + no_image: 3, + }, + topsites_pinned: 2, + topsites_search_shortcuts: 0, + }, + }) + ); + }); + it("should correctly count search shortcut Top Sites", () => { + const rows = [{ searchTopSite: true }, { searchTopSite: true }]; + sandbox.stub(DEFAULT_PROPS.TopSites, "rows").value(rows); + wrapper.instance()._dispatchTopSitesStats(); + + assert.calledOnce(DEFAULT_PROPS.dispatch); + assert.calledWithExactly( + DEFAULT_PROPS.dispatch, + ac.AlsoToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { + topsites_icon_stats: { + custom_screenshot: 0, + screenshot: 0, + tippytop: 0, + rich_icon: 0, + no_image: 2, + }, + topsites_pinned: 0, + topsites_search_shortcuts: 2, + }, + }) + ); + }); + it("should only count visible top sites on wide layout", () => { + globals.set("matchMedia", () => ({ matches: true })); + const rows = [ + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + ]; + sandbox.stub(DEFAULT_PROPS.TopSites, "rows").value(rows); + + wrapper.instance()._dispatchTopSitesStats(); + assert.calledOnce(DEFAULT_PROPS.dispatch); + assert.calledWithExactly( + DEFAULT_PROPS.dispatch, + ac.AlsoToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { + topsites_icon_stats: { + custom_screenshot: 0, + screenshot: 0, + tippytop: 0, + rich_icon: 0, + no_image: 8, + }, + topsites_pinned: 0, + topsites_search_shortcuts: 0, + }, + }) + ); + }); + it("should only count visible top sites on normal layout", () => { + globals.set("matchMedia", () => ({ matches: false })); + const rows = [ + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + ]; + sandbox.stub(DEFAULT_PROPS.TopSites, "rows").value(rows); + wrapper.instance()._dispatchTopSitesStats(); + assert.calledOnce(DEFAULT_PROPS.dispatch); + assert.calledWithExactly( + DEFAULT_PROPS.dispatch, + ac.AlsoToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { + topsites_icon_stats: { + custom_screenshot: 0, + screenshot: 0, + tippytop: 0, + rich_icon: 0, + no_image: 6, + }, + topsites_pinned: 0, + topsites_search_shortcuts: 0, + }, + }) + ); + }); + }); +}); + +describe("<TopSiteLink>", () => { + let globals; + let link; + let url; + beforeEach(() => { + globals = new GlobalOverrider(); + url = { + createObjectURL: globals.sandbox.stub().returns(DEFAULT_BLOB_URL), + revokeObjectURL: globals.sandbox.spy(), + }; + globals.set("URL", url); + link = { url: "https://foo.com", screenshot: "foo.jpg", hostname: "foo" }; + }); + afterEach(() => globals.restore()); + it("should add the right url", () => { + link.url = "https://www.foobar.org"; + const wrapper = shallow(<TopSiteLink link={link} />); + assert.propertyVal( + wrapper.find("a").props(), + "href", + "https://www.foobar.org" + ); + }); + it("should not add the url to the href if it a search shortcut", () => { + link.searchTopSite = true; + const wrapper = shallow(<TopSiteLink link={link} />); + assert.isUndefined(wrapper.find("a").props().href); + }); + it("should have rtl direction automatically set for text", () => { + const wrapper = shallow(<TopSiteLink link={link} />); + + assert.isTrue(!!wrapper.find("[dir='auto']").length); + }); + it("should render a title", () => { + const wrapper = shallow(<TopSiteLink link={link} title="foobar" />); + const titleEl = wrapper.find(".title"); + + assert.equal(titleEl.text(), "foobar"); + }); + it("should have only the title as the text of the link", () => { + const wrapper = shallow(<TopSiteLink link={link} title="foobar" />); + + assert.equal(wrapper.find("a").text(), "foobar"); + }); + it("should render the pin icon for pinned links", () => { + link.isPinned = true; + link.pinnedIndex = 7; + const wrapper = shallow(<TopSiteLink link={link} />); + assert.equal(wrapper.find(".icon-pin-small").length, 1); + }); + it("should not render the pin icon for non pinned links", () => { + link.isPinned = false; + const wrapper = shallow(<TopSiteLink link={link} />); + assert.equal(wrapper.find(".icon-pin-small").length, 0); + }); + it("should render the first letter of the title as a fallback for missing icons", () => { + const wrapper = shallow(<TopSiteLink link={link} title={"foo"} />); + assert.equal(wrapper.find(".icon-wrapper").prop("data-fallback"), "f"); + }); + it("should render the tippy top icon if provided and not a small icon", () => { + link.tippyTopIcon = "foo.png"; + link.backgroundColor = "#FFFFFF"; + const wrapper = shallow(<TopSiteLink link={link} />); + assert.lengthOf(wrapper.find(".screenshot"), 0); + assert.lengthOf(wrapper.find(".default-icon"), 0); + const tippyTop = wrapper.find(".rich-icon"); + assert.propertyVal( + tippyTop.props().style, + "backgroundImage", + "url(foo.png)" + ); + assert.propertyVal(tippyTop.props().style, "backgroundColor", "#FFFFFF"); + }); + it("should render a rich icon if provided and not a small icon", () => { + link.favicon = "foo.png"; + link.faviconSize = 196; + link.backgroundColor = "#FFFFFF"; + const wrapper = shallow(<TopSiteLink link={link} />); + assert.lengthOf(wrapper.find(".screenshot"), 0); + assert.lengthOf(wrapper.find(".default-icon"), 0); + const richIcon = wrapper.find(".rich-icon"); + assert.propertyVal( + richIcon.props().style, + "backgroundImage", + "url(foo.png)" + ); + assert.propertyVal(richIcon.props().style, "backgroundColor", "#FFFFFF"); + }); + it("should not render a rich icon if it is smaller than 96x96", () => { + link.favicon = "foo.png"; + link.faviconSize = 48; + link.backgroundColor = "#FFFFFF"; + const wrapper = shallow(<TopSiteLink link={link} />); + assert.lengthOf(wrapper.find(".default-icon"), 1); + assert.equal(wrapper.find(".rich-icon").length, 0); + }); + it("should apply just the default class name to the outer link if props.className is falsey", () => { + const wrapper = shallow(<TopSiteLink className={false} />); + assert.ok(wrapper.find("li").hasClass("top-site-outer")); + }); + it("should add props.className to the outer link element", () => { + const wrapper = shallow(<TopSiteLink className="foo bar" />); + assert.ok(wrapper.find("li").hasClass("top-site-outer foo bar")); + }); + describe("#_allowDrop", () => { + let wrapper; + let event; + beforeEach(() => { + event = { + dataTransfer: { + types: ["text/topsite-index"], + }, + }; + wrapper = shallow( + <TopSiteLink isDraggable={true} onDragEvent={() => {}} /> + ); + }); + it("should be droppable for basic case", () => { + const result = wrapper.instance()._allowDrop(event); + assert.isTrue(result); + }); + it("should not be droppable for sponsored_position", () => { + wrapper.setProps({ link: { sponsored_position: 1 } }); + const result = wrapper.instance()._allowDrop(event); + assert.isFalse(result); + }); + it("should not be droppable for link.type", () => { + wrapper.setProps({ link: { type: "SPOC" } }); + const result = wrapper.instance()._allowDrop(event); + assert.isFalse(result); + }); + }); + describe("#onDragEvent", () => { + let simulate; + let wrapper; + beforeEach(() => { + wrapper = shallow( + <TopSiteLink isDraggable={true} onDragEvent={() => {}} /> + ); + simulate = type => { + const event = { + dataTransfer: { setData() {}, types: { includes() {} } }, + preventDefault() { + this.prevented = true; + }, + target: { blur() {} }, + type, + }; + wrapper.simulate(type, event); + return event; + }; + }); + it("should allow clicks without dragging", () => { + simulate("mousedown"); + simulate("mouseup"); + + const event = simulate("click"); + + assert.notOk(event.prevented); + }); + it("should prevent clicks after dragging", () => { + simulate("mousedown"); + simulate("dragstart"); + simulate("dragenter"); + simulate("drop"); + simulate("dragend"); + simulate("mouseup"); + + const event = simulate("click"); + + assert.ok(event.prevented); + }); + it("should allow clicks after dragging then clicking", () => { + simulate("mousedown"); + simulate("dragstart"); + simulate("dragenter"); + simulate("drop"); + simulate("dragend"); + simulate("mouseup"); + simulate("click"); + + simulate("mousedown"); + simulate("mouseup"); + + const event = simulate("click"); + + assert.notOk(event.prevented); + }); + it("should prevent dragging with sponsored_position from dragstart", () => { + const preventDefault = sinon.stub(); + const blur = sinon.stub(); + wrapper.setProps({ link: { sponsored_position: 1 } }); + wrapper.instance().onDragEvent({ + type: "dragstart", + preventDefault, + target: { blur }, + }); + assert.calledOnce(preventDefault); + assert.calledOnce(blur); + assert.isUndefined(wrapper.instance().dragged); + }); + it("should prevent dragging with link.shim from dragstart", () => { + const preventDefault = sinon.stub(); + const blur = sinon.stub(); + wrapper.setProps({ link: { type: "SPOC" } }); + wrapper.instance().onDragEvent({ + type: "dragstart", + preventDefault, + target: { blur }, + }); + assert.calledOnce(preventDefault); + assert.calledOnce(blur); + assert.isUndefined(wrapper.instance().dragged); + }); + }); + + describe("#generateColor", () => { + let colors; + beforeEach(() => { + colors = "#0090ED,#FF4F5F,#2AC3A2"; + }); + + it("should generate a random color but always pick the same color for the same string", async () => { + let wrapper = shallow( + <TopSiteLink colors={colors} title={"food"} link={link} /> + ); + + assert.equal(wrapper.find(".icon-wrapper").prop("data-fallback"), "f"); + assert.equal( + wrapper.find(".icon-wrapper").prop("style").backgroundColor, + colors.split(",")[1] + ); + assert.ok(true); + }); + + it("should generate a different random color", async () => { + let wrapper = shallow( + <TopSiteLink colors={colors} title={"fam"} link={link} /> + ); + + assert.equal( + wrapper.find(".icon-wrapper").prop("style").backgroundColor, + colors.split(",")[2] + ); + assert.ok(true); + }); + + it("should generate a third random color", async () => { + let wrapper = shallow(<TopSiteLink colors={colors} title={"foo"} />); + + assert.equal(wrapper.find(".icon-wrapper").prop("data-fallback"), "f"); + assert.equal( + wrapper.find(".icon-wrapper").prop("style").backgroundColor, + colors.split(",")[0] + ); + assert.ok(true); + }); + }); +}); + +describe("<TopSite>", () => { + let link; + beforeEach(() => { + link = { url: "https://foo.com", screenshot: "foo.jpg", hostname: "foo" }; + }); + + // Build IntersectionObserver class with the arg `entries` for the intersect callback. + function buildIntersectionObserver(entries) { + return class { + constructor(callback) { + this.callback = callback; + } + + observe() { + this.callback(entries); + } + + unobserve() {} + }; + } + + it("should render a TopSite", () => { + const wrapper = shallow(<TopSite link={link} />); + assert.ok(wrapper.exists()); + }); + + it("should render a shortened title based off the url", () => { + link.url = "https://www.foobar.org"; + link.hostname = "foobar"; + link.eTLD = "org"; + const wrapper = shallow(<TopSite link={link} />); + + assert.equal(wrapper.find(TopSiteLink).props().title, "foobar"); + }); + + it("should parse args for fluent correctly", () => { + const title = '"fluent"'; + link.hostname = title; + + const wrapper = mount(<TopSite link={link} />); + const button = wrapper.find( + "button[data-l10n-id='newtab-menu-content-tooltip']" + ); + assert.equal(button.prop("data-l10n-args"), JSON.stringify({ title })); + }); + + it("should have .active class, on top-site-outer if context menu is open", () => { + const wrapper = shallow(<TopSite link={link} index={1} activeIndex={1} />); + wrapper.setState({ showContextMenu: true }); + + assert.equal(wrapper.find(TopSiteLink).props().className.trim(), "active"); + }); + it("should not add .active class, on top-site-outer if context menu is closed", () => { + const wrapper = shallow(<TopSite link={link} index={1} />); + wrapper.setState({ showContextMenu: false, activeTile: 1 }); + assert.equal(wrapper.find(TopSiteLink).props().className, ""); + }); + it("should render a context menu button", () => { + const wrapper = shallow(<TopSite link={link} />); + assert.equal(wrapper.find(ContextMenuButton).length, 1); + }); + it("should render a link menu", () => { + const wrapper = shallow(<TopSite link={link} />); + assert.equal(wrapper.find(LinkMenu).length, 1); + }); + it("should pass onUpdate, site, options, and index to LinkMenu", () => { + const wrapper = shallow(<TopSite link={link} />); + const linkMenuProps = wrapper.find(LinkMenu).props(); + ["onUpdate", "site", "index", "options"].forEach(prop => + assert.property(linkMenuProps, prop) + ); + }); + it("should pass through the correct menu options to LinkMenu", () => { + const wrapper = shallow(<TopSite link={link} />); + const linkMenuProps = wrapper.find(LinkMenu).props(); + assert.deepEqual(linkMenuProps.options, [ + "CheckPinTopSite", + "EditTopSite", + "Separator", + "OpenInNewWindow", + "OpenInPrivateWindow", + "Separator", + "BlockUrl", + "DeleteUrl", + ]); + }); + it("should record impressions for visible organic Top Sites", () => { + const dispatch = sinon.stub(); + const wrapper = shallow( + <TopSite + link={link} + index={3} + dispatch={dispatch} + IntersectionObserver={buildIntersectionObserver([ + { + isIntersecting: true, + intersectionRatio: INTERSECTION_RATIO, + }, + ])} + document={{ + visibilityState: "visible", + addEventListener: sinon.stub(), + removeEventListener: sinon.stub(), + }} + /> + ); + const linkWrapper = wrapper.find(TopSiteLink).dive(); + assert.ok(linkWrapper.exists()); + const impressionWrapper = linkWrapper.find(TopSiteImpressionWrapper).dive(); + assert.ok(impressionWrapper.exists()); + + assert.calledOnce(dispatch); + + let [action] = dispatch.firstCall.args; + assert.equal(action.type, at.TOP_SITES_ORGANIC_IMPRESSION_STATS); + + assert.propertyVal(action.data, "type", "impression"); + assert.propertyVal(action.data, "source", "newtab"); + assert.propertyVal(action.data, "position", 3); + }); + it("should record impressions for visible sponsored Top Sites", () => { + const dispatch = sinon.stub(); + const wrapper = shallow( + <TopSite + link={Object.assign({}, link, { + sponsored_position: 2, + sponsored_tile_id: 12345, + sponsored_impression_url: "http://impression.example.com/", + })} + index={3} + dispatch={dispatch} + IntersectionObserver={buildIntersectionObserver([ + { + isIntersecting: true, + intersectionRatio: INTERSECTION_RATIO, + }, + ])} + document={{ + visibilityState: "visible", + addEventListener: sinon.stub(), + removeEventListener: sinon.stub(), + }} + /> + ); + const linkWrapper = wrapper.find(TopSiteLink).dive(); + assert.ok(linkWrapper.exists()); + const impressionWrapper = linkWrapper.find(TopSiteImpressionWrapper).dive(); + assert.ok(impressionWrapper.exists()); + + assert.calledOnce(dispatch); + + let [action] = dispatch.firstCall.args; + assert.equal(action.type, at.TOP_SITES_SPONSORED_IMPRESSION_STATS); + + assert.propertyVal(action.data, "type", "impression"); + assert.propertyVal(action.data, "tile_id", 12345); + assert.propertyVal(action.data, "source", "newtab"); + assert.propertyVal(action.data, "position", 3); + assert.propertyVal( + action.data, + "reporting_url", + "http://impression.example.com/" + ); + assert.propertyVal(action.data, "advertiser", "foo"); + }); + + describe("#onLinkClick", () => { + it("should call dispatch when the link is clicked", () => { + const dispatch = sinon.stub(); + const wrapper = shallow( + <TopSite link={link} index={3} dispatch={dispatch} /> + ); + + wrapper.find(TopSiteLink).simulate("click", { preventDefault() {} }); + + let [action] = dispatch.firstCall.args; + assert.isUserEventAction(action); + + assert.propertyVal(action.data, "event", "CLICK"); + assert.propertyVal(action.data, "source", "TOP_SITES"); + assert.propertyVal(action.data, "action_position", 3); + + [action] = dispatch.secondCall.args; + assert.propertyVal(action, "type", at.OPEN_LINK); + + // Organic Top Site click event. + [action] = dispatch.thirdCall.args; + assert.equal(action.type, at.TOP_SITES_ORGANIC_IMPRESSION_STATS); + + assert.propertyVal(action.data, "type", "click"); + assert.propertyVal(action.data, "source", "newtab"); + assert.propertyVal(action.data, "position", 3); + }); + it("should dispatch a UserEventAction with the right data", () => { + const dispatch = sinon.stub(); + const wrapper = shallow( + <TopSite + link={Object.assign({}, link, { + iconType: "rich_icon", + isPinned: true, + })} + index={3} + dispatch={dispatch} + /> + ); + + wrapper.find(TopSiteLink).simulate("click", { preventDefault() {} }); + + const [action] = dispatch.firstCall.args; + assert.isUserEventAction(action); + + assert.propertyVal(action.data, "event", "CLICK"); + assert.propertyVal(action.data, "source", "TOP_SITES"); + assert.propertyVal(action.data, "action_position", 3); + assert.propertyVal(action.data.value, "card_type", "pinned"); + assert.propertyVal(action.data.value, "icon_type", "rich_icon"); + }); + it("should dispatch a UserEventAction with the right data for search top site", () => { + const dispatch = sinon.stub(); + const siteInfo = { + iconType: "tippytop", + isPinned: true, + searchTopSite: true, + hostname: "google", + label: "@google", + }; + const wrapper = shallow( + <TopSite + link={Object.assign({}, link, siteInfo)} + index={3} + dispatch={dispatch} + /> + ); + + wrapper.find(TopSiteLink).simulate("click", { preventDefault() {} }); + + const [action] = dispatch.firstCall.args; + assert.isUserEventAction(action); + + assert.propertyVal(action.data, "event", "CLICK"); + assert.propertyVal(action.data, "source", "TOP_SITES"); + assert.propertyVal(action.data, "action_position", 3); + assert.propertyVal(action.data.value, "card_type", "search"); + assert.propertyVal(action.data.value, "icon_type", "tippytop"); + assert.propertyVal(action.data.value, "search_vendor", "google"); + }); + it("should dispatch a UserEventAction with the right data for SPOC top site", () => { + const dispatch = sinon.stub(); + const siteInfo = { + id: 1, + iconType: "custom_screenshot", + type: "SPOC", + pos: 1, + label: "test advertiser", + shim: { click: "shim_click_id" }, + }; + const wrapper = shallow( + <TopSite + link={Object.assign({}, link, siteInfo)} + index={0} + dispatch={dispatch} + /> + ); + + wrapper.find(TopSiteLink).simulate("click", { preventDefault() {} }); + + let [action] = dispatch.firstCall.args; + assert.isUserEventAction(action); + + assert.propertyVal(action.data, "event", "CLICK"); + assert.propertyVal(action.data, "source", "TOP_SITES"); + assert.propertyVal(action.data, "action_position", 0); + assert.propertyVal(action.data.value, "card_type", "spoc"); + assert.propertyVal(action.data.value, "icon_type", "custom_screenshot"); + + // Pocket SPOC click event. + [action] = dispatch.getCall(2).args; + assert.equal(action.type, at.TELEMETRY_IMPRESSION_STATS); + + assert.propertyVal(action.data, "click", 0); + assert.propertyVal(action.data, "source", "TOP_SITES"); + + [action] = dispatch.getCall(3).args; + assert.equal(action.type, at.DISCOVERY_STREAM_USER_EVENT); + + assert.propertyVal(action.data, "event", "CLICK"); + assert.propertyVal(action.data, "action_position", 1); + assert.propertyVal(action.data, "source", "TOP_SITES"); + assert.propertyVal(action.data.value, "card_type", "spoc"); + assert.propertyVal(action.data.value, "tile_id", 1); + assert.propertyVal(action.data.value, "shim", "shim_click_id"); + + // Topsite SPOC click event. + [action] = dispatch.getCall(4).args; + assert.equal(action.type, at.TOP_SITES_SPONSORED_IMPRESSION_STATS); + + assert.propertyVal(action.data, "type", "click"); + assert.propertyVal(action.data, "tile_id", 1); + assert.propertyVal(action.data, "source", "newtab"); + assert.propertyVal(action.data, "position", 1); + assert.propertyVal(action.data, "advertiser", "test advertiser"); + }); + it("should dispatch OPEN_LINK with the right data", () => { + const dispatch = sinon.stub(); + const wrapper = shallow( + <TopSite + link={Object.assign({}, link, { typedBonus: true })} + index={3} + dispatch={dispatch} + /> + ); + + wrapper.find(TopSiteLink).simulate("click", { preventDefault() {} }); + + const [action] = dispatch.secondCall.args; + assert.propertyVal(action, "type", at.OPEN_LINK); + assert.propertyVal(action.data, "typedBonus", true); + }); + }); +}); + +describe("<TopSiteForm>", () => { + let wrapper; + let sandbox; + + function setup(props = {}) { + sandbox = sinon.createSandbox(); + const customProps = Object.assign( + {}, + { onClose: sandbox.spy(), dispatch: sandbox.spy() }, + props + ); + wrapper = mount(<TopSiteForm {...customProps} />); + } + + describe("validateForm", () => { + beforeEach(() => setup({ site: { url: "http://foo" } })); + + it("should return true for a correct URL", () => { + wrapper.setState({ url: "foo" }); + + assert.isTrue(wrapper.instance().validateForm()); + }); + + it("should return false for a incorrect URL", () => { + wrapper.setState({ url: " " }); + + assert.isNull(wrapper.instance().validateForm()); + assert.isTrue(wrapper.state().validationError); + }); + + it("should return true for a correct custom screenshot URL", () => { + wrapper.setState({ customScreenshotUrl: "foo" }); + + assert.isTrue(wrapper.instance().validateForm()); + }); + + it("should return false for a incorrect custom screenshot URL", () => { + wrapper.setState({ customScreenshotUrl: " " }); + + assert.isNull(wrapper.instance().validateForm()); + }); + + it("should return true for an empty custom screenshot URL", () => { + wrapper.setState({ customScreenshotUrl: "" }); + + assert.isTrue(wrapper.instance().validateForm()); + }); + + it("should return false for file: protocol", () => { + wrapper.setState({ customScreenshotUrl: "file:///C:/Users/foo" }); + + assert.isFalse(wrapper.instance().validateForm()); + }); + }); + + describe("#previewButton", () => { + beforeEach(() => + setup({ + site: { customScreenshotURL: "http://foo.com" }, + previewResponse: null, + }) + ); + + it("should render the preview button on invalid urls", () => { + assert.equal(0, wrapper.find(".preview").length); + + wrapper.setState({ customScreenshotUrl: " " }); + + assert.equal(1, wrapper.find(".preview").length); + }); + + it("should render the preview button when input value updated", () => { + assert.equal(0, wrapper.find(".preview").length); + + wrapper.setState({ + customScreenshotUrl: "http://baz.com", + screenshotPreview: null, + }); + + assert.equal(1, wrapper.find(".preview").length); + }); + }); + + describe("preview request", () => { + beforeEach(() => { + setup({ + site: { customScreenshotURL: "http://foo.com", url: "http://foo.com" }, + previewResponse: null, + }); + }); + + it("shouldn't dispatch a request for invalid urls", () => { + wrapper.setState({ customScreenshotUrl: " ", url: "foo" }); + + wrapper.find(".preview").simulate("click"); + + assert.notCalled(wrapper.props().dispatch); + }); + + it("should dispatch a PREVIEW_REQUEST", () => { + wrapper.setState({ customScreenshotUrl: "screenshot" }); + wrapper.find(".preview").simulate("submit"); + + assert.calledTwice(wrapper.props().dispatch); + assert.calledWith( + wrapper.props().dispatch, + ac.AlsoToMain({ + type: at.PREVIEW_REQUEST, + data: { url: "http://screenshot" }, + }) + ); + assert.calledWith( + wrapper.props().dispatch, + ac.UserEvent({ + event: "PREVIEW_REQUEST", + source: "TOP_SITES", + }) + ); + }); + }); + + describe("#TopSiteLink", () => { + beforeEach(() => { + setup(); + }); + + it("should display a TopSiteLink preview", () => { + assert.equal(wrapper.find(TopSiteLink).length, 1); + }); + + it("should display an icon for tippyTop sites", () => { + wrapper.setProps({ site: { tippyTopIcon: "bar" } }); + + assert.equal( + wrapper.find(".top-site-icon").getDOMNode().style["background-image"], + 'url("bar")' + ); + }); + + it("should not display a preview screenshot", () => { + wrapper.setProps({ previewResponse: "foo", previewUrl: "foo" }); + + assert.lengthOf(wrapper.find(".screenshot"), 0); + }); + + it("should not render any icon on error", () => { + wrapper.setProps({ previewResponse: "" }); + + assert.equal(wrapper.find(".top-site-icon").length, 0); + }); + + it("should render the search icon when searchTopSite is true", () => { + wrapper.setProps({ site: { tippyTopIcon: "bar", searchTopSite: true } }); + + assert.equal( + wrapper.find(".rich-icon").getDOMNode().style["background-image"], + 'url("bar")' + ); + assert.isTrue(wrapper.find(".search-topsite").exists()); + }); + }); + + describe("#addMode", () => { + beforeEach(() => setup()); + + it("should render the component", () => { + assert.ok(wrapper.find(TopSiteForm).exists()); + }); + it("should have the correct header", () => { + assert.equal( + wrapper.findWhere( + n => + n.length && + n.prop("data-l10n-id") === "newtab-topsites-add-shortcut-header" + ).length, + 1 + ); + }); + it("should have the correct button text", () => { + assert.equal( + wrapper.findWhere( + n => + n.length && n.prop("data-l10n-id") === "newtab-topsites-save-button" + ).length, + 0 + ); + assert.equal( + wrapper.findWhere( + n => + n.length && n.prop("data-l10n-id") === "newtab-topsites-add-button" + ).length, + 1 + ); + }); + it("should not render a preview button", () => { + assert.equal(0, wrapper.find(".custom-image-input-container").length); + }); + it("should call onClose if Cancel button is clicked", () => { + wrapper.find(".cancel").simulate("click"); + assert.calledOnce(wrapper.instance().props.onClose); + }); + it("should set validationError if url is empty", () => { + assert.equal(wrapper.state().validationError, false); + wrapper.find(".done").simulate("submit"); + assert.equal(wrapper.state().validationError, true); + }); + it("should set validationError if url is invalid", () => { + wrapper.setState({ url: "not valid" }); + assert.equal(wrapper.state().validationError, false); + wrapper.find(".done").simulate("submit"); + assert.equal(wrapper.state().validationError, true); + }); + it("should call onClose and dispatch with right args if URL is valid", () => { + wrapper.setState({ url: "valid.com", label: "a label" }); + wrapper.find(".done").simulate("submit"); + assert.calledOnce(wrapper.instance().props.onClose); + assert.calledWith(wrapper.instance().props.dispatch, { + data: { + site: { label: "a label", url: "http://valid.com" }, + index: -1, + }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: at.TOP_SITES_PIN, + }); + assert.calledWith(wrapper.instance().props.dispatch, { + data: { + action_position: -1, + source: "TOP_SITES", + event: "TOP_SITES_EDIT", + }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: at.TELEMETRY_USER_EVENT, + }); + }); + it("should not pass empty string label in dispatch data", () => { + wrapper.setState({ url: "valid.com", label: "" }); + wrapper.find(".done").simulate("submit"); + assert.calledWith(wrapper.instance().props.dispatch, { + data: { site: { url: "http://valid.com" }, index: -1 }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: at.TOP_SITES_PIN, + }); + }); + it("should open the custom screenshot input", () => { + assert.isFalse(wrapper.state().showCustomScreenshotForm); + + wrapper.find(A11yLinkButton).simulate("click"); + + assert.isTrue(wrapper.state().showCustomScreenshotForm); + }); + }); + + describe("edit existing Topsite", () => { + beforeEach(() => + setup({ + site: { + url: "https://foo.bar", + label: "baz", + customScreenshotURL: "http://foo", + }, + index: 7, + }) + ); + + it("should render the component", () => { + assert.ok(wrapper.find(TopSiteForm).exists()); + }); + it("should have the correct header", () => { + assert.equal( + wrapper.findWhere( + n => n.prop("data-l10n-id") === "newtab-topsites-edit-shortcut-header" + ).length, + 1 + ); + }); + it("should have the correct button text", () => { + assert.equal( + wrapper.findWhere( + n => n.prop("data-l10n-id") === "newtab-topsites-add-button" + ).length, + 0 + ); + assert.equal( + wrapper.findWhere( + n => n.prop("data-l10n-id") === "newtab-topsites-save-button" + ).length, + 1 + ); + }); + it("should call onClose if Cancel button is clicked", () => { + wrapper.find(".cancel").simulate("click"); + assert.calledOnce(wrapper.instance().props.onClose); + }); + it("should show error and not call onClose or dispatch if URL is empty", () => { + wrapper.setState({ url: "" }); + assert.equal(wrapper.state().validationError, false); + wrapper.find(".done").simulate("submit"); + assert.equal(wrapper.state().validationError, true); + assert.notCalled(wrapper.instance().props.onClose); + assert.notCalled(wrapper.instance().props.dispatch); + }); + it("should show error and not call onClose or dispatch if URL is invalid", () => { + wrapper.setState({ url: "not valid" }); + assert.equal(wrapper.state().validationError, false); + wrapper.find(".done").simulate("submit"); + assert.equal(wrapper.state().validationError, true); + assert.notCalled(wrapper.instance().props.onClose); + assert.notCalled(wrapper.instance().props.dispatch); + }); + it("should call onClose and dispatch with right args if URL is valid", () => { + wrapper.find(".done").simulate("submit"); + assert.calledOnce(wrapper.instance().props.onClose); + assert.calledTwice(wrapper.instance().props.dispatch); + assert.calledWith(wrapper.instance().props.dispatch, { + data: { + site: { + label: "baz", + url: "https://foo.bar", + customScreenshotURL: "http://foo", + }, + index: 7, + }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: at.TOP_SITES_PIN, + }); + assert.calledWith(wrapper.instance().props.dispatch, { + data: { + action_position: 7, + source: "TOP_SITES", + event: "TOP_SITES_EDIT", + }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: at.TELEMETRY_USER_EVENT, + }); + }); + it("should set customScreenshotURL to null if it was removed", () => { + wrapper.setState({ customScreenshotUrl: "" }); + + wrapper.find(".done").simulate("submit"); + + assert.calledWith(wrapper.instance().props.dispatch, { + data: { + site: { + label: "baz", + url: "https://foo.bar", + customScreenshotURL: null, + }, + index: 7, + }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: at.TOP_SITES_PIN, + }); + }); + it("should call onClose and dispatch with right args if URL is valid (negative index)", () => { + wrapper.setProps({ index: -1 }); + wrapper.find(".done").simulate("submit"); + assert.calledOnce(wrapper.instance().props.onClose); + assert.calledTwice(wrapper.instance().props.dispatch); + assert.calledWith(wrapper.instance().props.dispatch, { + data: { + site: { + label: "baz", + url: "https://foo.bar", + customScreenshotURL: "http://foo", + }, + index: -1, + }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: at.TOP_SITES_PIN, + }); + }); + it("should not pass empty string label in dispatch data", () => { + wrapper.setState({ label: "" }); + wrapper.find(".done").simulate("submit"); + assert.calledWith(wrapper.instance().props.dispatch, { + data: { + site: { url: "https://foo.bar", customScreenshotURL: "http://foo" }, + index: 7, + }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: at.TOP_SITES_PIN, + }); + }); + it("should render the save button if custom screenshot request finished", () => { + wrapper.setState({ + customScreenshotUrl: "foo", + screenshotPreview: "custom", + }); + assert.equal(0, wrapper.find(".preview").length); + assert.equal(1, wrapper.find(".done").length); + }); + it("should render the save button if custom screenshot url was cleared", () => { + wrapper.setState({ customScreenshotUrl: "" }); + wrapper.setProps({ site: { customScreenshotURL: "foo" } }); + assert.equal(0, wrapper.find(".preview").length); + assert.equal(1, wrapper.find(".done").length); + }); + }); + + describe("#previewMode", () => { + beforeEach(() => setup({ previewResponse: null })); + + it("should transition from save to preview", () => { + wrapper.setProps({ + site: { url: "https://foo.bar", customScreenshotURL: "baz" }, + index: 7, + }); + + assert.equal( + wrapper.findWhere( + n => + n.length && n.prop("data-l10n-id") === "newtab-topsites-save-button" + ).length, + 1 + ); + + wrapper.setState({ customScreenshotUrl: "foo" }); + + assert.equal( + wrapper.findWhere( + n => + n.length && + n.prop("data-l10n-id") === "newtab-topsites-preview-button" + ).length, + 1 + ); + }); + + it("should transition from add to preview", () => { + assert.equal( + wrapper.findWhere( + n => + n.length && n.prop("data-l10n-id") === "newtab-topsites-add-button" + ).length, + 1 + ); + + wrapper.setState({ customScreenshotUrl: "foo" }); + + assert.equal( + wrapper.findWhere( + n => + n.length && + n.prop("data-l10n-id") === "newtab-topsites-preview-button" + ).length, + 1 + ); + }); + }); + + describe("#validateUrl", () => { + it("should properly validate URLs", () => { + setup(); + assert.ok(wrapper.instance().validateUrl("mozilla.org")); + assert.ok(wrapper.instance().validateUrl("https://mozilla.org")); + assert.ok(wrapper.instance().validateUrl("http://mozilla.org")); + assert.ok( + wrapper + .instance() + .validateUrl( + "https://mozilla.invisionapp.com/d/main/#/projects/prototypes" + ) + ); + assert.ok(wrapper.instance().validateUrl("httpfoobar")); + assert.ok(wrapper.instance().validateUrl("httpsfoo.bar")); + assert.isNull(wrapper.instance().validateUrl("mozilla org")); + assert.isNull(wrapper.instance().validateUrl("")); + }); + }); + + describe("#cleanUrl", () => { + it("should properly prepend http:// to URLs when required", () => { + setup(); + assert.equal( + "http://mozilla.org", + wrapper.instance().cleanUrl("mozilla.org") + ); + assert.equal( + "http://https.org", + wrapper.instance().cleanUrl("https.org") + ); + assert.equal("http://httpcom", wrapper.instance().cleanUrl("httpcom")); + assert.equal( + "http://mozilla.org", + wrapper.instance().cleanUrl("http://mozilla.org") + ); + assert.equal( + "https://firefox.com", + wrapper.instance().cleanUrl("https://firefox.com") + ); + }); + }); +}); + +describe("<TopSiteList>", () => { + const APP = { isForStartupCache: false }; + + it("should render a TopSiteList element", () => { + const wrapper = shallow(<TopSiteList {...DEFAULT_PROPS} App={{ APP }} />); + assert.ok(wrapper.exists()); + }); + it("should render a TopSite for each link with the right url", () => { + const rows = [{ url: "https://foo.com" }, { url: "https://bar.com" }]; + const wrapper = shallow( + <TopSiteList {...DEFAULT_PROPS} TopSites={{ rows }} App={{ APP }} /> + ); + const links = wrapper.find(TopSite); + assert.lengthOf(links, 2); + rows.forEach((row, i) => + assert.equal(links.get(i).props.link.url, row.url) + ); + }); + it("should slice the TopSite rows to the TopSitesRows pref", () => { + const rows = []; + for ( + let i = 0; + i < TOP_SITES_DEFAULT_ROWS * TOP_SITES_MAX_SITES_PER_ROW + 3; + i++ + ) { + rows.push({ url: `https://foo${i}.com` }); + } + const wrapper = shallow( + <TopSiteList + {...DEFAULT_PROPS} + TopSites={{ rows }} + TopSitesRows={TOP_SITES_DEFAULT_ROWS} + App={{ APP }} + /> + ); + const links = wrapper.find(TopSite); + assert.lengthOf( + links, + TOP_SITES_DEFAULT_ROWS * TOP_SITES_MAX_SITES_PER_ROW + ); + }); + it("should fill with placeholders if TopSites rows is less than TopSitesRows", () => { + const rows = [{ url: "https://foo.com" }, { url: "https://bar.com" }]; + const wrapper = shallow( + <TopSiteList + {...DEFAULT_PROPS} + TopSites={{ rows }} + TopSitesRows={1} + App={{ APP }} + /> + ); + assert.lengthOf(wrapper.find(TopSite), 2, "topSites"); + assert.lengthOf( + wrapper.find(TopSitePlaceholder), + TOP_SITES_MAX_SITES_PER_ROW - 2, + "placeholders" + ); + }); + it("should fill sponsored top sites with placeholders while rendering for startup cache", () => { + const rows = [ + { url: "https://sponsored01.com", sponsored_position: 1 }, + { url: "https://sponsored02.com", sponsored_position: 2 }, + { url: "https://sponsored03.com", type: "SPOC" }, + { url: "https://foo.com" }, + { url: "https://bar.com" }, + ]; + const wrapper = shallow( + <TopSiteList + {...DEFAULT_PROPS} + TopSites={{ rows }} + TopSitesRows={1} + App={{ isForStartupCache: true }} + /> + ); + assert.lengthOf(wrapper.find(TopSite), 2, "topSites"); + assert.lengthOf( + wrapper.find(TopSitePlaceholder), + TOP_SITES_MAX_SITES_PER_ROW - 2, + "placeholders" + ); + }); + it("should fill any holes in TopSites with placeholders", () => { + const rows = [{ url: "https://foo.com" }]; + rows[3] = { url: "https://bar.com" }; + const wrapper = shallow( + <TopSiteList + {...DEFAULT_PROPS} + TopSites={{ rows }} + TopSitesRows={1} + App={{ APP }} + /> + ); + assert.lengthOf(wrapper.find(TopSite), 2, "topSites"); + assert.lengthOf( + wrapper.find(TopSitePlaceholder), + TOP_SITES_MAX_SITES_PER_ROW - 2, + "placeholders" + ); + }); + it("should update state onDragStart and clear it onDragEnd", () => { + const wrapper = shallow(<TopSiteList {...DEFAULT_PROPS} App={{ APP }} />); + const instance = wrapper.instance(); + const index = 7; + const link = { url: "https://foo.com" }; + const title = "foo"; + instance.onDragEvent({ type: "dragstart" }, index, link, title); + assert.equal(instance.state.draggedIndex, index); + assert.equal(instance.state.draggedSite, link); + assert.equal(instance.state.draggedTitle, title); + instance.onDragEvent({ type: "dragend" }); + assert.deepEqual(instance.state, TopSiteList.DEFAULT_STATE); + }); + it("should clear state when new props arrive after a drop", () => { + const site1 = { url: "https://foo.com" }; + const site2 = { url: "https://bar.com" }; + const rows = [site1, site2]; + const wrapper = shallow( + <TopSiteList {...DEFAULT_PROPS} TopSites={{ rows }} App={{ APP }} /> + ); + const instance = wrapper.instance(); + instance.setState({ + draggedIndex: 1, + draggedSite: site2, + draggedTitle: "bar", + topSitesPreview: [], + }); + wrapper.setProps({ TopSites: { rows: [site2, site1] } }); + assert.deepEqual(instance.state, TopSiteList.DEFAULT_STATE); + }); + it("should dispatch events on drop", () => { + const dispatch = sinon.spy(); + const wrapper = shallow( + <TopSiteList {...DEFAULT_PROPS} dispatch={dispatch} App={{ APP }} /> + ); + const instance = wrapper.instance(); + const index = 7; + const link = { url: "https://foo.com", customScreenshotURL: "foo" }; + const title = "foo"; + instance.onDragEvent({ type: "dragstart" }, index, link, title); + dispatch.resetHistory(); + instance.onDragEvent({ type: "drop" }, 3); + assert.calledTwice(dispatch); + assert.calledWith(dispatch, { + data: { + draggedFromIndex: 7, + index: 3, + site: { + label: "foo", + url: "https://foo.com", + customScreenshotURL: "foo", + }, + }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: "TOP_SITES_INSERT", + }); + assert.calledWith(dispatch, { + data: { action_position: 3, event: "DROP", source: "TOP_SITES" }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: "TELEMETRY_USER_EVENT", + }); + }); + it("should make a topSitesPreview onDragEnter", () => { + const wrapper = shallow(<TopSiteList {...DEFAULT_PROPS} App={{ APP }} />); + const instance = wrapper.instance(); + const site = { url: "https://foo.com" }; + instance.setState({ + draggedIndex: 4, + draggedSite: site, + draggedTitle: "foo", + }); + const draggedSite = Object.assign({}, site, { + isPinned: true, + isDragged: true, + }); + instance.onDragEvent({ type: "dragenter" }, 2); + assert.ok(instance.state.topSitesPreview); + assert.deepEqual(instance.state.topSitesPreview[2], draggedSite); + }); + it("should _makeTopSitesPreview correctly", () => { + const site1 = { url: "https://foo.com" }; + const site2 = { url: "https://bar.com" }; + const site3 = { url: "https://baz.com" }; + const rows = [site1, site2, site3]; + let wrapper = shallow( + <TopSiteList + {...DEFAULT_PROPS} + TopSites={{ rows }} + TopSitesRows={1} + App={{ APP }} + /> + ); + let instance = wrapper.instance(); + instance.setState({ + draggedIndex: 0, + draggedSite: site1, + draggedTitle: "foo", + }); + let draggedSite = Object.assign({}, site1, { + isPinned: true, + isDragged: true, + }); + assert.deepEqual(instance._makeTopSitesPreview(1), [ + site2, + draggedSite, + site3, + null, + null, + null, + null, + null, + ]); + assert.deepEqual(instance._makeTopSitesPreview(2), [ + site2, + site3, + draggedSite, + null, + null, + null, + null, + null, + ]); + assert.deepEqual(instance._makeTopSitesPreview(3), [ + site2, + site3, + null, + draggedSite, + null, + null, + null, + null, + ]); + site2.isPinned = true; + assert.deepEqual(instance._makeTopSitesPreview(1), [ + site2, + draggedSite, + site3, + null, + null, + null, + null, + null, + ]); + assert.deepEqual(instance._makeTopSitesPreview(2), [ + site3, + site2, + draggedSite, + null, + null, + null, + null, + null, + ]); + site3.isPinned = true; + assert.deepEqual(instance._makeTopSitesPreview(1), [ + site2, + draggedSite, + site3, + null, + null, + null, + null, + null, + ]); + assert.deepEqual(instance._makeTopSitesPreview(2), [ + site2, + site3, + draggedSite, + null, + null, + null, + null, + null, + ]); + site2.isPinned = false; + assert.deepEqual(instance._makeTopSitesPreview(1), [ + site2, + draggedSite, + site3, + null, + null, + null, + null, + null, + ]); + assert.deepEqual(instance._makeTopSitesPreview(2), [ + site2, + site3, + draggedSite, + null, + null, + null, + null, + null, + ]); + site3.isPinned = false; + instance.setState({ + draggedIndex: 1, + draggedSite: site2, + draggedTitle: "bar", + }); + draggedSite = Object.assign({}, site2, { isPinned: true, isDragged: true }); + assert.deepEqual(instance._makeTopSitesPreview(0), [ + draggedSite, + site1, + site3, + null, + null, + null, + null, + null, + ]); + assert.deepEqual(instance._makeTopSitesPreview(2), [ + site1, + site3, + draggedSite, + null, + null, + null, + null, + null, + ]); + site2.type = "SPOC"; + instance.setState({ + draggedIndex: 2, + draggedSite: site3, + draggedTitle: "baz", + }); + draggedSite = Object.assign({}, site3, { isPinned: true, isDragged: true }); + assert.deepEqual(instance._makeTopSitesPreview(0), [ + draggedSite, + site2, + site1, + null, + null, + null, + null, + null, + ]); + site2.type = ""; + site2.sponsored_position = 2; + instance.setState({ + draggedIndex: 2, + draggedSite: site3, + draggedTitle: "baz", + }); + draggedSite = Object.assign({}, site3, { isPinned: true, isDragged: true }); + assert.deepEqual(instance._makeTopSitesPreview(0), [ + draggedSite, + site2, + site1, + null, + null, + null, + null, + null, + ]); + }); + it("should add a className hide-for-narrow to sites after 6/row", () => { + const rows = []; + for (let i = 0; i < TOP_SITES_MAX_SITES_PER_ROW; i++) { + rows.push({ url: `https://foo${i}.com` }); + } + const wrapper = mount( + <TopSiteList + {...DEFAULT_PROPS} + TopSites={{ rows }} + TopSitesRows={1} + App={{ APP }} + /> + ); + assert.lengthOf(wrapper.find("li.hide-for-narrow"), 2); + }); +}); + +describe("TopSitePlaceholder", () => { + it("should dispatch a TOP_SITES_EDIT action when edit-button is clicked", () => { + const dispatch = sinon.spy(); + const wrapper = shallow( + <TopSitePlaceholder dispatch={dispatch} index={7} /> + ); + + wrapper.find(".edit-button").first().simulate("click"); + + assert.calledOnce(dispatch); + assert.calledWithExactly(dispatch, { + type: at.TOP_SITES_EDIT, + data: { index: 7 }, + }); + }); +}); + +describe("#TopSiteFormInput", () => { + let wrapper; + let onChangeStub; + + describe("no errors", () => { + beforeEach(() => { + onChangeStub = sinon.stub(); + + wrapper = mount( + <TopSiteFormInput + titleId="newtab-topsites-title-label" + placeholderId="newtab-topsites-title-input" + errorMessageId="newtab-topsites-url-validation" + onChange={onChangeStub} + value="foo" + /> + ); + }); + + it("should render the provided title", () => { + const title = wrapper.find("span"); + assert.propertyVal( + title.props(), + "data-l10n-id", + "newtab-topsites-title-label" + ); + }); + + it("should render the provided value", () => { + const input = wrapper.find("input"); + + assert.equal(input.getDOMNode().value, "foo"); + }); + + it("should render the clear button if cb is provided", () => { + assert.equal(wrapper.find(".icon-clear-input").length, 0); + + wrapper.setProps({ onClear: sinon.stub() }); + + assert.equal(wrapper.find(".icon-clear-input").length, 1); + }); + + it("should show the loading indicator", () => { + assert.equal(wrapper.find(".loading-container").length, 0); + + wrapper.setProps({ loading: true }); + + assert.equal(wrapper.find(".loading-container").length, 1); + }); + it("should disable the input when loading indicator is present", () => { + assert.isFalse(wrapper.find("input").getDOMNode().disabled); + + wrapper.setProps({ loading: true }); + + assert.isTrue(wrapper.find("input").getDOMNode().disabled); + }); + }); + + describe("with error", () => { + beforeEach(() => { + onChangeStub = sinon.stub(); + + wrapper = mount( + <TopSiteFormInput + titleId="newtab-topsites-title-label" + placeholderId="newtab-topsites-title-input" + onChange={onChangeStub} + validationError={true} + errorMessageId="newtab-topsites-url-validation" + value="foo" + /> + ); + }); + + it("should render the error message", () => { + assert.equal( + wrapper.findWhere( + n => n.prop("data-l10n-id") === "newtab-topsites-url-validation" + ).length, + 1 + ); + }); + + it("should reset the error state on value change", () => { + wrapper.find("input").simulate("change", { target: { value: "bar" } }); + + assert.isFalse(wrapper.state().validationError); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/TopSites/SearchShortcutsForm.test.jsx b/browser/components/newtab/test/unit/content-src/components/TopSites/SearchShortcutsForm.test.jsx new file mode 100644 index 0000000000..22c4e8192a --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/TopSites/SearchShortcutsForm.test.jsx @@ -0,0 +1,56 @@ +import { + SearchShortcutsForm, + SelectableSearchShortcut, +} from "content-src/components/TopSites/SearchShortcutsForm"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<SearchShortcutsForm>", () => { + let wrapper; + let sandbox; + let dispatchStub; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + dispatchStub = sandbox.stub(); + const defaultProps = { rows: [], searchShortcuts: [] }; + wrapper = shallow( + <SearchShortcutsForm TopSites={defaultProps} dispatch={dispatchStub} /> + ); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should render", () => { + assert.ok(wrapper.exists()); + assert.ok(wrapper.find(".topsite-form").exists()); + }); + + it("should render SelectableSearchShortcut components", () => { + wrapper.setState({ shortcuts: [{}, {}] }); + + assert.lengthOf( + wrapper.find(".search-shortcuts-container div").children(), + 2 + ); + assert.equal( + wrapper.find(".search-shortcuts-container div").children().at(0).type(), + SelectableSearchShortcut + ); + }); + + it("should render SelectableSearchShortcut components", () => { + const onCloseStub = sandbox.stub(); + const fakeEvent = { preventDefault: sandbox.stub() }; + wrapper.setState({ shortcuts: [{}, {}] }); + wrapper.setProps({ onClose: onCloseStub }); + + wrapper.find(".done").simulate("click", fakeEvent); + + assert.calledOnce(dispatchStub); + assert.calledOnce(fakeEvent.preventDefault); + assert.calledOnce(onCloseStub); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/TopSites/TopSiteImpressionWrapper.test.jsx b/browser/components/newtab/test/unit/content-src/components/TopSites/TopSiteImpressionWrapper.test.jsx new file mode 100644 index 0000000000..3f7e725de0 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/TopSites/TopSiteImpressionWrapper.test.jsx @@ -0,0 +1,148 @@ +import { + TopSiteImpressionWrapper, + INTERSECTION_RATIO, +} from "content-src/components/TopSites/TopSiteImpressionWrapper"; +import { actionTypes as at } from "common/Actions.sys.mjs"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<TopSiteImpressionWrapper>", () => { + const FullIntersectEntries = [ + { isIntersecting: true, intersectionRatio: INTERSECTION_RATIO }, + ]; + const ZeroIntersectEntries = [ + { isIntersecting: false, intersectionRatio: 0 }, + ]; + const PartialIntersectEntries = [ + { isIntersecting: true, intersectionRatio: INTERSECTION_RATIO / 2 }, + ]; + + // Build IntersectionObserver class with the arg `entries` for the intersect callback. + function buildIntersectionObserver(entries) { + return class { + constructor(callback) { + this.callback = callback; + } + + observe() { + this.callback(entries); + } + + unobserve() {} + }; + } + + const DEFAULT_PROPS = { + actionType: at.TOP_SITES_SPONSORED_IMPRESSION_STATS, + tile: { + tile_id: 1, + position: 1, + reporting_url: "https://test.reporting.com", + advertiser: "test_advertiser", + }, + IntersectionObserver: buildIntersectionObserver(FullIntersectEntries), + document: { + visibilityState: "visible", + addEventListener: sinon.stub(), + removeEventListener: sinon.stub(), + }, + }; + + const InnerEl = () => <div>Inner Element</div>; + + function renderTopSiteImpressionWrapper(props = {}) { + return shallow( + <TopSiteImpressionWrapper {...DEFAULT_PROPS} {...props}> + <InnerEl /> + </TopSiteImpressionWrapper> + ); + } + + it("should render props.children", () => { + const wrapper = renderTopSiteImpressionWrapper(); + assert.ok(wrapper.contains(<InnerEl />)); + }); + it("should not send impression when the wrapped item is visbible but below the ratio", () => { + const dispatch = sinon.spy(); + const props = { + dispatch, + IntersectionObserver: buildIntersectionObserver(PartialIntersectEntries), + }; + renderTopSiteImpressionWrapper(props); + + assert.notCalled(dispatch); + }); + it("should send an impression when the page is visible and the wrapped item meets the visibility ratio", () => { + const dispatch = sinon.spy(); + const props = { + dispatch, + IntersectionObserver: buildIntersectionObserver(FullIntersectEntries), + }; + renderTopSiteImpressionWrapper(props); + + assert.calledOnce(dispatch); + + let [action] = dispatch.firstCall.args; + assert.equal(action.type, at.TOP_SITES_SPONSORED_IMPRESSION_STATS); + assert.deepEqual(action.data, { + type: "impression", + ...DEFAULT_PROPS.tile, + }); + }); + it("should send an impression when the wrapped item transiting from invisible to visible", () => { + const dispatch = sinon.spy(); + const props = { + dispatch, + IntersectionObserver: buildIntersectionObserver(ZeroIntersectEntries), + }; + const wrapper = renderTopSiteImpressionWrapper(props); + + assert.notCalled(dispatch); + + dispatch.resetHistory(); + wrapper.instance().impressionObserver.callback(FullIntersectEntries); + + // For the impression + assert.calledOnce(dispatch); + + const [action] = dispatch.firstCall.args; + assert.equal(action.type, at.TOP_SITES_SPONSORED_IMPRESSION_STATS); + assert.deepEqual(action.data, { + type: "impression", + ...DEFAULT_PROPS.tile, + }); + }); + it("should remove visibility change listener when the wrapper is removed", () => { + const props = { + dispatch: sinon.spy(), + document: { + visibilityState: "hidden", + addEventListener: sinon.spy(), + removeEventListener: sinon.spy(), + }, + IntersectionObserver, + }; + + const wrapper = renderTopSiteImpressionWrapper(props); + assert.calledWith(props.document.addEventListener, "visibilitychange"); + const [, listener] = props.document.addEventListener.firstCall.args; + + wrapper.unmount(); + assert.calledWith( + props.document.removeEventListener, + "visibilitychange", + listener + ); + }); + it("should unobserve the intersection observer when the wrapper is removed", () => { + const IntersectionObserver = + buildIntersectionObserver(ZeroIntersectEntries); + const spy = sinon.spy(IntersectionObserver.prototype, "unobserve"); + const props = { dispatch: sinon.spy(), IntersectionObserver }; + + const wrapper = renderTopSiteImpressionWrapper(props); + wrapper.unmount(); + + assert.calledOnce(spy); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/components/Topics.test.jsx b/browser/components/newtab/test/unit/content-src/components/Topics.test.jsx new file mode 100644 index 0000000000..91d15c5d4e --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/components/Topics.test.jsx @@ -0,0 +1,22 @@ +import { Topic, Topics } from "content-src/components/Topics/Topics"; +import React from "react"; +import { shallow } from "enzyme"; + +describe("<Topics>", () => { + it("should render a Topics element", () => { + const wrapper = shallow(<Topics topics={[]} />); + assert.ok(wrapper.exists()); + }); + it("should render a Topic element for each topic with the right url", () => { + const data = [ + { name: "topic1", url: "https://topic1.com" }, + { name: "topic2", url: "https://topic2.com" }, + ]; + + const wrapper = shallow(<Topics topics={data} />); + + const topics = wrapper.find(Topic); + assert.lengthOf(topics, 2); + topics.forEach((topic, i) => assert.equal(topic.props().url, data[i].url)); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/lib/detect-user-session-start.test.js b/browser/components/newtab/test/unit/content-src/lib/detect-user-session-start.test.js new file mode 100644 index 0000000000..5a7fad7cc0 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/lib/detect-user-session-start.test.js @@ -0,0 +1,120 @@ +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { DetectUserSessionStart } from "content-src/lib/detect-user-session-start"; + +describe("detectUserSessionStart", () => { + let store; + class PerfService { + getMostRecentAbsMarkStartByName() { + return 1234; + } + mark() {} + } + + beforeEach(() => { + store = { dispatch: () => {} }; + }); + describe("#sendEventOrAddListener", () => { + it("should call ._sendEvent immediately if the document is visible", () => { + const mockDocument = { visibilityState: "visible" }; + const instance = new DetectUserSessionStart(store, { + document: mockDocument, + }); + sinon.stub(instance, "_sendEvent"); + + instance.sendEventOrAddListener(); + + assert.calledOnce(instance._sendEvent); + }); + it("should add an event listener on visibility changes the document is not visible", () => { + const mockDocument = { + visibilityState: "hidden", + addEventListener: sinon.spy(), + }; + const instance = new DetectUserSessionStart(store, { + document: mockDocument, + }); + sinon.stub(instance, "_sendEvent"); + + instance.sendEventOrAddListener(); + + assert.notCalled(instance._sendEvent); + assert.calledWith( + mockDocument.addEventListener, + "visibilitychange", + instance._onVisibilityChange + ); + }); + }); + describe("#_sendEvent", () => { + it("should dispatch an action with the SAVE_SESSION_PERF_DATA", () => { + const dispatch = sinon.spy(store, "dispatch"); + const instance = new DetectUserSessionStart(store); + + instance._sendEvent(); + + assert.calledWith( + dispatch, + ac.AlsoToMain({ + type: at.SAVE_SESSION_PERF_DATA, + data: { visibility_event_rcvd_ts: sinon.match.number }, + }) + ); + }); + + it("shouldn't send a message if getMostRecentAbsMarkStartByName throws", () => { + let perfService = new PerfService(); + sinon.stub(perfService, "getMostRecentAbsMarkStartByName").throws(); + const dispatch = sinon.spy(store, "dispatch"); + const instance = new DetectUserSessionStart(store, { perfService }); + + instance._sendEvent(); + + assert.notCalled(dispatch); + }); + + it('should call perfService.mark("visibility_event_rcvd_ts")', () => { + let perfService = new PerfService(); + sinon.stub(perfService, "mark"); + const instance = new DetectUserSessionStart(store, { perfService }); + + instance._sendEvent(); + + assert.calledWith(perfService.mark, "visibility_event_rcvd_ts"); + }); + }); + + describe("_onVisibilityChange", () => { + it("should not send an event if visiblity is not visible", () => { + const instance = new DetectUserSessionStart(store, { + document: { visibilityState: "hidden" }, + }); + sinon.stub(instance, "_sendEvent"); + + instance._onVisibilityChange(); + + assert.notCalled(instance._sendEvent); + }); + it("should send an event and remove the event listener if visibility is visible", () => { + const mockDocument = { + visibilityState: "visible", + removeEventListener: sinon.spy(), + }; + const instance = new DetectUserSessionStart(store, { + document: mockDocument, + }); + sinon.stub(instance, "_sendEvent"); + + instance._onVisibilityChange(); + + assert.calledOnce(instance._sendEvent); + assert.calledWith( + mockDocument.removeEventListener, + "visibilitychange", + instance._onVisibilityChange + ); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/lib/init-store.test.js b/browser/components/newtab/test/unit/content-src/lib/init-store.test.js new file mode 100644 index 0000000000..0dd510ef1a --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/lib/init-store.test.js @@ -0,0 +1,155 @@ +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { addNumberReducer, GlobalOverrider } from "test/unit/utils"; +import { + INCOMING_MESSAGE_NAME, + initStore, + MERGE_STORE_ACTION, + OUTGOING_MESSAGE_NAME, + rehydrationMiddleware, +} from "content-src/lib/init-store"; + +describe("initStore", () => { + let globals; + let store; + beforeEach(() => { + globals = new GlobalOverrider(); + globals.set("RPMSendAsyncMessage", globals.sandbox.spy()); + globals.set("RPMAddMessageListener", globals.sandbox.spy()); + store = initStore({ number: addNumberReducer }); + }); + afterEach(() => globals.restore()); + it("should create a store with the provided reducers", () => { + assert.ok(store); + assert.property(store.getState(), "number"); + }); + it("should add a listener that dispatches actions", () => { + assert.calledWith(global.RPMAddMessageListener, INCOMING_MESSAGE_NAME); + const [, listener] = global.RPMAddMessageListener.firstCall.args; + globals.sandbox.spy(store, "dispatch"); + const message = { name: INCOMING_MESSAGE_NAME, data: { type: "FOO" } }; + + listener(message); + + assert.calledWith(store.dispatch, message.data); + }); + it("should not throw if RPMAddMessageListener is not defined", () => { + // Note: this is being set/restored by GlobalOverrider + delete global.RPMAddMessageListener; + + assert.doesNotThrow(() => initStore({ number: addNumberReducer })); + }); + it("should log errors from failed messages", () => { + const [, callback] = global.RPMAddMessageListener.firstCall.args; + globals.sandbox.stub(global.console, "error"); + globals.sandbox.stub(store, "dispatch").throws(Error("failed")); + + const message = { + name: INCOMING_MESSAGE_NAME, + data: { type: MERGE_STORE_ACTION }, + }; + callback(message); + + assert.calledOnce(global.console.error); + }); + it("should replace the state if a MERGE_STORE_ACTION is dispatched", () => { + store.dispatch({ type: MERGE_STORE_ACTION, data: { number: 42 } }); + assert.deepEqual(store.getState(), { number: 42 }); + }); + it("should call .send and update the local store if an AlsoToMain action is dispatched", () => { + const subscriber = sinon.spy(); + const action = ac.AlsoToMain({ type: "FOO" }); + + store.subscribe(subscriber); + store.dispatch(action); + + assert.calledWith( + global.RPMSendAsyncMessage, + OUTGOING_MESSAGE_NAME, + action + ); + assert.calledOnce(subscriber); + }); + it("should call .send but not update the local store if an OnlyToMain action is dispatched", () => { + const subscriber = sinon.spy(); + const action = ac.OnlyToMain({ type: "FOO" }); + + store.subscribe(subscriber); + store.dispatch(action); + + assert.calledWith( + global.RPMSendAsyncMessage, + OUTGOING_MESSAGE_NAME, + action + ); + assert.notCalled(subscriber); + }); + it("should not send out other types of actions", () => { + store.dispatch({ type: "FOO" }); + assert.notCalled(global.RPMSendAsyncMessage); + }); + describe("rehydrationMiddleware", () => { + it("should allow NEW_TAB_STATE_REQUEST to go through", () => { + const action = ac.AlsoToMain({ type: at.NEW_TAB_STATE_REQUEST }); + const next = sinon.spy(); + rehydrationMiddleware(store)(next)(action); + assert.calledWith(next, action); + }); + it("should dispatch an additional NEW_TAB_STATE_REQUEST if INIT was received after a request", () => { + const requestAction = ac.AlsoToMain({ type: at.NEW_TAB_STATE_REQUEST }); + const next = sinon.spy(); + const dispatch = rehydrationMiddleware(store)(next); + + dispatch(requestAction); + next.resetHistory(); + dispatch({ type: at.INIT }); + + assert.calledWith(next, requestAction); + }); + it("should allow MERGE_STORE_ACTION to go through", () => { + const action = { type: MERGE_STORE_ACTION }; + const next = sinon.spy(); + rehydrationMiddleware(store)(next)(action); + assert.calledWith(next, action); + }); + it("should not allow actions from main to go through before MERGE_STORE_ACTION was received", () => { + const next = sinon.spy(); + const dispatch = rehydrationMiddleware(store)(next); + + dispatch(ac.BroadcastToContent({ type: "FOO" })); + dispatch(ac.AlsoToOneContent({ type: "FOO" }, 123)); + + assert.notCalled(next); + }); + it("should allow all local actions to go through", () => { + const action = { type: "FOO" }; + const next = sinon.spy(); + rehydrationMiddleware(store)(next)(action); + assert.calledWith(next, action); + }); + it("should allow actions from main to go through after MERGE_STORE_ACTION has been received", () => { + const next = sinon.spy(); + const dispatch = rehydrationMiddleware(store)(next); + + dispatch({ type: MERGE_STORE_ACTION }); + next.resetHistory(); + + const action = ac.AlsoToOneContent({ type: "FOO" }, 123); + dispatch(action); + assert.calledWith(next, action); + }); + it("should not let startup actions go through for the preloaded about:home document", () => { + globals.set("__FROM_STARTUP_CACHE__", true); + const next = sinon.spy(); + const dispatch = rehydrationMiddleware(store)(next); + const action = ac.BroadcastToContent( + { type: "FOO", meta: { isStartup: true } }, + 123 + ); + dispatch(action); + assert.notCalled(next); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/lib/perf-service.test.js b/browser/components/newtab/test/unit/content-src/lib/perf-service.test.js new file mode 100644 index 0000000000..9cabfb5029 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/lib/perf-service.test.js @@ -0,0 +1,89 @@ +/* globals assert, beforeEach, describe, it */ +import { _PerfService } from "content-src/lib/perf-service"; +import { FakePerformance } from "test/unit/utils.js"; + +let perfService; + +describe("_PerfService", () => { + let sandbox; + let fakePerfObj; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + fakePerfObj = new FakePerformance(); + perfService = new _PerfService({ performanceObj: fakePerfObj }); + }); + + afterEach(() => { + sandbox.restore(); + }); + + describe("#absNow", () => { + it("should return a number > the time origin", () => { + const absNow = perfService.absNow(); + + assert.isAbove(absNow, perfService.timeOrigin); + }); + }); + describe("#getEntriesByName", () => { + it("should call getEntriesByName on the appropriate Window.performance", () => { + sandbox.spy(fakePerfObj, "getEntriesByName"); + + perfService.getEntriesByName("monkey", "mark"); + + assert.calledOnce(fakePerfObj.getEntriesByName); + assert.calledWithExactly(fakePerfObj.getEntriesByName, "monkey", "mark"); + }); + + it("should return entries with the given name", () => { + sandbox.spy(fakePerfObj, "getEntriesByName"); + perfService.mark("monkey"); + perfService.mark("dog"); + + let marks = perfService.getEntriesByName("monkey", "mark"); + + assert.isArray(marks); + assert.lengthOf(marks, 1); + assert.propertyVal(marks[0], "name", "monkey"); + }); + }); + + describe("#getMostRecentAbsMarkStartByName", () => { + it("should throw an error if there is no mark with the given name", () => { + function bogusGet() { + perfService.getMostRecentAbsMarkStartByName("rheeeet"); + } + + assert.throws(bogusGet, Error, /No marks with the name/); + }); + + it("should return the Number from the most recent mark with the given name + the time origin", () => { + perfService.mark("dog"); + perfService.mark("dog"); + + let absMarkStart = perfService.getMostRecentAbsMarkStartByName("dog"); + + // 2 because we want the result of the 2nd call to mark, and an instance + // of FakePerformance just returns the number of time mark has been + // called. + assert.equal(absMarkStart - perfService.timeOrigin, 2); + }); + }); + + describe("#mark", () => { + it("should call the wrapped version of mark", () => { + sandbox.spy(fakePerfObj, "mark"); + + perfService.mark("monkey"); + + assert.calledOnce(fakePerfObj.mark); + assert.calledWithExactly(fakePerfObj.mark, "monkey"); + }); + }); + + describe("#timeOrigin", () => { + it("should get the origin of the wrapped performance object", () => { + assert.equal(perfService.timeOrigin, fakePerfObj.timeOrigin); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/lib/screenshot-utils.test.js b/browser/components/newtab/test/unit/content-src/lib/screenshot-utils.test.js new file mode 100644 index 0000000000..ef7e7cf5f6 --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/lib/screenshot-utils.test.js @@ -0,0 +1,147 @@ +import { GlobalOverrider } from "test/unit/utils"; +import { ScreenshotUtils } from "content-src/lib/screenshot-utils"; + +const DEFAULT_BLOB_URL = "blob://test"; + +describe("ScreenshotUtils", () => { + let globals; + let url; + beforeEach(() => { + globals = new GlobalOverrider(); + url = { + createObjectURL: globals.sandbox.stub().returns(DEFAULT_BLOB_URL), + revokeObjectURL: globals.sandbox.spy(), + }; + globals.set("URL", url); + }); + afterEach(() => globals.restore()); + describe("#createLocalImageObject", () => { + it("should return null if no remoteImage is supplied", () => { + let localImageObject = ScreenshotUtils.createLocalImageObject(null); + + assert.notCalled(url.createObjectURL); + assert.equal(localImageObject, null); + }); + it("should create a local image object with the correct properties if remoteImage is a blob", () => { + let localImageObject = ScreenshotUtils.createLocalImageObject({ + path: "/path1", + data: new Blob([0]), + }); + + assert.calledOnce(url.createObjectURL); + assert.deepEqual(localImageObject, { + path: "/path1", + url: DEFAULT_BLOB_URL, + }); + }); + it("should create a local image object with the correct properties if remoteImage is a normal image", () => { + const imageUrl = "https://test-url"; + let localImageObject = ScreenshotUtils.createLocalImageObject(imageUrl); + + assert.notCalled(url.createObjectURL); + assert.deepEqual(localImageObject, { url: imageUrl }); + }); + }); + describe("#maybeRevokeBlobObjectURL", () => { + // Note that we should also ensure that all the tests for #isBlob are green. + it("should call revokeObjectURL if image is a blob", () => { + ScreenshotUtils.maybeRevokeBlobObjectURL({ + path: "/path1", + url: "blob://test", + }); + + assert.calledOnce(url.revokeObjectURL); + }); + it("should not call revokeObjectURL if image is not a blob", () => { + ScreenshotUtils.maybeRevokeBlobObjectURL({ url: "https://test-url" }); + + assert.notCalled(url.revokeObjectURL); + }); + }); + describe("#isRemoteImageLocal", () => { + it("should return true if both propsImage and stateImage are not present", () => { + assert.isTrue(ScreenshotUtils.isRemoteImageLocal(null, null)); + }); + it("should return false if propsImage is present and stateImage is not present", () => { + assert.isFalse(ScreenshotUtils.isRemoteImageLocal(null, {})); + }); + it("should return false if propsImage is not present and stateImage is present", () => { + assert.isFalse(ScreenshotUtils.isRemoteImageLocal({}, null)); + }); + it("should return true if both propsImage and stateImage are equal blobs", () => { + const blobPath = "/test-blob-path/test.png"; + assert.isTrue( + ScreenshotUtils.isRemoteImageLocal( + { path: blobPath, url: "blob://test" }, // state + { path: blobPath, data: new Blob([0]) } // props + ) + ); + }); + it("should return false if both propsImage and stateImage are different blobs", () => { + assert.isFalse( + ScreenshotUtils.isRemoteImageLocal( + { path: "/path1", url: "blob://test" }, // state + { path: "/path2", data: new Blob([0]) } // props + ) + ); + }); + it("should return true if both propsImage and stateImage are equal normal images", () => { + assert.isTrue( + ScreenshotUtils.isRemoteImageLocal( + { url: "test url" }, // state + "test url" // props + ) + ); + }); + it("should return false if both propsImage and stateImage are different normal images", () => { + assert.isFalse( + ScreenshotUtils.isRemoteImageLocal( + { url: "test url 1" }, // state + "test url 2" // props + ) + ); + }); + it("should return false if both propsImage and stateImage are different type of images", () => { + assert.isFalse( + ScreenshotUtils.isRemoteImageLocal( + { path: "/path1", url: "blob://test" }, // state + "test url 2" // props + ) + ); + assert.isFalse( + ScreenshotUtils.isRemoteImageLocal( + { url: "https://test-url" }, // state + { path: "/path1", data: new Blob([0]) } // props + ) + ); + }); + }); + describe("#isBlob", () => { + let state = { + blobImage: { path: "/test", url: "blob://test" }, + normalImage: { url: "https://test-url" }, + }; + let props = { + blobImage: { path: "/test", data: new Blob([0]) }, + normalImage: "https://test-url", + }; + it("should return false if image is null", () => { + assert.isFalse(ScreenshotUtils.isBlob(true, null)); + assert.isFalse(ScreenshotUtils.isBlob(false, null)); + }); + it("should return true if image is a blob and type matches", () => { + assert.isTrue(ScreenshotUtils.isBlob(true, state.blobImage)); + assert.isTrue(ScreenshotUtils.isBlob(false, props.blobImage)); + }); + it("should return false if image is not a blob and type matches", () => { + assert.isFalse(ScreenshotUtils.isBlob(true, state.normalImage)); + assert.isFalse(ScreenshotUtils.isBlob(false, props.normalImage)); + }); + it("should return false if type does not match", () => { + assert.isFalse(ScreenshotUtils.isBlob(false, state.blobImage)); + assert.isFalse(ScreenshotUtils.isBlob(false, state.normalImage)); + assert.isFalse(ScreenshotUtils.isBlob(true, props.blobImage)); + assert.isFalse(ScreenshotUtils.isBlob(true, props.normalImage)); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/content-src/lib/selectLayoutRender.test.js b/browser/components/newtab/test/unit/content-src/lib/selectLayoutRender.test.js new file mode 100644 index 0000000000..233f31b6ca --- /dev/null +++ b/browser/components/newtab/test/unit/content-src/lib/selectLayoutRender.test.js @@ -0,0 +1,576 @@ +import { combineReducers, createStore } from "redux"; +import { actionTypes as at } from "common/Actions.sys.mjs"; +import { GlobalOverrider } from "test/unit/utils"; +import { reducers } from "common/Reducers.sys.mjs"; +import { selectLayoutRender } from "content-src/lib/selectLayoutRender"; +const FAKE_LAYOUT = [ + { + width: 3, + components: [ + { type: "foo", feed: { url: "foo.com" }, properties: { items: 2 } }, + ], + }, +]; +const FAKE_FEEDS = { + "foo.com": { data: { recommendations: [{ id: "foo" }, { id: "bar" }] } }, +}; + +describe("selectLayoutRender", () => { + let store; + let globals; + + beforeEach(() => { + globals = new GlobalOverrider(); + store = createStore(combineReducers(reducers)); + }); + + afterEach(() => { + globals.restore(); + }); + + it("should return an empty array given initial state", () => { + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + prefs: {}, + rollCache: [], + }); + assert.deepEqual(layoutRender, []); + }); + + it("should add .data property from feeds to each compontent in .layout", () => { + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: FAKE_LAYOUT }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: FAKE_FEEDS["foo.com"], url: "foo.com" }, + }); + store.dispatch({ type: at.DISCOVERY_STREAM_FEEDS_UPDATE }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.lengthOf(layoutRender, 1); + assert.propertyVal(layoutRender[0], "width", 3); + assert.deepEqual(layoutRender[0].components[0], { + type: "foo", + feed: { url: "foo.com" }, + properties: { items: 2 }, + data: { + recommendations: [ + { id: "foo", pos: 0 }, + { id: "bar", pos: 1 }, + ], + }, + }); + }); + + it("should return layout with placeholder data if feed doesn't have data", () => { + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: FAKE_LAYOUT }, + }); + store.dispatch({ type: at.DISCOVERY_STREAM_FEEDS_UPDATE }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.lengthOf(layoutRender, 1); + assert.propertyVal(layoutRender[0], "width", 3); + assert.deepEqual(layoutRender[0].components[0].data.recommendations, [ + { placeholder: true }, + { placeholder: true }, + ]); + }); + + it("should return layout with empty spocs data if feed isn't defined but spocs is", () => { + const fakeLayout = [ + { + width: 3, + components: [{ type: "foo", spocs: { positions: [{ index: 2 }] } }], + }, + ]; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + store.dispatch({ type: at.DISCOVERY_STREAM_FEEDS_UPDATE }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.lengthOf(layoutRender, 1); + assert.propertyVal(layoutRender[0], "width", 3); + assert.deepEqual(layoutRender[0].components[0].data.spocs, []); + }); + + it("should return layout with spocs data if feed isn't defined but spocs is", () => { + const fakeLayout = [ + { + width: 3, + components: [{ type: "foo", spocs: { positions: [{ index: 0 }] } }], + }, + ]; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + store.dispatch({ type: at.DISCOVERY_STREAM_FEEDS_UPDATE }); + store.dispatch({ + type: at.DISCOVERY_STREAM_SPOCS_UPDATE, + data: { + lastUpdated: 0, + spocs: { + spocs: { + items: [{ id: 1 }, { id: 2 }, { id: 3 }], + }, + }, + }, + }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.lengthOf(layoutRender, 1); + assert.propertyVal(layoutRender[0], "width", 3); + assert.deepEqual(layoutRender[0].components[0].data.spocs, [ + { id: 1, pos: 0 }, + { id: 2, pos: 1 }, + { id: 3, pos: 2 }, + ]); + }); + + it("should return layout with no spocs data if feed and spocs are unavailable", () => { + const fakeLayout = [ + { + width: 3, + components: [{ type: "foo", spocs: { positions: [{ index: 0 }] } }], + }, + ]; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + store.dispatch({ type: at.DISCOVERY_STREAM_FEEDS_UPDATE }); + store.dispatch({ + type: at.DISCOVERY_STREAM_SPOCS_UPDATE, + data: { + lastUpdated: 0, + spocs: { + spocs: { + items: [], + }, + }, + }, + }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.lengthOf(layoutRender, 1); + assert.propertyVal(layoutRender[0], "width", 3); + assert.equal(layoutRender[0].components[0].data.spocs.length, 0); + }); + + it("should return feed data offset by layout set prop", () => { + const fakeLayout = [ + { + width: 3, + components: [ + { type: "foo", properties: { offset: 1 }, feed: { url: "foo.com" } }, + ], + }, + ]; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: FAKE_FEEDS["foo.com"], url: "foo.com" }, + }); + store.dispatch({ type: at.DISCOVERY_STREAM_FEEDS_UPDATE }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.deepEqual(layoutRender[0].components[0].data, { + recommendations: [{ id: "bar" }], + }); + }); + + it("should return spoc result when there are more positions than spocs", () => { + const fakeSpocConfig = { + positions: [{ index: 0 }, { index: 1 }, { index: 2 }], + }; + const fakeLayout = [ + { + width: 3, + components: [ + { type: "foo", feed: { url: "foo.com" }, spocs: fakeSpocConfig }, + ], + }, + ]; + const fakeSpocsData = { + lastUpdated: 0, + spocs: { spocs: { items: ["fooSpoc", "barSpoc"] } }, + }; + + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: FAKE_FEEDS["foo.com"], url: "foo.com" }, + }); + store.dispatch({ type: at.DISCOVERY_STREAM_FEEDS_UPDATE }); + store.dispatch({ + type: at.DISCOVERY_STREAM_SPOCS_UPDATE, + data: fakeSpocsData, + }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.lengthOf(layoutRender, 1); + assert.deepEqual( + layoutRender[0].components[0].data.recommendations[0], + "fooSpoc" + ); + assert.deepEqual( + layoutRender[0].components[0].data.recommendations[1], + "barSpoc" + ); + assert.deepEqual(layoutRender[0].components[0].data.recommendations[2], { + id: "foo", + }); + assert.deepEqual(layoutRender[0].components[0].data.recommendations[3], { + id: "bar", + }); + }); + + it("should return a layout with feeds of items length with positions", () => { + const fakeLayout = [ + { + width: 3, + components: [ + { type: "foo", properties: { items: 3 }, feed: { url: "foo.com" } }, + ], + }, + ]; + const fakeRecommendations = [ + { name: "item1" }, + { name: "item2" }, + { name: "item3" }, + { name: "item4" }, + ]; + const fakeFeeds = { + "foo.com": { data: { recommendations: fakeRecommendations } }, + }; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: fakeFeeds["foo.com"], url: "foo.com" }, + }); + store.dispatch({ type: at.DISCOVERY_STREAM_FEEDS_UPDATE }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + const { recommendations } = layoutRender[0].components[0].data; + assert.equal(recommendations.length, 4); + assert.equal(recommendations[0].pos, 0); + assert.equal(recommendations[1].pos, 1); + assert.equal(recommendations[2].pos, 2); + assert.equal(recommendations[3].pos, undefined); + }); + it("should stop rendering feeds if we hit one that's not ready", () => { + const fakeLayout = [ + { + width: 3, + components: [ + { type: "foo1" }, + { type: "foo2", properties: { items: 3 }, feed: { url: "foo2.com" } }, + { type: "foo3", properties: { items: 3 }, feed: { url: "foo3.com" } }, + { type: "foo4", properties: { items: 3 }, feed: { url: "foo4.com" } }, + { type: "foo5" }, + ], + }, + ]; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: { data: { recommendations: [] } }, url: "foo2.com" }, + }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.equal(layoutRender[0].components[0].type, "foo1"); + assert.equal(layoutRender[0].components[1].type, "foo2"); + assert.isTrue( + layoutRender[0].components[2].data.recommendations[0].placeholder + ); + assert.lengthOf(layoutRender[0].components, 3); + assert.isUndefined(layoutRender[0].components[3]); + }); + it("should render everything if everything is ready", () => { + const fakeLayout = [ + { + width: 3, + components: [ + { type: "foo1" }, + { type: "foo2", properties: { items: 3 }, feed: { url: "foo2.com" } }, + { type: "foo3", properties: { items: 3 }, feed: { url: "foo3.com" } }, + { type: "foo4", properties: { items: 3 }, feed: { url: "foo4.com" } }, + { type: "foo5" }, + ], + }, + ]; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: { data: { recommendations: [] } }, url: "foo2.com" }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: { data: { recommendations: [] } }, url: "foo3.com" }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: { data: { recommendations: [] } }, url: "foo4.com" }, + }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.equal(layoutRender[0].components[0].type, "foo1"); + assert.equal(layoutRender[0].components[1].type, "foo2"); + assert.equal(layoutRender[0].components[2].type, "foo3"); + assert.equal(layoutRender[0].components[3].type, "foo4"); + assert.equal(layoutRender[0].components[4].type, "foo5"); + }); + it("should stop rendering feeds if we hit a not ready spoc", () => { + const fakeLayout = [ + { + width: 3, + components: [ + { type: "foo1" }, + { type: "foo2", properties: { items: 3 }, feed: { url: "foo2.com" } }, + { + type: "foo3", + properties: { items: 3 }, + feed: { url: "foo3.com" }, + spocs: { positions: [{ index: 0 }] }, + }, + { type: "foo4", properties: { items: 3 }, feed: { url: "foo4.com" } }, + { type: "foo5" }, + ], + }, + ]; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: { data: { recommendations: [] } }, url: "foo2.com" }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: { data: { recommendations: [] } }, url: "foo3.com" }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: { data: { recommendations: [] } }, url: "foo4.com" }, + }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.equal(layoutRender[0].components[0].type, "foo1"); + assert.equal(layoutRender[0].components[1].type, "foo2"); + assert.deepEqual(layoutRender[0].components[2].data.recommendations, [ + { placeholder: true }, + { placeholder: true }, + { placeholder: true }, + ]); + }); + it("should not render a spoc if there are no available spocs", () => { + const fakeLayout = [ + { + width: 3, + components: [ + { type: "foo1" }, + { type: "foo2", properties: { items: 3 }, feed: { url: "foo2.com" } }, + { + type: "foo3", + properties: { items: 3 }, + feed: { url: "foo3.com" }, + spocs: { positions: [{ index: 0 }] }, + }, + { type: "foo4", properties: { items: 3 }, feed: { url: "foo4.com" } }, + { type: "foo5" }, + ], + }, + ]; + const fakeSpocsData = { lastUpdated: 0, spocs: { spocs: [] } }; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: { data: { recommendations: [] } }, url: "foo2.com" }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { + feed: { data: { recommendations: [{ name: "rec" }] } }, + url: "foo3.com", + }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: { data: { recommendations: [] } }, url: "foo4.com" }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_SPOCS_UPDATE, + data: fakeSpocsData, + }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.deepEqual(layoutRender[0].components[2].data.recommendations[0], { + name: "rec", + pos: 0, + }); + }); + it("should not render a row if no components exist after filter in that row", () => { + const fakeLayout = [ + { + width: 3, + components: [{ type: "TopSites" }], + }, + { + width: 3, + components: [{ type: "Message" }], + }, + ]; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + prefs: { "feeds.topsites": true }, + }); + + assert.equal(layoutRender[0].components[0].type, "TopSites"); + assert.equal(layoutRender[1], undefined); + }); + it("should not render a component if filtered", () => { + const fakeLayout = [ + { + width: 3, + components: [{ type: "Message" }, { type: "TopSites" }], + }, + ]; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + + const { layoutRender } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + prefs: { "feeds.topsites": true }, + }); + + assert.equal(layoutRender[0].components[0].type, "TopSites"); + assert.equal(layoutRender[0].components[1], undefined); + }); + it("should skip rendering a spoc in position if that spoc is blocked for that session", () => { + const fakeLayout = [ + { + width: 3, + components: [ + { + type: "foo1", + properties: { items: 3 }, + feed: { url: "foo1.com" }, + spocs: { positions: [{ index: 0 }] }, + }, + ], + }, + ]; + const fakeSpocsData = { + lastUpdated: 0, + spocs: { + spocs: { items: [{ name: "spoc", url: "https://foo.com" }] }, + }, + }; + store.dispatch({ + type: at.DISCOVERY_STREAM_LAYOUT_UPDATE, + data: { layout: fakeLayout }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { + feed: { data: { recommendations: [{ name: "rec" }] } }, + url: "foo1.com", + }, + }); + store.dispatch({ + type: at.DISCOVERY_STREAM_SPOCS_UPDATE, + data: fakeSpocsData, + }); + + const { layoutRender: layout1 } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + store.dispatch({ + type: at.DISCOVERY_STREAM_SPOC_BLOCKED, + data: { url: "https://foo.com" }, + }); + + const { layoutRender: layout2 } = selectLayoutRender({ + state: store.getState().DiscoveryStream, + }); + + assert.deepEqual(layout1[0].components[0].data.recommendations[0], { + name: "spoc", + url: "https://foo.com", + pos: 0, + }); + assert.deepEqual(layout2[0].components[0].data.recommendations[0], { + name: "rec", + pos: 0, + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/AboutPreferences.test.js b/browser/components/newtab/test/unit/lib/AboutPreferences.test.js new file mode 100644 index 0000000000..7438d8247c --- /dev/null +++ b/browser/components/newtab/test/unit/lib/AboutPreferences.test.js @@ -0,0 +1,429 @@ +/* global Services */ +import { + AboutPreferences, + PREFERENCES_LOADED_EVENT, +} from "lib/AboutPreferences.sys.mjs"; +import { + actionTypes as at, + actionCreators as ac, +} from "common/Actions.sys.mjs"; +import { GlobalOverrider } from "test/unit/utils"; + +describe("AboutPreferences Feed", () => { + let globals; + let sandbox; + let Sections; + let DiscoveryStream; + let instance; + + beforeEach(() => { + globals = new GlobalOverrider(); + sandbox = globals.sandbox; + Sections = []; + DiscoveryStream = { config: { enabled: false } }; + instance = new AboutPreferences(); + instance.store = { + dispatch: sandbox.stub(), + getState: () => ({ Sections, DiscoveryStream }), + }; + globals.set("NimbusFeatures", { + newtab: { getAllVariables: sandbox.stub() }, + }); + }); + afterEach(() => { + globals.restore(); + }); + + describe("#onAction", () => { + it("should call .init() on an INIT action", () => { + const stub = sandbox.stub(instance, "init"); + + instance.onAction({ type: at.INIT }); + + assert.calledOnce(stub); + }); + it("should call .uninit() on an UNINIT action", () => { + const stub = sandbox.stub(instance, "uninit"); + + instance.onAction({ type: at.UNINIT }); + + assert.calledOnce(stub); + }); + it("should call .openPreferences on SETTINGS_OPEN", () => { + const action = { + type: at.SETTINGS_OPEN, + _target: { browser: { ownerGlobal: { openPreferences: sinon.spy() } } }, + }; + instance.onAction(action); + assert.calledOnce(action._target.browser.ownerGlobal.openPreferences); + }); + it("should call .BrowserOpenAddonsMgr with the extension id on OPEN_WEBEXT_SETTINGS", () => { + const action = { + type: at.OPEN_WEBEXT_SETTINGS, + data: "foo", + _target: { + browser: { ownerGlobal: { BrowserOpenAddonsMgr: sinon.spy() } }, + }, + }; + instance.onAction(action); + assert.calledWith( + action._target.browser.ownerGlobal.BrowserOpenAddonsMgr, + "addons://detail/foo" + ); + }); + }); + describe("#observe", () => { + it("should watch for about:preferences loading", () => { + sandbox.stub(Services.obs, "addObserver"); + + instance.init(); + + assert.calledOnce(Services.obs.addObserver); + assert.calledWith( + Services.obs.addObserver, + instance, + PREFERENCES_LOADED_EVENT + ); + }); + it("should stop watching on uninit", () => { + sandbox.stub(Services.obs, "removeObserver"); + + instance.uninit(); + + assert.calledOnce(Services.obs.removeObserver); + assert.calledWith( + Services.obs.removeObserver, + instance, + PREFERENCES_LOADED_EVENT + ); + }); + it("should try to render on event", async () => { + const stub = sandbox.stub(instance, "renderPreferences"); + Sections.push({}); + + await instance.observe(window, PREFERENCES_LOADED_EVENT); + + assert.calledOnce(stub); + assert.equal(stub.firstCall.args[0], window); + assert.include(stub.firstCall.args[1], Sections[0]); + }); + it("Hide topstories rows select in sections if discovery stream is enabled", async () => { + const stub = sandbox.stub(instance, "renderPreferences"); + + Sections.push({ + rowsPref: "row_pref", + maxRows: 3, + pref: { descString: "foo" }, + learnMore: { link: "https://foo.com" }, + id: "topstories", + }); + DiscoveryStream = { config: { enabled: true } }; + + await instance.observe(window, PREFERENCES_LOADED_EVENT); + + assert.calledOnce(stub); + const [, structure] = stub.firstCall.args; + assert.equal(structure[0].id, "search"); + assert.equal(structure[1].id, "topsites"); + assert.equal(structure[2].id, "topstories"); + assert.isEmpty(structure[2].rowsPref); + }); + }); + describe("#renderPreferences", () => { + let node; + let prefStructure; + let Preferences; + let gHomePane; + const testRender = () => + instance.renderPreferences( + { + document: { + createXULElement: sandbox.stub().returns(node), + l10n: { + setAttributes(el, id, args) { + el.setAttribute("data-l10n-id", id); + el.setAttribute("data-l10n-args", JSON.stringify(args)); + }, + }, + createProcessingInstruction: sandbox.stub(), + createElementNS: sandbox.stub().callsFake((NS, el) => node), + getElementById: sandbox.stub().returns(node), + insertBefore: sandbox.stub().returnsArg(0), + querySelector: sandbox + .stub() + .returns({ appendChild: sandbox.stub() }), + }, + Preferences, + gHomePane, + }, + prefStructure, + DiscoveryStream.config + ); + beforeEach(() => { + node = { + appendChild: sandbox.stub().returnsArg(0), + addEventListener: sandbox.stub(), + classList: { add: sandbox.stub(), remove: sandbox.stub() }, + cloneNode: sandbox.stub().returnsThis(), + insertAdjacentElement: sandbox.stub().returnsArg(1), + setAttribute: sandbox.stub(), + remove: sandbox.stub(), + style: {}, + }; + prefStructure = []; + Preferences = { + add: sandbox.stub(), + get: sandbox.stub().returns({ + on: sandbox.stub(), + }), + }; + gHomePane = { toggleRestoreDefaultsBtn: sandbox.stub() }; + }); + describe("#getString", () => { + it("should not fail if titleString is not provided", () => { + prefStructure = [{ pref: {} }]; + + testRender(); + assert.calledWith( + node.setAttribute, + "data-l10n-id", + sinon.match.typeOf("undefined") + ); + }); + it("should return the string id if titleString is just a string", () => { + const titleString = "foo"; + prefStructure = [{ pref: { titleString } }]; + + testRender(); + assert.calledWith(node.setAttribute, "data-l10n-id", titleString); + }); + it("should set id and args if titleString is an object with id and values", () => { + const titleString = { id: "foo", values: { provider: "bar" } }; + prefStructure = [{ pref: { titleString } }]; + + testRender(); + assert.calledWith(node.setAttribute, "data-l10n-id", titleString.id); + assert.calledWith( + node.setAttribute, + "data-l10n-args", + JSON.stringify(titleString.values) + ); + }); + }); + describe("#linkPref", () => { + it("should add a pref to the global", () => { + prefStructure = [{ pref: { feed: "feed" } }]; + + testRender(); + + assert.calledOnce(Preferences.add); + }); + it("should skip adding if not shown", () => { + prefStructure = [{ shouldHidePref: true }]; + + testRender(); + + assert.notCalled(Preferences.add); + }); + }); + describe("pref icon", () => { + it("should default to webextension icon", () => { + prefStructure = [{ pref: { feed: "feed" } }]; + + testRender(); + + assert.calledWith( + node.setAttribute, + "src", + "chrome://activity-stream/content/data/content/assets/glyph-webextension-16.svg" + ); + }); + it("should use desired glyph icon", () => { + prefStructure = [{ icon: "mail", pref: { feed: "feed" } }]; + + testRender(); + + assert.calledWith( + node.setAttribute, + "src", + "chrome://activity-stream/content/data/content/assets/glyph-mail-16.svg" + ); + }); + it("should use specified chrome icon", () => { + const icon = "chrome://the/icon.svg"; + prefStructure = [{ icon, pref: { feed: "feed" } }]; + + testRender(); + + assert.calledWith(node.setAttribute, "src", icon); + }); + }); + describe("title line", () => { + it("should render a title", () => { + const titleString = "the_title"; + prefStructure = [{ pref: { titleString } }]; + + testRender(); + + assert.calledWith(node.setAttribute, "data-l10n-id", titleString); + }); + }); + describe("top stories", () => { + const href = "https://disclaimer/"; + const eventSource = "https://disclaimer/"; + beforeEach(() => { + prefStructure = [ + { + id: "topstories", + pref: { feed: "feed", learnMore: { link: { href } } }, + eventSource, + }, + ]; + }); + it("should add a link for top stories", () => { + testRender(); + assert.calledWith(node.setAttribute, "href", href); + }); + it("should setup a user event for top stories eventSource", () => { + sinon.spy(instance, "setupUserEvent"); + testRender(); + assert.calledWith(node.addEventListener, "command"); + assert.calledWith(instance.setupUserEvent, node, eventSource); + }); + it("should setup a user event for top stories nested pref eventSource", () => { + sinon.spy(instance, "setupUserEvent"); + prefStructure = [ + { + id: "topstories", + pref: { + feed: "feed", + learnMore: { link: { href } }, + nestedPrefs: [ + { + name: "showSponsored", + titleString: + "home-prefs-recommended-by-option-sponsored-stories", + icon: "icon-info", + eventSource: "POCKET_SPOCS", + }, + ], + }, + }, + ]; + testRender(); + assert.calledWith(node.addEventListener, "command"); + assert.calledWith(instance.setupUserEvent, node, "POCKET_SPOCS"); + }); + it("should fire store dispatch with onCommand", () => { + const element = { + addEventListener: (command, action) => { + // Trigger the action right away because we only care about testing the action here. + action({ target: { checked: true } }); + }, + }; + instance.setupUserEvent(element, eventSource); + assert.calledWith( + instance.store.dispatch, + ac.UserEvent({ + event: "PREF_CHANGED", + source: eventSource, + value: { menu_source: "ABOUT_PREFERENCES", status: true }, + }) + ); + }); + }); + describe("description line", () => { + it("should render a description", () => { + const descString = "the_desc"; + prefStructure = [{ pref: { descString } }]; + + testRender(); + + assert.calledWith(node.setAttribute, "data-l10n-id", descString); + }); + it("should render rows dropdown with appropriate number", () => { + prefStructure = [ + { rowsPref: "row_pref", maxRows: 3, pref: { descString: "foo" } }, + ]; + + testRender(); + + assert.calledWith(node.setAttribute, "value", 1); + assert.calledWith(node.setAttribute, "value", 2); + assert.calledWith(node.setAttribute, "value", 3); + }); + }); + describe("nested prefs", () => { + const titleString = "im_nested"; + beforeEach(() => { + prefStructure = [{ pref: { nestedPrefs: [{ titleString }] } }]; + }); + it("should render a nested pref", () => { + testRender(); + + assert.calledWith(node.setAttribute, "data-l10n-id", titleString); + }); + it("should set node hidden to true", () => { + prefStructure[0].pref.nestedPrefs[0].hidden = true; + + testRender(); + + assert.isTrue(node.hidden); + }); + it("should add a change event", () => { + testRender(); + + assert.calledOnce(Preferences.get().on); + assert.calledWith(Preferences.get().on, "change"); + }); + it("should default node disabled to false", async () => { + Preferences.get = sandbox.stub().returns({ + on: sandbox.stub(), + _value: true, + }); + + testRender(); + + assert.isFalse(node.disabled); + }); + it("should default node disabled to true", async () => { + testRender(); + + assert.isTrue(node.disabled); + }); + it("should set node disabled to true", async () => { + const pref = { + on: sandbox.stub(), + _value: true, + }; + Preferences.get = sandbox.stub().returns(pref); + + testRender(); + pref._value = !pref._value; + await Preferences.get().on.firstCall.args[1](); + + assert.isTrue(node.disabled); + }); + it("should set node disabled to false", async () => { + const pref = { + on: sandbox.stub(), + _value: false, + }; + Preferences.get = sandbox.stub().returns(pref); + + testRender(); + pref._value = !pref._value; + await Preferences.get().on.firstCall.args[1](); + + assert.isFalse(node.disabled); + }); + }); + describe("restore defaults btn", () => { + it("should call toggleRestoreDefaultsBtn", () => { + testRender(); + + assert.calledOnce(gHomePane.toggleRestoreDefaultsBtn); + }); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/ActivityStream.test.js b/browser/components/newtab/test/unit/lib/ActivityStream.test.js new file mode 100644 index 0000000000..c127060021 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/ActivityStream.test.js @@ -0,0 +1,576 @@ +import { CONTENT_MESSAGE_TYPE } from "common/Actions.sys.mjs"; +import { ActivityStream, PREFS_CONFIG } from "lib/ActivityStream.sys.mjs"; +import { GlobalOverrider } from "test/unit/utils"; + +import { DEFAULT_SITES } from "lib/DefaultSites.sys.mjs"; +import { AboutPreferences } from "lib/AboutPreferences.sys.mjs"; +import { DefaultPrefs } from "lib/ActivityStreamPrefs.sys.mjs"; +import { NewTabInit } from "lib/NewTabInit.sys.mjs"; +import { SectionsFeed } from "lib/SectionsManager.sys.mjs"; +import { RecommendationProvider } from "lib/RecommendationProvider.sys.mjs"; +import { PlacesFeed } from "lib/PlacesFeed.sys.mjs"; +import { PrefsFeed } from "lib/PrefsFeed.sys.mjs"; +import { SystemTickFeed } from "lib/SystemTickFeed.sys.mjs"; +import { TelemetryFeed } from "lib/TelemetryFeed.sys.mjs"; +import { FaviconFeed } from "lib/FaviconFeed.sys.mjs"; +import { TopSitesFeed } from "lib/TopSitesFeed.sys.mjs"; +import { TopStoriesFeed } from "lib/TopStoriesFeed.sys.mjs"; +import { HighlightsFeed } from "lib/HighlightsFeed.sys.mjs"; +import { DiscoveryStreamFeed } from "lib/DiscoveryStreamFeed.sys.mjs"; + +import { LinksCache } from "lib/LinksCache.sys.mjs"; +import { PersistentCache } from "lib/PersistentCache.sys.mjs"; +import { DownloadsManager } from "lib/DownloadsManager.sys.mjs"; + +describe("ActivityStream", () => { + let sandbox; + let as; + function FakeStore() { + return { init: () => {}, uninit: () => {}, feeds: { get: () => {} } }; + } + + let globals; + beforeEach(() => { + globals = new GlobalOverrider(); + globals.set({ + Store: FakeStore, + + DEFAULT_SITES, + AboutPreferences, + DefaultPrefs, + NewTabInit, + SectionsFeed, + RecommendationProvider, + PlacesFeed, + PrefsFeed, + SystemTickFeed, + TelemetryFeed, + FaviconFeed, + TopSitesFeed, + TopStoriesFeed, + HighlightsFeed, + DiscoveryStreamFeed, + + LinksCache, + PersistentCache, + DownloadsManager, + }); + + as = new ActivityStream(); + sandbox = sinon.createSandbox(); + sandbox.stub(as.store, "init"); + sandbox.stub(as.store, "uninit"); + sandbox.stub(as._defaultPrefs, "init"); + PREFS_CONFIG.get("feeds.system.topstories").value = undefined; + }); + + afterEach(() => { + sandbox.restore(); + globals.restore(); + }); + + it("should exist", () => { + assert.ok(ActivityStream); + }); + it("should initialize with .initialized=false", () => { + assert.isFalse(as.initialized, ".initialized"); + }); + describe("#init", () => { + beforeEach(() => { + as.init(); + }); + it("should initialize default prefs", () => { + assert.calledOnce(as._defaultPrefs.init); + }); + it("should set .initialized to true", () => { + assert.isTrue(as.initialized, ".initialized"); + }); + it("should call .store.init", () => { + assert.calledOnce(as.store.init); + }); + it("should pass to Store an INIT event for content", () => { + as.init(); + + const [, action] = as.store.init.firstCall.args; + assert.equal(action.meta.to, CONTENT_MESSAGE_TYPE); + }); + it("should pass to Store an UNINIT event", () => { + as.init(); + + const [, , action] = as.store.init.firstCall.args; + assert.equal(action.type, "UNINIT"); + }); + it("should clear old default discoverystream config pref", () => { + sandbox.stub(global.Services.prefs, "prefHasUserValue").returns(true); + sandbox + .stub(global.Services.prefs, "getStringPref") + .returns( + `{"api_key_pref":"extensions.pocket.oAuthConsumerKey","enabled":false,"show_spocs":true,"layout_endpoint":"https://getpocket.cdn.mozilla.net/v3/newtab/layout?version=1&consumer_key=$apiKey&layout_variant=basic"}` + ); + sandbox.stub(global.Services.prefs, "clearUserPref"); + + as.init(); + + assert.calledWith( + global.Services.prefs.clearUserPref, + "browser.newtabpage.activity-stream.discoverystream.config" + ); + }); + it("should call addObserver for the app locales", () => { + sandbox.stub(global.Services.obs, "addObserver"); + as.init(); + assert.calledWith( + global.Services.obs.addObserver, + as, + "intl:app-locales-changed" + ); + }); + }); + describe("#uninit", () => { + beforeEach(() => { + as.init(); + as.uninit(); + }); + it("should set .initialized to false", () => { + assert.isFalse(as.initialized, ".initialized"); + }); + it("should call .store.uninit", () => { + assert.calledOnce(as.store.uninit); + }); + it("should call removeObserver for the region", () => { + sandbox.stub(global.Services.obs, "removeObserver"); + as.geo = ""; + as.uninit(); + assert.calledWith( + global.Services.obs.removeObserver, + as, + global.Region.REGION_TOPIC + ); + }); + it("should call removeObserver for the app locales", () => { + sandbox.stub(global.Services.obs, "removeObserver"); + as.uninit(); + assert.calledWith( + global.Services.obs.removeObserver, + as, + "intl:app-locales-changed" + ); + }); + }); + describe("#observe", () => { + it("should call _updateDynamicPrefs from observe", () => { + sandbox.stub(as, "_updateDynamicPrefs"); + as.observe(undefined, global.Region.REGION_TOPIC); + assert.calledOnce(as._updateDynamicPrefs); + }); + }); + describe("feeds", () => { + it("should create a NewTabInit feed", () => { + const feed = as.feeds.get("feeds.newtabinit")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a Places feed", () => { + const feed = as.feeds.get("feeds.places")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a TopSites feed", () => { + const feed = as.feeds.get("feeds.system.topsites")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a Telemetry feed", () => { + const feed = as.feeds.get("feeds.telemetry")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a Prefs feed", () => { + const feed = as.feeds.get("feeds.prefs")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a HighlightsFeed feed", () => { + const feed = as.feeds.get("feeds.section.highlights")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a TopStoriesFeed feed", () => { + const feed = as.feeds.get("feeds.system.topstories")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a AboutPreferences feed", () => { + const feed = as.feeds.get("feeds.aboutpreferences")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a SectionsFeed", () => { + const feed = as.feeds.get("feeds.sections")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a SystemTick feed", () => { + const feed = as.feeds.get("feeds.systemtick")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a Favicon feed", () => { + const feed = as.feeds.get("feeds.favicon")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a RecommendationProvider feed", () => { + const feed = as.feeds.get("feeds.recommendationprovider")(); + assert.ok(feed, "feed should exist"); + }); + it("should create a DiscoveryStreamFeed feed", () => { + const feed = as.feeds.get("feeds.discoverystreamfeed")(); + assert.ok(feed, "feed should exist"); + }); + }); + describe("_migratePref", () => { + it("should migrate a pref if the user has set a custom value", () => { + sandbox.stub(global.Services.prefs, "prefHasUserValue").returns(true); + sandbox.stub(global.Services.prefs, "getPrefType").returns("integer"); + sandbox.stub(global.Services.prefs, "getIntPref").returns(10); + as._migratePref("oldPrefName", result => assert.equal(10, result)); + }); + it("should not migrate a pref if the user has not set a custom value", () => { + // we bailed out early so we don't check the pref type later + sandbox.stub(global.Services.prefs, "prefHasUserValue").returns(false); + sandbox.stub(global.Services.prefs, "getPrefType"); + as._migratePref("oldPrefName"); + assert.notCalled(global.Services.prefs.getPrefType); + }); + it("should use the proper pref getter for each type", () => { + sandbox.stub(global.Services.prefs, "prefHasUserValue").returns(true); + + // Integer + sandbox.stub(global.Services.prefs, "getIntPref"); + sandbox.stub(global.Services.prefs, "getPrefType").returns("integer"); + as._migratePref("oldPrefName", () => {}); + assert.calledWith(global.Services.prefs.getIntPref, "oldPrefName"); + + // Boolean + sandbox.stub(global.Services.prefs, "getBoolPref"); + global.Services.prefs.getPrefType.returns("boolean"); + as._migratePref("oldPrefName", () => {}); + assert.calledWith(global.Services.prefs.getBoolPref, "oldPrefName"); + + // String + sandbox.stub(global.Services.prefs, "getStringPref"); + global.Services.prefs.getPrefType.returns("string"); + as._migratePref("oldPrefName", () => {}); + assert.calledWith(global.Services.prefs.getStringPref, "oldPrefName"); + }); + it("should clear the old pref after setting the new one", () => { + sandbox.stub(global.Services.prefs, "prefHasUserValue").returns(true); + sandbox.stub(global.Services.prefs, "clearUserPref"); + sandbox.stub(global.Services.prefs, "getPrefType").returns("integer"); + as._migratePref("oldPrefName", () => {}); + assert.calledWith(global.Services.prefs.clearUserPref, "oldPrefName"); + }); + }); + describe("discoverystream.region-basic-layout config", () => { + let getStringPrefStub; + beforeEach(() => { + getStringPrefStub = sandbox.stub(global.Services.prefs, "getStringPref"); + sandbox.stub(global.Region, "home").get(() => "CA"); + sandbox + .stub(global.Services.locale, "appLocaleAsBCP47") + .get(() => "en-CA"); + }); + it("should enable 7 row layout pref if no basic config is set and no geo is set", () => { + getStringPrefStub + .withArgs( + "browser.newtabpage.activity-stream.discoverystream.region-basic-config" + ) + .returns(""); + sandbox.stub(global.Region, "home").get(() => ""); + + as._updateDynamicPrefs(); + + assert.isFalse( + PREFS_CONFIG.get("discoverystream.region-basic-layout").value + ); + }); + it("should enable 1 row layout pref based on region layout pref", () => { + getStringPrefStub + .withArgs( + "browser.newtabpage.activity-stream.discoverystream.region-basic-config" + ) + .returns("CA"); + + as._updateDynamicPrefs(); + + assert.isTrue( + PREFS_CONFIG.get("discoverystream.region-basic-layout").value + ); + }); + it("should enable 7 row layout pref based on region layout pref", () => { + getStringPrefStub + .withArgs( + "browser.newtabpage.activity-stream.discoverystream.region-basic-config" + ) + .returns(""); + + as._updateDynamicPrefs(); + + assert.isFalse( + PREFS_CONFIG.get("discoverystream.region-basic-layout").value + ); + }); + }); + describe("_updateDynamicPrefs topstories default value", () => { + let getVariableStub; + let getBoolPrefStub; + let appLocaleAsBCP47Stub; + beforeEach(() => { + getVariableStub = sandbox.stub( + global.NimbusFeatures.pocketNewtab, + "getVariable" + ); + appLocaleAsBCP47Stub = sandbox.stub( + global.Services.locale, + "appLocaleAsBCP47" + ); + + getBoolPrefStub = sandbox.stub(global.Services.prefs, "getBoolPref"); + getBoolPrefStub + .withArgs("browser.newtabpage.activity-stream.feeds.section.topstories") + .returns(true); + + appLocaleAsBCP47Stub.get(() => "en-US"); + + sandbox.stub(global.Region, "home").get(() => "US"); + + getVariableStub.withArgs("regionStoriesConfig").returns("US,CA"); + }); + it("should be false with no geo/locale", () => { + appLocaleAsBCP47Stub.get(() => ""); + sandbox.stub(global.Region, "home").get(() => ""); + + as._updateDynamicPrefs(); + + assert.isFalse(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + it("should be false with no geo but an allowed locale", () => { + appLocaleAsBCP47Stub.get(() => ""); + sandbox.stub(global.Region, "home").get(() => ""); + appLocaleAsBCP47Stub.get(() => "en-US"); + getVariableStub + .withArgs("localeListConfig") + .returns("en-US,en-CA,en-GB") + // We only have this pref set to trigger a close to real situation. + .withArgs( + "browser.newtabpage.activity-stream.discoverystream.region-stories-block" + ) + .returns("FR"); + + as._updateDynamicPrefs(); + + assert.isFalse(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + it("should be false with unexpected geo", () => { + sandbox.stub(global.Region, "home").get(() => "NOGEO"); + + as._updateDynamicPrefs(); + + assert.isFalse(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + it("should be false with expected geo and unexpected locale", () => { + appLocaleAsBCP47Stub.get(() => "no-LOCALE"); + + as._updateDynamicPrefs(); + + assert.isFalse(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + it("should be true with expected geo and locale", () => { + as._updateDynamicPrefs(); + assert.isTrue(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + it("should be false after expected geo and locale then unexpected", () => { + sandbox + .stub(global.Region, "home") + .onFirstCall() + .get(() => "US") + .onSecondCall() + .get(() => "NOGEO"); + + as._updateDynamicPrefs(); + as._updateDynamicPrefs(); + + assert.isFalse(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + it("should be true with updated pref change", () => { + appLocaleAsBCP47Stub.get(() => "en-GB"); + sandbox.stub(global.Region, "home").get(() => "GB"); + getVariableStub.withArgs("regionStoriesConfig").returns("GB"); + + as._updateDynamicPrefs(); + + assert.isTrue(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + it("should be true with allowed locale in non US region", () => { + appLocaleAsBCP47Stub.get(() => "en-CA"); + sandbox.stub(global.Region, "home").get(() => "DE"); + getVariableStub.withArgs("localeListConfig").returns("en-US,en-CA,en-GB"); + + as._updateDynamicPrefs(); + + assert.isTrue(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + }); + describe("_updateDynamicPrefs topstories delayed default value", () => { + let clock; + beforeEach(() => { + clock = sinon.useFakeTimers(); + + // Have addObserver cause prefHasUserValue to now return true then observe + sandbox + .stub(global.Services.obs, "addObserver") + .callsFake((pref, obs) => { + setTimeout(() => { + Services.obs.notifyObservers("US", "browser-region-updated"); + }); + }); + }); + afterEach(() => clock.restore()); + + it("should set false with unexpected geo", () => { + sandbox + .stub(global.Services.prefs, "getStringPref") + .withArgs("browser.search.region") + .returns("NOGEO"); + + as._updateDynamicPrefs(); + + clock.tick(1); + + assert.isFalse(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + it("should set true with expected geo and locale", () => { + sandbox + .stub(global.NimbusFeatures.pocketNewtab, "getVariable") + .withArgs("regionStoriesConfig") + .returns("US"); + + sandbox.stub(global.Services.prefs, "getBoolPref").returns(true); + sandbox + .stub(global.Services.locale, "appLocaleAsBCP47") + .get(() => "en-US"); + + as._updateDynamicPrefs(); + clock.tick(1); + + assert.isTrue(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + it("should not change default even with expected geo and locale", () => { + as._defaultPrefs.set("feeds.system.topstories", false); + sandbox + .stub(global.Services.prefs, "getStringPref") + .withArgs( + "browser.newtabpage.activity-stream.discoverystream.region-stories-config" + ) + .returns("US"); + + sandbox + .stub(global.Services.locale, "appLocaleAsBCP47") + .get(() => "en-US"); + + as._updateDynamicPrefs(); + clock.tick(1); + + assert.isFalse(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + it("should set false with geo blocked", () => { + sandbox + .stub(global.Services.prefs, "getStringPref") + .withArgs( + "browser.newtabpage.activity-stream.discoverystream.region-stories-config" + ) + .returns("US") + .withArgs( + "browser.newtabpage.activity-stream.discoverystream.region-stories-block" + ) + .returns("US"); + + sandbox.stub(global.Services.prefs, "getBoolPref").returns(true); + sandbox + .stub(global.Services.locale, "appLocaleAsBCP47") + .get(() => "en-US"); + + as._updateDynamicPrefs(); + clock.tick(1); + + assert.isFalse(PREFS_CONFIG.get("feeds.system.topstories").value); + }); + }); + describe("telemetry reporting on init failure", () => { + it("should send a ping on init error", () => { + as = new ActivityStream(); + const telemetry = { handleUndesiredEvent: sandbox.spy() }; + sandbox.stub(as.store, "init").throws(); + sandbox.stub(as.store.feeds, "get").returns(telemetry); + try { + as.init(); + } catch (e) {} + assert.calledOnce(telemetry.handleUndesiredEvent); + }); + }); + + describe("searchs shortcuts shouldPin pref", () => { + const SEARCH_SHORTCUTS_SEARCH_ENGINES_PREF = + "improvesearch.topSiteSearchShortcuts.searchEngines"; + let stub; + + beforeEach(() => { + stub = sandbox.stub(global.Region, "home"); + }); + + it("should be an empty string when no geo is available", () => { + stub.get(() => ""); + as._updateDynamicPrefs(); + assert.equal( + PREFS_CONFIG.get(SEARCH_SHORTCUTS_SEARCH_ENGINES_PREF).value, + "" + ); + }); + + it("should be 'baidu' in China", () => { + stub.get(() => "CN"); + as._updateDynamicPrefs(); + assert.equal( + PREFS_CONFIG.get(SEARCH_SHORTCUTS_SEARCH_ENGINES_PREF).value, + "baidu" + ); + }); + + it("should be 'yandex' in Russia, Belarus, Kazakhstan, and Turkey", () => { + const geos = ["BY", "KZ", "RU", "TR"]; + for (const geo of geos) { + stub.get(() => geo); + as._updateDynamicPrefs(); + assert.equal( + PREFS_CONFIG.get(SEARCH_SHORTCUTS_SEARCH_ENGINES_PREF).value, + "yandex" + ); + } + }); + + it("should be 'google,amazon' in Germany, France, the UK, Japan, Italy, and the US", () => { + const geos = ["DE", "FR", "GB", "IT", "JP", "US"]; + for (const geo of geos) { + stub.returns(geo); + as._updateDynamicPrefs(); + assert.equal( + PREFS_CONFIG.get(SEARCH_SHORTCUTS_SEARCH_ENGINES_PREF).value, + "google,amazon" + ); + } + }); + + it("should be 'google' elsewhere", () => { + // A selection of other geos + const geos = ["BR", "CA", "ES", "ID", "IN"]; + for (const geo of geos) { + stub.get(() => geo); + as._updateDynamicPrefs(); + assert.equal( + PREFS_CONFIG.get(SEARCH_SHORTCUTS_SEARCH_ENGINES_PREF).value, + "google" + ); + } + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/ActivityStreamMessageChannel.test.js b/browser/components/newtab/test/unit/lib/ActivityStreamMessageChannel.test.js new file mode 100644 index 0000000000..4bea86331d --- /dev/null +++ b/browser/components/newtab/test/unit/lib/ActivityStreamMessageChannel.test.js @@ -0,0 +1,445 @@ +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { + ActivityStreamMessageChannel, + DEFAULT_OPTIONS, +} from "lib/ActivityStreamMessageChannel.sys.mjs"; +import { addNumberReducer, GlobalOverrider } from "test/unit/utils"; +import { applyMiddleware, createStore } from "redux"; + +const OPTIONS = [ + "pageURL, outgoingMessageName", + "incomingMessageName", + "dispatch", +]; + +// Create an object containing details about a tab as expected within +// the loaded tabs map in ActivityStreamMessageChannel.jsm. +function getTabDetails(portID, url = "about:newtab", extraArgs = {}) { + let actor = { + portID, + sendAsyncMessage: sinon.spy(), + }; + let browser = { + getAttribute: () => (extraArgs.preloaded ? "preloaded" : ""), + ownerGlobal: {}, + }; + let browsingContext = { + top: { + embedderElement: browser, + }, + }; + + let data = { + data: { + actor, + browser, + browsingContext, + portID, + url, + }, + target: { + browsingContext, + }, + }; + + if (extraArgs.loaded) { + data.data.loaded = extraArgs.loaded; + } + if (extraArgs.simulated) { + data.data.simulated = extraArgs.simulated; + } + + return data; +} + +describe("ActivityStreamMessageChannel", () => { + let globals; + let dispatch; + let mm; + beforeEach(() => { + globals = new GlobalOverrider(); + globals.set("AboutNewTab", { + reset: globals.sandbox.spy(), + }); + globals.set("AboutHomeStartupCache", { onPreloadedNewTabMessage() {} }); + globals.set("AboutNewTabParent", { + flushQueuedMessagesFromContent: globals.sandbox.stub(), + }); + + dispatch = globals.sandbox.spy(); + mm = new ActivityStreamMessageChannel({ dispatch }); + + assert.ok(mm.loadedTabs, []); + + let loadedTabs = new Map(); + let sandbox = sinon.createSandbox(); + sandbox.stub(mm, "loadedTabs").get(() => loadedTabs); + }); + + afterEach(() => globals.restore()); + + describe("portID validation", () => { + let sandbox; + beforeEach(() => { + sandbox = sinon.createSandbox(); + sandbox.spy(global.console, "error"); + }); + afterEach(() => { + sandbox.restore(); + }); + it("should log errors for an invalid portID", () => { + mm.validatePortID({}); + mm.validatePortID({}); + mm.validatePortID({}); + + assert.equal(global.console.error.callCount, 3); + }); + }); + + it("should exist", () => { + assert.ok(ActivityStreamMessageChannel); + }); + it("should apply default options", () => { + mm = new ActivityStreamMessageChannel(); + OPTIONS.forEach(o => assert.equal(mm[o], DEFAULT_OPTIONS[o], o)); + }); + it("should add options", () => { + const options = { + dispatch: () => {}, + pageURL: "FOO.html", + outgoingMessageName: "OUT", + incomingMessageName: "IN", + }; + mm = new ActivityStreamMessageChannel(options); + OPTIONS.forEach(o => assert.equal(mm[o], options[o], o)); + }); + it("should throw an error if no dispatcher was provided", () => { + mm = new ActivityStreamMessageChannel(); + assert.throws(() => mm.dispatch({ type: "FOO" })); + }); + describe("Creating/destroying the channel", () => { + describe("#simulateMessagesForExistingTabs", () => { + beforeEach(() => { + sinon.stub(mm, "onActionFromContent"); + }); + it("should simulate init for existing ports", () => { + let msg1 = getTabDetails("inited", "about:monkeys", { + simulated: true, + }); + mm.loadedTabs.set(msg1.data.browser, msg1.data); + + let msg2 = getTabDetails("loaded", "about:sheep", { + simulated: true, + }); + mm.loadedTabs.set(msg2.data.browser, msg2.data); + + mm.simulateMessagesForExistingTabs(); + + assert.calledWith(mm.onActionFromContent.firstCall, { + type: at.NEW_TAB_INIT, + data: msg1.data, + }); + assert.calledWith(mm.onActionFromContent.secondCall, { + type: at.NEW_TAB_INIT, + data: msg2.data, + }); + }); + it("should simulate load for loaded ports", () => { + let msg3 = getTabDetails("foo", null, { + preloaded: true, + loaded: true, + }); + mm.loadedTabs.set(msg3.data.browser, msg3.data); + + mm.simulateMessagesForExistingTabs(); + + assert.calledWith( + mm.onActionFromContent, + { type: at.NEW_TAB_LOAD }, + "foo" + ); + }); + it("should set renderLayers on preloaded browsers after load", () => { + let msg4 = getTabDetails("foo", null, { + preloaded: true, + loaded: true, + }); + msg4.data.browser.ownerGlobal = { + STATE_MAXIMIZED: 1, + STATE_MINIMIZED: 2, + STATE_NORMAL: 3, + STATE_FULLSCREEN: 4, + windowState: 3, + isFullyOccluded: false, + }; + mm.loadedTabs.set(msg4.data.browser, msg4.data); + mm.simulateMessagesForExistingTabs(); + assert.equal(msg4.data.browser.renderLayers, true); + }); + it("should flush queued messages from content when doing the simulation", () => { + assert.notCalled( + global.AboutNewTabParent.flushQueuedMessagesFromContent + ); + mm.simulateMessagesForExistingTabs(); + assert.calledOnce( + global.AboutNewTabParent.flushQueuedMessagesFromContent + ); + }); + }); + }); + describe("Message handling", () => { + describe("#getTargetById", () => { + it("should get an id if it exists", () => { + let msg = getTabDetails("foo:1"); + mm.loadedTabs.set(msg.data.browser, msg.data); + assert.equal(mm.getTargetById("foo:1"), msg.data.actor); + }); + it("should return null if the target doesn't exist", () => { + let msg = getTabDetails("foo:2"); + mm.loadedTabs.set(msg.data.browser, msg.data); + assert.equal(mm.getTargetById("bar:3"), null); + }); + }); + describe("#getPreloadedActors", () => { + it("should get a preloaded actor if it exists", () => { + let msg = getTabDetails("foo:3", null, { preloaded: true }); + mm.loadedTabs.set(msg.data.browser, msg.data); + assert.equal(mm.getPreloadedActors()[0].portID, "foo:3"); + }); + it("should get all the preloaded actors across windows if they exist", () => { + let msg = getTabDetails("foo:4a", null, { preloaded: true }); + mm.loadedTabs.set(msg.data.browser, msg.data); + msg = getTabDetails("foo:4b", null, { preloaded: true }); + mm.loadedTabs.set(msg.data.browser, msg.data); + assert.equal(mm.getPreloadedActors().length, 2); + }); + it("should return null if there is no preloaded actor", () => { + let msg = getTabDetails("foo:5"); + mm.loadedTabs.set(msg.data.browser, msg.data); + assert.equal(mm.getPreloadedActors(), null); + }); + }); + describe("#onNewTabInit", () => { + it("should dispatch a NEW_TAB_INIT action", () => { + let msg = getTabDetails("foo", "about:monkeys"); + sinon.stub(mm, "onActionFromContent"); + + mm.onNewTabInit(msg, msg.data); + + assert.calledWith(mm.onActionFromContent, { + type: at.NEW_TAB_INIT, + data: msg.data, + }); + }); + }); + describe("#onNewTabLoad", () => { + it("should dispatch a NEW_TAB_LOAD action", () => { + let msg = getTabDetails("foo", null, { preloaded: true }); + mm.loadedTabs.set(msg.data.browser, msg.data); + sinon.stub(mm, "onActionFromContent"); + mm.onNewTabLoad({ target: msg.target }, msg.data); + assert.calledWith( + mm.onActionFromContent, + { type: at.NEW_TAB_LOAD }, + "foo" + ); + }); + }); + describe("#onNewTabUnload", () => { + it("should dispatch a NEW_TAB_UNLOAD action", () => { + let msg = getTabDetails("foo"); + mm.loadedTabs.set(msg.data.browser, msg.data); + sinon.stub(mm, "onActionFromContent"); + mm.onNewTabUnload({ target: msg.target }, msg.data); + assert.calledWith( + mm.onActionFromContent, + { type: at.NEW_TAB_UNLOAD }, + "foo" + ); + }); + }); + describe("#onMessage", () => { + let sandbox; + beforeEach(() => { + sandbox = sinon.createSandbox(); + sandbox.spy(global.console, "error"); + }); + afterEach(() => sandbox.restore()); + it("return early when tab details are not present", () => { + let msg = getTabDetails("foo"); + sinon.stub(mm, "onActionFromContent"); + mm.onMessage(msg, msg.data); + assert.notCalled(mm.onActionFromContent); + }); + it("should report an error if the msg.data is missing", () => { + let msg = getTabDetails("foo"); + mm.loadedTabs.set(msg.data.browser, msg.data); + let tabDetails = msg.data; + delete msg.data; + mm.onMessage(msg, tabDetails); + assert.calledOnce(global.console.error); + }); + it("should report an error if the msg.data.type is missing", () => { + let msg = getTabDetails("foo"); + mm.loadedTabs.set(msg.data.browser, msg.data); + msg.data = "foo"; + mm.onMessage(msg, msg.data); + assert.calledOnce(global.console.error); + }); + it("should call onActionFromContent", () => { + sinon.stub(mm, "onActionFromContent"); + let msg = getTabDetails("foo"); + mm.loadedTabs.set(msg.data.browser, msg.data); + let action = { + data: { data: {}, type: "FOO" }, + target: msg.target, + }; + const expectedAction = { + type: action.data.type, + data: action.data.data, + _target: { browser: msg.data.browser }, + }; + mm.onMessage(action, msg.data); + assert.calledWith(mm.onActionFromContent, expectedAction, "foo"); + }); + }); + }); + describe("Sending and broadcasting", () => { + describe("#send", () => { + it("should send a message on the right port", () => { + let msg = getTabDetails("foo:6"); + mm.loadedTabs.set(msg.data.browser, msg.data); + const action = ac.AlsoToOneContent({ type: "HELLO" }, "foo:6"); + mm.send(action); + assert.calledWith( + msg.data.actor.sendAsyncMessage, + DEFAULT_OPTIONS.outgoingMessageName, + action + ); + }); + it("should not throw if the target isn't around", () => { + // port is not added to the channel + const action = ac.AlsoToOneContent({ type: "HELLO" }, "foo:7"); + + assert.doesNotThrow(() => mm.send(action)); + }); + }); + describe("#broadcast", () => { + it("should send a message on the channel", () => { + let msg = getTabDetails("foo:8"); + mm.loadedTabs.set(msg.data.browser, msg.data); + const action = ac.BroadcastToContent({ type: "HELLO" }); + mm.broadcast(action); + assert.calledWith( + msg.data.actor.sendAsyncMessage, + DEFAULT_OPTIONS.outgoingMessageName, + action + ); + }); + }); + describe("#preloaded browser", () => { + it("should send the message to the preloaded browser if there's data and a preloaded browser exists", () => { + let msg = getTabDetails("foo:9", null, { preloaded: true }); + mm.loadedTabs.set(msg.data.browser, msg.data); + const action = ac.AlsoToPreloaded({ type: "HELLO", data: 10 }); + mm.sendToPreloaded(action); + assert.calledWith( + msg.data.actor.sendAsyncMessage, + DEFAULT_OPTIONS.outgoingMessageName, + action + ); + }); + it("should send the message to all the preloaded browsers if there's data and they exist", () => { + let msg1 = getTabDetails("foo:10a", null, { preloaded: true }); + mm.loadedTabs.set(msg1.data.browser, msg1.data); + + let msg2 = getTabDetails("foo:10b", null, { preloaded: true }); + mm.loadedTabs.set(msg2.data.browser, msg2.data); + + mm.sendToPreloaded(ac.AlsoToPreloaded({ type: "HELLO", data: 10 })); + assert.calledOnce(msg1.data.actor.sendAsyncMessage); + assert.calledOnce(msg2.data.actor.sendAsyncMessage); + }); + it("should not send the message to the preloaded browser if there's no data and a preloaded browser does not exists", () => { + let msg = getTabDetails("foo:11"); + mm.loadedTabs.set(msg.data.browser, msg.data); + const action = ac.AlsoToPreloaded({ type: "HELLO" }); + mm.sendToPreloaded(action); + assert.notCalled(msg.data.actor.sendAsyncMessage); + }); + }); + }); + describe("Handling actions", () => { + describe("#onActionFromContent", () => { + beforeEach(() => mm.onActionFromContent({ type: "FOO" }, "foo:12")); + it("should dispatch a AlsoToMain action", () => { + assert.calledOnce(dispatch); + const [action] = dispatch.firstCall.args; + assert.equal(action.type, "FOO", "action.type"); + }); + it("should have the right fromTarget", () => { + const [action] = dispatch.firstCall.args; + assert.equal(action.meta.fromTarget, "foo:12", "meta.fromTarget"); + }); + }); + describe("#middleware", () => { + let store; + beforeEach(() => { + store = createStore(addNumberReducer, applyMiddleware(mm.middleware)); + }); + it("should just call next if no channel is found", () => { + store.dispatch({ type: "ADD", data: 10 }); + assert.equal(store.getState(), 10); + }); + it("should call .send but not affect the main store if an OnlyToOneContent action is dispatched", () => { + sinon.stub(mm, "send"); + const action = ac.OnlyToOneContent({ type: "ADD", data: 10 }, "foo"); + + store.dispatch(action); + + assert.calledWith(mm.send, action); + assert.equal(store.getState(), 0); + }); + it("should call .send and update the main store if an AlsoToOneContent action is dispatched", () => { + sinon.stub(mm, "send"); + const action = ac.AlsoToOneContent({ type: "ADD", data: 10 }, "foo"); + + store.dispatch(action); + + assert.calledWith(mm.send, action); + assert.equal(store.getState(), 10); + }); + it("should call .broadcast if the action is BroadcastToContent", () => { + sinon.stub(mm, "broadcast"); + const action = ac.BroadcastToContent({ type: "FOO" }); + + store.dispatch(action); + + assert.calledWith(mm.broadcast, action); + }); + it("should call .sendToPreloaded if the action is AlsoToPreloaded", () => { + sinon.stub(mm, "sendToPreloaded"); + const action = ac.AlsoToPreloaded({ type: "FOO" }); + + store.dispatch(action); + + assert.calledWith(mm.sendToPreloaded, action); + }); + it("should dispatch other actions normally", () => { + sinon.stub(mm, "send"); + sinon.stub(mm, "broadcast"); + sinon.stub(mm, "sendToPreloaded"); + + store.dispatch({ type: "ADD", data: 1 }); + + assert.equal(store.getState(), 1); + assert.notCalled(mm.send); + assert.notCalled(mm.broadcast); + assert.notCalled(mm.sendToPreloaded); + }); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/ActivityStreamPrefs.test.js b/browser/components/newtab/test/unit/lib/ActivityStreamPrefs.test.js new file mode 100644 index 0000000000..bff1708ef7 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/ActivityStreamPrefs.test.js @@ -0,0 +1,113 @@ +import { DefaultPrefs, Prefs } from "lib/ActivityStreamPrefs.sys.mjs"; + +const TEST_PREF_CONFIG = new Map([ + ["foo", { value: true }], + ["bar", { value: "BAR" }], + ["baz", { value: 1 }], + ["qux", { value: "foo", value_local_dev: "foofoo" }], +]); + +describe("ActivityStreamPrefs", () => { + describe("Prefs", () => { + let p; + beforeEach(() => { + p = new Prefs(); + }); + it("should have get, set, and observe methods", () => { + assert.property(p, "get"); + assert.property(p, "set"); + assert.property(p, "observe"); + }); + describe("#observeBranch", () => { + let listener; + beforeEach(() => { + p._prefBranch = { addObserver: sinon.stub() }; + listener = { onPrefChanged: sinon.stub() }; + p.observeBranch(listener); + }); + it("should add an observer", () => { + assert.calledOnce(p._prefBranch.addObserver); + assert.calledWith(p._prefBranch.addObserver, ""); + }); + it("should store the listener", () => { + assert.equal(p._branchObservers.size, 1); + assert.ok(p._branchObservers.has(listener)); + }); + it("should call listener's onPrefChanged", () => { + p._branchObservers.get(listener)(); + + assert.calledOnce(listener.onPrefChanged); + }); + }); + describe("#ignoreBranch", () => { + let listener; + beforeEach(() => { + p._prefBranch = { + addObserver: sinon.stub(), + removeObserver: sinon.stub(), + }; + listener = {}; + p.observeBranch(listener); + }); + it("should remove the observer", () => { + p.ignoreBranch(listener); + + assert.calledOnce(p._prefBranch.removeObserver); + assert.calledWith( + p._prefBranch.removeObserver, + p._prefBranch.addObserver.firstCall.args[0] + ); + }); + it("should remove the listener", () => { + assert.equal(p._branchObservers.size, 1); + + p.ignoreBranch(listener); + + assert.equal(p._branchObservers.size, 0); + }); + }); + }); + + describe("DefaultPrefs", () => { + describe("#init", () => { + let defaultPrefs; + let sandbox; + beforeEach(() => { + sandbox = sinon.createSandbox(); + defaultPrefs = new DefaultPrefs(TEST_PREF_CONFIG); + sinon.stub(defaultPrefs, "set"); + }); + afterEach(() => { + sandbox.restore(); + }); + it("should initialize a boolean pref", () => { + defaultPrefs.init(); + assert.calledWith(defaultPrefs.set, "foo", true); + }); + it("should not initialize a pref if a default exists", () => { + defaultPrefs.prefs.set("foo", false); + + defaultPrefs.init(); + + assert.neverCalledWith(defaultPrefs.set, "foo", true); + }); + it("should initialize a string pref", () => { + defaultPrefs.init(); + assert.calledWith(defaultPrefs.set, "bar", "BAR"); + }); + it("should initialize a integer pref", () => { + defaultPrefs.init(); + assert.calledWith(defaultPrefs.set, "baz", 1); + }); + it("should initialize a pref with value if Firefox is not a local build", () => { + defaultPrefs.init(); + assert.calledWith(defaultPrefs.set, "qux", "foo"); + }); + it("should initialize a pref with value_local_dev if Firefox is a local build", () => { + sandbox.stub(global.AppConstants, "MOZILLA_OFFICIAL").value(false); + defaultPrefs.init(); + assert.calledWith(defaultPrefs.set, "qux", "foofoo"); + }); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/ActivityStreamStorage.test.js b/browser/components/newtab/test/unit/lib/ActivityStreamStorage.test.js new file mode 100644 index 0000000000..0b8baef762 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/ActivityStreamStorage.test.js @@ -0,0 +1,161 @@ +import { ActivityStreamStorage } from "lib/ActivityStreamStorage.sys.mjs"; +import { GlobalOverrider } from "test/unit/utils"; + +let overrider = new GlobalOverrider(); + +describe("ActivityStreamStorage", () => { + let sandbox; + let indexedDB; + let storage; + beforeEach(() => { + sandbox = sinon.createSandbox(); + indexedDB = { + open: sandbox.stub().resolves({}), + deleteDatabase: sandbox.stub().resolves(), + }; + overrider.set({ IndexedDB: indexedDB }); + storage = new ActivityStreamStorage({ + storeNames: ["storage_test"], + telemetry: { handleUndesiredEvent: sandbox.stub() }, + }); + }); + afterEach(() => { + sandbox.restore(); + }); + it("should throw if required arguments not provided", () => { + assert.throws(() => new ActivityStreamStorage({ telemetry: true })); + }); + describe(".db", () => { + it("should not throw an error when accessing db", async () => { + assert.ok(storage.db); + }); + + it("should delete and recreate the db if opening db fails", async () => { + const newDb = {}; + indexedDB.open.onFirstCall().rejects(new Error("fake error")); + indexedDB.open.onSecondCall().resolves(newDb); + + const db = await storage.db; + assert.calledOnce(indexedDB.deleteDatabase); + assert.calledTwice(indexedDB.open); + assert.equal(db, newDb); + }); + }); + describe("#getDbTable", () => { + let testStorage; + let storeStub; + beforeEach(() => { + storeStub = { + getAll: sandbox.stub().resolves(), + get: sandbox.stub().resolves(), + put: sandbox.stub().resolves(), + }; + sandbox.stub(storage, "_getStore").resolves(storeStub); + testStorage = storage.getDbTable("storage_test"); + }); + it("should reverse key value parameters for put", async () => { + await testStorage.set("key", "value"); + + assert.calledOnce(storeStub.put); + assert.calledWith(storeStub.put, "value", "key"); + }); + it("should return the correct value for get", async () => { + storeStub.get.withArgs("foo").resolves("foo"); + + const result = await testStorage.get("foo"); + + assert.calledOnce(storeStub.get); + assert.equal(result, "foo"); + }); + it("should return the correct value for getAll", async () => { + storeStub.getAll.resolves(["bar"]); + + const result = await testStorage.getAll(); + + assert.calledOnce(storeStub.getAll); + assert.deepEqual(result, ["bar"]); + }); + it("should query the correct object store", async () => { + await testStorage.get(); + + assert.calledOnce(storage._getStore); + assert.calledWithExactly(storage._getStore, "storage_test"); + }); + it("should throw if table is not found", () => { + assert.throws(() => storage.getDbTable("undefined_store")); + }); + }); + it("should get the correct objectStore when calling _getStore", async () => { + const objectStoreStub = sandbox.stub(); + indexedDB.open.resolves({ objectStore: objectStoreStub }); + + await storage._getStore("foo"); + + assert.calledOnce(objectStoreStub); + assert.calledWithExactly(objectStoreStub, "foo", "readwrite"); + }); + it("should create a db with the correct store name", async () => { + const dbStub = { + createObjectStore: sandbox.stub(), + objectStoreNames: { contains: sandbox.stub().returns(false) }, + }; + await storage.db; + + // call the cb with a stub + indexedDB.open.args[0][2](dbStub); + + assert.calledOnce(dbStub.createObjectStore); + assert.calledWithExactly(dbStub.createObjectStore, "storage_test"); + }); + it("should handle an array of object store names", async () => { + storage = new ActivityStreamStorage({ + storeNames: ["store1", "store2"], + telemetry: {}, + }); + const dbStub = { + createObjectStore: sandbox.stub(), + objectStoreNames: { contains: sandbox.stub().returns(false) }, + }; + await storage.db; + + // call the cb with a stub + indexedDB.open.args[0][2](dbStub); + + assert.calledTwice(dbStub.createObjectStore); + assert.calledWith(dbStub.createObjectStore, "store1"); + assert.calledWith(dbStub.createObjectStore, "store2"); + }); + it("should skip creating existing stores", async () => { + storage = new ActivityStreamStorage({ + storeNames: ["store1", "store2"], + telemetry: {}, + }); + const dbStub = { + createObjectStore: sandbox.stub(), + objectStoreNames: { contains: sandbox.stub().returns(true) }, + }; + await storage.db; + + // call the cb with a stub + indexedDB.open.args[0][2](dbStub); + + assert.notCalled(dbStub.createObjectStore); + }); + describe("#_requestWrapper", () => { + it("should return a successful result", async () => { + const result = await storage._requestWrapper(() => + Promise.resolve("foo") + ); + + assert.equal(result, "foo"); + assert.notCalled(storage.telemetry.handleUndesiredEvent); + }); + it("should report failures", async () => { + try { + await storage._requestWrapper(() => Promise.reject(new Error())); + } catch (e) { + assert.calledOnce(storage.telemetry.handleUndesiredEvent); + } + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/DiscoveryStreamFeed.test.js b/browser/components/newtab/test/unit/lib/DiscoveryStreamFeed.test.js new file mode 100644 index 0000000000..92e10facb3 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/DiscoveryStreamFeed.test.js @@ -0,0 +1,3523 @@ +import { + actionCreators as ac, + actionTypes as at, + actionUtils as au, +} from "common/Actions.sys.mjs"; +import { combineReducers, createStore } from "redux"; +import { GlobalOverrider } from "test/unit/utils"; +import { DiscoveryStreamFeed } from "lib/DiscoveryStreamFeed.sys.mjs"; +import { RecommendationProvider } from "lib/RecommendationProvider.sys.mjs"; +import { reducers } from "common/Reducers.sys.mjs"; + +import { PersistentCache } from "lib/PersistentCache.sys.mjs"; + +const CONFIG_PREF_NAME = "discoverystream.config"; +const ENDPOINTS_PREF_NAME = "discoverystream.endpoints"; +const DUMMY_ENDPOINT = "https://getpocket.cdn.mozilla.net/dummy"; +const SPOC_IMPRESSION_TRACKING_PREF = "discoverystream.spoc.impressions"; +const REC_IMPRESSION_TRACKING_PREF = "discoverystream.rec.impressions"; +const THIRTY_MINUTES = 30 * 60 * 1000; +const ONE_WEEK = 7 * 24 * 60 * 60 * 1000; // 1 week + +const FAKE_UUID = "{foo-123-foo}"; + +// eslint-disable-next-line max-statements +describe("DiscoveryStreamFeed", () => { + let feed; + let feeds; + let recommendationProvider; + let sandbox; + let fetchStub; + let clock; + let fakeNewTabUtils; + let fakePktApi; + let globals; + + const setPref = (name, value) => { + const action = { + type: at.PREF_CHANGED, + data: { + name, + value: typeof value === "object" ? JSON.stringify(value) : value, + }, + }; + feed.store.dispatch(action); + feed.onAction(action); + }; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + + // Fetch + fetchStub = sandbox.stub(global, "fetch"); + + // Time + clock = sinon.useFakeTimers(); + + globals = new GlobalOverrider(); + globals.set({ + gUUIDGenerator: { generateUUID: () => FAKE_UUID }, + PersistentCache, + }); + + sandbox + .stub(global.Services.prefs, "getBoolPref") + .withArgs("browser.newtabpage.activity-stream.discoverystream.enabled") + .returns(true); + + recommendationProvider = new RecommendationProvider(); + recommendationProvider.store = createStore(combineReducers(reducers), {}); + feeds = { + "feeds.recommendationprovider": recommendationProvider, + }; + + // Feed + feed = new DiscoveryStreamFeed(); + feed.store = createStore(combineReducers(reducers), { + Prefs: { + values: { + [CONFIG_PREF_NAME]: JSON.stringify({ + enabled: false, + }), + [ENDPOINTS_PREF_NAME]: DUMMY_ENDPOINT, + "discoverystream.enabled": true, + "feeds.section.topstories": true, + "feeds.system.topstories": true, + "discoverystream.spocs.personalized": true, + "discoverystream.recs.personalized": true, + "system.showSponsored": false, + }, + }, + }); + feed.store.feeds = { + get: name => feeds[name], + }; + global.fetch.resetHistory(); + + sandbox.stub(feed, "_maybeUpdateCachedData").resolves(); + + globals.set("setTimeout", callback => { + callback(); + }); + + fakeNewTabUtils = { + blockedLinks: { + links: [], + isBlocked: () => false, + }, + }; + globals.set("NewTabUtils", fakeNewTabUtils); + + fakePktApi = { + isUserLoggedIn: () => false, + getRecentSavesCache: () => null, + getRecentSaves: () => null, + }; + globals.set("pktApi", fakePktApi); + }); + + afterEach(() => { + clock.restore(); + sandbox.restore(); + globals.restore(); + }); + + describe("#fetchFromEndpoint", () => { + beforeEach(() => { + feed._prefCache = { + config: { + api_key_pref: "", + }, + }; + fetchStub.resolves({ + json: () => Promise.resolve("hi"), + ok: true, + }); + }); + it("should get a response", async () => { + const response = await feed.fetchFromEndpoint(DUMMY_ENDPOINT); + + assert.equal(response, "hi"); + }); + it("should not send cookies", async () => { + await feed.fetchFromEndpoint(DUMMY_ENDPOINT); + + assert.propertyVal(fetchStub.firstCall.args[1], "credentials", "omit"); + }); + it("should allow unexpected response", async () => { + fetchStub.resolves({ ok: false }); + + const response = await feed.fetchFromEndpoint(DUMMY_ENDPOINT); + + assert.equal(response, null); + }); + it("should disallow unexpected endpoints", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { + [ENDPOINTS_PREF_NAME]: "https://other.site", + }, + }, + }); + + const response = await feed.fetchFromEndpoint(DUMMY_ENDPOINT); + + assert.equal(response, null); + }); + it("should allow multiple endpoints", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { + [ENDPOINTS_PREF_NAME]: `https://other.site,${DUMMY_ENDPOINT}`, + }, + }, + }); + + const response = await feed.fetchFromEndpoint(DUMMY_ENDPOINT); + + assert.equal(response, "hi"); + }); + it("should replace urls with $apiKey", async () => { + sandbox.stub(global.Services.prefs, "getCharPref").returns("replaced"); + + await feed.fetchFromEndpoint( + "https://getpocket.cdn.mozilla.net/dummy?consumer_key=$apiKey" + ); + + assert.calledWithMatch( + fetchStub, + "https://getpocket.cdn.mozilla.net/dummy?consumer_key=replaced", + { credentials: "omit" } + ); + }); + it("should replace locales with $locale", async () => { + feed.locale = "replaced"; + await feed.fetchFromEndpoint( + "https://getpocket.cdn.mozilla.net/dummy?locale_lang=$locale" + ); + + assert.calledWithMatch( + fetchStub, + "https://getpocket.cdn.mozilla.net/dummy?locale_lang=replaced", + { credentials: "omit" } + ); + }); + it("should allow POST and with other options", async () => { + await feed.fetchFromEndpoint("https://getpocket.cdn.mozilla.net/dummy", { + method: "POST", + body: "{}", + }); + + assert.calledWithMatch( + fetchStub, + "https://getpocket.cdn.mozilla.net/dummy", + { + credentials: "omit", + method: "POST", + body: "{}", + } + ); + }); + }); + + describe("#setupPocketState", () => { + it("should setup logged in state and recent saves with cache", async () => { + fakePktApi.isUserLoggedIn = () => true; + fakePktApi.getRecentSavesCache = () => [1, 2, 3]; + sandbox.spy(feed.store, "dispatch"); + await feed.setupPocketState({}); + assert.calledTwice(feed.store.dispatch); + assert.calledWith( + feed.store.dispatch.firstCall, + ac.OnlyToOneContent( + { + type: at.DISCOVERY_STREAM_POCKET_STATE_SET, + data: { isUserLoggedIn: true }, + }, + {} + ) + ); + assert.calledWith( + feed.store.dispatch.secondCall, + ac.OnlyToOneContent( + { + type: at.DISCOVERY_STREAM_RECENT_SAVES, + data: { recentSaves: [1, 2, 3] }, + }, + {} + ) + ); + }); + it("should setup logged in state and recent saves without cache", async () => { + fakePktApi.isUserLoggedIn = () => true; + fakePktApi.getRecentSaves = ({ success }) => success([1, 2, 3]); + sandbox.spy(feed.store, "dispatch"); + await feed.setupPocketState({}); + assert.calledTwice(feed.store.dispatch); + assert.calledWith( + feed.store.dispatch.firstCall, + ac.OnlyToOneContent( + { + type: at.DISCOVERY_STREAM_POCKET_STATE_SET, + data: { isUserLoggedIn: true }, + }, + {} + ) + ); + assert.calledWith( + feed.store.dispatch.secondCall, + ac.OnlyToOneContent( + { + type: at.DISCOVERY_STREAM_RECENT_SAVES, + data: { recentSaves: [1, 2, 3] }, + }, + {} + ) + ); + }); + }); + + describe("#getOrCreateImpressionId", () => { + it("should create impression id in constructor", async () => { + assert.equal(feed._impressionId, FAKE_UUID); + }); + it("should create impression id if none exists", async () => { + sandbox.stub(global.Services.prefs, "getCharPref").returns(""); + sandbox.stub(global.Services.prefs, "setCharPref").returns(); + + const result = feed.getOrCreateImpressionId(); + + assert.equal(result, FAKE_UUID); + assert.calledOnce(global.Services.prefs.setCharPref); + }); + it("should use impression id if exists", async () => { + sandbox.stub(global.Services.prefs, "getCharPref").returns("from get"); + + const result = feed.getOrCreateImpressionId(); + + assert.equal(result, "from get"); + assert.calledOnce(global.Services.prefs.getCharPref); + }); + }); + + describe("#parseGridPositions", () => { + it("should return an equivalent array for an array of non negative integers", async () => { + assert.deepEqual(feed.parseGridPositions([0, 2, 3]), [0, 2, 3]); + }); + it("should return undefined for an array containing negative integers", async () => { + assert.equal(feed.parseGridPositions([-2, 2, 3]), undefined); + }); + it("should return undefined for an undefined input", async () => { + assert.equal(feed.parseGridPositions(undefined), undefined); + }); + }); + + describe("#loadLayout", () => { + it("should use local basic layout with hardcoded_basic_layout being true", async () => { + feed.config.hardcoded_basic_layout = true; + + await feed.loadLayout(feed.store.dispatch); + + assert.equal( + feed.store.getState().DiscoveryStream.spocs.spocs_endpoint, + "https://spocs.getpocket.com/spocs" + ); + const { layout } = feed.store.getState().DiscoveryStream; + assert.equal(layout[0].components[2].properties.items, 3); + }); + it("should use 1 row layout if specified", async () => { + feed.store = createStore(combineReducers(reducers), { + Prefs: { + values: { + [CONFIG_PREF_NAME]: JSON.stringify({ + enabled: true, + }), + [ENDPOINTS_PREF_NAME]: DUMMY_ENDPOINT, + "discoverystream.enabled": true, + "discoverystream.region-basic-layout": true, + "system.showSponsored": false, + }, + }, + }); + + await feed.loadLayout(feed.store.dispatch); + + const { layout } = feed.store.getState().DiscoveryStream; + assert.equal(layout[0].components[2].properties.items, 3); + }); + it("should use 7 row layout if specified", async () => { + feed.store = createStore(combineReducers(reducers), { + Prefs: { + values: { + [CONFIG_PREF_NAME]: JSON.stringify({ + enabled: true, + }), + [ENDPOINTS_PREF_NAME]: DUMMY_ENDPOINT, + "discoverystream.enabled": true, + "discoverystream.region-basic-layout": false, + "system.showSponsored": false, + }, + }, + }); + + await feed.loadLayout(feed.store.dispatch); + + const { layout } = feed.store.getState().DiscoveryStream; + assert.equal(layout[0].components[2].properties.items, 21); + }); + it("should use new spocs endpoint if in the config", async () => { + feed.config.spocs_endpoint = "https://spocs.getpocket.com/spocs2"; + + await feed.loadLayout(feed.store.dispatch); + + assert.equal( + feed.store.getState().DiscoveryStream.spocs.spocs_endpoint, + "https://spocs.getpocket.com/spocs2" + ); + }); + it("should use local basic layout with FF pref hardcoded_basic_layout", async () => { + feed.store = createStore(combineReducers(reducers), { + Prefs: { + values: { + [CONFIG_PREF_NAME]: JSON.stringify({ + enabled: false, + }), + [ENDPOINTS_PREF_NAME]: DUMMY_ENDPOINT, + "discoverystream.enabled": true, + "discoverystream.hardcoded-basic-layout": true, + "system.showSponsored": false, + }, + }, + }); + + await feed.loadLayout(feed.store.dispatch); + + assert.equal( + feed.store.getState().DiscoveryStream.spocs.spocs_endpoint, + "https://spocs.getpocket.com/spocs" + ); + const { layout } = feed.store.getState().DiscoveryStream; + assert.equal(layout[0].components[2].properties.items, 3); + }); + it("should use new spocs endpoint if in a FF pref", async () => { + feed.store = createStore(combineReducers(reducers), { + Prefs: { + values: { + [CONFIG_PREF_NAME]: JSON.stringify({ + enabled: false, + }), + [ENDPOINTS_PREF_NAME]: DUMMY_ENDPOINT, + "discoverystream.enabled": true, + "discoverystream.spocs-endpoint": + "https://spocs.getpocket.com/spocs2", + "system.showSponsored": false, + }, + }, + }); + + await feed.loadLayout(feed.store.dispatch); + + assert.equal( + feed.store.getState().DiscoveryStream.spocs.spocs_endpoint, + "https://spocs.getpocket.com/spocs2" + ); + }); + it("should return enough stories to fill a four card layout", async () => { + feed.store = createStore(combineReducers(reducers), { + Prefs: { + values: { + pocketConfig: { fourCardLayout: true }, + }, + }, + }); + + await feed.loadLayout(feed.store.dispatch); + + const { layout } = feed.store.getState().DiscoveryStream; + assert.equal(layout[0].components[2].properties.items, 24); + }); + it("should create a layout with spoc and widget positions", async () => { + feed.store = createStore(combineReducers(reducers), { + Prefs: { + values: { + pocketConfig: { + spocPositions: "1, 2", + widgetPositions: "3, 4", + }, + }, + }, + }); + + await feed.loadLayout(feed.store.dispatch); + + const { layout } = feed.store.getState().DiscoveryStream; + assert.deepEqual(layout[0].components[2].spocs.positions, [ + { index: 1 }, + { index: 2 }, + ]); + assert.deepEqual(layout[0].components[2].widgets.positions, [ + { index: 3 }, + { index: 4 }, + ]); + }); + it("should create a layout with spoc position data", async () => { + feed.store = createStore(combineReducers(reducers), { + Prefs: { + values: { + pocketConfig: { + spocAdTypes: "1230", + spocZoneIds: "4560, 7890", + }, + }, + }, + }); + + await feed.loadLayout(feed.store.dispatch); + + const { layout } = feed.store.getState().DiscoveryStream; + assert.deepEqual(layout[0].components[2].placement.ad_types, [1230]); + assert.deepEqual( + layout[0].components[2].placement.zone_ids, + [4560, 7890] + ); + }); + it("should create a layout with spoc topsite position data", async () => { + feed.store = createStore(combineReducers(reducers), { + Prefs: { + values: { + pocketConfig: { + spocTopsitesPlacementEnabled: true, + spocTopsitesAdTypes: "1230", + spocTopsitesZoneIds: "4560, 7890", + }, + }, + }, + }); + + await feed.loadLayout(feed.store.dispatch); + + const { layout } = feed.store.getState().DiscoveryStream; + assert.deepEqual(layout[0].components[0].placement.ad_types, [1230]); + assert.deepEqual( + layout[0].components[0].placement.zone_ids, + [4560, 7890] + ); + }); + it("should create a layout with proper spoc url with a site id", async () => { + feed.store = createStore(combineReducers(reducers), { + Prefs: { + values: { + pocketConfig: { + spocSiteId: "1234", + }, + }, + }, + }); + + await feed.loadLayout(feed.store.dispatch); + const { spocs } = feed.store.getState().DiscoveryStream; + assert.deepEqual( + spocs.spocs_endpoint, + "https://spocs.getpocket.com/spocs?site=1234" + ); + }); + }); + + describe("#updatePlacements", () => { + it("should dispatch DISCOVERY_STREAM_SPOCS_PLACEMENTS", () => { + sandbox.spy(feed.store, "dispatch"); + feed.store.getState = () => ({ + Prefs: { + values: { showSponsored: true, "system.showSponsored": true }, + }, + }); + const fakeComponents = { + components: [ + { placement: { name: "first" }, spocs: {} }, + { placement: { name: "second" }, spocs: {} }, + ], + }; + const fakeLayout = [fakeComponents]; + + feed.updatePlacements(feed.store.dispatch, fakeLayout); + + assert.calledOnce(feed.store.dispatch); + assert.calledWith(feed.store.dispatch, { + type: "DISCOVERY_STREAM_SPOCS_PLACEMENTS", + data: { placements: [{ name: "first" }, { name: "second" }] }, + meta: { isStartup: false }, + }); + }); + it("should dispatch DISCOVERY_STREAM_SPOCS_PLACEMENTS with prefs array", () => { + sandbox.spy(feed.store, "dispatch"); + feed.store.getState = () => ({ + Prefs: { + values: { + showSponsored: true, + withPref: true, + "system.showSponsored": true, + }, + }, + }); + const fakeComponents = { + components: [ + { placement: { name: "withPref" }, spocs: { prefs: ["withPref"] } }, + { placement: { name: "withoutPref1" }, spocs: {} }, + { + placement: { name: "withoutPref2" }, + spocs: { prefs: ["whatever"] }, + }, + { placement: { name: "withoutPref3" }, spocs: { prefs: [] } }, + ], + }; + const fakeLayout = [fakeComponents]; + + feed.updatePlacements(feed.store.dispatch, fakeLayout); + + assert.calledOnce(feed.store.dispatch); + assert.calledWith(feed.store.dispatch, { + type: "DISCOVERY_STREAM_SPOCS_PLACEMENTS", + data: { placements: [{ name: "withPref" }, { name: "withoutPref1" }] }, + meta: { isStartup: false }, + }); + }); + it("should fire update placements from loadLayout", async () => { + sandbox.spy(feed, "updatePlacements"); + + await feed.loadLayout(feed.store.dispatch); + + assert.calledOnce(feed.updatePlacements); + }); + }); + + describe("#placementsForEach", () => { + it("should forEach through placements", () => { + feed.store.getState = () => ({ + DiscoveryStream: { + spocs: { + placements: [{ name: "first" }, { name: "second" }], + }, + }, + }); + + let items = []; + + feed.placementsForEach(item => items.push(item.name)); + + assert.deepEqual(items, ["first", "second"]); + }); + }); + + describe("#loadComponentFeeds", () => { + let fakeCache; + let fakeDiscoveryStream; + beforeEach(() => { + fakeDiscoveryStream = { + Prefs: {}, + DiscoveryStream: { + layout: [ + { components: [{ feed: { url: "foo.com" } }] }, + { components: [{}] }, + {}, + ], + }, + }; + fakeCache = {}; + sandbox.stub(feed.store, "getState").returns(fakeDiscoveryStream); + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("should not dispatch updates when layout is not defined", async () => { + fakeDiscoveryStream = { + DiscoveryStream: {}, + }; + feed.store.getState.returns(fakeDiscoveryStream); + sandbox.spy(feed.store, "dispatch"); + + await feed.loadComponentFeeds(feed.store.dispatch); + + assert.notCalled(feed.store.dispatch); + }); + + it("should populate feeds cache", async () => { + fakeCache = { + feeds: { "foo.com": { lastUpdated: Date.now(), data: "data" } }, + }; + sandbox.stub(feed.cache, "get").returns(Promise.resolve(fakeCache)); + + await feed.loadComponentFeeds(feed.store.dispatch); + + assert.calledWith(feed.cache.set, "feeds", { + "foo.com": { data: "data", lastUpdated: 0 }, + }); + }); + + it("should send feed update events with new feed data", async () => { + sandbox.stub(feed.cache, "get").returns(Promise.resolve(fakeCache)); + sandbox.spy(feed.store, "dispatch"); + feed._prefCache = { + config: { + api_key_pref: "", + }, + }; + + await feed.loadComponentFeeds(feed.store.dispatch); + + assert.calledWith(feed.store.dispatch.firstCall, { + type: at.DISCOVERY_STREAM_FEED_UPDATE, + data: { feed: { data: { status: "failed" } }, url: "foo.com" }, + meta: { isStartup: false }, + }); + assert.calledWith(feed.store.dispatch.secondCall, { + type: at.DISCOVERY_STREAM_FEEDS_UPDATE, + meta: { isStartup: false }, + }); + }); + + it("should return number of promises equal to unique urls", async () => { + sandbox.stub(feed.cache, "get").returns(Promise.resolve(fakeCache)); + sandbox.stub(global.Promise, "all").resolves(); + fakeDiscoveryStream = { + DiscoveryStream: { + layout: [ + { + components: [ + { feed: { url: "foo.com" } }, + { feed: { url: "bar.com" } }, + ], + }, + { components: [{ feed: { url: "foo.com" } }] }, + {}, + { components: [{ feed: { url: "baz.com" } }] }, + ], + }, + }; + feed.store.getState.returns(fakeDiscoveryStream); + + await feed.loadComponentFeeds(feed.store.dispatch); + + assert.calledOnce(global.Promise.all); + const { args } = global.Promise.all.firstCall; + assert.equal(args[0].length, 3); + }); + }); + + describe("#getComponentFeed", () => { + it("should fetch fresh feed data if cache is empty", async () => { + const fakeCache = {}; + sandbox.stub(feed.cache, "get").returns(Promise.resolve(fakeCache)); + sandbox.stub(feed, "rotate").callsFake(val => val); + sandbox + .stub(feed, "scoreItems") + .callsFake(val => ({ data: val, filtered: [], personalized: false })); + sandbox.stub(feed, "fetchFromEndpoint").resolves({ + recommendations: "data", + settings: { + recsExpireTime: 1, + }, + }); + + const feedResp = await feed.getComponentFeed("foo.com"); + + assert.equal(feedResp.data.recommendations, "data"); + }); + it("should fetch fresh feed data if cache is old", async () => { + const fakeCache = { feeds: { "foo.com": { lastUpdated: Date.now() } } }; + sandbox.stub(feed.cache, "get").returns(Promise.resolve(fakeCache)); + sandbox.stub(feed, "fetchFromEndpoint").resolves({ + recommendations: "data", + settings: { + recsExpireTime: 1, + }, + }); + sandbox.stub(feed, "rotate").callsFake(val => val); + sandbox + .stub(feed, "scoreItems") + .callsFake(val => ({ data: val, filtered: [], personalized: false })); + clock.tick(THIRTY_MINUTES + 1); + + const feedResp = await feed.getComponentFeed("foo.com"); + + assert.equal(feedResp.data.recommendations, "data"); + }); + it("should return feed data from cache if it is fresh", async () => { + const fakeCache = { + feeds: { "foo.com": { lastUpdated: Date.now(), data: "data" } }, + }; + sandbox.stub(feed.cache, "get").resolves(fakeCache); + sandbox.stub(feed, "fetchFromEndpoint").resolves("old data"); + clock.tick(THIRTY_MINUTES - 1); + + const feedResp = await feed.getComponentFeed("foo.com"); + + assert.equal(feedResp.data, "data"); + }); + it("should return null if no response was received", async () => { + sandbox.stub(feed, "fetchFromEndpoint").resolves(null); + + const feedResp = await feed.getComponentFeed("foo.com"); + + assert.deepEqual(feedResp, { data: { status: "failed" } }); + }); + }); + + describe("#loadSpocs", () => { + beforeEach(() => { + feed._prefCache = { + config: { + api_key_pref: "", + }, + }; + + sandbox.stub(feed, "getPlacements").returns([{ name: "spocs" }]); + Object.defineProperty(feed, "showSpocs", { get: () => true }); + }); + it("should not fetch or update cache if no spocs endpoint is defined", async () => { + feed.store.dispatch( + ac.BroadcastToContent({ + type: at.DISCOVERY_STREAM_SPOCS_ENDPOINT, + data: "", + }) + ); + + sandbox.spy(feed.cache, "set"); + + await feed.loadSpocs(feed.store.dispatch); + + assert.notCalled(global.fetch); + assert.calledWith(feed.cache.set, "spocs", { lastUpdated: 0, spocs: {} }); + }); + it("should fetch fresh spocs data if cache is empty", async () => { + sandbox.stub(feed.cache, "get").returns(Promise.resolve()); + sandbox.stub(feed, "fetchFromEndpoint").resolves({ placement: "data" }); + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + + await feed.loadSpocs(feed.store.dispatch); + + assert.calledWith(feed.cache.set, "spocs", { + spocs: { placement: "data" }, + lastUpdated: 0, + }); + assert.equal( + feed.store.getState().DiscoveryStream.spocs.data.placement, + "data" + ); + }); + it("should fetch fresh data if cache is old", async () => { + const cachedSpoc = { + spocs: { placement: "old" }, + lastUpdated: Date.now(), + }; + const cachedData = { spocs: cachedSpoc }; + sandbox.stub(feed.cache, "get").returns(Promise.resolve(cachedData)); + sandbox.stub(feed, "fetchFromEndpoint").resolves({ placement: "new" }); + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + clock.tick(THIRTY_MINUTES + 1); + + await feed.loadSpocs(feed.store.dispatch); + + assert.equal( + feed.store.getState().DiscoveryStream.spocs.data.placement, + "new" + ); + }); + it("should return spoc data from cache if it is fresh", async () => { + const cachedSpoc = { + spocs: { placement: "old" }, + lastUpdated: Date.now(), + }; + const cachedData = { spocs: cachedSpoc }; + sandbox.stub(feed.cache, "get").returns(Promise.resolve(cachedData)); + sandbox.stub(feed, "fetchFromEndpoint").resolves({ placement: "new" }); + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + clock.tick(THIRTY_MINUTES - 1); + + await feed.loadSpocs(feed.store.dispatch); + + assert.equal( + feed.store.getState().DiscoveryStream.spocs.data.placement, + "old" + ); + }); + it("should properly transform spocs using placements", async () => { + sandbox.stub(feed.cache, "get").returns(Promise.resolve()); + sandbox.stub(feed, "fetchFromEndpoint").resolves({ + spocs: { items: [{ id: "data" }] }, + }); + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + + await feed.loadSpocs(feed.store.dispatch); + + assert.calledWith(feed.cache.set, "spocs", { + spocs: { + spocs: { + personalized: false, + context: "", + title: "", + sponsor: "", + sponsored_by_override: undefined, + items: [{ id: "data", score: 1 }], + }, + }, + lastUpdated: 0, + }); + + assert.deepEqual( + feed.store.getState().DiscoveryStream.spocs.data.spocs.items[0], + { id: "data", score: 1 } + ); + }); + it("should normalizeSpocsItems for older spoc data", async () => { + sandbox.stub(feed.cache, "get").returns(Promise.resolve()); + sandbox + .stub(feed, "fetchFromEndpoint") + .resolves({ spocs: [{ id: "data" }] }); + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + + await feed.loadSpocs(feed.store.dispatch); + + assert.deepEqual( + feed.store.getState().DiscoveryStream.spocs.data.spocs.items[0], + { id: "data", score: 1 } + ); + }); + it("should dispatch DISCOVERY_STREAM_PERSONALIZATION_OVERRIDE with feature_flags", async () => { + sandbox.stub(feed.cache, "get").returns(Promise.resolve()); + sandbox.spy(feed.store, "dispatch"); + sandbox + .stub(feed, "fetchFromEndpoint") + .resolves({ settings: { feature_flags: {} }, spocs: [{ id: "data" }] }); + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + + await feed.loadSpocs(feed.store.dispatch); + + assert.calledWith( + feed.store.dispatch, + ac.OnlyToMain({ + type: at.DISCOVERY_STREAM_PERSONALIZATION_OVERRIDE, + data: { + override: true, + }, + }) + ); + }); + it("should return expected data if normalizeSpocsItems returns no spoc data", async () => { + // We don't need this for just this test, we are setting placements + // manually. + feed.getPlacements.restore(); + Object.defineProperty(feed, "showSponsoredStories", { + get: () => true, + }); + + sandbox.stub(feed.cache, "get").returns(Promise.resolve()); + sandbox + .stub(feed, "fetchFromEndpoint") + .resolves({ placement1: [{ id: "data" }], placement2: [] }); + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + + const fakeComponents = { + components: [ + { placement: { name: "placement1" }, spocs: {} }, + { placement: { name: "placement2" }, spocs: {} }, + ], + }; + feed.updatePlacements(feed.store.dispatch, [fakeComponents]); + + await feed.loadSpocs(feed.store.dispatch); + + assert.deepEqual(feed.store.getState().DiscoveryStream.spocs.data, { + placement1: { + personalized: false, + title: "", + context: "", + sponsor: "", + sponsored_by_override: undefined, + items: [{ id: "data", score: 1 }], + }, + placement2: { + title: "", + context: "", + items: [], + }, + }); + }); + it("should use title and context on spoc data", async () => { + // We don't need this for just this test, we are setting placements + // manually. + feed.getPlacements.restore(); + Object.defineProperty(feed, "showSponsoredStories", { + get: () => true, + }); + sandbox.stub(feed.cache, "get").returns(Promise.resolve()); + sandbox.stub(feed, "fetchFromEndpoint").resolves({ + placement1: { + title: "title", + context: "context", + sponsor: "", + sponsored_by_override: undefined, + items: [{ id: "data" }], + }, + }); + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + + const fakeComponents = { + components: [{ placement: { name: "placement1" }, spocs: {} }], + }; + feed.updatePlacements(feed.store.dispatch, [fakeComponents]); + + await feed.loadSpocs(feed.store.dispatch); + + assert.deepEqual(feed.store.getState().DiscoveryStream.spocs.data, { + placement1: { + personalized: false, + title: "title", + context: "context", + sponsor: "", + sponsored_by_override: undefined, + items: [{ id: "data", score: 1 }], + }, + }); + }); + describe("test SOV behaviour", () => { + beforeEach(() => { + globals.set("NimbusFeatures", { + pocketNewtab: { + getVariable: sandbox.stub(), + }, + }); + global.NimbusFeatures.pocketNewtab.getVariable + .withArgs("topSitesContileSovEnabled") + .returns(true); + // We don't need this for just this test, we are setting placements + // manually. + feed.getPlacements.restore(); + Object.defineProperty(feed, "showSponsoredStories", { + get: () => true, + }); + const fakeComponents = { + components: [ + { placement: { name: "sponsored-topsites" }, spocs: {} }, + { placement: { name: "spocs" }, spocs: {} }, + ], + }; + feed.updatePlacements(feed.store.dispatch, [fakeComponents]); + sandbox.stub(feed.cache, "get").returns(Promise.resolve()); + sandbox.stub(feed, "fetchFromEndpoint").resolves({ + spocs: [{ id: "spoc1" }], + "sponsored-topsites": [{ id: "topsite1" }], + }); + }); + it("should use topsites placement by default if there is no SOV", async () => { + await feed.loadSpocs(feed.store.dispatch); + + assert.equal( + feed.fetchFromEndpoint.firstCall.args[1].body, + JSON.stringify({ + pocket_id: "{foo-123-foo}", + version: 2, + placements: [ + { + name: "sponsored-topsites", + }, + { + name: "spocs", + }, + ], + }) + ); + }); + it("should use cache if cache is available and SOV is not ready", async () => { + const cache = { + sov: [{ assignedPartner: "amp" }], + }; + feed.cache.get.resolves(cache); + await feed.loadSpocs(feed.store.dispatch); + + assert.equal( + feed.fetchFromEndpoint.firstCall.args[1].body, + JSON.stringify({ + pocket_id: "{foo-123-foo}", + version: 2, + placements: [ + { + name: "spocs", + }, + ], + }) + ); + }); + it("should properly set placements", async () => { + sandbox.spy(feed.cache, "set"); + + // Testing only 1 placement type. + feed.store.dispatch( + ac.OnlyToMain({ + type: at.SOV_UPDATED, + data: { + ready: true, + positions: [ + { + position: 1, + assignedPartner: "amp", + }, + { + position: 2, + assignedPartner: "amp", + }, + ], + }, + }) + ); + + await feed.loadSpocs(feed.store.dispatch); + + const firstCall = feed.cache.set.getCall(0); + assert.deepEqual(firstCall.args[0], "sov"); + assert.deepEqual(firstCall.args[1], [ + { + position: 1, + assignedPartner: "amp", + }, + { + position: 2, + assignedPartner: "amp", + }, + ]); + assert.equal( + feed.fetchFromEndpoint.firstCall.args[1].body, + JSON.stringify({ + pocket_id: "{foo-123-foo}", + version: 2, + placements: [ + { + name: "spocs", + }, + ], + }) + ); + + // Testing 2 placement types. + feed.store.dispatch( + ac.OnlyToMain({ + type: at.SOV_UPDATED, + data: { + ready: true, + positions: [ + { + position: 1, + assignedPartner: "amp", + }, + { + position: 2, + assignedPartner: "moz-sales", + }, + ], + }, + }) + ); + + await feed.loadSpocs(feed.store.dispatch); + + const secondCall = feed.cache.set.getCall(2); + assert.deepEqual(secondCall.args[0], "sov"); + assert.deepEqual(secondCall.args[1], [ + { + position: 1, + assignedPartner: "amp", + }, + { + position: 2, + assignedPartner: "moz-sales", + }, + ]); + assert.equal( + feed.fetchFromEndpoint.secondCall.args[1].body, + JSON.stringify({ + pocket_id: "{foo-123-foo}", + version: 2, + placements: [ + { + name: "sponsored-topsites", + }, + { + name: "spocs", + }, + ], + }) + ); + }); + }); + }); + + describe("#normalizeSpocsItems", () => { + it("should return correct data if new data passed in", async () => { + const spocs = { + title: "title", + context: "context", + sponsor: "sponsor", + sponsored_by_override: "override", + items: [{ id: "id" }], + }; + const result = feed.normalizeSpocsItems(spocs); + assert.deepEqual(result, spocs); + }); + it("should return normalized data if new data passed in without title or context", async () => { + const spocs = { + items: [{ id: "id" }], + }; + const result = feed.normalizeSpocsItems(spocs); + assert.deepEqual(result, { + title: "", + context: "", + sponsor: "", + sponsored_by_override: undefined, + items: [{ id: "id" }], + }); + }); + it("should return normalized data if old data passed in", async () => { + const spocs = [{ id: "id" }]; + const result = feed.normalizeSpocsItems(spocs); + assert.deepEqual(result, { + title: "", + context: "", + sponsor: "", + sponsored_by_override: undefined, + items: [{ id: "id" }], + }); + }); + }); + + describe("#showSpocs", () => { + it("should return true from showSpocs if showSponsoredStories is false", async () => { + Object.defineProperty(feed, "showSponsoredStories", { + get: () => false, + }); + Object.defineProperty(feed, "showSponsoredTopsites", { + get: () => true, + }); + assert.isTrue(feed.showSpocs); + }); + it("should return true from showSpocs if showSponsoredTopsites is false", async () => { + Object.defineProperty(feed, "showSponsoredStories", { + get: () => true, + }); + Object.defineProperty(feed, "showSponsoredTopsites", { + get: () => false, + }); + assert.isTrue(feed.showSpocs); + }); + it("should return true from showSpocs if both are true", async () => { + Object.defineProperty(feed, "showSponsoredStories", { + get: () => true, + }); + Object.defineProperty(feed, "showSponsoredTopsites", { + get: () => true, + }); + assert.isTrue(feed.showSpocs); + }); + it("should return false from showSpocs if both are false", async () => { + Object.defineProperty(feed, "showSponsoredStories", { + get: () => false, + }); + Object.defineProperty(feed, "showSponsoredTopsites", { + get: () => false, + }); + assert.isFalse(feed.showSpocs); + }); + }); + + describe("#showSponsoredStories", () => { + it("should return false from showSponsoredStories if user pref showSponsored is false", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { showSponsored: false, "system.showSponsored": true }, + }, + }); + + assert.isFalse(feed.showSponsoredStories); + }); + it("should return false from showSponsoredStories if DiscoveryStream pref system.showSponsored is false", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { showSponsored: true, "system.showSponsored": false }, + }, + }); + + assert.isFalse(feed.showSponsoredStories); + }); + it("should return true from showSponsoredStories if both prefs are true", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { showSponsored: true, "system.showSponsored": true }, + }, + }); + + assert.isTrue(feed.showSponsoredStories); + }); + }); + + describe("#showSponsoredTopsites", () => { + it("should return false from showSponsoredTopsites if user pref showSponsoredTopSites is false", async () => { + feed.store.getState = () => ({ + Prefs: { values: { showSponsoredTopSites: false } }, + DiscoveryStream: { + spocs: { + placements: [{ name: "sponsored-topsites" }], + }, + }, + }); + assert.isFalse(feed.showSponsoredTopsites); + }); + it("should return true from showSponsoredTopsites if user pref showSponsoredTopSites is true", async () => { + feed.store.getState = () => ({ + Prefs: { values: { showSponsoredTopSites: true } }, + DiscoveryStream: { + spocs: { + placements: [{ name: "sponsored-topsites" }], + }, + }, + }); + assert.isTrue(feed.showSponsoredTopsites); + }); + }); + + describe("#showStories", () => { + it("should return false from showStories if user pref is false", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { + "feeds.section.topstories": false, + "feeds.system.topstories": true, + }, + }, + }); + assert.isFalse(feed.showStories); + }); + it("should return false from showStories if system pref is false", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { + "feeds.section.topstories": true, + "feeds.system.topstories": false, + }, + }, + }); + assert.isFalse(feed.showStories); + }); + it("should return true from showStories if both prefs are true", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { + "feeds.section.topstories": true, + "feeds.system.topstories": true, + }, + }, + }); + assert.isTrue(feed.showStories); + }); + }); + + describe("#showTopsites", () => { + it("should return false from showTopsites if user pref is false", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { + "feeds.topsites": false, + "feeds.system.topsites": true, + }, + }, + }); + assert.isFalse(feed.showTopsites); + }); + it("should return false from showTopsites if system pref is false", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { + "feeds.topsites": true, + "feeds.system.topsites": false, + }, + }, + }); + assert.isFalse(feed.showTopsites); + }); + it("should return true from showTopsites if both prefs are true", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { + "feeds.topsites": true, + "feeds.system.topsites": true, + }, + }, + }); + assert.isTrue(feed.showTopsites); + }); + }); + + describe("#clearSpocs", () => { + let defaultState; + let DiscoveryStream; + let Prefs; + beforeEach(() => { + DiscoveryStream = { + layout: [], + spocs: { + placements: [{ name: "sponsored-topsites" }], + }, + }; + Prefs = { + values: { + "feeds.section.topstories": true, + "feeds.system.topstories": true, + "feeds.topsites": true, + "feeds.system.topsites": true, + showSponsoredTopSites: true, + showSponsored: true, + "system.showSponsored": true, + }, + }; + defaultState = { + DiscoveryStream, + Prefs, + }; + feed.store.getState = () => defaultState; + }); + it("should not fail with no endpoint", async () => { + sandbox.stub(feed.store, "getState").returns({ + Prefs: { + values: { PREF_SPOCS_CLEAR_ENDPOINT: null }, + }, + }); + sandbox.stub(feed, "fetchFromEndpoint").resolves(null); + + await feed.clearSpocs(); + + assert.notCalled(feed.fetchFromEndpoint); + }); + it("should call DELETE with endpoint", async () => { + sandbox.stub(feed.store, "getState").returns({ + Prefs: { + values: { + "discoverystream.endpointSpocsClear": "https://spocs/user", + }, + }, + }); + sandbox.stub(feed, "fetchFromEndpoint").resolves(null); + feed._impressionId = "1234"; + + await feed.clearSpocs(); + + assert.equal( + feed.fetchFromEndpoint.firstCall.args[0], + "https://spocs/user" + ); + assert.equal(feed.fetchFromEndpoint.firstCall.args[1].method, "DELETE"); + assert.equal( + feed.fetchFromEndpoint.firstCall.args[1].body, + '{"pocket_id":"1234"}' + ); + }); + it("should properly call clearSpocs when sponsored content is changed", async () => { + sandbox.stub(feed, "clearSpocs").returns(Promise.resolve()); + // sandbox.stub(feed, "updatePlacements").returns(); + sandbox.stub(feed, "loadSpocs").returns(); + + await feed.onAction({ + type: at.PREF_CHANGED, + data: { name: "showSponsored" }, + }); + + assert.notCalled(feed.clearSpocs); + + Prefs.values.showSponsoredTopSites = false; + Prefs.values.showSponsored = false; + + await feed.onAction({ + type: at.PREF_CHANGED, + data: { name: "showSponsored" }, + }); + + assert.calledOnce(feed.clearSpocs); + }); + it("should call clearSpocs when top stories and top sites is turned off", async () => { + sandbox.stub(feed, "clearSpocs").returns(Promise.resolve()); + Prefs.values["feeds.section.topstories"] = false; + Prefs.values["feeds.topsites"] = false; + + await feed.onAction({ + type: at.PREF_CHANGED, + data: { name: "feeds.section.topstories" }, + }); + + assert.calledOnce(feed.clearSpocs); + + await feed.onAction({ + type: at.PREF_CHANGED, + data: { name: "feeds.topsites" }, + }); + + assert.calledTwice(feed.clearSpocs); + }); + }); + + describe("#rotate", () => { + it("should move seen first story to the back of the response", () => { + const recsExpireTime = 5600; + const feedResponse = { + recommendations: [ + { + id: "first", + }, + { + id: "second", + }, + { + id: "third", + }, + { + id: "fourth", + }, + ], + settings: { + recsExpireTime, + }, + }; + const fakeImpressions = { + first: Date.now() - recsExpireTime * 1000, + third: Date.now(), + }; + sandbox.stub(feed, "readDataPref").returns(fakeImpressions); + + const result = feed.rotate( + feedResponse.recommendations, + feedResponse.settings.recsExpireTime + ); + + assert.equal(result[3].id, "first"); + }); + }); + + describe("#reset", () => { + it("should fire all reset based functions", async () => { + sandbox.stub(global.Services.obs, "removeObserver").returns(); + + sandbox.stub(feed, "resetDataPrefs").returns(); + sandbox.stub(feed, "resetCache").returns(Promise.resolve()); + sandbox.stub(feed, "resetState").returns(); + + feed.loaded = true; + + await feed.reset(); + + assert.calledOnce(feed.resetDataPrefs); + assert.calledOnce(feed.resetCache); + assert.calledOnce(feed.resetState); + }); + }); + + describe("#resetCache", () => { + it("should set .feeds .spocs and .sov to {}", async () => { + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + + await feed.resetCache(); + + assert.callCount(feed.cache.set, 3); + const firstCall = feed.cache.set.getCall(0); + const secondCall = feed.cache.set.getCall(1); + const thirdCall = feed.cache.set.getCall(2); + assert.deepEqual(firstCall.args, ["feeds", {}]); + assert.deepEqual(secondCall.args, ["spocs", {}]); + assert.deepEqual(thirdCall.args, ["sov", {}]); + }); + }); + + describe("#scoreItems", () => { + it("should return initial data if spocs are empty", async () => { + const { data: result } = await feed.scoreItems([]); + + assert.equal(result.length, 0); + }); + + it("should sort based on item_score", async () => { + const { data: result } = await feed.scoreItems([ + { id: 2, flight_id: 2, item_score: 0.8 }, + { id: 4, flight_id: 4, item_score: 0.5 }, + { id: 3, flight_id: 3, item_score: 0.7 }, + { id: 1, flight_id: 1, item_score: 0.9 }, + ]); + + assert.deepEqual(result, [ + { id: 1, flight_id: 1, item_score: 0.9, score: 0.9 }, + { id: 2, flight_id: 2, item_score: 0.8, score: 0.8 }, + { id: 3, flight_id: 3, item_score: 0.7, score: 0.7 }, + { id: 4, flight_id: 4, item_score: 0.5, score: 0.5 }, + ]); + }); + + it("should sort based on priority", async () => { + const { data: result } = await feed.scoreItems([ + { id: 6, flight_id: 6, priority: 2, item_score: 0.7 }, + { id: 2, flight_id: 3, priority: 1, item_score: 0.2 }, + { id: 4, flight_id: 4, item_score: 0.6 }, + { id: 5, flight_id: 5, priority: 2, item_score: 0.8 }, + { id: 3, flight_id: 3, item_score: 0.8 }, + { id: 1, flight_id: 1, priority: 1, item_score: 0.3 }, + ]); + + assert.deepEqual(result, [ + { + id: 1, + flight_id: 1, + priority: 1, + score: 0.3, + item_score: 0.3, + }, + { + id: 2, + flight_id: 3, + priority: 1, + score: 0.2, + item_score: 0.2, + }, + { + id: 5, + flight_id: 5, + priority: 2, + score: 0.8, + item_score: 0.8, + }, + { + id: 6, + flight_id: 6, + priority: 2, + score: 0.7, + item_score: 0.7, + }, + { id: 3, flight_id: 3, item_score: 0.8, score: 0.8 }, + { id: 4, flight_id: 4, item_score: 0.6, score: 0.6 }, + ]); + }); + + it("should add a score prop to spocs", async () => { + const { data: result } = await feed.scoreItems([ + { flight_id: 1, item_score: 0.9 }, + ]); + + assert.equal(result[0].score, 0.9); + }); + }); + + describe("#filterBlocked", () => { + it("should return initial data if spocs are empty", () => { + const { data: result } = feed.filterBlocked([]); + + assert.equal(result.length, 0); + }); + it("should return initial data if links are not blocked", () => { + const { data: result } = feed.filterBlocked([ + { url: "https://foo.com" }, + { url: "test.com" }, + ]); + assert.equal(result.length, 2); + }); + it("should return initial recommendations data if links are not blocked", () => { + const { data: result } = feed.filterBlocked([ + { url: "https://foo.com" }, + { url: "test.com" }, + ]); + assert.equal(result.length, 2); + }); + it("filterRecommendations based on blockedlist by passing feed data", () => { + fakeNewTabUtils.blockedLinks.links = [{ url: "https://foo.com" }]; + fakeNewTabUtils.blockedLinks.isBlocked = site => + fakeNewTabUtils.blockedLinks.links[0].url === site.url; + + const result = feed.filterRecommendations({ + lastUpdated: 4, + data: { + recommendations: [{ url: "https://foo.com" }, { url: "test.com" }], + }, + }); + + assert.equal(result.lastUpdated, 4); + assert.lengthOf(result.data.recommendations, 1); + assert.equal(result.data.recommendations[0].url, "test.com"); + assert.notInclude( + result.data.recommendations, + fakeNewTabUtils.blockedLinks.links[0] + ); + }); + }); + + describe("#frequencyCapSpocs", () => { + it("should return filtered out spocs based on frequency caps", () => { + const fakeSpocs = [ + { + id: 1, + flight_id: "seen", + caps: { + lifetime: 3, + flight: { + count: 1, + period: 1, + }, + }, + }, + { + id: 2, + flight_id: "not-seen", + caps: { + lifetime: 3, + flight: { + count: 1, + period: 1, + }, + }, + }, + ]; + const fakeImpressions = { + seen: [Date.now() - 1], + }; + sandbox.stub(feed, "readDataPref").returns(fakeImpressions); + + const { data: result, filtered } = feed.frequencyCapSpocs(fakeSpocs); + + assert.equal(result.length, 1); + assert.equal(result[0].flight_id, "not-seen"); + assert.deepEqual(filtered, [fakeSpocs[0]]); + }); + it("should return simple structure and do nothing with no spocs", () => { + const { data: result, filtered } = feed.frequencyCapSpocs([]); + + assert.equal(result.length, 0); + assert.equal(filtered.length, 0); + }); + }); + + describe("#migrateFlightId", () => { + it("should migrate campaign to flight if no flight exists", () => { + const fakeSpocs = [ + { + id: 1, + campaign_id: "campaign", + caps: { + lifetime: 3, + campaign: { + count: 1, + period: 1, + }, + }, + }, + ]; + const { data: result } = feed.migrateFlightId(fakeSpocs); + + assert.deepEqual(result[0], { + id: 1, + flight_id: "campaign", + campaign_id: "campaign", + caps: { + lifetime: 3, + flight: { + count: 1, + period: 1, + }, + campaign: { + count: 1, + period: 1, + }, + }, + }); + }); + it("should not migrate campaign to flight if caps or id don't exist", () => { + const fakeSpocs = [{ id: 1 }]; + const { data: result } = feed.migrateFlightId(fakeSpocs); + + assert.deepEqual(result[0], { id: 1 }); + }); + it("should return simple structure and do nothing with no spocs", () => { + const { data: result } = feed.migrateFlightId([]); + + assert.equal(result.length, 0); + }); + }); + + describe("#isBelowFrequencyCap", () => { + it("should return true if there are no flight impressions", () => { + const fakeImpressions = { + seen: [Date.now() - 1], + }; + const fakeSpoc = { + flight_id: "not-seen", + caps: { + lifetime: 3, + flight: { + count: 1, + period: 1, + }, + }, + }; + + const result = feed.isBelowFrequencyCap(fakeImpressions, fakeSpoc); + + assert.isTrue(result); + }); + it("should return true if there are no flight caps", () => { + const fakeImpressions = { + seen: [Date.now() - 1], + }; + const fakeSpoc = { + flight_id: "seen", + caps: { + lifetime: 3, + }, + }; + + const result = feed.isBelowFrequencyCap(fakeImpressions, fakeSpoc); + + assert.isTrue(result); + }); + + it("should return false if lifetime cap is hit", () => { + const fakeImpressions = { + seen: [Date.now() - 1], + }; + const fakeSpoc = { + flight_id: "seen", + caps: { + lifetime: 1, + flight: { + count: 3, + period: 1, + }, + }, + }; + + const result = feed.isBelowFrequencyCap(fakeImpressions, fakeSpoc); + + assert.isFalse(result); + }); + + it("should return false if time based cap is hit", () => { + const fakeImpressions = { + seen: [Date.now() - 1], + }; + const fakeSpoc = { + flight_id: "seen", + caps: { + lifetime: 3, + flight: { + count: 1, + period: 1, + }, + }, + }; + + const result = feed.isBelowFrequencyCap(fakeImpressions, fakeSpoc); + + assert.isFalse(result); + }); + }); + + describe("#retryFeed", () => { + it("should retry a feed fetch", async () => { + sandbox.stub(feed, "getComponentFeed").returns(Promise.resolve({})); + sandbox.stub(feed, "filterRecommendations").returns({}); + sandbox.spy(feed.store, "dispatch"); + + await feed.retryFeed({ url: "https://feed.com" }); + + assert.calledOnce(feed.getComponentFeed); + assert.calledOnce(feed.filterRecommendations); + assert.calledOnce(feed.store.dispatch); + assert.equal( + feed.store.dispatch.firstCall.args[0].type, + "DISCOVERY_STREAM_FEED_UPDATE" + ); + assert.deepEqual(feed.store.dispatch.firstCall.args[0].data, { + feed: {}, + url: "https://feed.com", + }); + }); + }); + + describe("#recordFlightImpression", () => { + it("should return false if time based cap is hit", () => { + sandbox.stub(feed, "readDataPref").returns({}); + sandbox.stub(feed, "writeDataPref").returns(); + + feed.recordFlightImpression("seen"); + + assert.calledWith(feed.writeDataPref, SPOC_IMPRESSION_TRACKING_PREF, { + seen: [0], + }); + }); + }); + + describe("#recordBlockFlightId", () => { + it("should call writeDataPref with new flight id added", () => { + sandbox.stub(feed, "readDataPref").returns({ 1234: 1 }); + sandbox.stub(feed, "writeDataPref").returns(); + + feed.recordBlockFlightId("5678"); + + assert.calledOnce(feed.readDataPref); + assert.calledWith(feed.writeDataPref, "discoverystream.flight.blocks", { + 1234: 1, + 5678: 1, + }); + }); + }); + + describe("#cleanUpFlightImpressionPref", () => { + it("should remove flight-3 because it is no longer being used", async () => { + const fakeSpocs = { + spocs: { + items: [ + { + flight_id: "flight-1", + caps: { + lifetime: 3, + flight: { + count: 1, + period: 1, + }, + }, + }, + { + flight_id: "flight-2", + caps: { + lifetime: 3, + flight: { + count: 1, + period: 1, + }, + }, + }, + ], + }, + }; + const fakeImpressions = { + "flight-2": [Date.now() - 1], + "flight-3": [Date.now() - 1], + }; + sandbox.stub(feed, "getPlacements").returns([{ name: "spocs" }]); + sandbox.stub(feed, "readDataPref").returns(fakeImpressions); + sandbox.stub(feed, "writeDataPref").returns(); + + feed.cleanUpFlightImpressionPref(fakeSpocs); + + assert.calledWith(feed.writeDataPref, SPOC_IMPRESSION_TRACKING_PREF, { + "flight-2": [-1], + }); + }); + }); + + describe("#recordTopRecImpressions", () => { + it("should add a rec id to the rec impression pref", () => { + sandbox.stub(feed, "readDataPref").returns({}); + sandbox.stub(feed, "writeDataPref"); + + feed.recordTopRecImpressions("rec"); + + assert.calledWith(feed.writeDataPref, REC_IMPRESSION_TRACKING_PREF, { + rec: 0, + }); + }); + it("should not add an impression if it already exists", () => { + sandbox.stub(feed, "readDataPref").returns({ rec: 4 }); + sandbox.stub(feed, "writeDataPref"); + + feed.recordTopRecImpressions("rec"); + + assert.notCalled(feed.writeDataPref); + }); + }); + + describe("#cleanUpTopRecImpressionPref", () => { + it("should remove recs no longer being used", () => { + const newFeeds = { + "https://foo.com": { + data: { + recommendations: [ + { + id: "rec1", + }, + { + id: "rec2", + }, + ], + }, + }, + "https://bar.com": { + data: { + recommendations: [ + { + id: "rec3", + }, + { + id: "rec4", + }, + ], + }, + }, + }; + const fakeImpressions = { + rec2: Date.now() - 1, + rec3: Date.now() - 1, + rec5: Date.now() - 1, + }; + sandbox.stub(feed, "readDataPref").returns(fakeImpressions); + sandbox.stub(feed, "writeDataPref").returns(); + + feed.cleanUpTopRecImpressionPref(newFeeds); + + assert.calledWith(feed.writeDataPref, REC_IMPRESSION_TRACKING_PREF, { + rec2: -1, + rec3: -1, + }); + }); + }); + + describe("#writeDataPref", () => { + it("should call Services.prefs.setStringPref", () => { + sandbox.spy(feed.store, "dispatch"); + const fakeImpressions = { + foo: [Date.now() - 1], + bar: [Date.now() - 1], + }; + + feed.writeDataPref(SPOC_IMPRESSION_TRACKING_PREF, fakeImpressions); + + assert.calledWithMatch(feed.store.dispatch, { + data: { + name: SPOC_IMPRESSION_TRACKING_PREF, + value: JSON.stringify(fakeImpressions), + }, + type: at.SET_PREF, + }); + }); + }); + + describe("#addEndpointQuery", () => { + const url = "https://spocs.getpocket.com/spocs"; + + it("should return same url with no query", () => { + const result = feed.addEndpointQuery(url, ""); + assert.equal(result, url); + }); + + it("should add multiple query params to standard url", () => { + const params = "?first=first&second=second"; + const result = feed.addEndpointQuery(url, params); + assert.equal(result, url + params); + }); + + it("should add multiple query params to url with a query already", () => { + const params = "first=first&second=second"; + const initialParams = "?zero=zero"; + const result = feed.addEndpointQuery( + `${url}${initialParams}`, + `?${params}` + ); + assert.equal(result, `${url}${initialParams}&${params}`); + }); + }); + + describe("#readDataPref", () => { + it("should return what's in Services.prefs.getStringPref", () => { + const fakeImpressions = { + foo: [Date.now() - 1], + bar: [Date.now() - 1], + }; + setPref(SPOC_IMPRESSION_TRACKING_PREF, fakeImpressions); + + const result = feed.readDataPref(SPOC_IMPRESSION_TRACKING_PREF); + + assert.deepEqual(result, fakeImpressions); + }); + }); + + describe("#setupPrefs", () => { + it("should call setupPrefs", async () => { + sandbox.spy(feed, "setupPrefs"); + feed.onAction({ + type: at.INIT, + }); + assert.calledOnce(feed.setupPrefs); + }); + it("should dispatch to at.DISCOVERY_STREAM_PREFS_SETUP with proper data", async () => { + sandbox.spy(feed.store, "dispatch"); + globals.set("ExperimentAPI", { + getExperimentMetaData: () => ({ + slug: "experimentId", + branch: { + slug: "branchId", + }, + }), + getRolloutMetaData: () => ({}), + }); + global.Services.prefs.getBoolPref + .withArgs("extensions.pocket.enabled") + .returns(true); + feed.store.getState = () => ({ + Prefs: { + values: { + region: "CA", + pocketConfig: { + recentSavesEnabled: true, + hideDescriptions: false, + hideDescriptionsRegions: "US,CA,GB", + compactImages: true, + imageGradient: true, + newSponsoredLabel: true, + titleLines: "1", + descLines: "1", + readTime: true, + saveToPocketCard: false, + saveToPocketCardRegions: "US,CA,GB", + }, + }, + }, + }); + feed.setupPrefs(); + assert.deepEqual(feed.store.dispatch.firstCall.args[0].data, { + utmSource: "pocket-newtab", + utmCampaign: "experimentId", + utmContent: "branchId", + }); + assert.deepEqual(feed.store.dispatch.secondCall.args[0].data, { + recentSavesEnabled: true, + pocketButtonEnabled: true, + saveToPocketCard: true, + hideDescriptions: true, + compactImages: true, + imageGradient: true, + newSponsoredLabel: true, + titleLines: "1", + descLines: "1", + readTime: true, + }); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_IMPRESSION_STATS", () => { + it("should call recordTopRecImpressions from DISCOVERY_STREAM_IMPRESSION_STATS", async () => { + sandbox.stub(feed, "recordTopRecImpressions").returns(); + await feed.onAction({ + type: at.DISCOVERY_STREAM_IMPRESSION_STATS, + data: { tiles: [{ id: "seen" }] }, + }); + + assert.calledWith(feed.recordTopRecImpressions, "seen"); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_SPOC_IMPRESSION", () => { + beforeEach(() => { + const data = { + spocs: { + items: [ + { + id: 1, + flight_id: "seen", + caps: { + lifetime: 3, + flight: { + count: 1, + period: 1, + }, + }, + }, + { + id: 2, + flight_id: "not-seen", + caps: { + lifetime: 3, + flight: { + count: 1, + period: 1, + }, + }, + }, + ], + }, + }; + sandbox.stub(feed.store, "getState").returns({ + DiscoveryStream: { + spocs: { + data, + }, + }, + }); + }); + + it("should call dispatch to ac.AlsoToPreloaded with filtered spoc data", async () => { + sandbox.stub(feed, "getPlacements").returns([{ name: "spocs" }]); + Object.defineProperty(feed, "showSpocs", { get: () => true }); + const fakeImpressions = { + seen: [Date.now() - 1], + }; + const result = { + spocs: { + items: [ + { + id: 2, + flight_id: "not-seen", + caps: { + lifetime: 3, + flight: { + count: 1, + period: 1, + }, + }, + }, + ], + }, + }; + sandbox.stub(feed, "recordFlightImpression").returns(); + sandbox.stub(feed, "readDataPref").returns(fakeImpressions); + sandbox.spy(feed.store, "dispatch"); + + await feed.onAction({ + type: at.DISCOVERY_STREAM_SPOC_IMPRESSION, + data: { flightId: "seen" }, + }); + + assert.deepEqual( + feed.store.dispatch.secondCall.args[0].data.spocs, + result + ); + }); + it("should not call dispatch to ac.AlsoToPreloaded if spocs were not changed by frequency capping", async () => { + sandbox.stub(feed, "getPlacements").returns([{ name: "spocs" }]); + Object.defineProperty(feed, "showSpocs", { get: () => true }); + const fakeImpressions = {}; + sandbox.stub(feed, "recordFlightImpression").returns(); + sandbox.stub(feed, "readDataPref").returns(fakeImpressions); + sandbox.spy(feed.store, "dispatch"); + + await feed.onAction({ + type: at.DISCOVERY_STREAM_SPOC_IMPRESSION, + data: { flight_id: "seen" }, + }); + + assert.notCalled(feed.store.dispatch); + }); + it("should attempt feq cap on valid spocs with placements on impression", async () => { + sandbox.restore(); + Object.defineProperty(feed, "showSpocs", { get: () => true }); + const fakeImpressions = {}; + sandbox.stub(feed, "recordFlightImpression").returns(); + sandbox.stub(feed, "readDataPref").returns(fakeImpressions); + sandbox.spy(feed.store, "dispatch"); + sandbox.spy(feed, "frequencyCapSpocs"); + + const data = { + spocs: { + items: [ + { + id: 2, + flight_id: "seen-2", + caps: { + lifetime: 3, + flight: { + count: 1, + period: 1, + }, + }, + }, + ], + }, + }; + sandbox.stub(feed.store, "getState").returns({ + DiscoveryStream: { + spocs: { + data, + placements: [{ name: "spocs" }, { name: "notSpocs" }], + }, + }, + }); + + await feed.onAction({ + type: at.DISCOVERY_STREAM_SPOC_IMPRESSION, + data: { flight_id: "doesn't matter" }, + }); + + assert.calledOnce(feed.frequencyCapSpocs); + assert.calledWith(feed.frequencyCapSpocs, data.spocs.items); + }); + }); + + describe("#onAction: PLACES_LINK_BLOCKED", () => { + beforeEach(() => { + const data = { + spocs: { + items: [ + { + id: 1, + flight_id: "foo", + url: "foo.com", + }, + { + id: 2, + flight_id: "bar", + url: "bar.com", + }, + ], + }, + }; + sandbox.stub(feed.store, "getState").returns({ + DiscoveryStream: { + spocs: { + data, + placements: [{ name: "spocs" }], + }, + }, + }); + }); + + it("should call dispatch if found a blocked spoc", async () => { + Object.defineProperty(feed, "showSpocs", { get: () => true }); + + sandbox.spy(feed.store, "dispatch"); + + await feed.onAction({ + type: at.PLACES_LINK_BLOCKED, + data: { url: "foo.com" }, + }); + + assert.deepEqual( + feed.store.dispatch.firstCall.args[0].data.url, + "foo.com" + ); + }); + it("should dispatch once if the blocked is not a SPOC", async () => { + Object.defineProperty(feed, "showSpocs", { get: () => true }); + sandbox.spy(feed.store, "dispatch"); + + await feed.onAction({ + type: at.PLACES_LINK_BLOCKED, + data: { url: "not_a_spoc.com" }, + }); + + assert.calledOnce(feed.store.dispatch); + assert.deepEqual( + feed.store.dispatch.firstCall.args[0].data.url, + "not_a_spoc.com" + ); + }); + it("should dispatch a DISCOVERY_STREAM_SPOC_BLOCKED for a blocked spoc", async () => { + Object.defineProperty(feed, "showSpocs", { get: () => true }); + sandbox.spy(feed.store, "dispatch"); + + await feed.onAction({ + type: at.PLACES_LINK_BLOCKED, + data: { url: "foo.com" }, + }); + + assert.equal( + feed.store.dispatch.secondCall.args[0].type, + "DISCOVERY_STREAM_SPOC_BLOCKED" + ); + }); + }); + + describe("#onAction: BLOCK_URL", () => { + it("should call recordBlockFlightId whith BLOCK_URL", async () => { + sandbox.stub(feed, "recordBlockFlightId").returns(); + + await feed.onAction({ + type: at.BLOCK_URL, + data: [ + { + flight_id: "1234", + }, + ], + }); + + assert.calledWith(feed.recordBlockFlightId, "1234"); + }); + }); + + describe("#onAction: INIT", () => { + it("should be .loaded=false before initialization", () => { + assert.isFalse(feed.loaded); + }); + it("should load data and set .loaded=true if config.enabled is true", async () => { + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + setPref(CONFIG_PREF_NAME, { enabled: true }); + sandbox.stub(feed, "loadLayout").returns(Promise.resolve()); + + await feed.onAction({ type: at.INIT }); + + assert.calledOnce(feed.loadLayout); + assert.isTrue(feed.loaded); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_CONFIG_SET_VALUE", async () => { + it("should add the new value to the pref without changing the existing values", async () => { + sandbox.spy(feed.store, "dispatch"); + setPref(CONFIG_PREF_NAME, { enabled: true, other: "value" }); + + await feed.onAction({ + type: at.DISCOVERY_STREAM_CONFIG_SET_VALUE, + data: { name: "api_key_pref", value: "foo" }, + }); + + assert.calledWithMatch(feed.store.dispatch, { + data: { + name: CONFIG_PREF_NAME, + value: JSON.stringify({ + enabled: true, + other: "value", + api_key_pref: "foo", + }), + }, + type: at.SET_PREF, + }); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_POCKET_STATE_INIT", async () => { + it("should call setupPocketState", async () => { + sandbox.spy(feed, "setupPocketState"); + feed.onAction({ + type: at.DISCOVERY_STREAM_POCKET_STATE_INIT, + meta: { fromTarget: {} }, + }); + assert.calledOnce(feed.setupPocketState); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_CONFIG_RESET", async () => { + it("should call configReset", async () => { + sandbox.spy(feed, "configReset"); + feed.onAction({ + type: at.DISCOVERY_STREAM_CONFIG_RESET, + }); + assert.calledOnce(feed.configReset); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_CONFIG_RESET_DEFAULTS", async () => { + it("Should dispatch CLEAR_PREF with pref name", async () => { + sandbox.spy(feed.store, "dispatch"); + await feed.onAction({ + type: at.DISCOVERY_STREAM_CONFIG_RESET_DEFAULTS, + }); + + assert.calledWithMatch(feed.store.dispatch, { + data: { + name: CONFIG_PREF_NAME, + }, + type: at.CLEAR_PREF, + }); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_RETRY_FEED", async () => { + it("should call retryFeed", async () => { + sandbox.spy(feed, "retryFeed"); + feed.onAction({ + type: at.DISCOVERY_STREAM_RETRY_FEED, + data: { feed: { url: "https://feed.com" } }, + }); + assert.calledOnce(feed.retryFeed); + assert.calledWith(feed.retryFeed, { url: "https://feed.com" }); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_CONFIG_CHANGE", () => { + it("should call this.loadLayout if config.enabled changes to true ", async () => { + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + // First initialize + await feed.onAction({ type: at.INIT }); + assert.isFalse(feed.loaded); + + // force clear cached pref value + feed._prefCache = {}; + setPref(CONFIG_PREF_NAME, { enabled: true }); + + sandbox.stub(feed, "resetCache").returns(Promise.resolve()); + sandbox.stub(feed, "loadLayout").returns(Promise.resolve()); + await feed.onAction({ type: at.DISCOVERY_STREAM_CONFIG_CHANGE }); + + assert.calledOnce(feed.loadLayout); + assert.calledOnce(feed.resetCache); + assert.isTrue(feed.loaded); + }); + it("should clear the cache if a config change happens and config.enabled is true", async () => { + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + // force clear cached pref value + feed._prefCache = {}; + setPref(CONFIG_PREF_NAME, { enabled: true }); + + sandbox.stub(feed, "resetCache").returns(Promise.resolve()); + await feed.onAction({ type: at.DISCOVERY_STREAM_CONFIG_CHANGE }); + + assert.calledOnce(feed.resetCache); + }); + it("should dispatch DISCOVERY_STREAM_LAYOUT_RESET from DISCOVERY_STREAM_CONFIG_CHANGE", async () => { + sandbox.stub(feed, "resetDataPrefs"); + sandbox.stub(feed, "resetCache").resolves(); + sandbox.stub(feed, "enable").resolves(); + setPref(CONFIG_PREF_NAME, { enabled: true }); + sandbox.spy(feed.store, "dispatch"); + + await feed.onAction({ type: at.DISCOVERY_STREAM_CONFIG_CHANGE }); + + assert.calledWithMatch(feed.store.dispatch, { + type: at.DISCOVERY_STREAM_LAYOUT_RESET, + }); + }); + it("should not call this.loadLayout if config.enabled changes to false", async () => { + sandbox.stub(feed.cache, "set").returns(Promise.resolve()); + // force clear cached pref value + feed._prefCache = {}; + setPref(CONFIG_PREF_NAME, { enabled: true }); + + await feed.onAction({ type: at.INIT }); + assert.isTrue(feed.loaded); + + feed._prefCache = {}; + setPref(CONFIG_PREF_NAME, { enabled: false }); + sandbox.stub(feed, "resetCache").returns(Promise.resolve()); + sandbox.stub(feed, "loadLayout").returns(Promise.resolve()); + await feed.onAction({ type: at.DISCOVERY_STREAM_CONFIG_CHANGE }); + + assert.notCalled(feed.loadLayout); + assert.calledOnce(feed.resetCache); + assert.isFalse(feed.loaded); + }); + }); + + describe("#onAction: UNINIT", () => { + it("should reset pref cache", async () => { + feed._prefCache = { cached: "value" }; + + await feed.onAction({ type: at.UNINIT }); + + assert.deepEqual(feed._prefCache, {}); + }); + }); + + describe("#onAction: PREF_CHANGED", () => { + it("should update state.DiscoveryStream.config when the pref changes", async () => { + setPref(CONFIG_PREF_NAME, { + enabled: true, + api_key_pref: "foo", + }); + + assert.deepEqual(feed.store.getState().DiscoveryStream.config, { + enabled: true, + api_key_pref: "foo", + }); + }); + it("should fire loadSpocs is showSponsored pref changes", async () => { + sandbox.stub(feed, "loadSpocs").returns(Promise.resolve()); + + await feed.onAction({ + type: at.PREF_CHANGED, + data: { name: "showSponsored" }, + }); + + assert.calledOnce(feed.loadSpocs); + }); + it("should fire onPrefChange when pocketConfig pref changes", async () => { + sandbox.stub(feed, "onPrefChange").returns(Promise.resolve()); + + await feed.onAction({ + type: at.PREF_CHANGED, + data: { name: "pocketConfig", value: false }, + }); + + assert.calledOnce(feed.onPrefChange); + }); + it("should fire onCollectionsChanged when collections pref changes", async () => { + sandbox.stub(feed, "onCollectionsChanged").returns(Promise.resolve()); + + await feed.onAction({ + type: at.PREF_CHANGED, + data: { name: "discoverystream.sponsored-collections.enabled" }, + }); + + assert.calledOnce(feed.onCollectionsChanged); + }); + it("should re enable stories when top stories is turned on", async () => { + sandbox.stub(feed, "refreshAll").returns(Promise.resolve()); + feed.loaded = true; + setPref(CONFIG_PREF_NAME, { + enabled: true, + }); + + await feed.onAction({ + type: at.PREF_CHANGED, + data: { name: "feeds.section.topstories", value: true }, + }); + + assert.calledOnce(feed.refreshAll); + }); + it("shoud update allowlist", async () => { + assert.equal( + feed.store.getState().Prefs.values[ENDPOINTS_PREF_NAME], + DUMMY_ENDPOINT + ); + setPref(ENDPOINTS_PREF_NAME, "sick-kickflip.mozilla.net"); + assert.equal( + feed.store.getState().Prefs.values[ENDPOINTS_PREF_NAME], + "sick-kickflip.mozilla.net" + ); + }); + }); + + describe("#onAction: SYSTEM_TICK", () => { + it("should not refresh if DiscoveryStream has not been loaded", async () => { + sandbox.stub(feed, "refreshAll").resolves(); + setPref(CONFIG_PREF_NAME, { enabled: true }); + + await feed.onAction({ type: at.SYSTEM_TICK }); + assert.notCalled(feed.refreshAll); + }); + + it("should not refresh if no caches are expired", async () => { + sandbox.stub(feed.cache, "set").resolves(); + setPref(CONFIG_PREF_NAME, { enabled: true }); + + await feed.onAction({ type: at.INIT }); + + sandbox.stub(feed, "checkIfAnyCacheExpired").resolves(false); + sandbox.stub(feed, "refreshAll").resolves(); + + await feed.onAction({ type: at.SYSTEM_TICK }); + assert.notCalled(feed.refreshAll); + }); + + it("should refresh if DiscoveryStream has been loaded at least once and a cache has expired", async () => { + sandbox.stub(feed.cache, "set").resolves(); + setPref(CONFIG_PREF_NAME, { enabled: true }); + + await feed.onAction({ type: at.INIT }); + + sandbox.stub(feed, "checkIfAnyCacheExpired").resolves(true); + sandbox.stub(feed, "refreshAll").resolves(); + + await feed.onAction({ type: at.SYSTEM_TICK }); + assert.calledOnce(feed.refreshAll); + }); + + it("should refresh and not update open tabs if DiscoveryStream has been loaded at least once", async () => { + sandbox.stub(feed.cache, "set").resolves(); + setPref(CONFIG_PREF_NAME, { enabled: true }); + + await feed.onAction({ type: at.INIT }); + + sandbox.stub(feed, "checkIfAnyCacheExpired").resolves(true); + sandbox.stub(feed, "refreshAll").resolves(); + + await feed.onAction({ type: at.SYSTEM_TICK }); + assert.calledWith(feed.refreshAll, { updateOpenTabs: false }); + }); + }); + + describe("#onCollectionsChanged", () => { + it("should call loadLayout when Pocket config changes", async () => { + sandbox.stub(feed, "loadLayout").callsFake(dispatch => dispatch("foo")); + sandbox.stub(feed.store, "dispatch"); + await feed.onCollectionsChanged(); + assert.calledOnce(feed.loadLayout); + assert.calledWith(feed.store.dispatch, ac.AlsoToPreloaded("foo")); + }); + }); + + describe("#enable", () => { + it("should pass along proper options to refreshAll from enable", async () => { + sandbox.stub(feed, "refreshAll"); + await feed.enable(); + assert.calledWith(feed.refreshAll, {}); + await feed.enable({ updateOpenTabs: true }); + assert.calledWith(feed.refreshAll, { updateOpenTabs: true }); + await feed.enable({ isStartup: true }); + assert.calledWith(feed.refreshAll, { isStartup: true }); + await feed.enable({ updateOpenTabs: true, isStartup: true }); + assert.calledWith(feed.refreshAll, { + updateOpenTabs: true, + isStartup: true, + }); + }); + }); + + describe("#onPrefChange", () => { + it("should call loadLayout when Pocket config changes", async () => { + sandbox.stub(feed, "loadLayout"); + feed._prefCache.config = { + enabled: true, + }; + await feed.onPrefChange(); + assert.calledOnce(feed.loadLayout); + }); + it("should update open tabs but not startup with onPrefChange", async () => { + sandbox.stub(feed, "refreshAll"); + feed._prefCache.config = { + enabled: true, + }; + await feed.onPrefChange(); + assert.calledWith(feed.refreshAll, { updateOpenTabs: true }); + }); + }); + + describe("#onAction: PREF_SHOW_SPONSORED", () => { + it("should call loadSpocs when preference changes", async () => { + sandbox.stub(feed, "loadSpocs").resolves(); + sandbox.stub(feed.store, "dispatch"); + + await feed.onAction({ + type: at.PREF_CHANGED, + data: { name: "showSponsored" }, + }); + + assert.calledOnce(feed.loadSpocs); + const [dispatchFn] = feed.loadSpocs.firstCall.args; + dispatchFn({}); + assert.calledWith(feed.store.dispatch, ac.BroadcastToContent({})); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_DEV_SYNC_RS", () => { + it("should fire remote settings pollChanges", async () => { + sandbox.stub(global.RemoteSettings, "pollChanges").returns(); + await feed.onAction({ + type: at.DISCOVERY_STREAM_DEV_SYNC_RS, + }); + assert.calledOnce(global.RemoteSettings.pollChanges); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_DEV_SYSTEM_TICK", () => { + it("should refresh if DiscoveryStream has been loaded at least once and a cache has expired", async () => { + sandbox.stub(feed.cache, "set").resolves(); + setPref(CONFIG_PREF_NAME, { enabled: true }); + + await feed.onAction({ type: at.INIT }); + + sandbox.stub(feed, "checkIfAnyCacheExpired").resolves(true); + sandbox.stub(feed, "refreshAll").resolves(); + + await feed.onAction({ type: at.DISCOVERY_STREAM_DEV_SYSTEM_TICK }); + assert.calledOnce(feed.refreshAll); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_DEV_EXPIRE_CACHE", () => { + it("should fire resetCache", async () => { + sandbox.stub(feed, "resetContentCache").returns(); + await feed.onAction({ + type: at.DISCOVERY_STREAM_DEV_EXPIRE_CACHE, + }); + assert.calledOnce(feed.resetContentCache); + }); + }); + + describe("#spocsCacheUpdateTime", () => { + it("should call setupSpocsCacheUpdateTime", () => { + const defaultCacheTime = 30 * 60 * 1000; + sandbox.spy(feed, "setupSpocsCacheUpdateTime"); + const cacheTime = feed.spocsCacheUpdateTime; + assert.equal(feed._spocsCacheUpdateTime, defaultCacheTime); + assert.equal(cacheTime, defaultCacheTime); + assert.calledOnce(feed.setupSpocsCacheUpdateTime); + }); + it("should return _spocsCacheUpdateTime", () => { + sandbox.spy(feed, "setupSpocsCacheUpdateTime"); + const testCacheTime = 123; + feed._spocsCacheUpdateTime = testCacheTime; + const cacheTime = feed.spocsCacheUpdateTime; + // Ensure _spocsCacheUpdateTime was not changed. + assert.equal(feed._spocsCacheUpdateTime, testCacheTime); + assert.equal(cacheTime, testCacheTime); + assert.notCalled(feed.setupSpocsCacheUpdateTime); + }); + }); + + describe("#setupSpocsCacheUpdateTime", () => { + it("should set _spocsCacheUpdateTime with default value", () => { + const defaultCacheTime = 30 * 60 * 1000; + feed.setupSpocsCacheUpdateTime(); + assert.equal(feed._spocsCacheUpdateTime, defaultCacheTime); + }); + it("should set _spocsCacheUpdateTime with min", () => { + const defaultCacheTime = 30 * 60 * 1000; + feed.store.getState = () => ({ + Prefs: { + values: { + pocketConfig: { + spocsCacheTimeout: 1, + }, + }, + }, + }); + feed.setupSpocsCacheUpdateTime(); + assert.equal(feed._spocsCacheUpdateTime, defaultCacheTime); + }); + it("should set _spocsCacheUpdateTime with max", () => { + const defaultCacheTime = 30 * 60 * 1000; + feed.store.getState = () => ({ + Prefs: { + values: { + pocketConfig: { + spocsCacheTimeout: 31, + }, + }, + }, + }); + feed.setupSpocsCacheUpdateTime(); + assert.equal(feed._spocsCacheUpdateTime, defaultCacheTime); + }); + it("should set _spocsCacheUpdateTime with spocsCacheTimeout", () => { + feed.store.getState = () => ({ + Prefs: { + values: { + pocketConfig: { + spocsCacheTimeout: 20, + }, + }, + }, + }); + feed.setupSpocsCacheUpdateTime(); + assert.equal(feed._spocsCacheUpdateTime, 20 * 60 * 1000); + }); + }); + + describe("#isExpired", () => { + it("should throw if the key is not valid", () => { + assert.throws(() => { + feed.isExpired({}, "foo"); + }); + }); + it("should return false for spocs on startup for content under 1 week", () => { + const spocs = { lastUpdated: Date.now() }; + const result = feed.isExpired({ + cachedData: { spocs }, + key: "spocs", + isStartup: true, + }); + + assert.isFalse(result); + }); + it("should return true for spocs for isStartup=false after 30 mins", () => { + const spocs = { lastUpdated: Date.now() }; + clock.tick(THIRTY_MINUTES + 1); + const result = feed.isExpired({ cachedData: { spocs }, key: "spocs" }); + + assert.isTrue(result); + }); + it("should return true for spocs on startup for content over 1 week", () => { + const spocs = { lastUpdated: Date.now() }; + clock.tick(ONE_WEEK + 1); + const result = feed.isExpired({ + cachedData: { spocs }, + key: "spocs", + isStartup: true, + }); + + assert.isTrue(result); + }); + }); + + describe("#checkIfAnyCacheExpired", () => { + let cache; + beforeEach(() => { + cache = { + feeds: { "foo.com": { lastUpdated: Date.now() } }, + spocs: { lastUpdated: Date.now() }, + }; + Object.defineProperty(feed, "showSpocs", { get: () => true }); + sandbox.stub(feed.cache, "get").resolves(cache); + }); + + it("should return false if nothing in the cache is expired", async () => { + const result = await feed.checkIfAnyCacheExpired(); + assert.isFalse(result); + }); + it("should return true if .spocs is missing", async () => { + delete cache.spocs; + assert.isTrue(await feed.checkIfAnyCacheExpired()); + }); + it("should return true if .spocs is expired", async () => { + clock.tick(THIRTY_MINUTES + 1); + // Update other caches we aren't testing + cache.spocs.lastUpdated = Date.now(); + cache.feeds["foo.com"].lastUpdate = Date.now(); + + assert.isTrue(await feed.checkIfAnyCacheExpired()); + }); + + it("should return true if .feeds is missing", async () => { + delete cache.feeds; + assert.isTrue(await feed.checkIfAnyCacheExpired()); + }); + it("should return true if data for .feeds[url] is missing", async () => { + cache.feeds["foo.com"] = null; + assert.isTrue(await feed.checkIfAnyCacheExpired()); + }); + it("should return true if data for .feeds[url] is expired", async () => { + clock.tick(THIRTY_MINUTES + 1); + // Update other caches we aren't testing + cache.spocs.lastUpdate = Date.now(); + assert.isTrue(await feed.checkIfAnyCacheExpired()); + }); + }); + + describe("#refreshAll", () => { + beforeEach(() => { + sandbox.stub(feed, "loadLayout").resolves(); + sandbox.stub(feed, "loadComponentFeeds").resolves(); + sandbox.stub(feed, "loadSpocs").resolves(); + sandbox.spy(feed.store, "dispatch"); + Object.defineProperty(feed, "showSpocs", { get: () => true }); + }); + + it("should call layout, component, spocs update and telemetry reporting functions", async () => { + await feed.refreshAll(); + + assert.calledOnce(feed.loadLayout); + assert.calledOnce(feed.loadComponentFeeds); + assert.calledOnce(feed.loadSpocs); + }); + it("should pass in dispatch wrapped with broadcast if options.updateOpenTabs is true", async () => { + await feed.refreshAll({ updateOpenTabs: true }); + [feed.loadLayout, feed.loadComponentFeeds, feed.loadSpocs].forEach(fn => { + assert.calledOnce(fn); + const result = fn.firstCall.args[0]({ type: "FOO" }); + assert.isTrue(au.isBroadcastToContent(result)); + }); + }); + it("should pass in dispatch with regular actions if options.updateOpenTabs is false", async () => { + await feed.refreshAll({ updateOpenTabs: false }); + [feed.loadLayout, feed.loadComponentFeeds, feed.loadSpocs].forEach(fn => { + assert.calledOnce(fn); + const result = fn.firstCall.args[0]({ type: "FOO" }); + assert.deepEqual(result, { type: "FOO" }); + }); + }); + it("should set loaded to true if loadSpocs and loadComponentFeeds fails", async () => { + feed.loadComponentFeeds.rejects("loadComponentFeeds error"); + feed.loadSpocs.rejects("loadSpocs error"); + + await feed.enable(); + + assert.isTrue(feed.loaded); + }); + it("should call loadComponentFeeds and loadSpocs in Promise.all", async () => { + sandbox.stub(global.Promise, "all").resolves(); + + await feed.refreshAll(); + + assert.calledOnce(global.Promise.all); + const { args } = global.Promise.all.firstCall; + assert.equal(args[0].length, 2); + }); + describe("test startup cache behaviour", () => { + beforeEach(() => { + feed._maybeUpdateCachedData.restore(); + sandbox.stub(feed.cache, "set").resolves(); + }); + it("should not refresh layout on startup if it is under THIRTY_MINUTES", async () => { + feed.loadLayout.restore(); + sandbox.stub(feed.cache, "get").resolves({ + layout: { lastUpdated: Date.now(), layout: {} }, + }); + sandbox.stub(feed, "fetchFromEndpoint").resolves({ layout: {} }); + + await feed.refreshAll({ isStartup: true }); + + assert.notCalled(feed.fetchFromEndpoint); + }); + it("should refresh spocs on startup if it was served from cache", async () => { + feed.loadSpocs.restore(); + sandbox.stub(feed, "getPlacements").returns([{ name: "spocs" }]); + sandbox.stub(feed.cache, "get").resolves({ + spocs: { lastUpdated: Date.now() }, + }); + clock.tick(THIRTY_MINUTES + 1); + + await feed.refreshAll({ isStartup: true }); + + // Once from cache, once to update the store + assert.calledTwice(feed.store.dispatch); + assert.equal( + feed.store.dispatch.firstCall.args[0].type, + at.DISCOVERY_STREAM_SPOCS_UPDATE + ); + }); + it("should not refresh spocs on startup if it is under THIRTY_MINUTES", async () => { + feed.loadSpocs.restore(); + sandbox.stub(feed.cache, "get").resolves({ + spocs: { lastUpdated: Date.now() }, + }); + sandbox.stub(feed, "fetchFromEndpoint").resolves("data"); + + await feed.refreshAll({ isStartup: true }); + + assert.notCalled(feed.fetchFromEndpoint); + }); + it("should refresh feeds on startup if it was served from cache", async () => { + feed.loadComponentFeeds.restore(); + + const fakeComponents = { components: [{ feed: { url: "foo.com" } }] }; + const fakeLayout = [fakeComponents]; + const fakeDiscoveryStream = { + DiscoveryStream: { + layout: fakeLayout, + }, + Prefs: { + values: { + "feeds.section.topstories": true, + "feeds.system.topstories": true, + }, + }, + }; + sandbox.stub(feed.store, "getState").returns(fakeDiscoveryStream); + sandbox.stub(feed, "rotate").callsFake(val => val); + sandbox + .stub(feed, "scoreItems") + .callsFake(val => ({ data: val, filtered: [], personalized: false })); + sandbox.stub(feed, "cleanUpTopRecImpressionPref").callsFake(val => val); + + const fakeCache = { + feeds: { "foo.com": { lastUpdated: Date.now(), data: "data" } }, + }; + sandbox.stub(feed.cache, "get").resolves(fakeCache); + clock.tick(THIRTY_MINUTES + 1); + sandbox.stub(feed, "fetchFromEndpoint").resolves({ + recommendations: "data", + settings: { + recsExpireTime: 1, + }, + }); + + await feed.refreshAll({ isStartup: true }); + + assert.calledOnce(feed.fetchFromEndpoint); + // Once from cache, once to update the feed, once to update that all + // feeds are done. + assert.calledThrice(feed.store.dispatch); + assert.equal( + feed.store.dispatch.secondCall.args[0].type, + at.DISCOVERY_STREAM_FEEDS_UPDATE + ); + }); + }); + }); + + describe("#scoreFeeds", () => { + beforeEach(() => { + sandbox.stub(feed.cache, "set").resolves(); + sandbox.spy(feed.store, "dispatch"); + }); + it("should score feeds and set cache, and dispatch", async () => { + const fakeDiscoveryStream = { + Prefs: { + values: { + "discoverystream.spocs.personalized": true, + "discoverystream.recs.personalized": false, + }, + }, + Personalization: { + initialized: true, + }, + DiscoveryStream: { + spocs: { + placements: [ + { name: "placement1" }, + { name: "placement2" }, + { name: "placement3" }, + ], + }, + }, + }; + sandbox.stub(feed.store, "getState").returns(fakeDiscoveryStream); + const recsExpireTime = 5600; + const fakeImpressions = { + first: Date.now() - recsExpireTime * 1000, + third: Date.now(), + }; + sandbox.stub(feed, "readDataPref").returns(fakeImpressions); + const fakeFeeds = { + data: { + "https://foo.com": { + data: { + recommendations: [ + { + id: "first", + item_score: 0.7, + }, + { + id: "second", + item_score: 0.6, + }, + ], + settings: { + recsExpireTime, + }, + }, + }, + "https://bar.com": { + data: { + recommendations: [ + { + id: "third", + item_score: 0.4, + }, + { + id: "fourth", + item_score: 0.6, + }, + { + id: "fifth", + item_score: 0.8, + }, + ], + settings: { + recsExpireTime, + }, + }, + }, + }, + }; + const feedsTestResult = { + "https://foo.com": { + personalized: true, + data: { + recommendations: [ + { + id: "second", + item_score: 0.6, + score: 0.6, + }, + { + id: "first", + item_score: 0.7, + score: 0.7, + }, + ], + settings: { + recsExpireTime, + }, + }, + }, + "https://bar.com": { + personalized: true, + data: { + recommendations: [ + { + id: "fifth", + item_score: 0.8, + score: 0.8, + }, + { + id: "fourth", + item_score: 0.6, + score: 0.6, + }, + { + id: "third", + item_score: 0.4, + score: 0.4, + }, + ], + settings: { + recsExpireTime, + }, + }, + }, + }; + + await feed.scoreFeeds(fakeFeeds); + + assert.calledWith(feed.cache.set, "feeds", feedsTestResult); + assert.equal( + feed.store.dispatch.firstCall.args[0].type, + at.DISCOVERY_STREAM_FEED_UPDATE + ); + assert.deepEqual(feed.store.dispatch.firstCall.args[0].data, { + url: "https://foo.com", + feed: feedsTestResult["https://foo.com"], + }); + assert.equal( + feed.store.dispatch.secondCall.args[0].type, + at.DISCOVERY_STREAM_FEED_UPDATE + ); + assert.deepEqual(feed.store.dispatch.secondCall.args[0].data, { + url: "https://bar.com", + feed: feedsTestResult["https://bar.com"], + }); + }); + + it("should skip already personalized feeds", async () => { + sandbox.spy(feed, "scoreItems"); + const recsExpireTime = 5600; + const fakeFeeds = { + data: { + "https://foo.com": { + personalized: true, + data: { + recommendations: [ + { + id: "first", + item_score: 0.7, + }, + { + id: "second", + item_score: 0.6, + }, + ], + settings: { + recsExpireTime, + }, + }, + }, + }, + }; + + await feed.scoreFeeds(fakeFeeds); + + assert.notCalled(feed.scoreItems); + }); + }); + + describe("#scoreSpocs", () => { + beforeEach(() => { + sandbox.stub(feed.cache, "set").resolves(); + sandbox.spy(feed.store, "dispatch"); + }); + it("should score spocs and set cache, dispatch", async () => { + const fakeDiscoveryStream = { + Prefs: { + values: { + "discoverystream.spocs.personalized": true, + "discoverystream.recs.personalized": false, + }, + }, + Personalization: { + initialized: true, + }, + DiscoveryStream: { + spocs: { + placements: [ + { name: "placement1" }, + { name: "placement2" }, + { name: "placement3" }, + ], + }, + }, + }; + sandbox.stub(feed.store, "getState").returns(fakeDiscoveryStream); + const fakeSpocs = { + lastUpdated: 1234, + data: { + placement1: { + items: [ + { + item_score: 0.6, + }, + { + item_score: 0.4, + }, + { + item_score: 0.8, + }, + ], + }, + placement2: { + items: [ + { + item_score: 0.6, + }, + { + item_score: 0.8, + }, + ], + }, + placement3: { items: [] }, + }, + }; + + await feed.scoreSpocs(fakeSpocs); + + const spocsTestResult = { + lastUpdated: 1234, + spocs: { + placement1: { + personalized: true, + items: [ + { + score: 0.8, + item_score: 0.8, + }, + { + score: 0.6, + item_score: 0.6, + }, + { + score: 0.4, + item_score: 0.4, + }, + ], + }, + placement2: { + personalized: true, + items: [ + { + score: 0.8, + item_score: 0.8, + }, + { + score: 0.6, + item_score: 0.6, + }, + ], + }, + placement3: { items: [] }, + }, + }; + assert.calledWith(feed.cache.set, "spocs", spocsTestResult); + assert.equal( + feed.store.dispatch.firstCall.args[0].type, + at.DISCOVERY_STREAM_SPOCS_UPDATE + ); + assert.deepEqual( + feed.store.dispatch.firstCall.args[0].data, + spocsTestResult + ); + }); + + it("should skip already personalized spocs", async () => { + sandbox.spy(feed, "scoreItems"); + const fakeDiscoveryStream = { + Prefs: { + values: { + "discoverystream.spocs.personalized": true, + "discoverystream.recs.personalized": false, + }, + }, + Personalization: { + initialized: true, + }, + DiscoveryStream: { + spocs: { + placements: [{ name: "placement1" }], + }, + }, + }; + sandbox.stub(feed.store, "getState").returns(fakeDiscoveryStream); + const fakeSpocs = { + lastUpdated: 1234, + data: { + placement1: { + personalized: true, + items: [ + { + item_score: 0.6, + }, + { + item_score: 0.4, + }, + { + item_score: 0.8, + }, + ], + }, + }, + }; + + await feed.scoreSpocs(fakeSpocs); + + assert.notCalled(feed.scoreItems); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_PERSONALIZATION_UPDATED", () => { + it("should call scoreFeeds and scoreSpocs if loaded", async () => { + const fakeDiscoveryStream = { + Prefs: { + values: { + pocketConfig: { + recsPersonalized: true, + spocsPersonalized: true, + }, + }, + }, + DiscoveryStream: { + feeds: { loaded: false }, + spocs: { loaded: false }, + }, + }; + + sandbox.stub(feed, "scoreFeeds").resolves(); + sandbox.stub(feed, "scoreSpocs").resolves(); + Object.defineProperty(feed, "personalized", { get: () => true }); + sandbox.stub(feed.store, "getState").returns(fakeDiscoveryStream); + + await feed.onAction({ + type: at.DISCOVERY_STREAM_PERSONALIZATION_UPDATED, + }); + + assert.notCalled(feed.scoreFeeds); + assert.notCalled(feed.scoreSpocs); + + fakeDiscoveryStream.DiscoveryStream.feeds.loaded = true; + fakeDiscoveryStream.DiscoveryStream.spocs.loaded = true; + + await feed.onAction({ + type: at.DISCOVERY_STREAM_PERSONALIZATION_UPDATED, + }); + + assert.calledOnce(feed.scoreFeeds); + assert.calledOnce(feed.scoreSpocs); + }); + }); + + describe("#observe", () => { + it("should call configReset on Pocket button pref change", async () => { + sandbox.stub(feed, "configReset").returns(); + feed.observe(null, "nsPref:changed", "extensions.pocket.enabled"); + assert.calledOnce(feed.configReset); + }); + }); + + describe("#scoreItem", () => { + it("should call calculateItemRelevanceScore with recommendationProvider with initial score", async () => { + const item = { + item_score: 0.6, + }; + feed.recommendationProvider.store.getState = () => ({ + Prefs: { + values: { + pocketConfig: { + recsPersonalized: true, + spocsPersonalized: true, + }, + "discoverystream.personalization.enabled": true, + "feeds.section.topstories": true, + "feeds.system.topstories": true, + }, + }, + }); + feed.recommendationProvider.calculateItemRelevanceScore = sandbox + .stub() + .returns(); + const result = await feed.scoreItem(item, true); + assert.calledOnce( + feed.recommendationProvider.calculateItemRelevanceScore + ); + assert.equal(result.score, 0.6); + }); + it("should fallback to score 1 without an initial score", async () => { + const item = {}; + feed.store.getState = () => ({ + Prefs: { + values: { + "discoverystream.spocs.personalized": true, + "discoverystream.recs.personalized": true, + "discoverystream.personalization.enabled": true, + }, + }, + }); + feed.recommendationProvider.calculateItemRelevanceScore = sandbox + .stub() + .returns(); + const result = await feed.scoreItem(item, true); + assert.equal(result.score, 1); + }); + }); + describe("new proxy feed", () => { + beforeEach(() => { + feed.store = createStore(combineReducers(reducers), { + Prefs: { + values: { + pocketConfig: { regionBffConfig: "DE" }, + }, + }, + }); + sandbox.stub(global.Region, "home").get(() => "DE"); + globals.set("NimbusFeatures", { + saveToPocket: { + getVariable: sandbox.stub(), + }, + }); + global.NimbusFeatures.saveToPocket.getVariable + .withArgs("bffApi") + .returns("bffApi"); + global.NimbusFeatures.saveToPocket.getVariable + .withArgs("oAuthConsumerKeyBff") + .returns("oAuthConsumerKeyBff"); + }); + it("should return true with isBff", async () => { + assert.isUndefined(feed._isBff); + assert.isTrue(feed.isBff); + assert.isTrue(feed._isBff); + }); + it("should update to new feed url", async () => { + await feed.loadLayout(feed.store.dispatch); + const { layout } = feed.store.getState().DiscoveryStream; + assert.equal( + layout[0].components[2].feed.url, + "https://bffApi/desktop/v1/recommendations?locale=$locale®ion=$region&count=30" + ); + }); + it("should fetch proper data from getComponentFeed", async () => { + const fakeCache = {}; + sandbox.stub(feed.cache, "get").returns(Promise.resolve(fakeCache)); + sandbox.stub(feed, "rotate").callsFake(val => val); + sandbox + .stub(feed, "scoreItems") + .callsFake(val => ({ data: val, filtered: [], personalized: false })); + sandbox.stub(feed, "fetchFromEndpoint").resolves({ + data: [ + { + recommendationId: "decaf-c0ff33", + tileId: 1234, + url: "url", + title: "title", + excerpt: "excerpt", + publisher: "publisher", + timeToRead: "timeToRead", + imageUrl: "imageUrl", + }, + ], + }); + + const feedData = await feed.getComponentFeed("url"); + assert.deepEqual(feedData, { + lastUpdated: 0, + personalized: false, + data: { + settings: {}, + recommendations: [ + { + id: 1234, + url: "url", + title: "title", + excerpt: "excerpt", + publisher: "publisher", + time_to_read: "timeToRead", + raw_image_src: "imageUrl", + recommendation_id: "decaf-c0ff33", + }, + ], + status: "success", + }, + }); + assert.equal(feed.fetchFromEndpoint.firstCall.args[0], "url"); + assert.equal(feed.fetchFromEndpoint.firstCall.args[1].method, "GET"); + assert.equal( + feed.fetchFromEndpoint.firstCall.args[1].headers.get("consumer_key"), + "oAuthConsumerKeyBff" + ); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/DownloadsManager.test.js b/browser/components/newtab/test/unit/lib/DownloadsManager.test.js new file mode 100644 index 0000000000..ac262baf90 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/DownloadsManager.test.js @@ -0,0 +1,373 @@ +import { actionTypes as at } from "common/Actions.sys.mjs"; +import { DownloadsManager } from "lib/DownloadsManager.sys.mjs"; +import { GlobalOverrider } from "test/unit/utils"; + +describe("Downloads Manager", () => { + let downloadsManager; + let globals; + const DOWNLOAD_URL = "https://site.com/download.mov"; + + beforeEach(() => { + globals = new GlobalOverrider(); + global.Cc["@mozilla.org/timer;1"] = { + createInstance() { + return { + initWithCallback: sinon.stub().callsFake(callback => callback()), + cancel: sinon.spy(), + }; + }, + }; + + globals.set("DownloadsCommon", { + getData: sinon.stub().returns({ + addView: sinon.stub(), + removeView: sinon.stub(), + }), + copyDownloadLink: sinon.stub(), + deleteDownload: sinon.stub().returns(Promise.resolve()), + openDownload: sinon.stub(), + showDownloadedFile: sinon.stub(), + }); + + downloadsManager = new DownloadsManager(); + downloadsManager.init({ dispatch() {} }); + downloadsManager.onDownloadAdded({ + source: { url: DOWNLOAD_URL }, + endTime: Date.now(), + target: { path: "/path/to/download.mov", exists: true }, + succeeded: true, + refresh: async () => {}, + }); + assert.ok(downloadsManager._downloadItems.has(DOWNLOAD_URL)); + + globals.set("NewTabUtils", { blockedLinks: { isBlocked() {} } }); + }); + afterEach(() => { + downloadsManager._downloadItems.clear(); + globals.restore(); + }); + describe("#init", () => { + it("should add a DownloadsCommon view on init", () => { + downloadsManager.init({ dispatch() {} }); + assert.calledTwice(global.DownloadsCommon.getData().addView); + }); + }); + describe("#onAction", () => { + it("should copy the file on COPY_DOWNLOAD_LINK", () => { + downloadsManager.onAction({ + type: at.COPY_DOWNLOAD_LINK, + data: { url: DOWNLOAD_URL }, + }); + assert.calledOnce(global.DownloadsCommon.copyDownloadLink); + }); + it("should remove the file on REMOVE_DOWNLOAD_FILE", () => { + downloadsManager.onAction({ + type: at.REMOVE_DOWNLOAD_FILE, + data: { url: DOWNLOAD_URL }, + }); + assert.calledOnce(global.DownloadsCommon.deleteDownload); + }); + it("should show the file on SHOW_DOWNLOAD_FILE", () => { + downloadsManager.onAction({ + type: at.SHOW_DOWNLOAD_FILE, + data: { url: DOWNLOAD_URL }, + }); + assert.calledOnce(global.DownloadsCommon.showDownloadedFile); + }); + it("should open the file on OPEN_DOWNLOAD_FILE if the type is download", () => { + downloadsManager.onAction({ + type: at.OPEN_DOWNLOAD_FILE, + data: { url: DOWNLOAD_URL, type: "download" }, + _target: { browser: {} }, + }); + assert.calledOnce(global.DownloadsCommon.openDownload); + }); + it("should copy the file on UNINIT", () => { + // DownloadsManager._downloadData needs to exist first + downloadsManager.onAction({ type: at.UNINIT }); + assert.calledOnce(global.DownloadsCommon.getData().removeView); + }); + it("should not execute a download command if we do not have the correct url", () => { + downloadsManager.onAction({ + type: at.SHOW_DOWNLOAD_FILE, + data: { url: "unknown_url" }, + }); + assert.notCalled(global.DownloadsCommon.showDownloadedFile); + }); + }); + describe("#onDownloadAdded", () => { + let newDownload; + beforeEach(() => { + downloadsManager._downloadItems.clear(); + newDownload = { + source: { url: "https://site.com/newDownload.mov" }, + endTime: Date.now(), + target: { path: "/path/to/newDownload.mov", exists: true }, + succeeded: true, + refresh: async () => {}, + }; + }); + afterEach(() => { + downloadsManager._downloadItems.clear(); + }); + it("should add a download on onDownloadAdded", () => { + downloadsManager.onDownloadAdded(newDownload); + assert.ok( + downloadsManager._downloadItems.has("https://site.com/newDownload.mov") + ); + }); + it("should not add a download if it already exists", () => { + downloadsManager.onDownloadAdded(newDownload); + downloadsManager.onDownloadAdded(newDownload); + downloadsManager.onDownloadAdded(newDownload); + downloadsManager.onDownloadAdded(newDownload); + const results = downloadsManager._downloadItems; + assert.equal(results.size, 1); + }); + it("should not return any downloads if no threshold is provided", async () => { + downloadsManager.onDownloadAdded(newDownload); + const results = await downloadsManager.getDownloads(null, {}); + assert.equal(results.length, 0); + }); + it("should stop at numItems when it found one it's looking for", async () => { + const aDownload = { + source: { url: "https://site.com/aDownload.pdf" }, + endTime: Date.now(), + target: { path: "/path/to/aDownload.pdf", exists: true }, + succeeded: true, + refresh: async () => {}, + }; + downloadsManager.onDownloadAdded(aDownload); + downloadsManager.onDownloadAdded(newDownload); + const results = await downloadsManager.getDownloads(Infinity, { + numItems: 1, + onlySucceeded: true, + onlyExists: true, + }); + assert.equal(results.length, 1); + assert.equal(results[0].url, aDownload.source.url); + }); + it("should get all the downloads younger than the threshold provided", async () => { + const oldDownload = { + source: { url: "https://site.com/oldDownload.pdf" }, + endTime: Date.now() - 40 * 60 * 60 * 1000, + target: { path: "/path/to/oldDownload.pdf", exists: true }, + succeeded: true, + refresh: async () => {}, + }; + // Add an old download (older than 36 hours in this case) + downloadsManager.onDownloadAdded(oldDownload); + downloadsManager.onDownloadAdded(newDownload); + const RECENT_DOWNLOAD_THRESHOLD = 36 * 60 * 60 * 1000; + const results = await downloadsManager.getDownloads( + RECENT_DOWNLOAD_THRESHOLD, + { numItems: 5, onlySucceeded: true, onlyExists: true } + ); + assert.equal(results.length, 1); + assert.equal(results[0].url, newDownload.source.url); + }); + it("should dispatch DOWNLOAD_CHANGED when adding a download", () => { + downloadsManager._store.dispatch = sinon.spy(); + downloadsManager._downloadTimer = null; // Nuke the timer + downloadsManager.onDownloadAdded(newDownload); + assert.calledOnce(downloadsManager._store.dispatch); + }); + it("should refresh the downloads if onlyExists is true", async () => { + const aDownload = { + source: { url: "https://site.com/aDownload.pdf" }, + endTime: Date.now() - 40 * 60 * 60 * 1000, + target: { path: "/path/to/aDownload.pdf", exists: true }, + succeeded: true, + refresh: () => {}, + }; + sinon.stub(aDownload, "refresh").returns(Promise.resolve()); + downloadsManager.onDownloadAdded(aDownload); + await downloadsManager.getDownloads(Infinity, { + numItems: 5, + onlySucceeded: true, + onlyExists: true, + }); + assert.calledOnce(aDownload.refresh); + }); + it("should not refresh the downloads if onlyExists is false (by default)", async () => { + const aDownload = { + source: { url: "https://site.com/aDownload.pdf" }, + endTime: Date.now() - 40 * 60 * 60 * 1000, + target: { path: "/path/to/aDownload.pdf", exists: true }, + succeeded: true, + refresh: () => {}, + }; + sinon.stub(aDownload, "refresh").returns(Promise.resolve()); + downloadsManager.onDownloadAdded(aDownload); + await downloadsManager.getDownloads(Infinity, { + numItems: 5, + onlySucceeded: true, + }); + assert.notCalled(aDownload.refresh); + }); + it("should only return downloads that exist if specified", async () => { + const nonExistantDownload = { + source: { url: "https://site.com/nonExistantDownload.pdf" }, + endTime: Date.now() - 40 * 60 * 60 * 1000, + target: { path: "/path/to/nonExistantDownload.pdf", exists: false }, + succeeded: true, + refresh: async () => {}, + }; + downloadsManager.onDownloadAdded(newDownload); + downloadsManager.onDownloadAdded(nonExistantDownload); + const results = await downloadsManager.getDownloads(Infinity, { + numItems: 5, + onlySucceeded: true, + onlyExists: true, + }); + assert.equal(results.length, 1); + assert.equal(results[0].url, newDownload.source.url); + }); + it("should return all downloads that either exist or don't exist if not specified", async () => { + const nonExistantDownload = { + source: { url: "https://site.com/nonExistantDownload.pdf" }, + endTime: Date.now() - 40 * 60 * 60 * 1000, + target: { path: "/path/to/nonExistantDownload.pdf", exists: false }, + succeeded: true, + refresh: async () => {}, + }; + downloadsManager.onDownloadAdded(newDownload); + downloadsManager.onDownloadAdded(nonExistantDownload); + const results = await downloadsManager.getDownloads(Infinity, { + numItems: 5, + onlySucceeded: true, + }); + assert.equal(results.length, 2); + assert.equal(results[0].url, newDownload.source.url); + assert.equal(results[1].url, nonExistantDownload.source.url); + }); + it("should return only unblocked downloads", async () => { + const nonExistantDownload = { + source: { url: "https://site.com/nonExistantDownload.pdf" }, + endTime: Date.now() - 40 * 60 * 60 * 1000, + target: { path: "/path/to/nonExistantDownload.pdf", exists: false }, + succeeded: true, + refresh: async () => {}, + }; + downloadsManager.onDownloadAdded(newDownload); + downloadsManager.onDownloadAdded(nonExistantDownload); + globals.set("NewTabUtils", { + blockedLinks: { + isBlocked: item => item.url === nonExistantDownload.source.url, + }, + }); + + const results = await downloadsManager.getDownloads(Infinity, { + numItems: 5, + onlySucceeded: true, + }); + + assert.equal(results.length, 1); + assert.propertyVal(results[0], "url", newDownload.source.url); + }); + it("should only return downloads that were successful if specified", async () => { + const nonSuccessfulDownload = { + source: { url: "https://site.com/nonSuccessfulDownload.pdf" }, + endTime: Date.now() - 40 * 60 * 60 * 1000, + target: { path: "/path/to/nonSuccessfulDownload.pdf", exists: false }, + succeeded: false, + refresh: async () => {}, + }; + downloadsManager.onDownloadAdded(newDownload); + downloadsManager.onDownloadAdded(nonSuccessfulDownload); + const results = await downloadsManager.getDownloads(Infinity, { + numItems: 5, + onlySucceeded: true, + }); + assert.equal(results.length, 1); + assert.equal(results[0].url, newDownload.source.url); + }); + it("should return all downloads that were either successful or not if not specified", async () => { + const nonExistantDownload = { + source: { url: "https://site.com/nonExistantDownload.pdf" }, + endTime: Date.now() - 40 * 60 * 60 * 1000, + target: { path: "/path/to/nonExistantDownload.pdf", exists: true }, + succeeded: false, + refresh: async () => {}, + }; + downloadsManager.onDownloadAdded(newDownload); + downloadsManager.onDownloadAdded(nonExistantDownload); + const results = await downloadsManager.getDownloads(Infinity, { + numItems: 5, + }); + assert.equal(results.length, 2); + assert.equal(results[0].url, newDownload.source.url); + assert.equal(results[1].url, nonExistantDownload.source.url); + }); + it("should sort the downloads by recency", async () => { + const olderDownload1 = { + source: { url: "https://site.com/oldDownload1.pdf" }, + endTime: Date.now() - 2 * 60 * 60 * 1000, // 2 hours ago + target: { path: "/path/to/oldDownload1.pdf", exists: true }, + succeeded: true, + refresh: async () => {}, + }; + const olderDownload2 = { + source: { url: "https://site.com/oldDownload2.pdf" }, + endTime: Date.now() - 60 * 60 * 1000, // 1 hour ago + target: { path: "/path/to/oldDownload2.pdf", exists: true }, + succeeded: true, + refresh: async () => {}, + }; + // Add some older downloads and check that they are in order + downloadsManager.onDownloadAdded(olderDownload1); + downloadsManager.onDownloadAdded(olderDownload2); + downloadsManager.onDownloadAdded(newDownload); + const results = await downloadsManager.getDownloads(Infinity, { + numItems: 5, + onlySucceeded: true, + onlyExists: true, + }); + assert.equal(results.length, 3); + assert.equal(results[0].url, newDownload.source.url); + assert.equal(results[1].url, olderDownload2.source.url); + assert.equal(results[2].url, olderDownload1.source.url); + }); + it("should format the description properly if there is no file type", async () => { + newDownload.target.path = null; + downloadsManager.onDownloadAdded(newDownload); + const results = await downloadsManager.getDownloads(Infinity, { + numItems: 5, + onlySucceeded: true, + onlyExists: true, + }); + assert.equal(results.length, 1); + assert.equal(results[0].description, "1.5 MB"); // see unit-entry.js to see where this comes from + }); + }); + describe("#onDownloadRemoved", () => { + let newDownload; + beforeEach(() => { + downloadsManager._downloadItems.clear(); + newDownload = { + source: { url: "https://site.com/removeMe.mov" }, + endTime: Date.now(), + target: { path: "/path/to/removeMe.mov", exists: true }, + succeeded: true, + refresh: async () => {}, + }; + downloadsManager.onDownloadAdded(newDownload); + }); + it("should remove a download if it exists on onDownloadRemoved", async () => { + downloadsManager.onDownloadRemoved({ + source: { url: "https://site.com/removeMe.mov" }, + }); + const results = await downloadsManager.getDownloads(Infinity, { + numItems: 5, + }); + assert.deepEqual(results, []); + }); + it("should dispatch DOWNLOAD_CHANGED when removing a download", () => { + downloadsManager._store.dispatch = sinon.spy(); + downloadsManager.onDownloadRemoved({ + source: { url: "https://site.com/removeMe.mov" }, + }); + assert.calledOnce(downloadsManager._store.dispatch); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/FaviconFeed.test.js b/browser/components/newtab/test/unit/lib/FaviconFeed.test.js new file mode 100644 index 0000000000..e9be9b86ba --- /dev/null +++ b/browser/components/newtab/test/unit/lib/FaviconFeed.test.js @@ -0,0 +1,233 @@ +"use strict"; +import { FaviconFeed, fetchIconFromRedirects } from "lib/FaviconFeed.sys.mjs"; +import { actionTypes as at } from "common/Actions.sys.mjs"; +import { GlobalOverrider } from "test/unit/utils"; + +const FAKE_ENDPOINT = "https://foo.com/"; + +describe("FaviconFeed", () => { + let feed; + let globals; + let sandbox; + let clock; + let siteIconsPref; + + beforeEach(() => { + clock = sinon.useFakeTimers(); + globals = new GlobalOverrider(); + sandbox = globals.sandbox; + globals.set("PlacesUtils", { + favicons: { + setAndFetchFaviconForPage: sandbox.spy(), + getFaviconDataForPage: () => Promise.resolve(null), + FAVICON_LOAD_NON_PRIVATE: 1, + }, + history: { + TRANSITIONS: { + REDIRECT_TEMPORARY: 1, + REDIRECT_PERMANENT: 2, + }, + }, + }); + globals.set("NewTabUtils", { + activityStreamProvider: { executePlacesQuery: () => Promise.resolve([]) }, + }); + siteIconsPref = true; + sandbox + .stub(global.Services.prefs, "getBoolPref") + .withArgs("browser.chrome.site_icons") + .callsFake(() => siteIconsPref); + + feed = new FaviconFeed(); + feed.store = { + dispatch: sinon.spy(), + getState() { + return this.state; + }, + state: { + Prefs: { values: { "tippyTop.service.endpoint": FAKE_ENDPOINT } }, + }, + }; + }); + afterEach(() => { + clock.restore(); + globals.restore(); + }); + + it("should create a FaviconFeed", () => { + assert.instanceOf(feed, FaviconFeed); + }); + + describe("#fetchIcon", () => { + let domain; + let url; + beforeEach(() => { + domain = "mozilla.org"; + url = `https://${domain}/`; + feed.getSite = sandbox + .stub() + .returns(Promise.resolve({ domain, image_url: `${url}/icon.png` })); + feed._queryForRedirects.clear(); + }); + + it("should setAndFetchFaviconForPage if the url is in the TippyTop data", async () => { + await feed.fetchIcon(url); + + assert.calledOnce(global.PlacesUtils.favicons.setAndFetchFaviconForPage); + assert.calledWith( + global.PlacesUtils.favicons.setAndFetchFaviconForPage, + sinon.match({ spec: url }), + { ref: "tippytop", spec: `${url}/icon.png` }, + false, + global.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, + null, + undefined + ); + }); + it("should NOT setAndFetchFaviconForPage if site_icons pref is false", async () => { + siteIconsPref = false; + + await feed.fetchIcon(url); + + assert.notCalled(global.PlacesUtils.favicons.setAndFetchFaviconForPage); + }); + it("should NOT setAndFetchFaviconForPage if the url is NOT in the TippyTop data", async () => { + feed.getSite = sandbox.stub().returns(Promise.resolve(null)); + await feed.fetchIcon("https://example.com"); + + assert.notCalled(global.PlacesUtils.favicons.setAndFetchFaviconForPage); + }); + it("should issue a fetchIconFromRedirects if the url is NOT in the TippyTop data", async () => { + feed.getSite = sandbox.stub().returns(Promise.resolve(null)); + sandbox.spy(global.Services.tm, "idleDispatchToMainThread"); + + await feed.fetchIcon("https://example.com"); + + assert.calledOnce(global.Services.tm.idleDispatchToMainThread); + }); + it("should only issue fetchIconFromRedirects once on the same url", async () => { + feed.getSite = sandbox.stub().returns(Promise.resolve(null)); + sandbox.spy(global.Services.tm, "idleDispatchToMainThread"); + + await feed.fetchIcon("https://example.com"); + await feed.fetchIcon("https://example.com"); + + assert.calledOnce(global.Services.tm.idleDispatchToMainThread); + }); + it("should issue fetchIconFromRedirects twice on two different urls", async () => { + feed.getSite = sandbox.stub().returns(Promise.resolve(null)); + sandbox.spy(global.Services.tm, "idleDispatchToMainThread"); + + await feed.fetchIcon("https://example.com"); + await feed.fetchIcon("https://another.example.com"); + + assert.calledTwice(global.Services.tm.idleDispatchToMainThread); + }); + }); + + describe("#getSite", () => { + it("should return site data if RemoteSettings has an entry for the domain", async () => { + const get = () => + Promise.resolve([{ domain: "example.com", image_url: "foo.img" }]); + feed._tippyTop = { get }; + const site = await feed.getSite("example.com"); + assert.equal(site.domain, "example.com"); + }); + it("should return null if RemoteSettings doesn't have an entry for the domain", async () => { + const get = () => Promise.resolve([]); + feed._tippyTop = { get }; + const site = await feed.getSite("example.com"); + assert.isNull(site); + }); + it("should lazy init _tippyTop", async () => { + assert.isUndefined(feed._tippyTop); + await feed.getSite("example.com"); + assert.ok(feed._tippyTop); + }); + }); + + describe("#onAction", () => { + it("should fetchIcon on RICH_ICON_MISSING", async () => { + feed.fetchIcon = sinon.spy(); + const url = "https://mozilla.org"; + feed.onAction({ type: at.RICH_ICON_MISSING, data: { url } }); + assert.calledOnce(feed.fetchIcon); + assert.calledWith(feed.fetchIcon, url); + }); + }); + + describe("#fetchIconFromRedirects", () => { + let domain; + let url; + let iconUrl; + + beforeEach(() => { + domain = "mozilla.org"; + url = `https://${domain}/`; + iconUrl = `${url}/icon.png`; + }); + it("should setAndFetchFaviconForPage if the url was redirected with a icon", async () => { + sandbox + .stub(global.NewTabUtils.activityStreamProvider, "executePlacesQuery") + .resolves([ + { visit_id: 1, url: domain }, + { visit_id: 2, url }, + ]); + sandbox + .stub(global.PlacesUtils.favicons, "getFaviconDataForPage") + .callsArgWith(1, { spec: iconUrl }, 0, null, null, 96); + + await fetchIconFromRedirects(domain); + + assert.calledOnce(global.PlacesUtils.favicons.setAndFetchFaviconForPage); + assert.calledWith( + global.PlacesUtils.favicons.setAndFetchFaviconForPage, + sinon.match({ spec: domain }), + { spec: iconUrl }, + false, + global.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, + null, + undefined + ); + }); + it("should NOT setAndFetchFaviconForPage if the url doesn't have any redirect", async () => { + sandbox + .stub(global.NewTabUtils.activityStreamProvider, "executePlacesQuery") + .resolves([]); + + await fetchIconFromRedirects(domain); + + assert.notCalled(global.PlacesUtils.favicons.setAndFetchFaviconForPage); + }); + it("should NOT setAndFetchFaviconForPage if the original url doesn't have a icon", async () => { + sandbox + .stub(global.NewTabUtils.activityStreamProvider, "executePlacesQuery") + .resolves([ + { visit_id: 1, url: domain }, + { visit_id: 2, url }, + ]); + sandbox + .stub(global.PlacesUtils.favicons, "getFaviconDataForPage") + .callsArgWith(1, null, null, null, null, null); + + await fetchIconFromRedirects(domain); + + assert.notCalled(global.PlacesUtils.favicons.setAndFetchFaviconForPage); + }); + it("should NOT setAndFetchFaviconForPage if the original url doesn't have a rich icon", async () => { + sandbox + .stub(global.NewTabUtils.activityStreamProvider, "executePlacesQuery") + .resolves([ + { visit_id: 1, url: domain }, + { visit_id: 2, url }, + ]); + sandbox + .stub(global.PlacesUtils.favicons, "getFaviconDataForPage") + .callsArgWith(1, { spec: iconUrl }, 0, null, null, 16); + + await fetchIconFromRedirects(domain); + + assert.notCalled(global.PlacesUtils.favicons.setAndFetchFaviconForPage); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/FilterAdult.test.js b/browser/components/newtab/test/unit/lib/FilterAdult.test.js new file mode 100644 index 0000000000..0e98a0d006 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/FilterAdult.test.js @@ -0,0 +1,112 @@ +import { FilterAdult } from "lib/FilterAdult.sys.mjs"; +import { GlobalOverrider } from "test/unit/utils"; + +describe("FilterAdult", () => { + let hashStub; + let hashValue; + let globals; + + beforeEach(() => { + globals = new GlobalOverrider(); + hashStub = { + finish: sinon.stub().callsFake(() => hashValue), + init: sinon.stub(), + update: sinon.stub(), + }; + globals.set("Cc", { + "@mozilla.org/security/hash;1": { + createInstance() { + return hashStub; + }, + }, + }); + globals.set("gFilterAdultEnabled", true); + }); + + afterEach(() => { + hashValue = ""; + globals.restore(); + }); + + describe("filter", () => { + it("should default to include on unexpected urls", () => { + const empty = {}; + + const result = FilterAdult.filter([empty]); + + assert.equal(result.length, 1); + assert.equal(result[0], empty); + }); + it("should not filter out non-adult urls", () => { + const link = { url: "https://mozilla.org/" }; + + const result = FilterAdult.filter([link]); + + assert.equal(result.length, 1); + assert.equal(result[0], link); + }); + it("should filter out adult urls", () => { + // Use a hash value that is in the adult set + hashValue = "+/UCpAhZhz368iGioEO8aQ=="; + const link = { url: "https://some-adult-site/" }; + + const result = FilterAdult.filter([link]); + + assert.equal(result.length, 0); + }); + it("should not filter out adult urls if the preference is turned off", () => { + // Use a hash value that is in the adult set + hashValue = "+/UCpAhZhz368iGioEO8aQ=="; + globals.set("gFilterAdultEnabled", false); + const link = { url: "https://some-adult-site/" }; + + const result = FilterAdult.filter([link]); + + assert.equal(result.length, 1); + assert.equal(result[0], link); + }); + }); + + describe("isAdultUrl", () => { + it("should default to false on unexpected urls", () => { + const result = FilterAdult.isAdultUrl(""); + + assert.equal(result, false); + }); + it("should return false for non-adult urls", () => { + const result = FilterAdult.isAdultUrl("https://mozilla.org/"); + + assert.equal(result, false); + }); + it("should return true for adult urls", () => { + // Use a hash value that is in the adult set + hashValue = "+/UCpAhZhz368iGioEO8aQ=="; + const result = FilterAdult.isAdultUrl("https://some-adult-site/"); + + assert.equal(result, true); + }); + it("should return false for adult urls when the preference is turned off", () => { + // Use a hash value that is in the adult set + hashValue = "+/UCpAhZhz368iGioEO8aQ=="; + globals.set("gFilterAdultEnabled", false); + const result = FilterAdult.isAdultUrl("https://some-adult-site/"); + + assert.equal(result, false); + }); + + describe("test functions", () => { + it("should add and remove a filter in the adult list", () => { + // Use a hash value that is in the adult set + FilterAdult.addDomainToList("https://some-adult-site/"); + let result = FilterAdult.isAdultUrl("https://some-adult-site/"); + + assert.equal(result, true); + + FilterAdult.removeDomainFromList("https://some-adult-site/"); + result = FilterAdult.isAdultUrl("https://some-adult-site/"); + + assert.equal(result, false); + }); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/LinksCache.test.js b/browser/components/newtab/test/unit/lib/LinksCache.test.js new file mode 100644 index 0000000000..8a4d33d2f2 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/LinksCache.test.js @@ -0,0 +1,16 @@ +import { LinksCache } from "lib/LinksCache.sys.mjs"; + +describe("LinksCache", () => { + it("throws when failing request", async () => { + const cache = new LinksCache(); + + let rejected = false; + try { + await cache.request(); + } catch (error) { + rejected = true; + } + + assert(rejected); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/NewTabInit.test.js b/browser/components/newtab/test/unit/lib/NewTabInit.test.js new file mode 100644 index 0000000000..68ab9d7821 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/NewTabInit.test.js @@ -0,0 +1,81 @@ +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { NewTabInit } from "lib/NewTabInit.sys.mjs"; + +describe("NewTabInit", () => { + let instance; + let store; + let STATE; + const requestFromTab = portID => + instance.onAction( + ac.AlsoToMain({ type: at.NEW_TAB_STATE_REQUEST }, portID) + ); + beforeEach(() => { + STATE = {}; + store = { getState: sinon.stub().returns(STATE), dispatch: sinon.stub() }; + instance = new NewTabInit(); + instance.store = store; + }); + it("should reply with a copy of the state immediately", () => { + requestFromTab(123); + + const resp = ac.AlsoToOneContent( + { type: at.NEW_TAB_INITIAL_STATE, data: STATE }, + 123 + ); + assert.calledWith(store.dispatch, resp); + }); + describe("early / simulated new tabs", () => { + const simulateTabInit = portID => + instance.onAction({ + type: at.NEW_TAB_INIT, + data: { portID, simulated: true }, + }); + beforeEach(() => { + simulateTabInit("foo"); + }); + it("should dispatch if not replied yet", () => { + requestFromTab("foo"); + + assert.calledWith( + store.dispatch, + ac.AlsoToOneContent( + { type: at.NEW_TAB_INITIAL_STATE, data: STATE }, + "foo" + ) + ); + }); + it("should dispatch once for multiple requests", () => { + requestFromTab("foo"); + requestFromTab("foo"); + requestFromTab("foo"); + + assert.calledOnce(store.dispatch); + }); + describe("multiple tabs", () => { + beforeEach(() => { + simulateTabInit("bar"); + }); + it("should dispatch once to each tab", () => { + requestFromTab("foo"); + requestFromTab("bar"); + assert.calledTwice(store.dispatch); + requestFromTab("foo"); + requestFromTab("bar"); + + assert.calledTwice(store.dispatch); + }); + it("should clean up when tabs close", () => { + assert.propertyVal(instance._repliedEarlyTabs, "size", 2); + instance.onAction(ac.AlsoToMain({ type: at.NEW_TAB_UNLOAD }, "foo")); + assert.propertyVal(instance._repliedEarlyTabs, "size", 1); + instance.onAction(ac.AlsoToMain({ type: at.NEW_TAB_UNLOAD }, "foo")); + assert.propertyVal(instance._repliedEarlyTabs, "size", 1); + instance.onAction(ac.AlsoToMain({ type: at.NEW_TAB_UNLOAD }, "bar")); + assert.propertyVal(instance._repliedEarlyTabs, "size", 0); + }); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/PersistentCache.test.js b/browser/components/newtab/test/unit/lib/PersistentCache.test.js new file mode 100644 index 0000000000..e645b8d398 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/PersistentCache.test.js @@ -0,0 +1,142 @@ +import { GlobalOverrider } from "test/unit/utils"; +import { PersistentCache } from "lib/PersistentCache.sys.mjs"; + +describe("PersistentCache", () => { + let fakeIOUtils; + let fakePathUtils; + let cache; + let filename = "cache.json"; + let consoleErrorStub; + let globals; + let sandbox; + + beforeEach(() => { + globals = new GlobalOverrider(); + sandbox = sinon.createSandbox(); + fakeIOUtils = { + writeJSON: sinon.stub().resolves(0), + readJSON: sinon.stub().resolves({}), + }; + fakePathUtils = { + join: sinon.stub().returns(filename), + localProfileDir: "/", + }; + consoleErrorStub = sandbox.stub(); + globals.set("console", { error: consoleErrorStub }); + globals.set("IOUtils", fakeIOUtils); + globals.set("PathUtils", fakePathUtils); + + cache = new PersistentCache(filename); + }); + afterEach(() => { + globals.restore(); + sandbox.restore(); + }); + + describe("#get", () => { + it("tries to read the file", async () => { + await cache.get("foo"); + assert.calledOnce(fakeIOUtils.readJSON); + }); + it("doesnt try to read the file if it was already loaded", async () => { + await cache._load(); + fakeIOUtils.readJSON.resetHistory(); + await cache.get("foo"); + assert.notCalled(fakeIOUtils.readJSON); + }); + it("should catch and report errors", async () => { + fakeIOUtils.readJSON.rejects(new SyntaxError("Failed to parse JSON")); + await cache._load(); + assert.calledOnce(consoleErrorStub); + + cache._cache = undefined; + consoleErrorStub.resetHistory(); + + fakeIOUtils.readJSON.rejects( + new DOMException("IOUtils shutting down", "AbortError") + ); + await cache._load(); + assert.calledOnce(consoleErrorStub); + + cache._cache = undefined; + consoleErrorStub.resetHistory(); + + fakeIOUtils.readJSON.rejects( + new DOMException("File not found", "NotFoundError") + ); + await cache._load(); + assert.notCalled(consoleErrorStub); + }); + it("returns data for a given cache key", async () => { + fakeIOUtils.readJSON.resolves({ foo: "bar" }); + let value = await cache.get("foo"); + assert.equal(value, "bar"); + }); + it("returns undefined for a cache key that doesn't exist", async () => { + let value = await cache.get("baz"); + assert.equal(value, undefined); + }); + it("returns all the data if no cache key is specified", async () => { + fakeIOUtils.readJSON.resolves({ foo: "bar" }); + let value = await cache.get(); + assert.deepEqual(value, { foo: "bar" }); + }); + }); + + describe("#set", () => { + it("tries to read the file on the first set", async () => { + await cache.set("foo", { x: 42 }); + assert.calledOnce(fakeIOUtils.readJSON); + }); + it("doesnt try to read the file if it was already loaded", async () => { + cache = new PersistentCache(filename, true); + await cache._load(); + fakeIOUtils.readJSON.resetHistory(); + await cache.set("foo", { x: 42 }); + assert.notCalled(fakeIOUtils.readJSON); + }); + it("sets a string value", async () => { + const key = "testkey"; + const value = "testvalue"; + await cache.set(key, value); + const cachedValue = await cache.get(key); + assert.equal(cachedValue, value); + }); + it("sets an object value", async () => { + const key = "testkey"; + const value = { x: 1, y: 2, z: 3 }; + await cache.set(key, value); + const cachedValue = await cache.get(key); + assert.deepEqual(cachedValue, value); + }); + it("writes the data to file", async () => { + const key = "testkey"; + const value = { x: 1, y: 2, z: 3 }; + + await cache.set(key, value); + assert.calledOnce(fakeIOUtils.writeJSON); + assert.calledWith( + fakeIOUtils.writeJSON, + filename, + { [[key]]: value }, + { tmpPath: `${filename}.tmp` } + ); + }); + it("throws when failing to get file path", async () => { + Object.defineProperty(fakePathUtils, "localProfileDir", { + get() { + throw new Error(); + }, + }); + + let rejected = false; + try { + await cache.set("key", "val"); + } catch (error) { + rejected = true; + } + + assert(rejected); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/PersonalityProvider/NaiveBayesTextTagger.test.js b/browser/components/newtab/test/unit/lib/PersonalityProvider/NaiveBayesTextTagger.test.js new file mode 100644 index 0000000000..18c634d43d --- /dev/null +++ b/browser/components/newtab/test/unit/lib/PersonalityProvider/NaiveBayesTextTagger.test.js @@ -0,0 +1,95 @@ +import { NaiveBayesTextTagger } from "lib/PersonalityProvider/NaiveBayesTextTagger.mjs"; +import { + tokenize, + toksToTfIdfVector, +} from "lib/PersonalityProvider/Tokenize.mjs"; + +const EPSILON = 0.00001; + +describe("Naive Bayes Tagger", () => { + describe("#tag", () => { + let model = { + model_type: "nb", + positive_class_label: "military", + positive_class_id: 0, + positive_class_threshold_log_prob: -0.5108256237659907, + classes: [ + { + log_prior: -0.6881346387364013, + feature_log_probs: [ + -6.2149425847276, -6.829869141665873, -7.124856122235796, + -7.116661287797188, -6.694751331313906, -7.11798266787003, + -6.5094904366004185, -7.1639509366900604, -7.218981434452414, + -6.854842907887801, -7.080328841624584, + ], + }, + { + log_prior: -0.6981849745899025, + feature_log_probs: [ + -7.0575941199203465, -6.632333513597953, -7.382756370680115, + -7.1160793981275905, -8.467120918791892, -8.369201274990882, + -8.518506617006922, -7.015756380369387, -7.739036845511857, + -9.748294397894645, -3.9353548206941955, + ], + }, + ], + vocab_idfs: { + deal: [0, 5.5058519847862275], + easy: [1, 5.5058519847862275], + tanks: [2, 5.601162164590552], + sites: [3, 5.957837108529285], + care: [4, 5.957837108529285], + needs: [5, 5.824305715904762], + finally: [6, 5.706522680248379], + super: [7, 5.264689927969339], + heard: [8, 5.5058519847862275], + reached: [9, 5.957837108529285], + words: [10, 5.070533913528382], + }, + }; + let instance = new NaiveBayesTextTagger(model, toksToTfIdfVector); + + let testCases = [ + { + input: "Finally! Super easy care for your tanks!", + expected: { + label: "military", + logProb: -0.16299510296630082, + confident: true, + }, + }, + { + input: "heard", + expected: { + label: "military", + logProb: -0.4628170738373294, + confident: false, + }, + }, + { + input: "words", + expected: { + label: null, + logProb: -0.04258339303757985, + confident: false, + }, + }, + ]; + + let checkTag = tc => { + let actual = instance.tagTokens(tokenize(tc.input)); + it(`should tag ${tc.input} with ${tc.expected.label}`, () => { + assert.equal(tc.expected.label, actual.label); + }); + it(`should give ${tc.input} the correct probability`, () => { + let delta = Math.abs(tc.expected.logProb - actual.logProb); + assert.isTrue(delta <= EPSILON); + }); + }; + + // RELEASE THE TESTS! + for (let tc of testCases) { + checkTag(tc); + } + }); +}); diff --git a/browser/components/newtab/test/unit/lib/PersonalityProvider/NmfTextTagger.test.js b/browser/components/newtab/test/unit/lib/PersonalityProvider/NmfTextTagger.test.js new file mode 100644 index 0000000000..aae070b305 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/PersonalityProvider/NmfTextTagger.test.js @@ -0,0 +1,479 @@ +import { NmfTextTagger } from "lib/PersonalityProvider/NmfTextTagger.mjs"; +import { + tokenize, + toksToTfIdfVector, +} from "lib/PersonalityProvider/Tokenize.mjs"; + +const EPSILON = 0.00001; + +describe("NMF Tagger", () => { + describe("#tag", () => { + // The numbers in this model were pulled from existing trained model. + let model = { + document_topic: { + environment: [ + 0.05313956429537541, 0.07314019377743895, 0.03247190024863182, + 0.016189529772591395, 0.003812317145412572, 0.03863075834647775, + 0.007495425135831521, 0.005100298003919777, 0.005245622179405364, + 0.036196010766427554, 0.02189970342121833, 0.03514130992119014, + 0.001248114096050196, 0.0030908722594824665, 0.0023874256586350626, + 0.008533674814792993, 0.0009424690250135675, 0.01603124888144218, + 0.00752822798092765, 0.0039046678154748796, 0.03521776907836766, + 0.00614546613169027, 0.0008272200196643818, 0.01405638079154697, + 0.001990670259485496, 0.002803666919676377, 0.013841677883061631, + 0.004093362693745272, 0.009310678536276432, 0.006158920150866703, + 0.006821027337091937, 0.002712031105462971, 0.009093298611644996, + 0.014642160500331744, 0.0067239941045715386, 0.007150418784462898, + 0.0064652818600521265, 0.0006735690394489199, 0.02063188588742841, + 0.003213083349614106, 0.0031998068360970093, 0.00264520606931871, + 0.008854824468146531, 0.0024170562884908786, 0.0013705390639746128, + 0.0030575940757273288, 0.010417378215688392, 0.002356164040132228, + 0.0026710154645455007, 0.0007295327370144145, 0.0585307418954327, + 0.0037987763460599574, 0.003199095437138493, 0.004368800434950577, + 0.005087168372751965, 0.0011100904433965942, 0.01700096791869979, + 0.01929226435023826, 0.010536397909643058, 0.001734999985783697, + 0.003852807194017686, 0.007916805773686475, 0.028375307444815964, + 0.0012422599635274355, 0.0009298594944844238, 0.02095410849846837, + 0.0017269844428419192, 0.002152880993141985, 0.0030226616228192387, + 0.004804812297400959, 0.0012383636748462198, 0.006991278216261148, + 0.0013747035300597538, 0.002041541234639563, 0.012076270996247411, + 0.006643837514421182, 0.003974012776560734, 0.015794539051705442, + 0.007601190171659186, 0.016474925942594837, 0.002729423078513777, + 0.007635146179880609, 0.013457547041824648, 0.0007592338429017099, + 0.002947096673767141, 0.006371935735541048, 0.003356178481568716, + 0.00451933490245723, 0.0019006306992329104, 0.013048046603391707, + 0.023541628496101297, 0.027659066125377194, 0.002312727786055524, + 0.0014189157259186062, 0.01963766030236683, 0.0026014761547439634, + 0.002333697870992923, 0.003401734295211338, 0.002522073778255918, + 0.0015769783084977752, + ], + space: [ + 0.045976774394786174, 0.04386532305052323, 0.03346748817597193, + 0.008498345884036708, 0.005802390890667938, 0.0017673346473868704, + 0.00468037374691276, 0.0036807899985757367, 0.0034951488381868424, + 0.015073756869093244, 0.006784747891785806, 0.03069702365741547, + 0.004945214461908244, 0.002527030239506901, 0.0012201743197690308, + 0.010191409658936534, 0.0013882500616525532, 0.014559679471816162, + 0.005308140956577744, 0.002067005832569046, 0.006092496689239475, + 0.0029308442356851265, 0.0006407392160713908, 0.01669972147417425, + 0.0018920321527190246, 0.002436089537269062, 0.05542174181989591, + 0.006448761215865303, 0.012804154851567844, 0.014553974971946687, + 0.004927456148063145, 0.006085620881900181, 0.011626122370522652, + 0.002994267915422563, 0.0038291031528493898, 0.006987917175322377, + 0.00719289436611732, 0.0008398926158042337, 0.019068654506361523, + 0.004453895285397824, 0.00401164781243836, 0.0031309255764704544, + 0.013210118660087334, 0.0015542151889036313, 0.0013951089590218057, + 0.002790924761398501, 0.008739250167366135, 0.0027834569638271025, + 0.09198161284531065, 0.0019488047187835441, 0.001739971582806101, + 0.005113637251322287, 0.12140493794373561, 0.005535368890812829, + 0.004198222617607059, 0.0010670629105233682, 0.005298717616708989, + 0.0048291586850982855, 0.005140125537186181, 0.0011663683373124493, + 0.0024499638218810943, 0.012532772497286819, 0.0015564613278042862, + 0.0012252899339204029, 0.0005095187051357676, 0.0035442657060978655, + 0.014030578705118285, 0.0017653534252553718, 0.004026729875153457, + 0.004002067082856801, 0.00809773970333208, 0.017160384509220625, + 0.002981945110677171, 0.0018338446554387704, 0.0031886913904107484, + 0.004654622711785796, 0.0053886727821435415, 0.009023511029300392, + 0.005246967669202147, 0.022806469628558337, 0.0035142224878495355, + 0.006793295047927272, 0.017396620747821886, 0.000922278971300957, + 0.001695889413253992, 0.007015197552957029, 0.003908581792868586, + 0.010136260994789877, 0.0032880552208979508, 0.0039712539426523625, + 0.009672046620728448, 0.007290428293346, 0.0017814796852793386, + 0.0005388988974780036, 0.013936726486762537, 0.003427738251710856, + 0.002206664729558829, 0.05072392472622557, 0.004424158921356747, + 0.0003680061331891622, + ], + biology: [ + 0.054433533850037796, 0.039689474154513994, 0.027661000660240884, + 0.021655563357213067, 0.007862624595639219, 0.006280655377019006, + 0.013407714984668861, 0.004038592819712647, 0.009652765217013826, + 0.0011353987945632667, 0.00925298156804724, 0.004870163054917538, + 0.04911204317171355, 0.006921538451191124, 0.004003624507234068, + 0.016600722822360296, 0.002179735905957767, 0.010801493818182368, + 0.00918922860910538, 0.022115576350545514, 0.0027720850555002148, + 0.003290714340925284, 0.0006359939927595049, 0.020564054347194806, + 0.019590591011010666, 0.0029008397180383077, 0.030414664509122412, + 0.002864704837438281, 0.030933936414333993, 0.00222576969791357, + 0.007077232390623289, 0.005876547862506722, 0.016917705934608753, + 0.016466207380001166, 0.006648808144677746, 0.017876914915160164, + 0.008216930648675583, 0.0026813239798232098, 0.012171904585413245, + 0.012319763594831614, 0.003909608203628946, 0.003205613981613637, + 0.027729523430009183, 0.0019938396819227074, 0.002752482544417343, + 0.0016746657427111145, 0.019564250521109314, 0.027250898086440583, + 0.000954251437229793, 0.0020431321836649734, 0.0014636128217840221, + 0.006821766389705783, 0.003272989792090916, 0.011086677363737012, + 0.0044279892365732595, 0.0029213721398486203, 0.013081117655947345, + 0.012102962176204816, 0.0029165848047082825, 0.002363073972325097, + 0.0028567640089643695, 0.013692951578614878, 0.0013189478722657382, + 0.0030662419379415885, 0.001688218039583749, 0.0007806438728749603, + 0.025458033834110355, 0.009584308792578437, 0.0033243840056188263, + 0.0068361098488461045, 0.005178034666939756, 0.006831575853694424, + 0.010170774789130092, 0.004639315532453418, 0.00655511046953238, + 0.005661100806175219, 0.006238755352678196, 0.023282136482285103, + 0.007790828526461584, 0.011840304456780202, 0.0021953903460442225, + 0.011205225479328193, 0.01665869590158306, 0.0009257333679666402, + 0.0032380769616003604, 0.007379754534437712, 0.01804771060116468, + 0.02540492978451049, 0.0027900782593570507, 0.0029721824342474694, + 0.005666888959879564, 0.003629523931553047, 0.0017838703067849428, + 0.004996486217852931, 0.006086510142627035, 0.0023570031997685236, + 0.002718397814380002, 0.003908858478916721, 0.02080129902865465, + 0.005591305783253238, + ], + }, + topic_word: [ + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.003173633134427233, 0.0, 0.0, + 0.0019409914586816176, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 5.135548639746091e-5, 0.0, 0.0, 0.0, + 0.00015384770766669982, + ], + [ + 0.0, 0.0, 0.0005001441880557176, 0.0, 0.0, 0.0012069823147301646, + 0.02401141538644239, 8.831990149479376e-5, 0.001813504147854849, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0003577161362340021, 0.0005744157863408606, + 0.0, 0.0, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.002662246533243532, 0.0, 0.0, + 0.0008394369973758684, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 4.768637450522633e-5, 0.0, 0.0, 0.0, 0.0, 0.0010421065429755969, + 0.0, 0.0, 2.3210938729937306e-5, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0006034363278588148, + 0.001690622339085902, 0.0, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.004257728522853072, 0.0, 0.0, 0.0, 0.0], + [ + 0.0007238839225620208, 0.0, 0.0, 0.0, 0.0, 0.0009507496006759083, + 0.0012635532859311572, 0.0, 0.0, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.2699264109324263e-5, + 0.00032868342552128994, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0011157667743487598, 0.001278875789622101, + 9.011724853181247e-6, 0.0, 3.22069766200917e-5, 0.004124963644732435, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.00011961487736485771], + [0.0, 0.0, 0.0, 5.734703813314615e-5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.0340264022466226e-5, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.00039701897786057513, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.19635202968946042, 0.0, 0.0008873887898279083, 0.0, + 0.0, 0.0, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 1.552973162326247e-5, 0.0, + 0.002284331845105356, 0.0, 0.0, + ], + [ + 0.0, 0.0, 0.005561738919282601, 0.0, 0.0, 0.0, 0.010700323065082812, + 0.0, 0.0005795117202094265, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0005085828329663487, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.029261090049475084, 0.0020864946050332834, + 0.0018513709831557076, 0.0, 0.0, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0008328286790309667, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0013227647245223537, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0024010554774254685, 5.357245317969706e-5, 0.0, + 0.0, 0.0, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0014484032312145462, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0012081428144960678, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.000616488580813398, 0.0, 0.0, 0.0017954524796671627, 0.0, + 0.0, 0.0, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0006660554263924299, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0011891151421092303, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0024885434472066534, 0.0, + 0.0010165824086743897, 0.0, 0.0, + ], + [ + 0.0, 5.692292246819767e-5, 0.0, 0.0, 0.001006289633741549, 0.0, 0.0, + 0.001897882990870404, 0.0, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.00010646854330751878, 0.0, + 0.0013480243353754932, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0002608785715957589, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0010620422134845085, 0.0, 0.0, + 0.0002032215308376943, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0008928062238389307, 0.0, 0.0, + 5.727265080002417e-5, 0.0, + ], + [ + 0.0, 0.0, 0.06061253593083364, 0.0, 0.02739898181912798, 0.0, 0.0, + 0.0, 0.0, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0014338134220455178, 0.0, + 0.0011276871850520397, 0.002840121913315777, + ], + [0.0008014293374641945, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.000345858724152025, 0.013078498367906305, 0.0, + 0.002815596608197659, 0.0, 0.0, 0.0030778986683343023, 0.0, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0010177321509216356, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.00015333347872060042, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0009655934464519347, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0008542046515290346, 0.0, 0.0, + 0.00016472517230317488, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0007759590139787148, + 0.0037535348789227703, 0.0007205740927611773, + ], + [ + 0.0, 0.0, 0.0010313963595627862, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0069665132800572115, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0006880323929924655, 9.207429290830475e-5, + 0.0, 0.0, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0008404475484102756, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.00016603822882009137, 0.0, 0.0, 0.0, + 0.0004386724451378034, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.003971386830918022, 0.0, 0.0, 0.0, 0.0], + [0.000983926199078037, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.001299108775819868, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.16326515307916822, 0.0, 0.0, 0.0, 0.0, 0.0028677496385613155, + 0.023677620702293598, 0.0, 0.0, 0.0, + ], + [0.0, 0.0, 5.737710913345495e-6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0002081792662367579, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0002840163488982256, + ], + [0.0, 0.0, 0.0, 0.0, 0.0005021534925351664, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.001057424953719077, 0.0, + 0.003578658690485632, 0.0, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.00022950619982206556, + 0.0018791783657735252, 0.0008530683004027156, 4.5513911743540586e-5, + 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0045523319463242765, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0006160628426134845, 0.0, 0.0023393152617350653, + 0.0, 0.0, 0.0012979890699731222, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.003391399407584813, 0.0, 0.0, 0.000719659722017165, 0.0, + 0.004722518573572638, 0.002758841738663124, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.002127862313876461, 0.0, 0.005031998155190167, + 0.0, 0.0, 0.0, + ], + [ + 0.0, 0.0, 0.00055401373160389, 0.0, 0.0, 0.000333325450244618, + 0.0017824446558959168, 0.0011398506826041158, 0.0, + 0.0006366915431430632, + ], + [ + 0.0, 0.21687336139378274, 0.0, 0.0, 0.0, 0.0030345303266644387, 0.0, + 0.0, 0.0, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0012637173523723526, 0.0, + 0.0010158476831041915, 0.0035425832276585615, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0015451984659512325, 0.019909953764629045, + 0.0013484737840911303, 0.0033472098053086113, 0.0016951819626954759, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.00015923419851654453, 0.0, + 0.0024056492047359367, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.01305313280419075, + 0.00014197157780982973, 0.0, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.000746430999979358, 0.0, + 0.0010041202546700189, 0.004557016648181857, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.00021372865758801545, + 0.00025925151316940747, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.001658746582791234, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.00973640859923001, 0.0012404719999980969, + 0.0006365355864806626, 0.0008291013715577852, 0.0, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.001473459191608214, 0.0, 0.0, + 0.0009195459918865811, 0.002012929485852207, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0005850456523130979, 0.0, + 0.00014396718214395852, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0011858302272740567, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0046803403116507545, 0.002083219444498354, 0.0, + 0.0, 0.0, 0.006104495765365948, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.005456944646675863, 0.0, + 0.00011428354610339084, 0.0, 0.0, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0013384597578988894, 0.0, 0.0, 0.0, 0.0], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0018450592044551373, 0.0, + 0.005182965872305058, 0.0, 0.0, + ], + [ + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0003041074021307749, 0.0, + 0.0020827735275448823, 0.0, 0.0008494429669380388, + ], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + ], + vocab_idfs: { + blood: [0, 5.0948820521571045], + earth: [1, 4.2248041634380815], + rocket: [2, 5.666668375712782], + brain: [3, 4.616846251214104], + mars: [4, 6.226284163648205], + nothing: [5, 5.270772718620769], + nada: [6, 4.815297189937943], + star: [7, 6.38880309314598], + zilch: [8, 5.889811927026992], + soil: [9, 7.14257489552236], + }, + }; + + let instance = new NmfTextTagger(model, toksToTfIdfVector); + + let testCases = [ + { + input: "blood is in the brain", + expected: { + environment: 0.00037336337061919943, + space: 0.0003307690554984028, + biology: 0.0026549079818439627, + }, + }, + + { + input: "rocket to the star", + expected: { + environment: 0.0002855180592590448, + space: 0.004006242743506598, + biology: 0.0003094182371360131, + }, + }, + { + input: "rocket to the star mars", + expected: { + environment: 0.0004180326651780644, + space: 0.003844259295376754, + biology: 0.0003135623817729136, + }, + }, + { + input: "rocket rocket rocket", + expected: { + environment: 0.00033052002469507015, + space: 0.007519787053895712, + biology: 0.00031862864995569246, + }, + }, + { + input: "nothing nada rocket", + expected: { + environment: 0.0008597524218029812, + space: 0.0035401031629944506, + biology: 0.000950627767326667, + }, + }, + { + input: "rocket", + expected: { + environment: 0.00033052002469507015, + space: 0.007519787053895712, + biology: 0.00031862864995569246, + }, + }, + { + input: "this sentence is out of vocabulary", + expected: { + environment: 0.0, + space: 0.0, + biology: 0.0, + }, + }, + { + input: "this sentence is out of vocabulary except for rocket", + expected: { + environment: 0.00033052002469507015, + space: 0.007519787053895712, + biology: 0.00031862864995569246, + }, + }, + ]; + + let checkTag = tc => { + let actual = instance.tagTokens(tokenize(tc.input)); + it(`should score ${tc.input} correctly`, () => { + Object.keys(actual).forEach(tag => { + let delta = Math.abs(tc.expected[tag] - actual[tag]); + assert.isTrue(delta <= EPSILON); + }); + }); + }; + + // RELEASE THE TESTS! + for (let tc of testCases) { + checkTag(tc); + } + }); +}); diff --git a/browser/components/newtab/test/unit/lib/PersonalityProvider/PersonalityProvider.test.js b/browser/components/newtab/test/unit/lib/PersonalityProvider/PersonalityProvider.test.js new file mode 100644 index 0000000000..0058fd7c76 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/PersonalityProvider/PersonalityProvider.test.js @@ -0,0 +1,356 @@ +import { GlobalOverrider } from "test/unit/utils"; +import { PersonalityProvider } from "lib/PersonalityProvider/PersonalityProvider.sys.mjs"; + +describe("Personality Provider", () => { + let instance; + let RemoteSettingsStub; + let RemoteSettingsOnStub; + let RemoteSettingsOffStub; + let RemoteSettingsGetStub; + let sandbox; + let globals; + let baseURLStub; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + globals = new GlobalOverrider(); + + RemoteSettingsOnStub = sandbox.stub().returns(); + RemoteSettingsOffStub = sandbox.stub().returns(); + RemoteSettingsGetStub = sandbox.stub().returns([]); + + RemoteSettingsStub = name => ({ + get: RemoteSettingsGetStub, + on: RemoteSettingsOnStub, + off: RemoteSettingsOffStub, + }); + + sinon.spy(global, "BasePromiseWorker"); + sinon.spy(global.BasePromiseWorker.prototype, "post"); + + baseURLStub = "https://baseattachmentsurl"; + global.fetch = async server => ({ + ok: true, + json: async () => { + if (server === "bogus://foo/") { + return { capabilities: { attachments: { base_url: baseURLStub } } }; + } + return {}; + }, + }); + globals.set("RemoteSettings", RemoteSettingsStub); + + instance = new PersonalityProvider(); + instance.interestConfig = { + history_item_builder: "history_item_builder", + history_required_fields: ["a", "b", "c"], + interest_finalizer: "interest_finalizer", + item_to_rank_builder: "item_to_rank_builder", + item_ranker: "item_ranker", + interest_combiner: "interest_combiner", + }; + }); + afterEach(() => { + sinon.restore(); + sandbox.restore(); + globals.restore(); + }); + describe("#personalityProviderWorker", () => { + it("should create a new promise worker on first call", async () => { + const { personalityProviderWorker } = instance; + assert.calledOnce(global.BasePromiseWorker); + assert.isDefined(personalityProviderWorker); + }); + it("should cache _personalityProviderWorker on first call", async () => { + instance._personalityProviderWorker = null; + const { personalityProviderWorker } = instance; + assert.isDefined(instance._personalityProviderWorker); + assert.isDefined(personalityProviderWorker); + }); + it("should use old promise worker on second call", async () => { + let { personalityProviderWorker } = instance; + personalityProviderWorker = instance.personalityProviderWorker; + assert.calledOnce(global.BasePromiseWorker); + assert.isDefined(personalityProviderWorker); + }); + }); + describe("#_getBaseAttachmentsURL", () => { + it("should return a fresh value", async () => { + await instance._getBaseAttachmentsURL(); + assert.equal(instance._baseAttachmentsURL, baseURLStub); + }); + it("should return a cached value", async () => { + const cachedURL = "cached"; + instance._baseAttachmentsURL = cachedURL; + await instance._getBaseAttachmentsURL(); + assert.equal(instance._baseAttachmentsURL, cachedURL); + }); + }); + describe("#setup", () => { + it("should setup two sync attachments", () => { + sinon.spy(instance, "setupSyncAttachment"); + instance.setup(); + assert.calledTwice(instance.setupSyncAttachment); + }); + }); + describe("#teardown", () => { + it("should teardown two sync attachments", () => { + sinon.spy(instance, "teardownSyncAttachment"); + instance.teardown(); + assert.calledTwice(instance.teardownSyncAttachment); + }); + it("should terminate worker", () => { + const terminateStub = sandbox.stub().returns(); + instance._personalityProviderWorker = { + terminate: terminateStub, + }; + instance.teardown(); + assert.calledOnce(terminateStub); + }); + }); + describe("#setupSyncAttachment", () => { + it("should call remote settings on twice for setupSyncAttachment", () => { + assert.calledTwice(RemoteSettingsOnStub); + }); + }); + describe("#teardownSyncAttachment", () => { + it("should call remote settings off for teardownSyncAttachment", () => { + instance.teardownSyncAttachment(); + assert.calledOnce(RemoteSettingsOffStub); + }); + }); + describe("#onSync", () => { + it("should call worker onSync", () => { + instance.onSync(); + assert.calledWith(global.BasePromiseWorker.prototype.post, "onSync"); + }); + }); + describe("#getAttachment", () => { + it("should call worker onSync", () => { + instance.getAttachment(); + assert.calledWith( + global.BasePromiseWorker.prototype.post, + "getAttachment" + ); + }); + }); + describe("#getRecipe", () => { + it("should call worker getRecipe and remote settings get", async () => { + RemoteSettingsGetStub = sandbox.stub().returns([ + { + key: 1, + }, + ]); + sinon.spy(instance, "getAttachment"); + RemoteSettingsStub = name => ({ + get: RemoteSettingsGetStub, + on: RemoteSettingsOnStub, + off: RemoteSettingsOffStub, + }); + globals.set("RemoteSettings", RemoteSettingsStub); + + const result = await instance.getRecipe(); + assert.calledOnce(RemoteSettingsGetStub); + assert.calledOnce(instance.getAttachment); + assert.equal(result.recordKey, 1); + }); + }); + describe("#fetchHistory", () => { + it("should return a history object for fetchHistory", async () => { + const history = await instance.fetchHistory(["requiredColumn"], 1, 1); + assert.equal( + history.sql, + `SELECT url, title, visit_count, frecency, last_visit_date, description\n FROM moz_places\n WHERE last_visit_date >= 1000000\n AND last_visit_date < 1000000 AND IFNULL(requiredColumn, '') <> '' LIMIT 30000` + ); + assert.equal(history.options.columns.length, 1); + assert.equal(Object.keys(history.options.params).length, 0); + }); + }); + describe("#getHistory", () => { + it("should return an empty array", async () => { + instance.interestConfig = { + history_required_fields: [], + }; + const result = await instance.getHistory(); + assert.equal(result.length, 0); + }); + it("should call fetchHistory", async () => { + sinon.spy(instance, "fetchHistory"); + await instance.getHistory(); + }); + }); + describe("#setBaseAttachmentsURL", () => { + it("should call worker setBaseAttachmentsURL", async () => { + await instance.setBaseAttachmentsURL(); + assert.calledWith( + global.BasePromiseWorker.prototype.post, + "setBaseAttachmentsURL" + ); + }); + }); + describe("#setInterestConfig", () => { + it("should call worker setInterestConfig", async () => { + await instance.setInterestConfig(); + assert.calledWith( + global.BasePromiseWorker.prototype.post, + "setInterestConfig" + ); + }); + }); + describe("#setInterestVector", () => { + it("should call worker setInterestVector", async () => { + await instance.setInterestVector(); + assert.calledWith( + global.BasePromiseWorker.prototype.post, + "setInterestVector" + ); + }); + }); + describe("#fetchModels", () => { + it("should call worker fetchModels and remote settings get", async () => { + await instance.fetchModels(); + assert.calledOnce(RemoteSettingsGetStub); + assert.calledWith(global.BasePromiseWorker.prototype.post, "fetchModels"); + }); + }); + describe("#generateTaggers", () => { + it("should call worker generateTaggers", async () => { + await instance.generateTaggers(); + assert.calledWith( + global.BasePromiseWorker.prototype.post, + "generateTaggers" + ); + }); + }); + describe("#generateRecipeExecutor", () => { + it("should call worker generateRecipeExecutor", async () => { + await instance.generateRecipeExecutor(); + assert.calledWith( + global.BasePromiseWorker.prototype.post, + "generateRecipeExecutor" + ); + }); + }); + describe("#createInterestVector", () => { + it("should call worker createInterestVector", async () => { + await instance.createInterestVector(); + assert.calledWith( + global.BasePromiseWorker.prototype.post, + "createInterestVector" + ); + }); + }); + describe("#init", () => { + it("should return early if setInterestConfig fails", async () => { + sandbox.stub(instance, "setBaseAttachmentsURL").returns(); + sandbox.stub(instance, "setInterestConfig").returns(); + instance.interestConfig = null; + const callback = globals.sandbox.stub(); + await instance.init(callback); + assert.notCalled(callback); + }); + it("should return early if fetchModels fails", async () => { + sandbox.stub(instance, "setBaseAttachmentsURL").returns(); + sandbox.stub(instance, "setInterestConfig").returns(); + sandbox.stub(instance, "fetchModels").resolves({ + ok: false, + }); + const callback = globals.sandbox.stub(); + await instance.init(callback); + assert.notCalled(callback); + }); + it("should return early if createInterestVector fails", async () => { + sandbox.stub(instance, "setBaseAttachmentsURL").returns(); + sandbox.stub(instance, "setInterestConfig").returns(); + sandbox.stub(instance, "fetchModels").resolves({ + ok: true, + }); + sandbox.stub(instance, "generateRecipeExecutor").resolves({ + ok: true, + }); + sandbox.stub(instance, "createInterestVector").resolves({ + ok: false, + }); + const callback = globals.sandbox.stub(); + await instance.init(callback); + assert.notCalled(callback); + }); + it("should call callback on successful init", async () => { + sandbox.stub(instance, "setBaseAttachmentsURL").returns(); + sandbox.stub(instance, "setInterestConfig").returns(); + sandbox.stub(instance, "fetchModels").resolves({ + ok: true, + }); + sandbox.stub(instance, "generateRecipeExecutor").resolves({ + ok: true, + }); + sandbox.stub(instance, "createInterestVector").resolves({ + ok: true, + }); + sandbox.stub(instance, "setInterestVector").resolves(); + const callback = globals.sandbox.stub(); + await instance.init(callback); + assert.calledOnce(callback); + assert.isTrue(instance.initialized); + }); + it("should do generic init stuff when calling init with no cache", async () => { + sandbox.stub(instance, "setBaseAttachmentsURL").returns(); + sandbox.stub(instance, "setInterestConfig").returns(); + sandbox.stub(instance, "fetchModels").resolves({ + ok: true, + }); + sandbox.stub(instance, "generateRecipeExecutor").resolves({ + ok: true, + }); + sandbox.stub(instance, "createInterestVector").resolves({ + ok: true, + interestVector: "interestVector", + }); + sandbox.stub(instance, "setInterestVector").resolves(); + await instance.init(); + assert.calledOnce(instance.setBaseAttachmentsURL); + assert.calledOnce(instance.setInterestConfig); + assert.calledOnce(instance.fetchModels); + assert.calledOnce(instance.generateRecipeExecutor); + assert.calledOnce(instance.createInterestVector); + assert.calledOnce(instance.setInterestVector); + }); + }); + describe("#calculateItemRelevanceScore", () => { + it("should return score for uninitialized provider", async () => { + instance.initialized = false; + assert.equal( + await instance.calculateItemRelevanceScore({ item_score: 2 }), + 2 + ); + }); + it("should return score for initialized provider", async () => { + instance.initialized = true; + + instance._personalityProviderWorker = { + post: (postName, [item]) => ({ + rankingVector: { score: item.item_score }, + }), + }; + + assert.equal( + await instance.calculateItemRelevanceScore({ item_score: 2 }), + 2 + ); + }); + it("should post calculateItemRelevanceScore to PersonalityProviderWorker", async () => { + instance.initialized = true; + await instance.calculateItemRelevanceScore({ item_score: 2 }); + assert.calledWith( + global.BasePromiseWorker.prototype.post, + "calculateItemRelevanceScore" + ); + }); + }); + describe("#getScores", () => { + it("should return correct data for getScores", () => { + const scores = instance.getScores(); + assert.isDefined(scores.interestConfig); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/PersonalityProvider/PersonalityProviderWorkerClass.test.js b/browser/components/newtab/test/unit/lib/PersonalityProvider/PersonalityProviderWorkerClass.test.js new file mode 100644 index 0000000000..da6454c6d6 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/PersonalityProvider/PersonalityProviderWorkerClass.test.js @@ -0,0 +1,456 @@ +import { GlobalOverrider } from "test/unit/utils"; +import { PersonalityProviderWorker } from "lib/PersonalityProvider/PersonalityProviderWorkerClass.mjs"; +import { + tokenize, + toksToTfIdfVector, +} from "lib/PersonalityProvider/Tokenize.mjs"; +import { RecipeExecutor } from "lib/PersonalityProvider/RecipeExecutor.mjs"; +import { NmfTextTagger } from "lib/PersonalityProvider/NmfTextTagger.mjs"; +import { NaiveBayesTextTagger } from "lib/PersonalityProvider/NaiveBayesTextTagger.mjs"; + +describe("Personality Provider Worker Class", () => { + let instance; + let globals; + let sandbox; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + globals = new GlobalOverrider(); + globals.set("tokenize", tokenize); + globals.set("toksToTfIdfVector", toksToTfIdfVector); + globals.set("NaiveBayesTextTagger", NaiveBayesTextTagger); + globals.set("NmfTextTagger", NmfTextTagger); + globals.set("RecipeExecutor", RecipeExecutor); + instance = new PersonalityProviderWorker(); + + // mock the RecipeExecutor + instance.recipeExecutor = { + executeRecipe: (item, recipe) => { + if (recipe === "history_item_builder") { + if (item.title === "fail") { + return null; + } + return { + title: item.title, + score: item.frecency, + type: "history_item", + }; + } else if (recipe === "interest_finalizer") { + return { + title: item.title, + score: item.score * 100, + type: "interest_vector", + }; + } else if (recipe === "item_to_rank_builder") { + if (item.title === "fail") { + return null; + } + return { + item_title: item.title, + item_score: item.score, + type: "item_to_rank", + }; + } else if (recipe === "item_ranker") { + if (item.title === "fail" || item.item_title === "fail") { + return null; + } + return { + title: item.title, + score: item.item_score * item.score, + type: "ranked_item", + }; + } + return null; + }, + executeCombinerRecipe: (item1, item2, recipe) => { + if (recipe === "interest_combiner") { + if ( + item1.title === "combiner_fail" || + item2.title === "combiner_fail" + ) { + return null; + } + if (item1.type === undefined) { + item1.type = "combined_iv"; + } + if (item1.score === undefined) { + item1.score = 0; + } + return { type: item1.type, score: item1.score + item2.score }; + } + return null; + }, + }; + + instance.interestConfig = { + history_item_builder: "history_item_builder", + history_required_fields: ["a", "b", "c"], + interest_finalizer: "interest_finalizer", + item_to_rank_builder: "item_to_rank_builder", + item_ranker: "item_ranker", + interest_combiner: "interest_combiner", + }; + }); + afterEach(() => { + sinon.restore(); + sandbox.restore(); + globals.restore(); + }); + describe("#setBaseAttachmentsURL", () => { + it("should set baseAttachmentsURL", () => { + instance.setBaseAttachmentsURL("url"); + assert.equal(instance.baseAttachmentsURL, "url"); + }); + }); + describe("#setInterestConfig", () => { + it("should set interestConfig", () => { + instance.setInterestConfig("config"); + assert.equal(instance.interestConfig, "config"); + }); + }); + describe("#setInterestVector", () => { + it("should set interestVector", () => { + instance.setInterestVector("vector"); + assert.equal(instance.interestVector, "vector"); + }); + }); + describe("#onSync", async () => { + it("should sync remote settings collection from onSync", async () => { + sinon.stub(instance, "deleteAttachment").resolves(); + sinon.stub(instance, "maybeDownloadAttachment").resolves(); + + instance.onSync({ + data: { + created: ["create-1", "create-2"], + updated: [ + { old: "update-old-1", new: "update-new-1" }, + { old: "update-old-2", new: "update-new-2" }, + ], + deleted: ["delete-2", "delete-1"], + }, + }); + + assert(instance.maybeDownloadAttachment.withArgs("create-1").calledOnce); + assert(instance.maybeDownloadAttachment.withArgs("create-2").calledOnce); + assert( + instance.maybeDownloadAttachment.withArgs("update-new-1").calledOnce + ); + assert( + instance.maybeDownloadAttachment.withArgs("update-new-2").calledOnce + ); + + assert(instance.deleteAttachment.withArgs("delete-1").calledOnce); + assert(instance.deleteAttachment.withArgs("delete-2").calledOnce); + assert(instance.deleteAttachment.withArgs("update-old-1").calledOnce); + assert(instance.deleteAttachment.withArgs("update-old-2").calledOnce); + }); + }); + describe("#maybeDownloadAttachment", () => { + it("should attempt _downloadAttachment three times for maybeDownloadAttachment", async () => { + let existsStub; + let statStub; + let attachmentStub; + sinon.stub(instance, "_downloadAttachment").resolves(); + const makeDirStub = globals.sandbox + .stub(global.IOUtils, "makeDirectory") + .resolves(); + + existsStub = globals.sandbox + .stub(global.IOUtils, "exists") + .resolves(true); + + statStub = globals.sandbox + .stub(global.IOUtils, "stat") + .resolves({ size: "1" }); + + attachmentStub = { + attachment: { + filename: "file", + size: "1", + // This hash matches the hash generated from the empty Uint8Array returned by the IOUtils.read stub. + hash: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + }, + }; + + await instance.maybeDownloadAttachment(attachmentStub); + assert.calledWith(makeDirStub, "personality-provider"); + assert.calledOnce(existsStub); + assert.calledOnce(statStub); + assert.notCalled(instance._downloadAttachment); + + existsStub.resetHistory(); + statStub.resetHistory(); + instance._downloadAttachment.resetHistory(); + + attachmentStub = { + attachment: { + filename: "file", + size: "2", + }, + }; + + await instance.maybeDownloadAttachment(attachmentStub); + assert.calledThrice(existsStub); + assert.calledThrice(statStub); + assert.calledThrice(instance._downloadAttachment); + + existsStub.resetHistory(); + statStub.resetHistory(); + instance._downloadAttachment.resetHistory(); + + attachmentStub = { + attachment: { + filename: "file", + size: "1", + // Bogus hash to trigger an update. + hash: "1234", + }, + }; + + await instance.maybeDownloadAttachment(attachmentStub); + assert.calledThrice(existsStub); + assert.calledThrice(statStub); + assert.calledThrice(instance._downloadAttachment); + }); + }); + describe("#_downloadAttachment", () => { + beforeEach(() => { + globals.set("Uint8Array", class Uint8Array {}); + }); + it("should write a file from _downloadAttachment", async () => { + globals.set( + "XMLHttpRequest", + class { + constructor() { + this.status = 200; + this.response = "response!"; + } + open() {} + setRequestHeader() {} + send() {} + } + ); + + const ioutilsWriteStub = globals.sandbox + .stub(global.IOUtils, "write") + .resolves(); + + await instance._downloadAttachment({ + attachment: { location: "location", filename: "filename" }, + }); + + const writeArgs = ioutilsWriteStub.firstCall.args; + assert.equal(writeArgs[0], "filename"); + assert.equal(writeArgs[2].tmpPath, "filename.tmp"); + }); + it("should call console.error from _downloadAttachment if not valid response", async () => { + globals.set( + "XMLHttpRequest", + class { + constructor() { + this.status = 0; + this.response = "response!"; + } + open() {} + setRequestHeader() {} + send() {} + } + ); + + const consoleErrorStub = globals.sandbox + .stub(console, "error") + .resolves(); + + await instance._downloadAttachment({ + attachment: { location: "location", filename: "filename" }, + }); + + assert.calledOnce(consoleErrorStub); + }); + }); + describe("#deleteAttachment", () => { + it("should remove attachments when calling deleteAttachment", async () => { + const makeDirStub = globals.sandbox + .stub(global.IOUtils, "makeDirectory") + .resolves(); + const removeStub = globals.sandbox + .stub(global.IOUtils, "remove") + .resolves(); + await instance.deleteAttachment({ attachment: { filename: "filename" } }); + assert.calledOnce(makeDirStub); + assert.calledTwice(removeStub); + assert.calledWith(removeStub.firstCall, "filename", { + ignoreAbsent: true, + }); + assert.calledWith(removeStub.secondCall, "personality-provider", { + ignoreAbsent: true, + }); + }); + }); + describe("#getAttachment", () => { + it("should return JSON when calling getAttachment", async () => { + sinon.stub(instance, "maybeDownloadAttachment").resolves(); + const readJSONStub = globals.sandbox + .stub(global.IOUtils, "readJSON") + .resolves({}); + const record = { attachment: { filename: "filename" } }; + let returnValue = await instance.getAttachment(record); + + assert.calledOnce(readJSONStub); + assert.calledWith(readJSONStub, "filename"); + assert.calledOnce(instance.maybeDownloadAttachment); + assert.calledWith(instance.maybeDownloadAttachment, record); + assert.deepEqual(returnValue, {}); + + readJSONStub.restore(); + globals.sandbox.stub(global.IOUtils, "readJSON").throws("foo"); + const consoleErrorStub = globals.sandbox + .stub(console, "error") + .resolves(); + returnValue = await instance.getAttachment(record); + + assert.calledOnce(consoleErrorStub); + assert.deepEqual(returnValue, {}); + }); + }); + describe("#fetchModels", () => { + it("should return ok true", async () => { + sinon.stub(instance, "getAttachment").resolves(); + const result = await instance.fetchModels([{ key: 1234 }]); + assert.isTrue(result.ok); + assert.deepEqual(instance.models, [{ recordKey: 1234 }]); + }); + it("should return ok false", async () => { + sinon.stub(instance, "getAttachment").resolves(); + const result = await instance.fetchModels([]); + assert.isTrue(!result.ok); + }); + }); + describe("#generateTaggers", () => { + it("should generate taggers from modelKeys", () => { + const modelKeys = ["nb_model_sports", "nmf_model_sports"]; + + instance.models = [ + { recordKey: "nb_model_sports", model_type: "nb" }, + { + recordKey: "nmf_model_sports", + model_type: "nmf", + parent_tag: "nmf_sports_parent_tag", + }, + ]; + + instance.generateTaggers(modelKeys); + assert.equal(instance.taggers.nbTaggers.length, 1); + assert.equal(Object.keys(instance.taggers.nmfTaggers).length, 1); + }); + it("should skip any models not in modelKeys", () => { + const modelKeys = ["nb_model_sports"]; + + instance.models = [ + { recordKey: "nb_model_sports", model_type: "nb" }, + { + recordKey: "nmf_model_sports", + model_type: "nmf", + parent_tag: "nmf_sports_parent_tag", + }, + ]; + + instance.generateTaggers(modelKeys); + assert.equal(instance.taggers.nbTaggers.length, 1); + assert.equal(Object.keys(instance.taggers.nmfTaggers).length, 0); + }); + it("should skip any models not defined", () => { + const modelKeys = ["nb_model_sports", "nmf_model_sports"]; + + instance.models = [{ recordKey: "nb_model_sports", model_type: "nb" }]; + instance.generateTaggers(modelKeys); + assert.equal(instance.taggers.nbTaggers.length, 1); + assert.equal(Object.keys(instance.taggers.nmfTaggers).length, 0); + }); + }); + describe("#generateRecipeExecutor", () => { + it("should generate a recipeExecutor", () => { + instance.recipeExecutor = null; + instance.taggers = {}; + instance.generateRecipeExecutor(); + assert.isNotNull(instance.recipeExecutor); + }); + }); + describe("#createInterestVector", () => { + let mockHistory = []; + beforeEach(() => { + mockHistory = [ + { + title: "automotive", + description: "something about automotive", + url: "http://example.com/automotive", + frecency: 10, + }, + { + title: "fashion", + description: "something about fashion", + url: "http://example.com/fashion", + frecency: 5, + }, + { + title: "tech", + description: "something about tech", + url: "http://example.com/tech", + frecency: 1, + }, + ]; + }); + it("should gracefully handle history entries that fail", () => { + mockHistory.push({ title: "fail" }); + assert.isNotNull(instance.createInterestVector(mockHistory)); + }); + + it("should fail if the combiner fails", () => { + mockHistory.push({ title: "combiner_fail", frecency: 111 }); + let actual = instance.createInterestVector(mockHistory); + assert.isNull(actual); + }); + + it("should process history, combine, and finalize", () => { + let actual = instance.createInterestVector(mockHistory); + assert.equal(actual.interestVector.score, 1600); + }); + }); + describe("#calculateItemRelevanceScore", () => { + it("should return null for busted item", () => { + assert.equal( + instance.calculateItemRelevanceScore({ title: "fail" }), + null + ); + }); + it("should return null for a busted ranking", () => { + instance.interestVector = { title: "fail", score: 10 }; + assert.equal( + instance.calculateItemRelevanceScore({ title: "some item", score: 6 }), + null + ); + }); + it("should return a score, and not change with interestVector", () => { + instance.interestVector = { score: 10 }; + assert.equal( + instance.calculateItemRelevanceScore({ score: 2 }).rankingVector.score, + 20 + ); + assert.deepEqual(instance.interestVector, { score: 10 }); + }); + it("should use defined personalization_models if available", () => { + instance.interestVector = { score: 10 }; + const item = { + score: 2, + personalization_models: { + entertainment: 1, + }, + }; + assert.equal( + instance.calculateItemRelevanceScore(item).scorableItem.item_tags + .entertainment, + 1 + ); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/PersonalityProvider/RecipeExecutor.test.js b/browser/components/newtab/test/unit/lib/PersonalityProvider/RecipeExecutor.test.js new file mode 100644 index 0000000000..fdbcae9613 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/PersonalityProvider/RecipeExecutor.test.js @@ -0,0 +1,1543 @@ +import { RecipeExecutor } from "lib/PersonalityProvider/RecipeExecutor.mjs"; +import { tokenize } from "lib/PersonalityProvider/Tokenize.mjs"; + +class MockTagger { + constructor(mode, tagScoreMap) { + this.mode = mode; + this.tagScoreMap = tagScoreMap; + } + tagTokens(tokens) { + if (this.mode === "nb") { + // eslint-disable-next-line prefer-destructuring + let tag = Object.keys(this.tagScoreMap)[0]; + // eslint-disable-next-line prefer-destructuring + let prob = this.tagScoreMap[tag]; + let conf = prob >= 0.85; + return { + label: tag, + logProb: Math.log(prob), + confident: conf, + }; + } + return this.tagScoreMap; + } + tag(text) { + return this.tagTokens([text]); + } +} + +describe("RecipeExecutor", () => { + let makeItem = () => { + let x = { + lhs: 2, + one: 1, + two: 2, + three: 3, + foo: "FOO", + bar: "BAR", + baz: ["one", "two", "three"], + qux: 42, + text: "This Is A_sentence.", + url: "http://www.wonder.example.com/dir1/dir2a-dir2b/dir3+4?key1&key2=val2&key3&%26amp=%3D3+4", + url2: "http://wonder.example.com/dir1/dir2a-dir2b/dir3+4?key1&key2=val2&key3&%26amp=%3D3+4", + map: { + c: 3, + a: 1, + b: 2, + }, + map2: { + b: 2, + c: 3, + d: 4, + }, + arr1: [2, 3, 4], + arr2: [3, 4, 5], + long: [3, 4, 5, 6, 7], + tags: { + a: { + aa: 0.1, + ab: 0.2, + ac: 0.3, + }, + b: { + ba: 4, + bb: 5, + bc: 6, + }, + }, + bogus: { + a: { + aa: "0.1", + ab: "0.2", + ac: "0.3", + }, + b: { + ba: "4", + bb: "5", + bc: "6", + }, + }, + zero: { + a: 0, + b: 0, + }, + zaro: [0, 0], + }; + return x; + }; + + let EPSILON = 0.00001; + + let instance = new RecipeExecutor( + [ + new MockTagger("nb", { tag1: 0.7 }), + new MockTagger("nb", { tag2: 0.86 }), + new MockTagger("nb", { tag3: 0.9 }), + new MockTagger("nb", { tag5: 0.9 }), + ], + { + tag1: new MockTagger("nmf", { + tag11: 0.9, + tag12: 0.8, + tag13: 0.7, + }), + tag2: new MockTagger("nmf", { + tag21: 0.8, + tag22: 0.7, + tag23: 0.6, + }), + tag3: new MockTagger("nmf", { + tag31: 0.7, + tag32: 0.6, + tag33: 0.5, + }), + tag4: new MockTagger("nmf", { tag41: 0.99 }), + }, + tokenize + ); + let item = null; + + beforeEach(() => { + item = makeItem(); + }); + + describe("#_assembleText", () => { + it("should simply copy a single string", () => { + assert.equal(instance._assembleText(item, ["foo"]), "FOO"); + }); + it("should append some strings with a space", () => { + assert.equal(instance._assembleText(item, ["foo", "bar"]), "FOO BAR"); + }); + it("should give an empty string for a missing field", () => { + assert.equal(instance._assembleText(item, ["missing"]), ""); + }); + it("should not double space an interior missing field", () => { + assert.equal( + instance._assembleText(item, ["foo", "missing", "bar"]), + "FOO BAR" + ); + }); + it("should splice in an array of strings", () => { + assert.equal( + instance._assembleText(item, ["foo", "baz", "bar"]), + "FOO one two three BAR" + ); + }); + it("should handle numbers", () => { + assert.equal( + instance._assembleText(item, ["foo", "qux", "bar"]), + "FOO 42 BAR" + ); + }); + }); + + describe("#naiveBayesTag", () => { + it("should understand NaiveBayesTextTagger", () => { + item = instance.naiveBayesTag(item, { fields: ["text"] }); + assert.isTrue("nb_tags" in item); + assert.isTrue(!("tag1" in item.nb_tags)); + assert.equal(item.nb_tags.tag2, 0.86); + assert.equal(item.nb_tags.tag3, 0.9); + assert.equal(item.nb_tags.tag5, 0.9); + assert.isTrue("nb_tokens" in item); + assert.deepEqual(item.nb_tokens, ["this", "is", "a", "sentence"]); + assert.isTrue("nb_tags_extended" in item); + assert.isTrue(!("tag1" in item.nb_tags_extended)); + assert.deepEqual(item.nb_tags_extended.tag2, { + label: "tag2", + logProb: Math.log(0.86), + confident: true, + }); + assert.deepEqual(item.nb_tags_extended.tag3, { + label: "tag3", + logProb: Math.log(0.9), + confident: true, + }); + assert.deepEqual(item.nb_tags_extended.tag5, { + label: "tag5", + logProb: Math.log(0.9), + confident: true, + }); + assert.isTrue("nb_tokens" in item); + assert.deepEqual(item.nb_tokens, ["this", "is", "a", "sentence"]); + }); + }); + + describe("#conditionallyNmfTag", () => { + it("should do nothing if it's not nb tagged", () => { + item = instance.conditionallyNmfTag(item, {}); + assert.equal(item, null); + }); + it("should populate nmf tags for the nb tags", () => { + item = instance.naiveBayesTag(item, { fields: ["text"] }); + item = instance.conditionallyNmfTag(item, {}); + assert.isTrue("nb_tags" in item); + assert.deepEqual(item.nmf_tags, { + tag2: { + tag21: 0.8, + tag22: 0.7, + tag23: 0.6, + }, + tag3: { + tag31: 0.7, + tag32: 0.6, + tag33: 0.5, + }, + }); + assert.deepEqual(item.nmf_tags_parent, { + tag21: "tag2", + tag22: "tag2", + tag23: "tag2", + tag31: "tag3", + tag32: "tag3", + tag33: "tag3", + }); + }); + it("should not populate nmf tags for things that were not nb tagged", () => { + item = instance.naiveBayesTag(item, { fields: ["text"] }); + item = instance.conditionallyNmfTag(item, {}); + assert.isTrue("nmf_tags" in item); + assert.isTrue(!("tag4" in item.nmf_tags)); + assert.isTrue("nmf_tags_parent" in item); + assert.isTrue(!("tag4" in item.nmf_tags_parent)); + }); + }); + + describe("#acceptItemByFieldValue", () => { + it("should implement ==", () => { + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "==", + rhsValue: 2, + }) !== null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "==", + rhsValue: 3, + }) === null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "==", + rhsField: "two", + }) !== null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "==", + rhsField: "three", + }) === null + ); + }); + it("should implement !=", () => { + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "!=", + rhsValue: 2, + }) === null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "!=", + rhsValue: 3, + }) !== null + ); + }); + it("should implement < ", () => { + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "<", + rhsValue: 1, + }) === null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "<", + rhsValue: 2, + }) === null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "<", + rhsValue: 3, + }) !== null + ); + }); + it("should implement <= ", () => { + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "<=", + rhsValue: 1, + }) === null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "<=", + rhsValue: 2, + }) !== null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "<=", + rhsValue: 3, + }) !== null + ); + }); + it("should implement > ", () => { + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: ">", + rhsValue: 1, + }) !== null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: ">", + rhsValue: 2, + }) === null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: ">", + rhsValue: 3, + }) === null + ); + }); + it("should implement >= ", () => { + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: ">=", + rhsValue: 1, + }) !== null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: ">=", + rhsValue: 2, + }) !== null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: ">=", + rhsValue: 3, + }) === null + ); + }); + it("should skip items with missing fields", () => { + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "no-left", + op: "==", + rhsValue: 1, + }) === null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "==", + rhsField: "no-right", + }) === null + ); + assert.isTrue( + instance.acceptItemByFieldValue(item, { field: "lhs", op: "==" }) === + null + ); + }); + it("should skip items with bogus operators", () => { + assert.isTrue( + instance.acceptItemByFieldValue(item, { + field: "lhs", + op: "bogus", + rhsField: "two", + }) === null + ); + }); + }); + + describe("#tokenizeUrl", () => { + it("should strip the leading www from a url", () => { + item = instance.tokenizeUrl(item, { field: "url", dest: "url_toks" }); + assert.deepEqual( + [ + "wonder", + "example", + "com", + "dir1", + "dir2a", + "dir2b", + "dir3", + "4", + "key1", + "key2", + "val2", + "key3", + "amp", + "3", + "4", + ], + item.url_toks + ); + }); + it("should tokenize the not strip the leading non-wwww token from a url", () => { + item = instance.tokenizeUrl(item, { field: "url2", dest: "url_toks" }); + assert.deepEqual( + [ + "wonder", + "example", + "com", + "dir1", + "dir2a", + "dir2b", + "dir3", + "4", + "key1", + "key2", + "val2", + "key3", + "amp", + "3", + "4", + ], + item.url_toks + ); + }); + it("should error for a missing url", () => { + item = instance.tokenizeUrl(item, { field: "missing", dest: "url_toks" }); + assert.equal(item, null); + }); + }); + + describe("#getUrlDomain", () => { + it("should get only the hostname skipping the www", () => { + item = instance.getUrlDomain(item, { field: "url", dest: "url_domain" }); + assert.isTrue("url_domain" in item); + assert.deepEqual("wonder.example.com", item.url_domain); + }); + it("should get only the hostname", () => { + item = instance.getUrlDomain(item, { field: "url2", dest: "url_domain" }); + assert.isTrue("url_domain" in item); + assert.deepEqual("wonder.example.com", item.url_domain); + }); + it("should get the hostname and 2 levels of directories", () => { + item = instance.getUrlDomain(item, { + field: "url", + path_length: 2, + dest: "url_plus_2", + }); + assert.isTrue("url_plus_2" in item); + assert.deepEqual("wonder.example.com/dir1/dir2a-dir2b", item.url_plus_2); + }); + it("should error for a missing url", () => { + item = instance.getUrlDomain(item, { + field: "missing", + dest: "url_domain", + }); + assert.equal(item, null); + }); + }); + + describe("#tokenizeField", () => { + it("should tokenize the field", () => { + item = instance.tokenizeField(item, { field: "text", dest: "toks" }); + assert.isTrue("toks" in item); + assert.deepEqual(["this", "is", "a", "sentence"], item.toks); + }); + it("should error for a missing field", () => { + item = instance.tokenizeField(item, { field: "missing", dest: "toks" }); + assert.equal(item, null); + }); + it("should error for a broken config", () => { + item = instance.tokenizeField(item, {}); + assert.equal(item, null); + }); + }); + + describe("#_typeOf", () => { + it("should know this is a map", () => { + assert.equal(instance._typeOf({}), "map"); + }); + it("should know this is an array", () => { + assert.equal(instance._typeOf([]), "array"); + }); + it("should know this is a string", () => { + assert.equal(instance._typeOf("blah"), "string"); + }); + it("should know this is a boolean", () => { + assert.equal(instance._typeOf(true), "boolean"); + }); + + it("should know this is a null", () => { + assert.equal(instance._typeOf(null), "null"); + }); + }); + + describe("#_lookupScalar", () => { + it("should return the constant", () => { + assert.equal(instance._lookupScalar({}, 1, 0), 1); + }); + it("should return the default", () => { + assert.equal(instance._lookupScalar({}, "blah", 42), 42); + }); + it("should return the field's value", () => { + assert.equal(instance._lookupScalar({ blah: 11 }, "blah", 42), 11); + }); + }); + + describe("#copyValue", () => { + it("should copy values", () => { + item = instance.copyValue(item, { src: "one", dest: "again" }); + assert.isTrue("again" in item); + assert.equal(item.again, 1); + item.one = 100; + assert.equal(item.one, 100); + assert.equal(item.again, 1); + }); + it("should handle maps corrects", () => { + item = instance.copyValue(item, { src: "map", dest: "again" }); + assert.deepEqual(item.again, { a: 1, b: 2, c: 3 }); + item.map.c = 100; + assert.deepEqual(item.again, { a: 1, b: 2, c: 3 }); + item.map = 342; + assert.deepEqual(item.again, { a: 1, b: 2, c: 3 }); + }); + it("should error for a missing field", () => { + item = instance.copyValue(item, { src: "missing", dest: "toks" }); + assert.equal(item, null); + }); + }); + + describe("#keepTopK", () => { + it("should keep the 2 smallest", () => { + item = instance.keepTopK(item, { field: "map", k: 2, descending: false }); + assert.equal(Object.keys(item.map).length, 2); + assert.isTrue("a" in item.map); + assert.equal(item.map.a, 1); + assert.isTrue("b" in item.map); + assert.equal(item.map.b, 2); + assert.isTrue(!("c" in item.map)); + }); + it("should keep the 2 largest", () => { + item = instance.keepTopK(item, { field: "map", k: 2, descending: true }); + assert.equal(Object.keys(item.map).length, 2); + assert.isTrue(!("a" in item.map)); + assert.isTrue("b" in item.map); + assert.equal(item.map.b, 2); + assert.isTrue("c" in item.map); + assert.equal(item.map.c, 3); + }); + it("should still keep the 2 largest", () => { + item = instance.keepTopK(item, { field: "map", k: 2 }); + assert.equal(Object.keys(item.map).length, 2); + assert.isTrue(!("a" in item.map)); + assert.isTrue("b" in item.map); + assert.equal(item.map.b, 2); + assert.isTrue("c" in item.map); + assert.equal(item.map.c, 3); + }); + it("should promote up nested fields", () => { + item = instance.keepTopK(item, { field: "tags", k: 2 }); + assert.equal(Object.keys(item.tags).length, 2); + assert.deepEqual(item.tags, { bb: 5, bc: 6 }); + }); + it("should error for a missing field", () => { + item = instance.keepTopK(item, { field: "missing", k: 3 }); + assert.equal(item, null); + }); + }); + + describe("#scalarMultiply", () => { + it("should use constants", () => { + item = instance.scalarMultiply(item, { field: "map", k: 2 }); + assert.equal(item.map.a, 2); + assert.equal(item.map.b, 4); + assert.equal(item.map.c, 6); + }); + it("should use fields", () => { + item = instance.scalarMultiply(item, { field: "map", k: "three" }); + assert.equal(item.map.a, 3); + assert.equal(item.map.b, 6); + assert.equal(item.map.c, 9); + }); + it("should use default", () => { + item = instance.scalarMultiply(item, { + field: "map", + k: "missing", + dfault: 4, + }); + assert.equal(item.map.a, 4); + assert.equal(item.map.b, 8); + assert.equal(item.map.c, 12); + }); + it("should error for a missing field", () => { + item = instance.scalarMultiply(item, { field: "missing", k: 3 }); + assert.equal(item, null); + }); + it("should multiply numbers", () => { + item = instance.scalarMultiply(item, { field: "lhs", k: 2 }); + assert.equal(item.lhs, 4); + }); + it("should multiply arrays", () => { + item = instance.scalarMultiply(item, { field: "arr1", k: 2 }); + assert.deepEqual(item.arr1, [4, 6, 8]); + }); + it("should should error on strings", () => { + item = instance.scalarMultiply(item, { field: "foo", k: 2 }); + assert.equal(item, null); + }); + }); + + describe("#elementwiseMultiply", () => { + it("should handle maps", () => { + item = instance.elementwiseMultiply(item, { + left: "tags", + right: "map2", + }); + assert.deepEqual(item.tags, { + a: { aa: 0, ab: 0, ac: 0 }, + b: { ba: 8, bb: 10, bc: 12 }, + }); + }); + it("should handle arrays of same length", () => { + item = instance.elementwiseMultiply(item, { + left: "arr1", + right: "arr2", + }); + assert.deepEqual(item.arr1, [6, 12, 20]); + }); + it("should error for arrays of different lengths", () => { + item = instance.elementwiseMultiply(item, { + left: "arr1", + right: "long", + }); + assert.equal(item, null); + }); + it("should error for a missing left", () => { + item = instance.elementwiseMultiply(item, { + left: "missing", + right: "arr2", + }); + assert.equal(item, null); + }); + it("should error for a missing right", () => { + item = instance.elementwiseMultiply(item, { + left: "arr1", + right: "missing", + }); + assert.equal(item, null); + }); + it("should handle numbers", () => { + item = instance.elementwiseMultiply(item, { + left: "three", + right: "two", + }); + assert.equal(item.three, 6); + }); + it("should error for mismatched types", () => { + item = instance.elementwiseMultiply(item, { left: "arr1", right: "two" }); + assert.equal(item, null); + }); + it("should error for strings", () => { + item = instance.elementwiseMultiply(item, { left: "foo", right: "bar" }); + assert.equal(item, null); + }); + }); + + describe("#vectorMultiply", () => { + it("should calculate dot products from maps", () => { + item = instance.vectorMultiply(item, { + left: "map", + right: "map2", + dest: "dot", + }); + assert.equal(item.dot, 13); + }); + it("should calculate dot products from arrays", () => { + item = instance.vectorMultiply(item, { + left: "arr1", + right: "arr2", + dest: "dot", + }); + assert.equal(item.dot, 38); + }); + it("should error for arrays of different lengths", () => { + item = instance.vectorMultiply(item, { left: "arr1", right: "long" }); + assert.equal(item, null); + }); + it("should error for a missing left", () => { + item = instance.vectorMultiply(item, { left: "missing", right: "arr2" }); + assert.equal(item, null); + }); + it("should error for a missing right", () => { + item = instance.vectorMultiply(item, { left: "arr1", right: "missing" }); + assert.equal(item, null); + }); + it("should error for mismatched types", () => { + item = instance.vectorMultiply(item, { left: "arr1", right: "two" }); + assert.equal(item, null); + }); + it("should error for strings", () => { + item = instance.vectorMultiply(item, { left: "foo", right: "bar" }); + assert.equal(item, null); + }); + }); + + describe("#scalarAdd", () => { + it("should error for a missing field", () => { + item = instance.scalarAdd(item, { field: "missing", k: 10 }); + assert.equal(item, null); + }); + it("should error for strings", () => { + item = instance.scalarAdd(item, { field: "foo", k: 10 }); + assert.equal(item, null); + }); + it("should work for numbers", () => { + item = instance.scalarAdd(item, { field: "one", k: 10 }); + assert.equal(item.one, 11); + }); + it("should add a constant to every cell on a map", () => { + item = instance.scalarAdd(item, { field: "map", k: 10 }); + assert.deepEqual(item.map, { a: 11, b: 12, c: 13 }); + }); + it("should add a value from a field to every cell on a map", () => { + item = instance.scalarAdd(item, { field: "map", k: "qux" }); + assert.deepEqual(item.map, { a: 43, b: 44, c: 45 }); + }); + it("should add a constant to every cell on an array", () => { + item = instance.scalarAdd(item, { field: "arr1", k: 10 }); + assert.deepEqual(item.arr1, [12, 13, 14]); + }); + }); + + describe("#vectorAdd", () => { + it("should calculate add vectors from maps", () => { + item = instance.vectorAdd(item, { left: "map", right: "map2" }); + assert.equal(Object.keys(item.map).length, 4); + assert.isTrue("a" in item.map); + assert.equal(item.map.a, 1); + assert.isTrue("b" in item.map); + assert.equal(item.map.b, 4); + assert.isTrue("c" in item.map); + assert.equal(item.map.c, 6); + assert.isTrue("d" in item.map); + assert.equal(item.map.d, 4); + }); + it("should work for missing left", () => { + item = instance.vectorAdd(item, { left: "missing", right: "arr2" }); + assert.deepEqual(item.missing, [3, 4, 5]); + }); + it("should error for missing right", () => { + item = instance.vectorAdd(item, { left: "arr2", right: "missing" }); + assert.equal(item, null); + }); + it("should error error for strings", () => { + item = instance.vectorAdd(item, { left: "foo", right: "bar" }); + assert.equal(item, null); + }); + it("should error for different types", () => { + item = instance.vectorAdd(item, { left: "arr2", right: "map" }); + assert.equal(item, null); + }); + it("should calculate add vectors from arrays", () => { + item = instance.vectorAdd(item, { left: "arr1", right: "arr2" }); + assert.deepEqual(item.arr1, [5, 7, 9]); + }); + it("should abort on different sized arrays", () => { + item = instance.vectorAdd(item, { left: "arr1", right: "long" }); + assert.equal(item, null); + }); + it("should calculate add vectors from arrays", () => { + item = instance.vectorAdd(item, { left: "arr1", right: "arr2" }); + assert.deepEqual(item.arr1, [5, 7, 9]); + }); + }); + + describe("#makeBoolean", () => { + it("should error for missing field", () => { + item = instance.makeBoolean(item, { field: "missing", threshold: 2 }); + assert.equal(item, null); + }); + it("should 0/1 a map", () => { + item = instance.makeBoolean(item, { field: "map", threshold: 2 }); + assert.deepEqual(item.map, { a: 0, b: 0, c: 1 }); + }); + it("should a map of all 1s", () => { + item = instance.makeBoolean(item, { field: "map" }); + assert.deepEqual(item.map, { a: 1, b: 1, c: 1 }); + }); + it("should -1/1 a map", () => { + item = instance.makeBoolean(item, { + field: "map", + threshold: 2, + keep_negative: true, + }); + assert.deepEqual(item.map, { a: -1, b: -1, c: 1 }); + }); + it("should work an array", () => { + item = instance.makeBoolean(item, { field: "arr1", threshold: 3 }); + assert.deepEqual(item.arr1, [0, 0, 1]); + }); + it("should -1/1 an array", () => { + item = instance.makeBoolean(item, { + field: "arr1", + threshold: 3, + keep_negative: true, + }); + assert.deepEqual(item.arr1, [-1, -1, 1]); + }); + it("should 1 a high number", () => { + item = instance.makeBoolean(item, { field: "qux", threshold: 3 }); + assert.equal(item.qux, 1); + }); + it("should 0 a low number", () => { + item = instance.makeBoolean(item, { field: "qux", threshold: 70 }); + assert.equal(item.qux, 0); + }); + it("should -1 a low number", () => { + item = instance.makeBoolean(item, { + field: "qux", + threshold: 83, + keep_negative: true, + }); + assert.equal(item.qux, -1); + }); + it("should fail a string", () => { + item = instance.makeBoolean(item, { field: "foo", threshold: 3 }); + assert.equal(item, null); + }); + }); + + describe("#allowFields", () => { + it("should filter the keys out of a map", () => { + item = instance.allowFields(item, { + fields: ["foo", "missing", "bar"], + }); + assert.deepEqual(item, { foo: "FOO", bar: "BAR" }); + }); + }); + + describe("#filterByValue", () => { + it("should fail on missing field", () => { + item = instance.filterByValue(item, { field: "missing", threshold: 2 }); + assert.equal(item, null); + }); + it("should filter the keys out of a map", () => { + item = instance.filterByValue(item, { field: "map", threshold: 2 }); + assert.deepEqual(item.map, { c: 3 }); + }); + }); + + describe("#l2Normalize", () => { + it("should fail on missing field", () => { + item = instance.l2Normalize(item, { field: "missing" }); + assert.equal(item, null); + }); + it("should L2 normalize an array", () => { + item = instance.l2Normalize(item, { field: "arr1" }); + assert.deepEqual( + item.arr1, + [0.3713906763541037, 0.5570860145311556, 0.7427813527082074] + ); + }); + it("should L2 normalize a map", () => { + item = instance.l2Normalize(item, { field: "map" }); + assert.deepEqual(item.map, { + a: 0.2672612419124244, + b: 0.5345224838248488, + c: 0.8017837257372732, + }); + }); + it("should fail a string", () => { + item = instance.l2Normalize(item, { field: "foo" }); + assert.equal(item, null); + }); + it("should not bomb on a zero vector", () => { + item = instance.l2Normalize(item, { field: "zero" }); + assert.deepEqual(item.zero, { a: 0, b: 0 }); + item = instance.l2Normalize(item, { field: "zaro" }); + assert.deepEqual(item.zaro, [0, 0]); + }); + }); + + describe("#probNormalize", () => { + it("should fail on missing field", () => { + item = instance.probNormalize(item, { field: "missing" }); + assert.equal(item, null); + }); + it("should normalize an array to sum to 1", () => { + item = instance.probNormalize(item, { field: "arr1" }); + assert.deepEqual( + item.arr1, + [0.2222222222222222, 0.3333333333333333, 0.4444444444444444] + ); + }); + it("should normalize a map to sum to 1", () => { + item = instance.probNormalize(item, { field: "map" }); + assert.equal(Object.keys(item.map).length, 3); + assert.isTrue("a" in item.map); + assert.isTrue(Math.abs(item.map.a - 0.16667) <= EPSILON); + assert.isTrue("b" in item.map); + assert.isTrue(Math.abs(item.map.b - 0.33333) <= EPSILON); + assert.isTrue("c" in item.map); + assert.isTrue(Math.abs(item.map.c - 0.5) <= EPSILON); + }); + it("should fail a string", () => { + item = instance.probNormalize(item, { field: "foo" }); + assert.equal(item, null); + }); + it("should not bomb on a zero vector", () => { + item = instance.probNormalize(item, { field: "zero" }); + assert.deepEqual(item.zero, { a: 0, b: 0 }); + item = instance.probNormalize(item, { field: "zaro" }); + assert.deepEqual(item.zaro, [0, 0]); + }); + }); + + describe("#scalarMultiplyTag", () => { + it("should fail on missing field", () => { + item = instance.scalarMultiplyTag(item, { field: "missing", k: 3 }); + assert.equal(item, null); + }); + it("should scalar multiply a nested map", () => { + item = instance.scalarMultiplyTag(item, { + field: "tags", + k: 3, + log_scale: false, + }); + assert.isTrue(Math.abs(item.tags.a.aa - 0.3) <= EPSILON); + assert.isTrue(Math.abs(item.tags.a.ab - 0.6) <= EPSILON); + assert.isTrue(Math.abs(item.tags.a.ac - 0.9) <= EPSILON); + assert.isTrue(Math.abs(item.tags.b.ba - 12) <= EPSILON); + assert.isTrue(Math.abs(item.tags.b.bb - 15) <= EPSILON); + assert.isTrue(Math.abs(item.tags.b.bc - 18) <= EPSILON); + }); + it("should scalar multiply a nested map with logrithms", () => { + item = instance.scalarMultiplyTag(item, { + field: "tags", + k: 3, + log_scale: true, + }); + assert.isTrue( + Math.abs(item.tags.a.aa - Math.log(0.1 + 0.000001) * 3) <= EPSILON + ); + assert.isTrue( + Math.abs(item.tags.a.ab - Math.log(0.2 + 0.000001) * 3) <= EPSILON + ); + assert.isTrue( + Math.abs(item.tags.a.ac - Math.log(0.3 + 0.000001) * 3) <= EPSILON + ); + assert.isTrue( + Math.abs(item.tags.b.ba - Math.log(4.0 + 0.000001) * 3) <= EPSILON + ); + assert.isTrue( + Math.abs(item.tags.b.bb - Math.log(5.0 + 0.000001) * 3) <= EPSILON + ); + assert.isTrue( + Math.abs(item.tags.b.bc - Math.log(6.0 + 0.000001) * 3) <= EPSILON + ); + }); + it("should fail a string", () => { + item = instance.scalarMultiplyTag(item, { field: "foo", k: 3 }); + assert.equal(item, null); + }); + }); + + describe("#setDefault", () => { + it("should store a missing value", () => { + item = instance.setDefault(item, { field: "missing", value: 1111 }); + assert.equal(item.missing, 1111); + }); + it("should not overwrite an existing value", () => { + item = instance.setDefault(item, { field: "lhs", value: 1111 }); + assert.equal(item.lhs, 2); + }); + it("should store a complex value", () => { + item = instance.setDefault(item, { field: "missing", value: { a: 1 } }); + assert.deepEqual(item.missing, { a: 1 }); + }); + }); + + describe("#lookupValue", () => { + it("should promote a value", () => { + item = instance.lookupValue(item, { + haystack: "map", + needle: "c", + dest: "ccc", + }); + assert.equal(item.ccc, 3); + }); + it("should handle a missing haystack", () => { + item = instance.lookupValue(item, { + haystack: "missing", + needle: "c", + dest: "ccc", + }); + assert.isTrue(!("ccc" in item)); + }); + it("should handle a missing needle", () => { + item = instance.lookupValue(item, { + haystack: "map", + needle: "missing", + dest: "ccc", + }); + assert.isTrue(!("ccc" in item)); + }); + }); + + describe("#copyToMap", () => { + it("should copy a value to a map", () => { + item = instance.copyToMap(item, { + src: "qux", + dest_map: "map", + dest_key: "zzz", + }); + assert.isTrue("zzz" in item.map); + assert.equal(item.map.zzz, item.qux); + }); + it("should create a new map to hold the key", () => { + item = instance.copyToMap(item, { + src: "qux", + dest_map: "missing", + dest_key: "zzz", + }); + assert.equal(Object.keys(item.missing).length, 1); + assert.equal(item.missing.zzz, item.qux); + }); + it("should not create an empty map if the src is missing", () => { + item = instance.copyToMap(item, { + src: "missing", + dest_map: "no_map", + dest_key: "zzz", + }); + assert.isTrue(!("no_map" in item)); + }); + }); + + describe("#applySoftmaxTags", () => { + it("should error on missing field", () => { + item = instance.applySoftmaxTags(item, { field: "missing" }); + assert.equal(item, null); + }); + it("should error on nonmaps", () => { + item = instance.applySoftmaxTags(item, { field: "arr1" }); + assert.equal(item, null); + }); + it("should error on unnested maps", () => { + item = instance.applySoftmaxTags(item, { field: "map" }); + assert.equal(item, null); + }); + it("should error on wrong nested maps", () => { + item = instance.applySoftmaxTags(item, { field: "bogus" }); + assert.equal(item, null); + }); + it("should apply softmax across the subtags", () => { + item = instance.applySoftmaxTags(item, { field: "tags" }); + assert.isTrue("a" in item.tags); + assert.isTrue("aa" in item.tags.a); + assert.isTrue("ab" in item.tags.a); + assert.isTrue("ac" in item.tags.a); + assert.isTrue(Math.abs(item.tags.a.aa - 0.30061) <= EPSILON); + assert.isTrue(Math.abs(item.tags.a.ab - 0.33222) <= EPSILON); + assert.isTrue(Math.abs(item.tags.a.ac - 0.36717) <= EPSILON); + + assert.isTrue("b" in item.tags); + assert.isTrue("ba" in item.tags.b); + assert.isTrue("bb" in item.tags.b); + assert.isTrue("bc" in item.tags.b); + assert.isTrue(Math.abs(item.tags.b.ba - 0.09003) <= EPSILON); + assert.isTrue(Math.abs(item.tags.b.bb - 0.24473) <= EPSILON); + assert.isTrue(Math.abs(item.tags.b.bc - 0.66524) <= EPSILON); + }); + }); + + describe("#combinerAdd", () => { + it("should do nothing when right field is missing", () => { + let right = makeItem(); + let combined = instance.combinerAdd(item, right, { field: "missing" }); + assert.deepEqual(combined, item); + }); + it("should handle missing left maps", () => { + let right = makeItem(); + right.missingmap = { a: 5, b: -1, c: 3 }; + let combined = instance.combinerAdd(item, right, { field: "missingmap" }); + assert.deepEqual(combined.missingmap, { a: 5, b: -1, c: 3 }); + }); + it("should add equal sized maps", () => { + let right = makeItem(); + let combined = instance.combinerAdd(item, right, { field: "map" }); + assert.deepEqual(combined.map, { a: 2, b: 4, c: 6 }); + }); + it("should add long map to short map", () => { + let right = makeItem(); + right.map.d = 999; + let combined = instance.combinerAdd(item, right, { field: "map" }); + assert.deepEqual(combined.map, { a: 2, b: 4, c: 6, d: 999 }); + }); + it("should add short map to long map", () => { + let right = makeItem(); + item.map.d = 999; + let combined = instance.combinerAdd(item, right, { field: "map" }); + assert.deepEqual(combined.map, { a: 2, b: 4, c: 6, d: 999 }); + }); + it("should add equal sized arrays", () => { + let right = makeItem(); + let combined = instance.combinerAdd(item, right, { field: "arr1" }); + assert.deepEqual(combined.arr1, [4, 6, 8]); + }); + it("should handle missing left arrays", () => { + let right = makeItem(); + right.missingarray = [5, 1, 4]; + let combined = instance.combinerAdd(item, right, { + field: "missingarray", + }); + assert.deepEqual(combined.missingarray, [5, 1, 4]); + }); + it("should add long array to short array", () => { + let right = makeItem(); + right.arr1 = [2, 3, 4, 12]; + let combined = instance.combinerAdd(item, right, { field: "arr1" }); + assert.deepEqual(combined.arr1, [4, 6, 8, 12]); + }); + it("should add short array to long array", () => { + let right = makeItem(); + item.arr1 = [2, 3, 4, 12]; + let combined = instance.combinerAdd(item, right, { field: "arr1" }); + assert.deepEqual(combined.arr1, [4, 6, 8, 12]); + }); + it("should handle missing left number", () => { + let right = makeItem(); + right.missingnumber = 999; + let combined = instance.combinerAdd(item, right, { + field: "missingnumber", + }); + assert.deepEqual(combined.missingnumber, 999); + }); + it("should add numbers", () => { + let right = makeItem(); + let combined = instance.combinerAdd(item, right, { field: "lhs" }); + assert.equal(combined.lhs, 4); + }); + it("should error on missing left, and right is a string", () => { + let right = makeItem(); + right.error = "error"; + let combined = instance.combinerAdd(item, right, { field: "error" }); + assert.equal(combined, null); + }); + it("should error on left string", () => { + let right = makeItem(); + let combined = instance.combinerAdd(item, right, { field: "foo" }); + assert.equal(combined, null); + }); + it("should error on mismatch types", () => { + let right = makeItem(); + right.lhs = [1, 2, 3]; + let combined = instance.combinerAdd(item, right, { field: "lhs" }); + assert.equal(combined, null); + }); + }); + + describe("#combinerMax", () => { + it("should do nothing when right field is missing", () => { + let right = makeItem(); + let combined = instance.combinerMax(item, right, { field: "missing" }); + assert.deepEqual(combined, item); + }); + it("should handle missing left maps", () => { + let right = makeItem(); + right.missingmap = { a: 5, b: -1, c: 3 }; + let combined = instance.combinerMax(item, right, { field: "missingmap" }); + assert.deepEqual(combined.missingmap, { a: 5, b: -1, c: 3 }); + }); + it("should handle equal sized maps", () => { + let right = makeItem(); + right.map = { a: 5, b: -1, c: 3 }; + let combined = instance.combinerMax(item, right, { field: "map" }); + assert.deepEqual(combined.map, { a: 5, b: 2, c: 3 }); + }); + it("should handle short map to long map", () => { + let right = makeItem(); + right.map = { a: 5, b: -1, c: 3, d: 999 }; + let combined = instance.combinerMax(item, right, { field: "map" }); + assert.deepEqual(combined.map, { a: 5, b: 2, c: 3, d: 999 }); + }); + it("should handle long map to short map", () => { + let right = makeItem(); + right.map = { a: 5, b: -1, c: 3 }; + item.map.d = 999; + let combined = instance.combinerMax(item, right, { field: "map" }); + assert.deepEqual(combined.map, { a: 5, b: 2, c: 3, d: 999 }); + }); + it("should handle equal sized arrays", () => { + let right = makeItem(); + right.arr1 = [5, 1, 4]; + let combined = instance.combinerMax(item, right, { field: "arr1" }); + assert.deepEqual(combined.arr1, [5, 3, 4]); + }); + it("should handle missing left arrays", () => { + let right = makeItem(); + right.missingarray = [5, 1, 4]; + let combined = instance.combinerMax(item, right, { + field: "missingarray", + }); + assert.deepEqual(combined.missingarray, [5, 1, 4]); + }); + it("should handle short array to long array", () => { + let right = makeItem(); + right.arr1 = [5, 1, 4, 7]; + let combined = instance.combinerMax(item, right, { field: "arr1" }); + assert.deepEqual(combined.arr1, [5, 3, 4, 7]); + }); + it("should handle long array to short array", () => { + let right = makeItem(); + right.arr1 = [5, 1, 4]; + item.arr1.push(7); + let combined = instance.combinerMax(item, right, { field: "arr1" }); + assert.deepEqual(combined.arr1, [5, 3, 4, 7]); + }); + it("should handle missing left number", () => { + let right = makeItem(); + right.missingnumber = 999; + let combined = instance.combinerMax(item, right, { + field: "missingnumber", + }); + assert.deepEqual(combined.missingnumber, 999); + }); + it("should handle big number", () => { + let right = makeItem(); + right.lhs = 99; + let combined = instance.combinerMax(item, right, { field: "lhs" }); + assert.equal(combined.lhs, 99); + }); + it("should handle small number", () => { + let right = makeItem(); + item.lhs = 99; + let combined = instance.combinerMax(item, right, { field: "lhs" }); + assert.equal(combined.lhs, 99); + }); + it("should error on missing left, and right is a string", () => { + let right = makeItem(); + right.error = "error"; + let combined = instance.combinerMax(item, right, { field: "error" }); + assert.equal(combined, null); + }); + it("should error on left string", () => { + let right = makeItem(); + let combined = instance.combinerMax(item, right, { field: "foo" }); + assert.equal(combined, null); + }); + it("should error on mismatch types", () => { + let right = makeItem(); + right.lhs = [1, 2, 3]; + let combined = instance.combinerMax(item, right, { field: "lhs" }); + assert.equal(combined, null); + }); + }); + + describe("#combinerCollectValues", () => { + it("should error on bogus operation", () => { + let right = makeItem(); + right.url_domain = "maseratiusa.com/maserati"; + right.time = 41; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "missing", + }); + assert.equal(combined, null); + }); + it("should sum when missing left", () => { + let right = makeItem(); + right.url_domain = "maseratiusa.com/maserati"; + right.time = 41; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "sum", + }); + assert.deepEqual(combined.combined_map, { + "maseratiusa.com/maserati": 41, + }); + }); + it("should sum when missing right", () => { + let right = makeItem(); + item.combined_map = { fake: 42 }; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "sum", + }); + assert.deepEqual(combined.combined_map, { fake: 42 }); + }); + it("should sum when both", () => { + let right = makeItem(); + right.url_domain = "maseratiusa.com/maserati"; + right.time = 41; + item.combined_map = { fake: 42, "maseratiusa.com/maserati": 41 }; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "sum", + }); + assert.deepEqual(combined.combined_map, { + fake: 42, + "maseratiusa.com/maserati": 82, + }); + }); + + it("should max when missing left", () => { + let right = makeItem(); + right.url_domain = "maseratiusa.com/maserati"; + right.time = 41; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "max", + }); + assert.deepEqual(combined.combined_map, { + "maseratiusa.com/maserati": 41, + }); + }); + it("should max when missing right", () => { + let right = makeItem(); + item.combined_map = { fake: 42 }; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "max", + }); + assert.deepEqual(combined.combined_map, { fake: 42 }); + }); + it("should max when both (right)", () => { + let right = makeItem(); + right.url_domain = "maseratiusa.com/maserati"; + right.time = 99; + item.combined_map = { fake: 42, "maseratiusa.com/maserati": 41 }; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "max", + }); + assert.deepEqual(combined.combined_map, { + fake: 42, + "maseratiusa.com/maserati": 99, + }); + }); + it("should max when both (left)", () => { + let right = makeItem(); + right.url_domain = "maseratiusa.com/maserati"; + right.time = -99; + item.combined_map = { fake: 42, "maseratiusa.com/maserati": 41 }; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "max", + }); + assert.deepEqual(combined.combined_map, { + fake: 42, + "maseratiusa.com/maserati": 41, + }); + }); + + it("should overwrite when missing left", () => { + let right = makeItem(); + right.url_domain = "maseratiusa.com/maserati"; + right.time = 41; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "overwrite", + }); + assert.deepEqual(combined.combined_map, { + "maseratiusa.com/maserati": 41, + }); + }); + it("should overwrite when missing right", () => { + let right = makeItem(); + item.combined_map = { fake: 42 }; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "overwrite", + }); + assert.deepEqual(combined.combined_map, { fake: 42 }); + }); + it("should overwrite when both", () => { + let right = makeItem(); + right.url_domain = "maseratiusa.com/maserati"; + right.time = 41; + item.combined_map = { fake: 42, "maseratiusa.com/maserati": 77 }; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "overwrite", + }); + assert.deepEqual(combined.combined_map, { + fake: 42, + "maseratiusa.com/maserati": 41, + }); + }); + + it("should count when missing left", () => { + let right = makeItem(); + right.url_domain = "maseratiusa.com/maserati"; + right.time = 41; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "count", + }); + assert.deepEqual(combined.combined_map, { + "maseratiusa.com/maserati": 1, + }); + }); + it("should count when missing right", () => { + let right = makeItem(); + item.combined_map = { fake: 42 }; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "count", + }); + assert.deepEqual(combined.combined_map, { fake: 42 }); + }); + it("should count when both", () => { + let right = makeItem(); + right.url_domain = "maseratiusa.com/maserati"; + right.time = 41; + item.combined_map = { fake: 42, "maseratiusa.com/maserati": 1 }; + let combined = instance.combinerCollectValues(item, right, { + left_field: "combined_map", + right_key_field: "url_domain", + right_value_field: "time", + operation: "count", + }); + assert.deepEqual(combined.combined_map, { + fake: 42, + "maseratiusa.com/maserati": 2, + }); + }); + }); + + describe("#executeRecipe", () => { + it("should handle working steps", () => { + let final = instance.executeRecipe({}, [ + { function: "set_default", field: "foo", value: 1 }, + { function: "set_default", field: "bar", value: 10 }, + ]); + assert.equal(final.foo, 1); + assert.equal(final.bar, 10); + }); + it("should handle unknown steps", () => { + let final = instance.executeRecipe({}, [ + { function: "set_default", field: "foo", value: 1 }, + { function: "missing" }, + { function: "set_default", field: "bar", value: 10 }, + ]); + assert.equal(final, null); + }); + it("should handle erroring steps", () => { + let final = instance.executeRecipe({}, [ + { function: "set_default", field: "foo", value: 1 }, + { + function: "accept_item_by_field_value", + field: "missing", + op: "invalid", + rhsField: "moot", + rhsValue: "m00t", + }, + { function: "set_default", field: "bar", value: 10 }, + ]); + assert.equal(final, null); + }); + }); + + describe("#executeCombinerRecipe", () => { + it("should handle working steps", () => { + let final = instance.executeCombinerRecipe( + { foo: 1, bar: 10 }, + { foo: 1, bar: 10 }, + [ + { function: "combiner_add", field: "foo" }, + { function: "combiner_add", field: "bar" }, + ] + ); + assert.equal(final.foo, 2); + assert.equal(final.bar, 20); + }); + it("should handle unknown steps", () => { + let final = instance.executeCombinerRecipe( + { foo: 1, bar: 10 }, + { foo: 1, bar: 10 }, + [ + { function: "combiner_add", field: "foo" }, + { function: "missing" }, + { function: "combiner_add", field: "bar" }, + ] + ); + assert.equal(final, null); + }); + it("should handle erroring steps", () => { + let final = instance.executeCombinerRecipe( + { foo: 1, bar: 10, baz: 0 }, + { foo: 1, bar: 10, baz: "hundred" }, + [ + { function: "combiner_add", field: "foo" }, + { function: "combiner_add", field: "baz" }, + { function: "combiner_add", field: "bar" }, + ] + ); + assert.equal(final, null); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/PersonalityProvider/Tokenize.test.js b/browser/components/newtab/test/unit/lib/PersonalityProvider/Tokenize.test.js new file mode 100644 index 0000000000..19e738d451 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/PersonalityProvider/Tokenize.test.js @@ -0,0 +1,134 @@ +import { + tokenize, + toksToTfIdfVector, +} from "lib/PersonalityProvider/Tokenize.mjs"; + +const EPSILON = 0.00001; + +describe("TF-IDF Term Vectorizer", () => { + describe("#tokenize", () => { + let testCases = [ + { input: "HELLO there", expected: ["hello", "there"] }, + { input: "blah,,,blah,blah", expected: ["blah", "blah", "blah"] }, + { + input: "Call Jenny: 967-5309", + expected: ["call", "jenny", "967", "5309"], + }, + { + input: "Yo(what)[[hello]]{{jim}}}bob{1:2:1+2=$3", + expected: [ + "yo", + "what", + "hello", + "jim", + "bob", + "1", + "2", + "1", + "2", + "3", + ], + }, + { input: "čÄfė 80's", expected: ["čäfė", "80", "s"] }, + { input: "我知道很多东西。", expected: ["我知道很多东西"] }, + ]; + let checkTokenization = tc => { + it(`${tc.input} should tokenize to ${tc.expected}`, () => { + assert.deepEqual(tc.expected, tokenize(tc.input)); + }); + }; + + for (let i = 0; i < testCases.length; i++) { + checkTokenization(testCases[i]); + } + }); + + describe("#tfidf", () => { + let vocab_idfs = { + deal: [221, 5.5058519847862275], + easy: [269, 5.5058519847862275], + tanks: [867, 5.601162164590552], + sites: [792, 5.957837108529285], + care: [153, 5.957837108529285], + needs: [596, 5.824305715904762], + finally: [334, 5.706522680248379], + }; + let testCases = [ + { + input: "Finally! Easy care for your tanks!", + expected: { + finally: [334, 0.5009816295853761], + easy: [269, 0.48336453811728713], + care: [153, 0.5230447876368227], + tanks: [867, 0.49173191907236774], + }, + }, + { + input: "Easy easy EASY", + expected: { easy: [269, 1.0] }, + }, + { + input: "Easy easy care", + expected: { + easy: [269, 0.8795205218806832], + care: [153, 0.4758609582543317], + }, + }, + { + input: "easy care", + expected: { + easy: [269, 0.6786999710383944], + care: [153, 0.7344156515982504], + }, + }, + { + input: "这个空间故意留空。", + expected: { + /* This space is left intentionally blank. */ + }, + }, + ]; + let checkTokenGeneration = tc => { + describe(`${tc.input} should have only vocabulary tokens`, () => { + let actual = toksToTfIdfVector(tokenize(tc.input), vocab_idfs); + + it(`${tc.input} should generate exactly ${Object.keys( + tc.expected + )}`, () => { + let seen = {}; + Object.keys(actual).forEach(actualTok => { + assert.isTrue(actualTok in tc.expected); + seen[actualTok] = true; + }); + Object.keys(tc.expected).forEach(expectedTok => { + assert.isTrue(expectedTok in seen); + }); + }); + + it(`${tc.input} should have the correct token ids`, () => { + Object.keys(actual).forEach(actualTok => { + assert.equal(tc.expected[actualTok][0], actual[actualTok][0]); + }); + }); + }); + }; + + let checkTfIdfVector = tc => { + let actual = toksToTfIdfVector(tokenize(tc.input), vocab_idfs); + it(`${tc.input} should have the correct tf-idf`, () => { + Object.keys(actual).forEach(actualTok => { + let delta = Math.abs( + tc.expected[actualTok][1] - actual[actualTok][1] + ); + assert.isTrue(delta <= EPSILON); + }); + }); + }; + + // run the tests + for (let i = 0; i < testCases.length; i++) { + checkTokenGeneration(testCases[i]); + checkTfIdfVector(testCases[i]); + } + }); +}); diff --git a/browser/components/newtab/test/unit/lib/PrefsFeed.test.js b/browser/components/newtab/test/unit/lib/PrefsFeed.test.js new file mode 100644 index 0000000000..498c7198ab --- /dev/null +++ b/browser/components/newtab/test/unit/lib/PrefsFeed.test.js @@ -0,0 +1,357 @@ +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { GlobalOverrider } from "test/unit/utils"; +import { PrefsFeed } from "lib/PrefsFeed.sys.mjs"; + +let overrider = new GlobalOverrider(); + +describe("PrefsFeed", () => { + let feed; + let FAKE_PREFS; + let sandbox; + let ServicesStub; + beforeEach(() => { + sandbox = sinon.createSandbox(); + FAKE_PREFS = new Map([ + ["foo", 1], + ["bar", 2], + ["baz", { value: 1, skipBroadcast: true }], + ["qux", { value: 1, skipBroadcast: true, alsoToPreloaded: true }], + ]); + feed = new PrefsFeed(FAKE_PREFS); + const storage = { + getAll: sandbox.stub().resolves(), + set: sandbox.stub().resolves(), + }; + ServicesStub = { + prefs: { + clearUserPref: sinon.spy(), + getStringPref: sinon.spy(), + getIntPref: sinon.spy(), + getBoolPref: sinon.spy(), + }, + obs: { + removeObserver: sinon.spy(), + addObserver: sinon.spy(), + }, + }; + sinon.spy(feed, "_setPref"); + feed.store = { + dispatch: sinon.spy(), + getState() { + return this.state; + }, + dbStorage: { getDbTable: sandbox.stub().returns(storage) }, + }; + // Setup for tests that don't call `init` + feed._storage = storage; + feed._prefs = { + get: sinon.spy(item => FAKE_PREFS.get(item)), + set: sinon.spy((name, value) => FAKE_PREFS.set(name, value)), + observe: sinon.spy(), + observeBranch: sinon.spy(), + ignore: sinon.spy(), + ignoreBranch: sinon.spy(), + reset: sinon.stub(), + _branchStr: "branch.str.", + }; + overrider.set({ + PrivateBrowsingUtils: { enabled: true }, + Services: ServicesStub, + }); + }); + afterEach(() => { + overrider.restore(); + sandbox.restore(); + }); + + it("should set a pref when a SET_PREF action is received", () => { + feed.onAction(ac.SetPref("foo", 2)); + assert.calledWith(feed._prefs.set, "foo", 2); + }); + it("should call clearUserPref with action CLEAR_PREF", () => { + feed.onAction({ type: at.CLEAR_PREF, data: { name: "pref.test" } }); + assert.calledWith(ServicesStub.prefs.clearUserPref, "branch.str.pref.test"); + }); + it("should dispatch PREFS_INITIAL_VALUES on init with pref values and .isPrivateBrowsingEnabled", () => { + feed.onAction({ type: at.INIT }); + assert.calledOnce(feed.store.dispatch); + assert.equal( + feed.store.dispatch.firstCall.args[0].type, + at.PREFS_INITIAL_VALUES + ); + const [{ data }] = feed.store.dispatch.firstCall.args; + assert.equal(data.foo, 1); + assert.equal(data.bar, 2); + assert.isTrue(data.isPrivateBrowsingEnabled); + }); + it("should dispatch PREFS_INITIAL_VALUES with a .featureConfig", () => { + sandbox.stub(global.NimbusFeatures.newtab, "getAllVariables").returns({ + prefsButtonIcon: "icon-foo", + }); + feed.onAction({ type: at.INIT }); + assert.equal( + feed.store.dispatch.firstCall.args[0].type, + at.PREFS_INITIAL_VALUES + ); + const [{ data }] = feed.store.dispatch.firstCall.args; + assert.deepEqual(data.featureConfig, { prefsButtonIcon: "icon-foo" }); + }); + it("should dispatch PREFS_INITIAL_VALUES with an empty object if no experiment is returned", () => { + sandbox.stub(global.NimbusFeatures.newtab, "getAllVariables").returns(null); + feed.onAction({ type: at.INIT }); + assert.equal( + feed.store.dispatch.firstCall.args[0].type, + at.PREFS_INITIAL_VALUES + ); + const [{ data }] = feed.store.dispatch.firstCall.args; + assert.deepEqual(data.featureConfig, {}); + }); + it("should add one branch observer on init", () => { + feed.onAction({ type: at.INIT }); + assert.calledOnce(feed._prefs.observeBranch); + assert.calledWith(feed._prefs.observeBranch, feed); + }); + it("should initialise the storage on init", () => { + feed.init(); + + assert.calledOnce(feed.store.dbStorage.getDbTable); + assert.calledWithExactly(feed.store.dbStorage.getDbTable, "sectionPrefs"); + }); + it("should handle region on init", () => { + feed.init(); + assert.equal(feed.geo, "US"); + }); + it("should add region observer on init", () => { + sandbox.stub(global.Region, "home").get(() => ""); + feed.init(); + assert.equal(feed.geo, ""); + assert.calledWith( + ServicesStub.obs.addObserver, + feed, + global.Region.REGION_TOPIC + ); + }); + it("should remove the branch observer on uninit", () => { + feed.onAction({ type: at.UNINIT }); + assert.calledOnce(feed._prefs.ignoreBranch); + assert.calledWith(feed._prefs.ignoreBranch, feed); + }); + it("should call removeObserver", () => { + feed.geo = ""; + feed.uninit(); + assert.calledWith( + ServicesStub.obs.removeObserver, + feed, + global.Region.REGION_TOPIC + ); + }); + it("should send a PREF_CHANGED action when onPrefChanged is called", () => { + feed.onPrefChanged("foo", 2); + assert.calledWith( + feed.store.dispatch, + ac.BroadcastToContent({ + type: at.PREF_CHANGED, + data: { name: "foo", value: 2 }, + }) + ); + }); + it("should send a PREF_CHANGED actions when onPocketExperimentUpdated is called", () => { + sandbox + .stub(global.NimbusFeatures.pocketNewtab, "getAllVariables") + .returns({ + prefsButtonIcon: "icon-new", + }); + feed.onPocketExperimentUpdated(); + assert.calledWith( + feed.store.dispatch, + ac.BroadcastToContent({ + type: at.PREF_CHANGED, + data: { + name: "pocketConfig", + value: { + prefsButtonIcon: "icon-new", + }, + }, + }) + ); + }); + it("should not send a PREF_CHANGED actions when onPocketExperimentUpdated is called during startup", () => { + sandbox + .stub(global.NimbusFeatures.pocketNewtab, "getAllVariables") + .returns({ + prefsButtonIcon: "icon-new", + }); + feed.onPocketExperimentUpdated({}, "feature-experiment-loaded"); + assert.notCalled(feed.store.dispatch); + feed.onPocketExperimentUpdated({}, "feature-rollout-loaded"); + assert.notCalled(feed.store.dispatch); + }); + it("should send a PREF_CHANGED actions when onExperimentUpdated is called", () => { + sandbox.stub(global.NimbusFeatures.newtab, "getAllVariables").returns({ + prefsButtonIcon: "icon-new", + }); + feed.onExperimentUpdated(); + assert.calledWith( + feed.store.dispatch, + ac.BroadcastToContent({ + type: at.PREF_CHANGED, + data: { + name: "featureConfig", + value: { + prefsButtonIcon: "icon-new", + }, + }, + }) + ); + }); + + it("should remove all events on removeListeners", () => { + feed.geo = ""; + sandbox.spy(global.NimbusFeatures.pocketNewtab, "offUpdate"); + sandbox.spy(global.NimbusFeatures.newtab, "offUpdate"); + feed.removeListeners(); + assert.calledWith( + global.NimbusFeatures.pocketNewtab.offUpdate, + feed.onPocketExperimentUpdated + ); + assert.calledWith( + global.NimbusFeatures.newtab.offUpdate, + feed.onExperimentUpdated + ); + assert.calledWith( + ServicesStub.obs.removeObserver, + feed, + global.Region.REGION_TOPIC + ); + }); + + it("should set storage pref on UPDATE_SECTION_PREFS", async () => { + await feed.onAction({ + type: at.UPDATE_SECTION_PREFS, + data: { id: "topsites", value: { collapsed: false } }, + }); + assert.calledWith(feed._storage.set, "topsites", { collapsed: false }); + }); + it("should set storage pref with section prefix on UPDATE_SECTION_PREFS", async () => { + await feed.onAction({ + type: at.UPDATE_SECTION_PREFS, + data: { id: "topstories", value: { collapsed: false } }, + }); + assert.calledWith(feed._storage.set, "feeds.section.topstories", { + collapsed: false, + }); + }); + it("should catch errors on UPDATE_SECTION_PREFS", async () => { + feed._storage.set.throws(new Error("foo")); + assert.doesNotThrow(async () => { + await feed.onAction({ + type: at.UPDATE_SECTION_PREFS, + data: { id: "topstories", value: { collapsed: false } }, + }); + }); + }); + it("should send OnlyToMain pref update if config for pref has skipBroadcast: true", async () => { + feed.onPrefChanged("baz", { value: 2, skipBroadcast: true }); + assert.calledWith( + feed.store.dispatch, + ac.OnlyToMain({ + type: at.PREF_CHANGED, + data: { name: "baz", value: { value: 2, skipBroadcast: true } }, + }) + ); + }); + it("should send AlsoToPreloaded pref update if config for pref has skipBroadcast: true and alsoToPreloaded: true", async () => { + feed.onPrefChanged("qux", { + value: 2, + skipBroadcast: true, + alsoToPreloaded: true, + }); + assert.calledWith( + feed.store.dispatch, + ac.AlsoToPreloaded({ + type: at.PREF_CHANGED, + data: { + name: "qux", + value: { value: 2, skipBroadcast: true, alsoToPreloaded: true }, + }, + }) + ); + }); + describe("#observe", () => { + it("should call dispatch from observe", () => { + feed.observe(undefined, global.Region.REGION_TOPIC); + assert.calledOnce(feed.store.dispatch); + }); + }); + describe("#_setStringPref", () => { + it("should call _setPref and getStringPref from _setStringPref", () => { + feed._setStringPref({}, "fake.pref", "default"); + assert.calledOnce(feed._setPref); + assert.calledWith( + feed._setPref, + { "fake.pref": undefined }, + "fake.pref", + "default" + ); + assert.calledOnce(ServicesStub.prefs.getStringPref); + assert.calledWith( + ServicesStub.prefs.getStringPref, + "browser.newtabpage.activity-stream.fake.pref", + "default" + ); + }); + }); + describe("#_setBoolPref", () => { + it("should call _setPref and getBoolPref from _setBoolPref", () => { + feed._setBoolPref({}, "fake.pref", false); + assert.calledOnce(feed._setPref); + assert.calledWith( + feed._setPref, + { "fake.pref": undefined }, + "fake.pref", + false + ); + assert.calledOnce(ServicesStub.prefs.getBoolPref); + assert.calledWith( + ServicesStub.prefs.getBoolPref, + "browser.newtabpage.activity-stream.fake.pref", + false + ); + }); + }); + describe("#_setIntPref", () => { + it("should call _setPref and getIntPref from _setIntPref", () => { + feed._setIntPref({}, "fake.pref", 1); + assert.calledOnce(feed._setPref); + assert.calledWith( + feed._setPref, + { "fake.pref": undefined }, + "fake.pref", + 1 + ); + assert.calledOnce(ServicesStub.prefs.getIntPref); + assert.calledWith( + ServicesStub.prefs.getIntPref, + "browser.newtabpage.activity-stream.fake.pref", + 1 + ); + }); + }); + describe("#_setPref", () => { + it("should set pref value with _setPref", () => { + const getPrefFunctionSpy = sinon.spy(); + const values = {}; + feed._setPref(values, "fake.pref", "default", getPrefFunctionSpy); + assert.deepEqual(values, { "fake.pref": undefined }); + assert.calledOnce(getPrefFunctionSpy); + assert.calledWith( + getPrefFunctionSpy, + "browser.newtabpage.activity-stream.fake.pref", + "default" + ); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/RecommendationProvider.test.js b/browser/components/newtab/test/unit/lib/RecommendationProvider.test.js new file mode 100644 index 0000000000..9e68f4869a --- /dev/null +++ b/browser/components/newtab/test/unit/lib/RecommendationProvider.test.js @@ -0,0 +1,331 @@ +import { + actionCreators as ac, + actionTypes as at, +} from "common/Actions.sys.mjs"; +import { RecommendationProvider } from "lib/RecommendationProvider.sys.mjs"; +import { combineReducers, createStore } from "redux"; +import { reducers } from "common/Reducers.sys.mjs"; +import { GlobalOverrider } from "test/unit/utils"; + +import { PersonalityProvider } from "lib/PersonalityProvider/PersonalityProvider.sys.mjs"; +import { PersistentCache } from "lib/PersistentCache.sys.mjs"; + +const PREF_PERSONALIZATION_ENABLED = "discoverystream.personalization.enabled"; +const PREF_PERSONALIZATION_MODEL_KEYS = + "discoverystream.personalization.modelKeys"; +describe("RecommendationProvider", () => { + let feed; + let sandbox; + let clock; + let globals; + + beforeEach(() => { + globals = new GlobalOverrider(); + globals.set({ + PersistentCache, + PersonalityProvider, + }); + + sandbox = sinon.createSandbox(); + clock = sinon.useFakeTimers(); + feed = new RecommendationProvider(); + feed.store = createStore(combineReducers(reducers), {}); + }); + + afterEach(() => { + sandbox.restore(); + clock.restore(); + globals.restore(); + }); + + describe("#setProvider", () => { + it("should setup proper provider with modelKeys", async () => { + feed.setProvider(); + + assert.equal(feed.provider.modelKeys, undefined); + + feed.provider = null; + feed._modelKeys = "1234"; + + feed.setProvider(); + + assert.equal(feed.provider.modelKeys, "1234"); + feed._modelKeys = "12345"; + + // Calling it again should not rebuild the provider. + feed.setProvider(); + assert.equal(feed.provider.modelKeys, "1234"); + }); + }); + + describe("#calculateItemRelevanceScore", () => { + it("should use personalized score with provider", async () => { + const item = {}; + feed.provider = { + calculateItemRelevanceScore: async () => 0.5, + }; + await feed.calculateItemRelevanceScore(item); + assert.equal(item.score, 0.5); + }); + }); + + describe("#teardown", () => { + it("should call provider.teardown ", () => { + sandbox.stub(global.Services.obs, "removeObserver").returns(); + feed.loaded = true; + feed.provider = { + teardown: sandbox.stub().resolves(), + }; + feed.teardown(); + assert.calledOnce(feed.provider.teardown); + assert.calledOnce(global.Services.obs.removeObserver); + assert.calledWith(global.Services.obs.removeObserver, feed, "idle-daily"); + }); + }); + + describe("#resetState", () => { + it("should null affinityProviderV2 and affinityProvider", () => { + feed._modelKeys = {}; + feed.provider = {}; + + feed.resetState(); + + assert.equal(feed._modelKeys, null); + assert.equal(feed.provider, null); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_CONFIG_CHANGE", () => { + it("should call teardown, resetState, and setVersion", async () => { + sandbox.spy(feed, "teardown"); + sandbox.spy(feed, "resetState"); + feed.onAction({ + type: at.DISCOVERY_STREAM_CONFIG_CHANGE, + }); + assert.calledOnce(feed.teardown); + assert.calledOnce(feed.resetState); + }); + }); + + describe("#onAction: PREF_CHANGED", () => { + beforeEach(() => { + sandbox.spy(feed.store, "dispatch"); + }); + it("should dispatch to DISCOVERY_STREAM_CONFIG_RESET PREF_PERSONALIZATION_MODEL_KEYS", async () => { + feed.onAction({ + type: at.PREF_CHANGED, + data: { + name: PREF_PERSONALIZATION_MODEL_KEYS, + }, + }); + + assert.calledWith( + feed.store.dispatch, + ac.BroadcastToContent({ + type: at.DISCOVERY_STREAM_CONFIG_RESET, + }) + ); + }); + }); + + describe("#personalizationOverride", () => { + it("should dispatch setPref", async () => { + sandbox.spy(feed.store, "dispatch"); + feed.store.getState = () => ({ + Prefs: { + values: { + "discoverystream.personalization.enabled": true, + }, + }, + }); + + feed.personalizationOverride(true); + + assert.calledWithMatch(feed.store.dispatch, { + data: { + name: "discoverystream.personalization.override", + value: true, + }, + type: at.SET_PREF, + }); + }); + it("should dispatch CLEAR_PREF", async () => { + sandbox.spy(feed.store, "dispatch"); + feed.store.getState = () => ({ + Prefs: { + values: { + "discoverystream.personalization.enabled": true, + "discoverystream.personalization.override": true, + }, + }, + }); + + feed.personalizationOverride(false); + + assert.calledWithMatch(feed.store.dispatch, { + data: { + name: "discoverystream.personalization.override", + }, + type: at.CLEAR_PREF, + }); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_DEV_IDLE_DAILY", () => { + it("should trigger idle-daily observer", async () => { + sandbox.stub(global.Services.obs, "notifyObservers").returns(); + await feed.onAction({ + type: at.DISCOVERY_STREAM_DEV_IDLE_DAILY, + }); + assert.calledWith( + global.Services.obs.notifyObservers, + null, + "idle-daily" + ); + }); + }); + + describe("#onAction: INIT", () => { + it("should ", async () => { + sandbox.stub(feed, "enable").returns(); + await feed.onAction({ + type: at.INIT, + }); + assert.calledOnce(feed.enable); + assert.calledWith(feed.enable, true); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_PERSONALIZATION_OVERRIDE", () => { + it("should ", async () => { + sandbox.stub(feed, "personalizationOverride").returns(); + await feed.onAction({ + type: at.DISCOVERY_STREAM_PERSONALIZATION_OVERRIDE, + data: { override: true }, + }); + assert.calledOnce(feed.personalizationOverride); + assert.calledWith(feed.personalizationOverride, true); + }); + }); + + describe("#loadPersonalizationScoresCache", () => { + it("should create a personalization provider from cached scores", async () => { + sandbox.spy(feed.store, "dispatch"); + sandbox.spy(feed.cache, "set"); + feed.provider = { + init: async () => {}, + getScores: () => "scores", + }; + feed.store.getState = () => ({ + Prefs: { + values: { + pocketConfig: { + recsPersonalized: true, + spocsPersonalized: true, + }, + "discoverystream.personalization.enabled": true, + "feeds.section.topstories": true, + "feeds.system.topstories": true, + }, + }, + }); + const fakeCache = { + personalization: { + scores: 123, + _timestamp: 456, + }, + }; + sandbox.stub(feed.cache, "get").returns(Promise.resolve(fakeCache)); + + await feed.loadPersonalizationScoresCache(); + + assert.equal(feed.personalizationLastUpdated, 456); + }); + }); + + describe("#updatePersonalizationScores", () => { + beforeEach(() => { + sandbox.spy(feed.store, "dispatch"); + sandbox.spy(feed.cache, "set"); + sandbox.spy(feed, "setProvider"); + feed.provider = { + init: async () => {}, + getScores: () => "scores", + }; + }); + it("should update provider on updatePersonalizationScores", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { + pocketConfig: { + recsPersonalized: true, + spocsPersonalized: true, + }, + "discoverystream.personalization.enabled": true, + "feeds.section.topstories": true, + "feeds.system.topstories": true, + }, + }, + }); + + await feed.updatePersonalizationScores(); + + assert.calledWith( + feed.store.dispatch, + ac.BroadcastToContent({ + type: at.DISCOVERY_STREAM_PERSONALIZATION_LAST_UPDATED, + data: { + lastUpdated: 0, + }, + }) + ); + assert.calledWith(feed.cache.set, "personalization", { + scores: "scores", + _timestamp: 0, + }); + }); + it("should not update provider on updatePersonalizationScores", async () => { + feed.store.getState = () => ({ + Prefs: { + values: { + "discoverystream.spocs.personalized": true, + "discoverystream.recs.personalized": true, + "discoverystream.personalization.enabled": false, + }, + }, + }); + await feed.updatePersonalizationScores(); + + assert.notCalled(feed.setProvider); + }); + }); + + describe("#onAction: DISCOVERY_STREAM_PERSONALIZATION_TOGGLE", () => { + it("should fire SET_PREF with enabled", async () => { + sandbox.spy(feed.store, "dispatch"); + feed.store.getState = () => ({ + Prefs: { + values: { + [PREF_PERSONALIZATION_ENABLED]: false, + }, + }, + }); + + await feed.onAction({ + type: at.DISCOVERY_STREAM_PERSONALIZATION_TOGGLE, + }); + assert.calledWith( + feed.store.dispatch, + ac.SetPref(PREF_PERSONALIZATION_ENABLED, true) + ); + }); + }); + + describe("#observe", () => { + it("should call updatePersonalizationScores on idle daily", async () => { + sandbox.stub(feed, "updatePersonalizationScores").returns(); + feed.observe(null, "idle-daily"); + assert.calledOnce(feed.updatePersonalizationScores); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/Screenshots.test.js b/browser/components/newtab/test/unit/lib/Screenshots.test.js new file mode 100644 index 0000000000..a03f06890d --- /dev/null +++ b/browser/components/newtab/test/unit/lib/Screenshots.test.js @@ -0,0 +1,209 @@ +"use strict"; +import { GlobalOverrider } from "test/unit/utils"; +import { Screenshots } from "lib/Screenshots.sys.mjs"; + +const URL = "foo.com"; +const FAKE_THUMBNAIL_PATH = "fake/path/thumb.jpg"; +const FAKE_THUMBNAIL_THUMB = + "moz-page-thumb://thumbnail?url=http%3A%2F%2Ffoo.com%2F"; + +describe("Screenshots", () => { + let globals; + let sandbox; + let fakeServices; + let testFile; + + beforeEach(() => { + globals = new GlobalOverrider(); + sandbox = globals.sandbox; + fakeServices = { + wm: { + getEnumerator() { + return Array(10); + }, + }, + }; + globals.set("BackgroundPageThumbs", { + captureIfMissing: sandbox.spy(() => Promise.resolve()), + }); + globals.set("PageThumbs", { + _store: sandbox.stub(), + getThumbnailPath: sandbox.spy(() => FAKE_THUMBNAIL_PATH), + getThumbnailURL: sandbox.spy(() => FAKE_THUMBNAIL_THUMB), + }); + globals.set("PrivateBrowsingUtils", { + isWindowPrivate: sandbox.spy(() => false), + }); + testFile = { size: 1 }; + globals.set("Services", fakeServices); + globals.set( + "fetch", + sandbox.spy(() => + Promise.resolve({ blob: () => Promise.resolve(testFile) }) + ) + ); + }); + afterEach(() => { + globals.restore(); + }); + + describe("#getScreenshotForURL", () => { + it("should call BackgroundPageThumbs.captureIfMissing with the correct url", async () => { + await Screenshots.getScreenshotForURL(URL); + assert.calledWith(global.BackgroundPageThumbs.captureIfMissing, URL); + }); + it("should call PageThumbs.getThumbnailPath with the correct url", async () => { + globals.set("gPrivilegedAboutProcessEnabled", false); + await Screenshots.getScreenshotForURL(URL); + assert.calledWith(global.PageThumbs.getThumbnailPath, URL); + }); + it("should call fetch", async () => { + await Screenshots.getScreenshotForURL(URL); + assert.calledOnce(global.fetch); + }); + it("should have the necessary keys in the response object", async () => { + const screenshot = await Screenshots.getScreenshotForURL(URL); + + assert.notEqual(screenshot.path, undefined); + assert.notEqual(screenshot.data, undefined); + }); + it("should get null if something goes wrong", async () => { + globals.set("BackgroundPageThumbs", { + captureIfMissing: () => + Promise.reject(new Error("Cannot capture thumbnail")), + }); + + const screenshot = await Screenshots.getScreenshotForURL(URL); + + assert.calledOnce(global.PageThumbs._store); + assert.equal(screenshot, null); + }); + it("should get direct thumbnail url for privileged process", async () => { + globals.set("gPrivilegedAboutProcessEnabled", true); + await Screenshots.getScreenshotForURL(URL); + assert.calledWith(global.PageThumbs.getThumbnailURL, URL); + }); + it("should get null without storing if existing thumbnail is empty", async () => { + testFile.size = 0; + + const screenshot = await Screenshots.getScreenshotForURL(URL); + + assert.notCalled(global.PageThumbs._store); + assert.equal(screenshot, null); + }); + }); + + describe("#maybeCacheScreenshot", () => { + let link; + beforeEach(() => { + link = { + __sharedCache: { + updateLink: (prop, val) => { + link[prop] = val; + }, + }, + }; + }); + it("should call getScreenshotForURL", () => { + sandbox.stub(Screenshots, "getScreenshotForURL"); + sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true); + Screenshots.maybeCacheScreenshot( + link, + "mozilla.com", + "image", + sinon.stub() + ); + + assert.calledOnce(Screenshots.getScreenshotForURL); + assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com"); + }); + it("should not call getScreenshotForURL twice if a fetch is in progress", () => { + sandbox + .stub(Screenshots, "getScreenshotForURL") + .returns(new Promise(() => {})); + sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true); + Screenshots.maybeCacheScreenshot( + link, + "mozilla.com", + "image", + sinon.stub() + ); + Screenshots.maybeCacheScreenshot( + link, + "mozilla.org", + "image", + sinon.stub() + ); + + assert.calledOnce(Screenshots.getScreenshotForURL); + assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com"); + }); + it("should not call getScreenshotsForURL if property !== undefined", async () => { + sandbox + .stub(Screenshots, "getScreenshotForURL") + .returns(Promise.resolve(null)); + sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true); + await Screenshots.maybeCacheScreenshot( + link, + "mozilla.com", + "image", + sinon.stub() + ); + await Screenshots.maybeCacheScreenshot( + link, + "mozilla.org", + "image", + sinon.stub() + ); + + assert.calledOnce(Screenshots.getScreenshotForURL); + assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com"); + }); + it("should check if we are in private browsing before getting screenshots", async () => { + sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true); + await Screenshots.maybeCacheScreenshot( + link, + "mozilla.com", + "image", + sinon.stub() + ); + + assert.calledOnce(Screenshots._shouldGetScreenshots); + }); + it("should not get a screenshot if we are in private browsing", async () => { + sandbox.stub(Screenshots, "getScreenshotForURL"); + sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(false); + await Screenshots.maybeCacheScreenshot( + link, + "mozilla.com", + "image", + sinon.stub() + ); + + assert.notCalled(Screenshots.getScreenshotForURL); + }); + }); + + describe("#_shouldGetScreenshots", () => { + beforeEach(() => { + let more = 2; + sandbox + .stub(global.Services.wm, "getEnumerator") + .callsFake(() => Array(Math.max(more--, 0))); + }); + it("should use private browsing utils to determine if a window is private", () => { + Screenshots._shouldGetScreenshots(); + assert.calledOnce(global.PrivateBrowsingUtils.isWindowPrivate); + }); + it("should return true if there exists at least 1 non-private window", () => { + assert.isTrue(Screenshots._shouldGetScreenshots()); + }); + it("should return false if there exists private windows", () => { + global.PrivateBrowsingUtils = { + isWindowPrivate: sandbox.spy(() => true), + }; + assert.isFalse(Screenshots._shouldGetScreenshots()); + assert.calledTwice(global.PrivateBrowsingUtils.isWindowPrivate); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/SectionsManager.test.js b/browser/components/newtab/test/unit/lib/SectionsManager.test.js new file mode 100644 index 0000000000..b3a9abd70c --- /dev/null +++ b/browser/components/newtab/test/unit/lib/SectionsManager.test.js @@ -0,0 +1,897 @@ +"use strict"; +import { + actionCreators as ac, + actionTypes as at, + CONTENT_MESSAGE_TYPE, + MAIN_MESSAGE_TYPE, + PRELOAD_MESSAGE_TYPE, +} from "common/Actions.sys.mjs"; +import { EventEmitter, GlobalOverrider } from "test/unit/utils"; +import { SectionsFeed, SectionsManager } from "lib/SectionsManager.sys.mjs"; + +const FAKE_ID = "FAKE_ID"; +const FAKE_OPTIONS = { icon: "FAKE_ICON", title: "FAKE_TITLE" }; +const FAKE_ROWS = [ + { url: "1.example.com", type: "bookmark" }, + { url: "2.example.com", type: "pocket" }, + { url: "3.example.com", type: "history" }, +]; +const FAKE_TRENDING_ROWS = [{ url: "bar", type: "trending" }]; +const FAKE_URL = "2.example.com"; +const FAKE_CARD_OPTIONS = { title: "Some fake title" }; + +describe("SectionsManager", () => { + let globals; + let fakeServices; + let fakePlacesUtils; + let sandbox; + let storage; + + beforeEach(async () => { + sandbox = sinon.createSandbox(); + globals = new GlobalOverrider(); + fakeServices = { + prefs: { + getBoolPref: sandbox.stub(), + addObserver: sandbox.stub(), + removeObserver: sandbox.stub(), + }, + }; + fakePlacesUtils = { + history: { update: sinon.stub(), insert: sinon.stub() }, + }; + globals.set({ + Services: fakeServices, + PlacesUtils: fakePlacesUtils, + NimbusFeatures: { + newtab: { getAllVariables: sandbox.stub() }, + pocketNewtab: { getAllVariables: sandbox.stub() }, + }, + }); + // Redecorate SectionsManager to remove any listeners that have been added + EventEmitter.decorate(SectionsManager); + storage = { + get: sandbox.stub().resolves(), + set: sandbox.stub().resolves(), + }; + }); + + afterEach(() => { + globals.restore(); + sandbox.restore(); + }); + + describe("#init", () => { + it("should initialise the sections map with the built in sections", async () => { + SectionsManager.sections.clear(); + SectionsManager.initialized = false; + await SectionsManager.init({}, storage); + assert.equal(SectionsManager.sections.size, 2); + assert.ok(SectionsManager.sections.has("topstories")); + assert.ok(SectionsManager.sections.has("highlights")); + }); + it("should set .initialized to true", async () => { + SectionsManager.sections.clear(); + SectionsManager.initialized = false; + await SectionsManager.init({}, storage); + assert.ok(SectionsManager.initialized); + }); + it("should add observer for context menu prefs", async () => { + SectionsManager.CONTEXT_MENU_PREFS = { MENU_ITEM: "MENU_ITEM_PREF" }; + await SectionsManager.init({}, storage); + assert.calledOnce(fakeServices.prefs.addObserver); + assert.calledWith( + fakeServices.prefs.addObserver, + "MENU_ITEM_PREF", + SectionsManager + ); + }); + it("should save the reference to `storage` passed in", async () => { + await SectionsManager.init({}, storage); + + assert.equal(SectionsManager._storage, storage); + }); + }); + describe("#uninit", () => { + it("should remove observer for context menu prefs", () => { + SectionsManager.CONTEXT_MENU_PREFS = { MENU_ITEM: "MENU_ITEM_PREF" }; + SectionsManager.initialized = true; + SectionsManager.uninit(); + assert.calledOnce(fakeServices.prefs.removeObserver); + assert.calledWith( + fakeServices.prefs.removeObserver, + "MENU_ITEM_PREF", + SectionsManager + ); + assert.isFalse(SectionsManager.initialized); + }); + }); + describe("#addBuiltInSection", () => { + it("should not report an error if options is undefined", async () => { + globals.sandbox.spy(global.console, "error"); + SectionsManager._storage.get = sandbox.stub().returns(Promise.resolve()); + await SectionsManager.addBuiltInSection( + "feeds.section.topstories", + undefined + ); + + assert.notCalled(console.error); + }); + it("should report an error if options is malformed", async () => { + globals.sandbox.spy(global.console, "error"); + SectionsManager._storage.get = sandbox.stub().returns(Promise.resolve()); + await SectionsManager.addBuiltInSection( + "feeds.section.topstories", + "invalid" + ); + + assert.calledOnce(console.error); + }); + it("should not throw if the indexedDB operation fails", async () => { + globals.sandbox.spy(global.console, "error"); + storage.get = sandbox.stub().throws(); + SectionsManager._storage = storage; + + try { + await SectionsManager.addBuiltInSection("feeds.section.topstories"); + } catch (e) { + assert.fail(); + } + + assert.calledOnce(storage.get); + assert.calledOnce(console.error); + }); + }); + describe("#updateSectionPrefs", () => { + it("should update the collapsed value of the section", async () => { + sandbox.stub(SectionsManager, "updateSection"); + let topstories = SectionsManager.sections.get("topstories"); + assert.isFalse(topstories.pref.collapsed); + + await SectionsManager.updateSectionPrefs("topstories", { + collapsed: true, + }); + topstories = SectionsManager.sections.get("topstories"); + + assert.isTrue(SectionsManager.updateSection.args[0][1].pref.collapsed); + }); + it("should ignore invalid ids", async () => { + sandbox.stub(SectionsManager, "updateSection"); + await SectionsManager.updateSectionPrefs("foo", { collapsed: true }); + + assert.notCalled(SectionsManager.updateSection); + }); + }); + describe("#addSection", () => { + it("should add the id to sections and emit an ADD_SECTION event", () => { + const spy = sinon.spy(); + SectionsManager.on(SectionsManager.ADD_SECTION, spy); + SectionsManager.addSection(FAKE_ID, FAKE_OPTIONS); + assert.ok(SectionsManager.sections.has(FAKE_ID)); + assert.calledOnce(spy); + assert.calledWith( + spy, + SectionsManager.ADD_SECTION, + FAKE_ID, + FAKE_OPTIONS + ); + }); + }); + describe("#removeSection", () => { + it("should remove the id from sections and emit an REMOVE_SECTION event", () => { + // Ensure we start with the id in the set + assert.ok(SectionsManager.sections.has(FAKE_ID)); + const spy = sinon.spy(); + SectionsManager.on(SectionsManager.REMOVE_SECTION, spy); + SectionsManager.removeSection(FAKE_ID); + assert.notOk(SectionsManager.sections.has(FAKE_ID)); + assert.calledOnce(spy); + assert.calledWith(spy, SectionsManager.REMOVE_SECTION, FAKE_ID); + }); + }); + describe("#enableSection", () => { + it("should call updateSection with {enabled: true}", () => { + sinon.spy(SectionsManager, "updateSection"); + SectionsManager.addSection(FAKE_ID, FAKE_OPTIONS); + SectionsManager.enableSection(FAKE_ID); + assert.calledOnce(SectionsManager.updateSection); + assert.calledWith( + SectionsManager.updateSection, + FAKE_ID, + { enabled: true }, + true + ); + SectionsManager.updateSection.restore(); + }); + it("should emit an ENABLE_SECTION event", () => { + const spy = sinon.spy(); + SectionsManager.on(SectionsManager.ENABLE_SECTION, spy); + SectionsManager.enableSection(FAKE_ID); + assert.calledOnce(spy); + assert.calledWith(spy, SectionsManager.ENABLE_SECTION, FAKE_ID); + }); + }); + describe("#disableSection", () => { + it("should call updateSection with {enabled: false, rows: [], initialized: false}", () => { + sinon.spy(SectionsManager, "updateSection"); + SectionsManager.addSection(FAKE_ID, FAKE_OPTIONS); + SectionsManager.disableSection(FAKE_ID); + assert.calledOnce(SectionsManager.updateSection); + assert.calledWith( + SectionsManager.updateSection, + FAKE_ID, + { enabled: false, rows: [], initialized: false }, + true + ); + SectionsManager.updateSection.restore(); + }); + it("should emit a DISABLE_SECTION event", () => { + const spy = sinon.spy(); + SectionsManager.on(SectionsManager.DISABLE_SECTION, spy); + SectionsManager.disableSection(FAKE_ID); + assert.calledOnce(spy); + assert.calledWith(spy, SectionsManager.DISABLE_SECTION, FAKE_ID); + }); + }); + describe("#updateSection", () => { + it("should emit an UPDATE_SECTION event with correct arguments", () => { + SectionsManager.addSection(FAKE_ID, FAKE_OPTIONS); + const spy = sinon.spy(); + const dedupeConfigurations = [ + { id: "topstories", dedupeFrom: ["highlights"] }, + ]; + SectionsManager.on(SectionsManager.UPDATE_SECTION, spy); + SectionsManager.updateSection(FAKE_ID, { rows: FAKE_ROWS }, true); + assert.calledOnce(spy); + assert.calledWith( + spy, + SectionsManager.UPDATE_SECTION, + FAKE_ID, + { rows: FAKE_ROWS, dedupeConfigurations }, + true + ); + }); + it("should do nothing if the section doesn't exist", () => { + SectionsManager.removeSection(FAKE_ID); + const spy = sinon.spy(); + SectionsManager.on(SectionsManager.UPDATE_SECTION, spy); + SectionsManager.updateSection(FAKE_ID, { rows: FAKE_ROWS }, true); + assert.notCalled(spy); + }); + it("should update all sections", () => { + SectionsManager.sections.clear(); + const updateSectionOrig = SectionsManager.updateSection; + SectionsManager.updateSection = sinon.spy(); + + SectionsManager.addSection("ID1", { title: "FAKE_TITLE_1" }); + SectionsManager.addSection("ID2", { title: "FAKE_TITLE_2" }); + SectionsManager.updateSections(); + + assert.calledTwice(SectionsManager.updateSection); + assert.calledWith( + SectionsManager.updateSection, + "ID1", + { title: "FAKE_TITLE_1" }, + true + ); + assert.calledWith( + SectionsManager.updateSection, + "ID2", + { title: "FAKE_TITLE_2" }, + true + ); + SectionsManager.updateSection = updateSectionOrig; + }); + it("context menu pref change should update sections", async () => { + let observer; + const services = { + prefs: { + getBoolPref: sinon.spy(), + addObserver: (pref, o) => (observer = o), + removeObserver: sinon.spy(), + }, + }; + globals.set("Services", services); + + SectionsManager.updateSections = sinon.spy(); + SectionsManager.CONTEXT_MENU_PREFS = { MENU_ITEM: "MENU_ITEM_PREF" }; + await SectionsManager.init({}, storage); + observer.observe("", "nsPref:changed", "MENU_ITEM_PREF"); + + assert.calledOnce(SectionsManager.updateSections); + }); + }); + describe("#_addCardTypeLinkMenuOptions", () => { + const addCardTypeLinkMenuOptionsOrig = + SectionsManager._addCardTypeLinkMenuOptions; + const contextMenuOptionsOrig = + SectionsManager.CONTEXT_MENU_OPTIONS_FOR_HIGHLIGHT_TYPES; + beforeEach(() => { + // Add a topstories section and a highlights section, with types for each card + SectionsManager.addSection("topstories", { FAKE_TRENDING_ROWS }); + SectionsManager.addSection("highlights", { FAKE_ROWS }); + }); + it("should only call _addCardTypeLinkMenuOptions if the section update is for highlights", () => { + SectionsManager._addCardTypeLinkMenuOptions = sinon.spy(); + SectionsManager.updateSection("topstories", { rows: FAKE_ROWS }, false); + assert.notCalled(SectionsManager._addCardTypeLinkMenuOptions); + + SectionsManager.updateSection("highlights", { rows: FAKE_ROWS }, false); + assert.calledWith(SectionsManager._addCardTypeLinkMenuOptions, FAKE_ROWS); + }); + it("should only call _addCardTypeLinkMenuOptions if the section update has rows", () => { + SectionsManager._addCardTypeLinkMenuOptions = sinon.spy(); + SectionsManager.updateSection("highlights", {}, false); + assert.notCalled(SectionsManager._addCardTypeLinkMenuOptions); + }); + it("should assign the correct context menu options based on the type of highlight", () => { + SectionsManager._addCardTypeLinkMenuOptions = + addCardTypeLinkMenuOptionsOrig; + + SectionsManager.updateSection("highlights", { rows: FAKE_ROWS }, false); + const highlights = SectionsManager.sections.get("highlights").FAKE_ROWS; + + // FAKE_ROWS was added in the following order: bookmark, pocket, history + assert.deepEqual( + highlights[0].contextMenuOptions, + SectionsManager.CONTEXT_MENU_OPTIONS_FOR_HIGHLIGHT_TYPES.bookmark + ); + assert.deepEqual( + highlights[1].contextMenuOptions, + SectionsManager.CONTEXT_MENU_OPTIONS_FOR_HIGHLIGHT_TYPES.pocket + ); + assert.deepEqual( + highlights[2].contextMenuOptions, + SectionsManager.CONTEXT_MENU_OPTIONS_FOR_HIGHLIGHT_TYPES.history + ); + }); + it("should throw an error if you are assigning a context menu to a non-existant highlight type", () => { + globals.sandbox.spy(global.console, "error"); + SectionsManager.updateSection( + "highlights", + { rows: [{ url: "foo", type: "badtype" }] }, + false + ); + const highlights = SectionsManager.sections.get("highlights").rows; + assert.calledOnce(console.error); + assert.equal(highlights[0].contextMenuOptions, undefined); + }); + it("should filter out context menu options that are in CONTEXT_MENU_PREFS", () => { + const services = { + prefs: { + getBoolPref: o => + SectionsManager.CONTEXT_MENU_PREFS[o] !== "RemoveMe", + addObserver() {}, + removeObserver() {}, + }, + }; + globals.set("Services", services); + SectionsManager.CONTEXT_MENU_PREFS = { RemoveMe: "RemoveMe" }; + SectionsManager.CONTEXT_MENU_OPTIONS_FOR_HIGHLIGHT_TYPES = { + bookmark: ["KeepMe", "RemoveMe"], + pocket: ["KeepMe", "RemoveMe"], + history: ["KeepMe", "RemoveMe"], + }; + SectionsManager.updateSection("highlights", { rows: FAKE_ROWS }, false); + const highlights = SectionsManager.sections.get("highlights").FAKE_ROWS; + + // Only keep context menu options that were not supposed to be removed based on CONTEXT_MENU_PREFS + assert.deepEqual(highlights[0].contextMenuOptions, ["KeepMe"]); + assert.deepEqual(highlights[1].contextMenuOptions, ["KeepMe"]); + assert.deepEqual(highlights[2].contextMenuOptions, ["KeepMe"]); + SectionsManager.CONTEXT_MENU_OPTIONS_FOR_HIGHLIGHT_TYPES = + contextMenuOptionsOrig; + globals.restore(); + }); + }); + describe("#onceInitialized", () => { + it("should call the callback immediately if SectionsManager is initialised", () => { + SectionsManager.initialized = true; + const callback = sinon.spy(); + SectionsManager.onceInitialized(callback); + assert.calledOnce(callback); + }); + it("should bind the callback to .once(INIT) if SectionsManager is not initialised", () => { + SectionsManager.initialized = false; + sinon.spy(SectionsManager, "once"); + const callback = () => {}; + SectionsManager.onceInitialized(callback); + assert.calledOnce(SectionsManager.once); + assert.calledWith(SectionsManager.once, SectionsManager.INIT, callback); + }); + }); + describe("#updateSectionCard", () => { + it("should emit an UPDATE_SECTION_CARD event with correct arguments", () => { + SectionsManager.addSection( + FAKE_ID, + Object.assign({}, FAKE_OPTIONS, { rows: FAKE_ROWS }) + ); + const spy = sinon.spy(); + SectionsManager.on(SectionsManager.UPDATE_SECTION_CARD, spy); + SectionsManager.updateSectionCard( + FAKE_ID, + FAKE_URL, + FAKE_CARD_OPTIONS, + true + ); + assert.calledOnce(spy); + assert.calledWith( + spy, + SectionsManager.UPDATE_SECTION_CARD, + FAKE_ID, + FAKE_URL, + FAKE_CARD_OPTIONS, + true + ); + }); + it("should do nothing if the section doesn't exist", () => { + SectionsManager.removeSection(FAKE_ID); + const spy = sinon.spy(); + SectionsManager.on(SectionsManager.UPDATE_SECTION_CARD, spy); + SectionsManager.updateSectionCard( + FAKE_ID, + FAKE_URL, + FAKE_CARD_OPTIONS, + true + ); + assert.notCalled(spy); + }); + }); + describe("#removeSectionCard", () => { + it("should dispatch an SECTION_UPDATE action in which cards corresponding to the given url are removed", () => { + const rows = [{ url: "foo.com" }, { url: "bar.com" }]; + + SectionsManager.addSection( + FAKE_ID, + Object.assign({}, FAKE_OPTIONS, { rows }) + ); + const spy = sinon.spy(); + SectionsManager.on(SectionsManager.UPDATE_SECTION, spy); + SectionsManager.removeSectionCard(FAKE_ID, "foo.com"); + + assert.calledOnce(spy); + assert.equal(spy.firstCall.args[1], FAKE_ID); + assert.deepEqual(spy.firstCall.args[2].rows, [{ url: "bar.com" }]); + }); + it("should do nothing if the section doesn't exist", () => { + SectionsManager.removeSection(FAKE_ID); + const spy = sinon.spy(); + SectionsManager.on(SectionsManager.UPDATE_SECTION, spy); + SectionsManager.removeSectionCard(FAKE_ID, "bar.com"); + assert.notCalled(spy); + }); + }); + describe("#updateBookmarkMetadata", () => { + beforeEach(() => { + let rows = [ + { + url: "bar", + title: "title", + description: "description", + image: "image", + type: "trending", + }, + ]; + SectionsManager.addSection("topstories", { rows }); + // Simulate 2 sections. + rows = [ + { + url: "foo", + title: "title", + description: "description", + image: "image", + type: "bookmark", + }, + ]; + SectionsManager.addSection("highlights", { rows }); + }); + + it("shouldn't call PlacesUtils if URL is not in topstories", () => { + SectionsManager.updateBookmarkMetadata({ url: "foo" }); + + assert.notCalled(fakePlacesUtils.history.update); + }); + it("should call PlacesUtils.history.update", () => { + SectionsManager.updateBookmarkMetadata({ url: "bar" }); + + assert.calledOnce(fakePlacesUtils.history.update); + assert.calledWithExactly(fakePlacesUtils.history.update, { + url: "bar", + title: "title", + description: "description", + previewImageURL: "image", + }); + }); + it("should call PlacesUtils.history.insert", () => { + SectionsManager.updateBookmarkMetadata({ url: "bar" }); + + assert.calledOnce(fakePlacesUtils.history.insert); + assert.calledWithExactly(fakePlacesUtils.history.insert, { + url: "bar", + title: "title", + visits: [{}], + }); + }); + }); +}); + +describe("SectionsFeed", () => { + let feed; + let sandbox; + let storage; + let globals; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + SectionsManager.sections.clear(); + SectionsManager.initialized = false; + globals = new GlobalOverrider(); + globals.set("NimbusFeatures", { + newtab: { getAllVariables: sandbox.stub() }, + pocketNewtab: { getAllVariables: sandbox.stub() }, + }); + storage = { + get: sandbox.stub().resolves(), + set: sandbox.stub().resolves(), + }; + feed = new SectionsFeed(); + feed.store = { dispatch: sinon.spy() }; + feed.store = { + dispatch: sinon.spy(), + getState() { + return this.state; + }, + state: { + Prefs: { + values: { + sectionOrder: "topsites,topstories,highlights", + "feeds.topsites": true, + }, + }, + Sections: [{ initialized: false }], + }, + dbStorage: { getDbTable: sandbox.stub().returns(storage) }, + }; + }); + afterEach(() => { + feed.uninit(); + globals.restore(); + }); + describe("#init", () => { + it("should create a SectionsFeed", () => { + assert.instanceOf(feed, SectionsFeed); + }); + it("should bind appropriate listeners", () => { + sinon.spy(SectionsManager, "on"); + feed.init(); + assert.callCount(SectionsManager.on, 4); + for (const [event, listener] of [ + [SectionsManager.ADD_SECTION, feed.onAddSection], + [SectionsManager.REMOVE_SECTION, feed.onRemoveSection], + [SectionsManager.UPDATE_SECTION, feed.onUpdateSection], + [SectionsManager.UPDATE_SECTION_CARD, feed.onUpdateSectionCard], + ]) { + assert.calledWith(SectionsManager.on, event, listener); + } + }); + it("should call onAddSection for any already added sections in SectionsManager", async () => { + await SectionsManager.init({}, storage); + assert.ok(SectionsManager.sections.has("topstories")); + assert.ok(SectionsManager.sections.has("highlights")); + const topstories = SectionsManager.sections.get("topstories"); + const highlights = SectionsManager.sections.get("highlights"); + sinon.spy(feed, "onAddSection"); + feed.init(); + assert.calledTwice(feed.onAddSection); + assert.calledWith( + feed.onAddSection, + SectionsManager.ADD_SECTION, + "topstories", + topstories + ); + assert.calledWith( + feed.onAddSection, + SectionsManager.ADD_SECTION, + "highlights", + highlights + ); + }); + }); + describe("#uninit", () => { + it("should unbind all listeners", () => { + sinon.spy(SectionsManager, "off"); + feed.init(); + feed.uninit(); + assert.callCount(SectionsManager.off, 4); + for (const [event, listener] of [ + [SectionsManager.ADD_SECTION, feed.onAddSection], + [SectionsManager.REMOVE_SECTION, feed.onRemoveSection], + [SectionsManager.UPDATE_SECTION, feed.onUpdateSection], + [SectionsManager.UPDATE_SECTION_CARD, feed.onUpdateSectionCard], + ]) { + assert.calledWith(SectionsManager.off, event, listener); + } + }); + it("should emit an UNINIT event and set SectionsManager.initialized to false", () => { + const spy = sinon.spy(); + SectionsManager.on(SectionsManager.UNINIT, spy); + feed.init(); + feed.uninit(); + assert.calledOnce(spy); + assert.notOk(SectionsManager.initialized); + }); + }); + describe("#onAddSection", () => { + it("should broadcast a SECTION_REGISTER action with the correct data", () => { + feed.onAddSection(null, FAKE_ID, FAKE_OPTIONS); + const [action] = feed.store.dispatch.firstCall.args; + assert.equal(action.type, "SECTION_REGISTER"); + assert.deepEqual( + action.data, + Object.assign({ id: FAKE_ID }, FAKE_OPTIONS) + ); + assert.equal(action.meta.from, MAIN_MESSAGE_TYPE); + assert.equal(action.meta.to, CONTENT_MESSAGE_TYPE); + }); + it("should prepend id to sectionOrder pref if not already included", () => { + feed.store.state.Sections = [ + { id: "topstories", enabled: true }, + { id: "highlights", enabled: true }, + ]; + feed.onAddSection(null, FAKE_ID, FAKE_OPTIONS); + assert.calledWith(feed.store.dispatch, { + data: { + name: "sectionOrder", + value: `${FAKE_ID},topsites,topstories,highlights`, + }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: "SET_PREF", + }); + }); + }); + describe("#onRemoveSection", () => { + it("should broadcast a SECTION_DEREGISTER action with the correct data", () => { + feed.onRemoveSection(null, FAKE_ID); + const [action] = feed.store.dispatch.firstCall.args; + assert.equal(action.type, "SECTION_DEREGISTER"); + assert.deepEqual(action.data, FAKE_ID); + // Should be broadcast + assert.equal(action.meta.from, MAIN_MESSAGE_TYPE); + assert.equal(action.meta.to, CONTENT_MESSAGE_TYPE); + }); + }); + describe("#onUpdateSection", () => { + it("should do nothing if no options are provided", () => { + feed.onUpdateSection(null, FAKE_ID, null); + assert.notCalled(feed.store.dispatch); + }); + it("should dispatch a SECTION_UPDATE action with the correct data", () => { + feed.onUpdateSection(null, FAKE_ID, { rows: FAKE_ROWS }); + const [action] = feed.store.dispatch.firstCall.args; + assert.equal(action.type, "SECTION_UPDATE"); + assert.deepEqual(action.data, { id: FAKE_ID, rows: FAKE_ROWS }); + // Should be not broadcast by default, but should update the preloaded tab, so check meta + assert.equal(action.meta.from, MAIN_MESSAGE_TYPE); + assert.equal(action.meta.to, PRELOAD_MESSAGE_TYPE); + }); + it("should broadcast the action only if shouldBroadcast is true", () => { + feed.onUpdateSection(null, FAKE_ID, { rows: FAKE_ROWS }, true); + const [action] = feed.store.dispatch.firstCall.args; + // Should be broadcast + assert.equal(action.meta.from, MAIN_MESSAGE_TYPE); + assert.equal(action.meta.to, CONTENT_MESSAGE_TYPE); + }); + }); + describe("#onUpdateSectionCard", () => { + it("should do nothing if no options are provided", () => { + feed.onUpdateSectionCard(null, FAKE_ID, FAKE_URL, null); + assert.notCalled(feed.store.dispatch); + }); + it("should dispatch a SECTION_UPDATE_CARD action with the correct data", () => { + feed.onUpdateSectionCard(null, FAKE_ID, FAKE_URL, FAKE_CARD_OPTIONS); + const [action] = feed.store.dispatch.firstCall.args; + assert.equal(action.type, "SECTION_UPDATE_CARD"); + assert.deepEqual(action.data, { + id: FAKE_ID, + url: FAKE_URL, + options: FAKE_CARD_OPTIONS, + }); + // Should be not broadcast by default, but should update the preloaded tab, so check meta + assert.equal(action.meta.from, MAIN_MESSAGE_TYPE); + assert.equal(action.meta.to, PRELOAD_MESSAGE_TYPE); + }); + it("should broadcast the action only if shouldBroadcast is true", () => { + feed.onUpdateSectionCard( + null, + FAKE_ID, + FAKE_URL, + FAKE_CARD_OPTIONS, + true + ); + const [action] = feed.store.dispatch.firstCall.args; + // Should be broadcast + assert.equal(action.meta.from, MAIN_MESSAGE_TYPE); + assert.equal(action.meta.to, CONTENT_MESSAGE_TYPE); + }); + }); + describe("#onAction", () => { + it("should bind this.init to SectionsManager.INIT on INIT", () => { + sinon.spy(SectionsManager, "once"); + feed.onAction({ type: "INIT" }); + assert.calledOnce(SectionsManager.once); + assert.calledWith(SectionsManager.once, SectionsManager.INIT, feed.init); + }); + it("should call SectionsManager.init on action PREFS_INITIAL_VALUES", () => { + sinon.spy(SectionsManager, "init"); + feed.onAction({ type: "PREFS_INITIAL_VALUES", data: { foo: "bar" } }); + assert.calledOnce(SectionsManager.init); + assert.calledWith(SectionsManager.init, { foo: "bar" }); + assert.calledOnce(feed.store.dbStorage.getDbTable); + assert.calledWithExactly(feed.store.dbStorage.getDbTable, "sectionPrefs"); + }); + it("should call SectionsManager.addBuiltInSection on suitable PREF_CHANGED events", () => { + sinon.spy(SectionsManager, "addBuiltInSection"); + feed.onAction({ + type: "PREF_CHANGED", + data: { name: "feeds.section.topstories.options", value: "foo" }, + }); + assert.calledOnce(SectionsManager.addBuiltInSection); + assert.calledWith( + SectionsManager.addBuiltInSection, + "feeds.section.topstories", + "foo" + ); + }); + it("should fire SECTION_OPTIONS_UPDATED on suitable PREF_CHANGED events", async () => { + await feed.onAction({ + type: "PREF_CHANGED", + data: { name: "feeds.section.topstories.options", value: "foo" }, + }); + assert.calledOnce(feed.store.dispatch); + const [action] = feed.store.dispatch.firstCall.args; + assert.equal(action.type, "SECTION_OPTIONS_CHANGED"); + assert.equal(action.data, "topstories"); + }); + it("should call SectionsManager.disableSection on SECTION_DISABLE", () => { + sinon.spy(SectionsManager, "disableSection"); + feed.onAction({ type: "SECTION_DISABLE", data: 1234 }); + assert.calledOnce(SectionsManager.disableSection); + assert.calledWith(SectionsManager.disableSection, 1234); + SectionsManager.disableSection.restore(); + }); + it("should call SectionsManager.enableSection on SECTION_ENABLE", () => { + sinon.spy(SectionsManager, "enableSection"); + feed.onAction({ type: "SECTION_ENABLE", data: 1234 }); + assert.calledOnce(SectionsManager.enableSection); + assert.calledWith(SectionsManager.enableSection, 1234); + SectionsManager.enableSection.restore(); + }); + it("should call the feed's uninit on UNINIT", () => { + sinon.stub(feed, "uninit"); + + feed.onAction({ type: "UNINIT" }); + + assert.calledOnce(feed.uninit); + }); + it("should emit a ACTION_DISPATCHED event and forward any action in ACTIONS_TO_PROXY if there are any sections", () => { + const spy = sinon.spy(); + const allowedActions = SectionsManager.ACTIONS_TO_PROXY; + const disallowedActions = ["PREF_CHANGED", "OPEN_PRIVATE_WINDOW"]; + feed.init(); + SectionsManager.on(SectionsManager.ACTION_DISPATCHED, spy); + // Make sure we start with no sections - no event should be emitted + SectionsManager.sections.clear(); + feed.onAction({ type: allowedActions[0] }); + assert.notCalled(spy); + // Then add a section and check correct behaviour + SectionsManager.addSection(FAKE_ID, FAKE_OPTIONS); + for (const action of allowedActions.concat(disallowedActions)) { + feed.onAction({ type: action }); + } + for (const action of allowedActions) { + assert.calledWith(spy, "ACTION_DISPATCHED", action); + } + for (const action of disallowedActions) { + assert.neverCalledWith(spy, "ACTION_DISPATCHED", action); + } + }); + it("should call updateBookmarkMetadata on PLACES_BOOKMARK_ADDED", () => { + const stub = sinon.stub(SectionsManager, "updateBookmarkMetadata"); + + feed.onAction({ type: "PLACES_BOOKMARK_ADDED", data: {} }); + + assert.calledOnce(stub); + }); + it("should call updateSectionPrefs on UPDATE_SECTION_PREFS", () => { + const stub = sinon.stub(SectionsManager, "updateSectionPrefs"); + + feed.onAction({ type: "UPDATE_SECTION_PREFS", data: {} }); + + assert.calledOnce(stub); + }); + it("should call SectionManager.removeSectionCard on WEBEXT_DISMISS", () => { + const stub = sinon.stub(SectionsManager, "removeSectionCard"); + + feed.onAction( + ac.WebExtEvent(at.WEBEXT_DISMISS, { source: "Foo", url: "bar.com" }) + ); + + assert.calledOnce(stub); + assert.calledWith(stub, "Foo", "bar.com"); + }); + it("should call the feed's moveSection on SECTION_MOVE", () => { + sinon.stub(feed, "moveSection"); + const id = "topsites"; + const direction = +1; + feed.onAction({ type: "SECTION_MOVE", data: { id, direction } }); + + assert.calledOnce(feed.moveSection); + assert.calledWith(feed.moveSection, id, direction); + }); + }); + describe("#moveSection", () => { + it("should Move Down correctly", () => { + feed.store.state.Sections = [ + { id: "topstories", enabled: true }, + { id: "highlights", enabled: true }, + ]; + feed.moveSection("topsites", +1); + assert.calledOnce(feed.store.dispatch); + assert.calledWith(feed.store.dispatch, { + data: { name: "sectionOrder", value: "topstories,topsites,highlights" }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: "SET_PREF", + }); + feed.store.dispatch.resetHistory(); + feed.moveSection("topstories", +1); + assert.calledOnce(feed.store.dispatch); + assert.calledWith(feed.store.dispatch, { + data: { name: "sectionOrder", value: "topsites,highlights,topstories" }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: "SET_PREF", + }); + }); + it("should Move Up correctly", () => { + feed.store.state.Sections = [ + { id: "topstories", enabled: true }, + { id: "highlights", enabled: true }, + ]; + feed.moveSection("topstories", -1); + assert.calledOnce(feed.store.dispatch); + assert.calledWith(feed.store.dispatch, { + data: { name: "sectionOrder", value: "topstories,topsites,highlights" }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: "SET_PREF", + }); + feed.store.dispatch.resetHistory(); + feed.moveSection("highlights", -1); + assert.calledOnce(feed.store.dispatch); + assert.calledWith(feed.store.dispatch, { + data: { name: "sectionOrder", value: "topsites,highlights,topstories" }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: "SET_PREF", + }); + }); + it("should skip over sections that aren't enabled", () => { + feed.store.state.Sections = [ + { id: "topstories", enabled: false }, + { id: "highlights", enabled: true }, + ]; + feed.moveSection("highlights", -1); + assert.calledOnce(feed.store.dispatch); + assert.calledWith(feed.store.dispatch, { + data: { name: "sectionOrder", value: "highlights,topsites,topstories" }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: "SET_PREF", + }); + feed.store.dispatch.resetHistory(); + feed.moveSection("topsites", +1); + assert.calledOnce(feed.store.dispatch); + assert.calledWith(feed.store.dispatch, { + data: { name: "sectionOrder", value: "topstories,highlights,topsites" }, + meta: { from: "ActivityStream:Content", to: "ActivityStream:Main" }, + type: "SET_PREF", + }); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/ShortUrl.test.js b/browser/components/newtab/test/unit/lib/ShortUrl.test.js new file mode 100644 index 0000000000..201e5226fd --- /dev/null +++ b/browser/components/newtab/test/unit/lib/ShortUrl.test.js @@ -0,0 +1,104 @@ +import { GlobalOverrider } from "test/unit/utils"; +import { shortURL } from "lib/ShortURL.sys.mjs"; + +const puny = "xn--kpry57d"; +const idn = "台灣"; + +describe("shortURL", () => { + let globals; + let IDNStub; + let getPublicSuffixFromHostStub; + + beforeEach(() => { + IDNStub = sinon.stub().callsFake(host => host.replace(puny, idn)); + getPublicSuffixFromHostStub = sinon.stub().returns("com"); + + globals = new GlobalOverrider(); + globals.set("IDNService", { convertToDisplayIDN: IDNStub }); + globals.set("Services", { + eTLD: { getPublicSuffixFromHost: getPublicSuffixFromHostStub }, + }); + }); + + afterEach(() => { + globals.restore(); + }); + + it("should return a blank string if url is falsey", () => { + assert.equal(shortURL({ url: false }), ""); + assert.equal(shortURL({ url: "" }), ""); + assert.equal(shortURL({}), ""); + }); + + it("should return the 'url' if not a valid url", () => { + const checkInvalid = url => assert.equal(shortURL({ url }), url); + checkInvalid(true); + checkInvalid("something"); + checkInvalid("http:"); + checkInvalid("http::double"); + checkInvalid("http://badport:65536/"); + }); + + it("should remove the eTLD", () => { + assert.equal(shortURL({ url: "http://com.blah.com" }), "com.blah"); + }); + + it("should convert host to idn when calling shortURL", () => { + assert.equal(shortURL({ url: `http://${puny}.blah.com` }), `${idn}.blah`); + }); + + it("should get the hostname from .url", () => { + assert.equal(shortURL({ url: "http://bar.com" }), "bar"); + }); + + it("should not strip out www if not first subdomain", () => { + assert.equal(shortURL({ url: "http://foo.www.com" }), "foo.www"); + }); + + it("should convert to lowercase", () => { + assert.equal(shortURL({ url: "HTTP://FOO.COM" }), "foo"); + }); + + it("should not include the port", () => { + assert.equal(shortURL({ url: "http://foo.com:8888" }), "foo"); + }); + + it("should return hostname for localhost", () => { + getPublicSuffixFromHostStub.throws("insufficient domain levels"); + + assert.equal(shortURL({ url: "http://localhost:8000/" }), "localhost"); + }); + + it("should return hostname for ip address", () => { + getPublicSuffixFromHostStub.throws("host is ip address"); + + assert.equal(shortURL({ url: "http://127.0.0.1/foo" }), "127.0.0.1"); + }); + + it("should return etld for www.gov.uk (www-only non-etld)", () => { + getPublicSuffixFromHostStub.returns("gov.uk"); + + assert.equal( + shortURL({ url: "https://www.gov.uk/countersigning" }), + "gov.uk" + ); + }); + + it("should return idn etld for www-only non-etld", () => { + getPublicSuffixFromHostStub.returns(puny); + + assert.equal(shortURL({ url: `https://www.${puny}/foo` }), idn); + }); + + it("should return not the protocol for file:", () => { + assert.equal(shortURL({ url: "file:///foo/bar.txt" }), "/foo/bar.txt"); + }); + + it("should return not the protocol for about:", () => { + assert.equal(shortURL({ url: "about:newtab" }), "newtab"); + }); + + it("should fall back to full url as a last resort", () => { + assert.equal(shortURL({ url: "about:" }), "about:"); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/SiteClassifier.test.js b/browser/components/newtab/test/unit/lib/SiteClassifier.test.js new file mode 100644 index 0000000000..cd97707e9b --- /dev/null +++ b/browser/components/newtab/test/unit/lib/SiteClassifier.test.js @@ -0,0 +1,252 @@ +import { classifySite } from "lib/SiteClassifier.sys.mjs"; + +const FAKE_CLASSIFIER_DATA = [ + { + type: "hostname-and-params-match", + criteria: [ + { + hostname: "hostnameandparams.com", + params: [ + { + key: "param1", + value: "val1", + }, + ], + }, + ], + weight: 300, + }, + { + type: "url-match", + criteria: [{ url: "https://fullurl.com/must/match" }], + weight: 400, + }, + { + type: "params-match", + criteria: [ + { + params: [ + { + key: "param1", + value: "val1", + }, + { + key: "param2", + value: "val2", + }, + ], + }, + ], + weight: 200, + }, + { + type: "params-prefix-match", + criteria: [ + { + params: [ + { + key: "client", + prefix: "fir", + }, + ], + }, + ], + weight: 200, + }, + { + type: "has-params", + criteria: [ + { + params: [{ key: "has-param1" }, { key: "has-param2" }], + }, + ], + weight: 100, + }, + { + type: "search-engine", + criteria: [ + { sld: "google" }, + { hostname: "bing.com" }, + { hostname: "duckduckgo.com" }, + ], + weight: 1, + }, + { + type: "news-portal", + criteria: [ + { hostname: "yahoo.com" }, + { hostname: "aol.com" }, + { hostname: "msn.com" }, + ], + weight: 1, + }, + { + type: "social-media", + criteria: [{ hostname: "facebook.com" }, { hostname: "twitter.com" }], + weight: 1, + }, + { + type: "ecommerce", + criteria: [{ sld: "amazon" }, { hostname: "ebay.com" }], + weight: 1, + }, +]; + +describe("SiteClassifier", () => { + function RemoteSettings() { + return { + get() { + return Promise.resolve(FAKE_CLASSIFIER_DATA); + }, + }; + } + + it("should return the right category", async () => { + assert.equal( + "hostname-and-params-match", + await classifySite( + "https://hostnameandparams.com?param1=val1", + RemoteSettings + ) + ); + assert.equal( + "other", + await classifySite( + "https://hostnameandparams.com?param1=val", + RemoteSettings + ) + ); + assert.equal( + "other", + await classifySite( + "https://hostnameandparams.com?param=val1", + RemoteSettings + ) + ); + assert.equal( + "other", + await classifySite("https://hostnameandparams.com", RemoteSettings) + ); + assert.equal( + "other", + await classifySite("https://params.com?param1=val1", RemoteSettings) + ); + + assert.equal( + "url-match", + await classifySite("https://fullurl.com/must/match", RemoteSettings) + ); + assert.equal( + "other", + await classifySite("http://fullurl.com/must/match", RemoteSettings) + ); + + assert.equal( + "params-match", + await classifySite( + "https://example.com?param1=val1¶m2=val2", + RemoteSettings + ) + ); + assert.equal( + "params-match", + await classifySite( + "https://example.com?param1=val1¶m2=val2&other=other", + RemoteSettings + ) + ); + assert.equal( + "other", + await classifySite( + "https://example.com?param1=val2¶m2=val1", + RemoteSettings + ) + ); + assert.equal( + "other", + await classifySite("https://example.com?param1¶m2", RemoteSettings) + ); + + assert.equal( + "params-prefix-match", + await classifySite("https://search.com?client=firefox", RemoteSettings) + ); + assert.equal( + "params-prefix-match", + await classifySite("https://search.com?client=fir", RemoteSettings) + ); + assert.equal( + "other", + await classifySite( + "https://search.com?client=mozillafirefox", + RemoteSettings + ) + ); + + assert.equal( + "has-params", + await classifySite( + "https://example.com?has-param1=val1&has-param2=val2", + RemoteSettings + ) + ); + assert.equal( + "has-params", + await classifySite( + "https://example.com?has-param1&has-param2", + RemoteSettings + ) + ); + assert.equal( + "has-params", + await classifySite( + "https://example.com?has-param1&has-param2&other=other", + RemoteSettings + ) + ); + assert.equal( + "other", + await classifySite("https://example.com?has-param1", RemoteSettings) + ); + assert.equal( + "other", + await classifySite("https://example.com?has-param2", RemoteSettings) + ); + + assert.equal( + "search-engine", + await classifySite("https://google.com", RemoteSettings) + ); + assert.equal( + "search-engine", + await classifySite("https://google.de", RemoteSettings) + ); + assert.equal( + "search-engine", + await classifySite("http://bing.com/?q=firefox", RemoteSettings) + ); + + assert.equal( + "news-portal", + await classifySite("https://yahoo.com", RemoteSettings) + ); + + assert.equal( + "social-media", + await classifySite("http://twitter.com/firefox", RemoteSettings) + ); + + assert.equal( + "ecommerce", + await classifySite("https://amazon.com", RemoteSettings) + ); + assert.equal( + "ecommerce", + await classifySite("https://amazon.ca", RemoteSettings) + ); + assert.equal( + "ecommerce", + await classifySite("https://ebay.com", RemoteSettings) + ); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/SystemTickFeed.test.js b/browser/components/newtab/test/unit/lib/SystemTickFeed.test.js new file mode 100644 index 0000000000..a0789b182e --- /dev/null +++ b/browser/components/newtab/test/unit/lib/SystemTickFeed.test.js @@ -0,0 +1,79 @@ +import { + SYSTEM_TICK_INTERVAL, + SystemTickFeed, +} from "lib/SystemTickFeed.sys.mjs"; +import { actionTypes as at } from "common/Actions.sys.mjs"; +import { GlobalOverrider } from "test/unit/utils"; + +describe("System Tick Feed", () => { + let globals; + let instance; + let clock; + + beforeEach(() => { + globals = new GlobalOverrider(); + clock = sinon.useFakeTimers(); + + instance = new SystemTickFeed(); + instance.store = { + getState() { + return {}; + }, + dispatch() {}, + }; + }); + afterEach(() => { + globals.restore(); + clock.restore(); + }); + it("should create a SystemTickFeed", () => { + assert.instanceOf(instance, SystemTickFeed); + }); + it("should fire SYSTEM_TICK events at configured interval", () => { + globals.set("ChromeUtils", { + idleDispatch: f => f(), + }); + let expectation = sinon + .mock(instance.store) + .expects("dispatch") + .twice() + .withExactArgs({ type: at.SYSTEM_TICK }); + + instance.onAction({ type: at.INIT }); + clock.tick(SYSTEM_TICK_INTERVAL * 2); + expectation.verify(); + }); + it("should not fire SYSTEM_TICK events after UNINIT", () => { + let expectation = sinon.mock(instance.store).expects("dispatch").never(); + + instance.onAction({ type: at.UNINIT }); + clock.tick(SYSTEM_TICK_INTERVAL * 2); + expectation.verify(); + }); + it("should not fire SYSTEM_TICK events while the user is away", () => { + let expectation = sinon.mock(instance.store).expects("dispatch").never(); + + instance.onAction({ type: at.INIT }); + instance._idleService = { idleTime: SYSTEM_TICK_INTERVAL + 1 }; + clock.tick(SYSTEM_TICK_INTERVAL * 3); + expectation.verify(); + instance.onAction({ type: at.UNINIT }); + }); + it("should fire SYSTEM_TICK immediately when the user is active again", () => { + globals.set("ChromeUtils", { + idleDispatch: f => f(), + }); + let expectation = sinon + .mock(instance.store) + .expects("dispatch") + .once() + .withExactArgs({ type: at.SYSTEM_TICK }); + + instance.onAction({ type: at.INIT }); + instance._idleService = { idleTime: SYSTEM_TICK_INTERVAL + 1 }; + clock.tick(SYSTEM_TICK_INTERVAL * 3); + instance.observe(); + expectation.verify(); + instance.onAction({ type: at.UNINIT }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/TippyTopProvider.test.js b/browser/components/newtab/test/unit/lib/TippyTopProvider.test.js new file mode 100644 index 0000000000..661a6b7b83 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/TippyTopProvider.test.js @@ -0,0 +1,121 @@ +import { GlobalOverrider } from "test/unit/utils"; +import { TippyTopProvider } from "lib/TippyTopProvider.sys.mjs"; + +describe("TippyTopProvider", () => { + let instance; + let globals; + beforeEach(async () => { + globals = new GlobalOverrider(); + let fetchStub = globals.sandbox.stub(); + globals.set("fetch", fetchStub); + fetchStub.resolves({ + ok: true, + status: 200, + json: () => + Promise.resolve([ + { + domains: ["facebook.com"], + image_url: "images/facebook-com.png", + favicon_url: "images/facebook-com.png", + background_color: "#3b5998", + }, + { + domains: ["gmail.com", "mail.google.com"], + image_url: "images/gmail-com.png", + favicon_url: "images/gmail-com.png", + background_color: "#000000", + }, + ]), + }); + instance = new TippyTopProvider(); + await instance.init(); + }); + it("should provide an icon for facebook.com", () => { + const site = instance.processSite({ url: "https://facebook.com" }); + assert.equal( + site.tippyTopIcon, + "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png" + ); + assert.equal( + site.smallFavicon, + "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png" + ); + assert.equal(site.backgroundColor, "#3b5998"); + }); + it("should provide an icon for www.facebook.com", () => { + const site = instance.processSite({ url: "https://www.facebook.com" }); + assert.equal( + site.tippyTopIcon, + "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png" + ); + assert.equal( + site.smallFavicon, + "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png" + ); + assert.equal(site.backgroundColor, "#3b5998"); + }); + it("should not provide an icon for other.facebook.com", () => { + const site = instance.processSite({ url: "https://other.facebook.com" }); + assert.isUndefined(site.tippyTopIcon); + }); + it("should provide an icon for other.facebook.com with stripping", () => { + const site = instance.processSite( + { url: "https://other.facebook.com" }, + "*" + ); + assert.equal( + site.tippyTopIcon, + "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png" + ); + }); + it("should provide an icon for facebook.com/foobar", () => { + const site = instance.processSite({ url: "https://facebook.com/foobar" }); + assert.equal( + site.tippyTopIcon, + "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png" + ); + assert.equal( + site.smallFavicon, + "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png" + ); + assert.equal(site.backgroundColor, "#3b5998"); + }); + it("should provide an icon for gmail.com", () => { + const site = instance.processSite({ url: "https://gmail.com" }); + assert.equal( + site.tippyTopIcon, + "chrome://activity-stream/content/data/content/tippytop/images/gmail-com.png" + ); + assert.equal( + site.smallFavicon, + "chrome://activity-stream/content/data/content/tippytop/images/gmail-com.png" + ); + assert.equal(site.backgroundColor, "#000000"); + }); + it("should provide an icon for mail.google.com", () => { + const site = instance.processSite({ url: "https://mail.google.com" }); + assert.equal( + site.tippyTopIcon, + "chrome://activity-stream/content/data/content/tippytop/images/gmail-com.png" + ); + assert.equal( + site.smallFavicon, + "chrome://activity-stream/content/data/content/tippytop/images/gmail-com.png" + ); + assert.equal(site.backgroundColor, "#000000"); + }); + it("should handle garbage URLs gracefully", () => { + const site = instance.processSite({ url: "garbagejlfkdsa" }); + assert.isUndefined(site.tippyTopIcon); + assert.isUndefined(site.backgroundColor); + }); + it("should handle error when fetching and parsing manifest", async () => { + globals = new GlobalOverrider(); + let fetchStub = globals.sandbox.stub(); + globals.set("fetch", fetchStub); + fetchStub.rejects("whaaaa"); + instance = new TippyTopProvider(); + await instance.init(); + instance.processSite({ url: "https://facebook.com" }); + }); +}); diff --git a/browser/components/newtab/test/unit/lib/UTEventReporting.test.js b/browser/components/newtab/test/unit/lib/UTEventReporting.test.js new file mode 100644 index 0000000000..6255568438 --- /dev/null +++ b/browser/components/newtab/test/unit/lib/UTEventReporting.test.js @@ -0,0 +1,115 @@ +import { UTSessionPing, UTUserEventPing } from "test/schemas/pings"; +import { GlobalOverrider } from "test/unit/utils"; +import { UTEventReporting } from "lib/UTEventReporting.sys.mjs"; + +const FAKE_EVENT_PING_PC = { + event: "CLICK", + source: "TOP_SITES", + addon_version: "123", + user_prefs: 63, + session_id: "abc", + page: "about:newtab", + action_position: 5, + locale: "en-US", +}; +const FAKE_SESSION_PING_PC = { + session_duration: 1234, + addon_version: "123", + user_prefs: 63, + session_id: "abc", + page: "about:newtab", + locale: "en-US", +}; +const FAKE_EVENT_PING_UT = [ + "activity_stream", + "event", + "CLICK", + "TOP_SITES", + { + addon_version: "123", + user_prefs: "63", + session_id: "abc", + page: "about:newtab", + action_position: "5", + }, +]; +const FAKE_SESSION_PING_UT = [ + "activity_stream", + "end", + "session", + "1234", + { + addon_version: "123", + user_prefs: "63", + session_id: "abc", + page: "about:newtab", + }, +]; + +describe("UTEventReporting", () => { + let globals; + let sandbox; + let utEvents; + + beforeEach(() => { + globals = new GlobalOverrider(); + sandbox = globals.sandbox; + sandbox.stub(global.Services.telemetry, "setEventRecordingEnabled"); + sandbox.stub(global.Services.telemetry, "recordEvent"); + + utEvents = new UTEventReporting(); + }); + + afterEach(() => { + globals.restore(); + }); + + describe("#sendUserEvent()", () => { + it("should queue up the correct data to send to Events Telemetry", async () => { + utEvents.sendUserEvent(FAKE_EVENT_PING_PC); + assert.calledWithExactly( + global.Services.telemetry.recordEvent, + ...FAKE_EVENT_PING_UT + ); + + let ping = global.Services.telemetry.recordEvent.firstCall.args; + assert.validate(ping, UTUserEventPing); + }); + }); + + describe("#sendSessionEndEvent()", () => { + it("should queue up the correct data to send to Events Telemetry", async () => { + utEvents.sendSessionEndEvent(FAKE_SESSION_PING_PC); + assert.calledWithExactly( + global.Services.telemetry.recordEvent, + ...FAKE_SESSION_PING_UT + ); + + let ping = global.Services.telemetry.recordEvent.firstCall.args; + assert.validate(ping, UTSessionPing); + }); + }); + + describe("#uninit()", () => { + it("should call setEventRecordingEnabled with a false value", () => { + assert.equal( + global.Services.telemetry.setEventRecordingEnabled.firstCall.args[0], + "activity_stream" + ); + assert.equal( + global.Services.telemetry.setEventRecordingEnabled.firstCall.args[1], + true + ); + + utEvents.uninit(); + assert.equal( + global.Services.telemetry.setEventRecordingEnabled.secondCall.args[0], + "activity_stream" + ); + assert.equal( + global.Services.telemetry.setEventRecordingEnabled.secondCall.args[1], + false + ); + }); + }); +}); diff --git a/browser/components/newtab/test/unit/unit-entry.js b/browser/components/newtab/test/unit/unit-entry.js new file mode 100644 index 0000000000..5b32269ca8 --- /dev/null +++ b/browser/components/newtab/test/unit/unit-entry.js @@ -0,0 +1,733 @@ +import { + EventEmitter, + FakePrefs, + FakensIPrefService, + GlobalOverrider, + FakeConsoleAPI, + FakeLogger, +} from "test/unit/utils"; +import Adapter from "enzyme-adapter-react-16"; +import { chaiAssertions } from "test/schemas/pings"; +import enzyme from "enzyme"; + +enzyme.configure({ adapter: new Adapter() }); + +// Cause React warnings to make tests that trigger them fail +const origConsoleError = console.error; +console.error = function (msg, ...args) { + origConsoleError.apply(console, [msg, ...args]); + + if ( + /(Invalid prop|Failed prop type|Check the render method|React Intl)/.test( + msg + ) + ) { + throw new Error(msg); + } +}; + +const req = require.context(".", true, /\.test\.jsx?$/); +const files = req.keys(); + +// This exposes sinon assertions to chai.assert +sinon.assert.expose(assert, { prefix: "" }); + +chai.use(chaiAssertions); + +const overrider = new GlobalOverrider(); + +const RemoteSettings = name => ({ + get: () => { + if (name === "attachment") { + return Promise.resolve([{ attachment: {} }]); + } + return Promise.resolve([]); + }, + on: () => {}, + off: () => {}, +}); +RemoteSettings.pollChanges = () => {}; + +class JSWindowActorParent { + sendAsyncMessage(name, data) { + return { name, data }; + } +} + +class JSWindowActorChild { + sendAsyncMessage(name, data) { + return { name, data }; + } + + sendQuery(name, data) { + return Promise.resolve({ name, data }); + } + + get contentWindow() { + return { + Promise, + }; + } +} + +// Detect plain object passed to lazy getter APIs, and set its prototype to +// global object, and return the global object for further modification. +// Returns the object if it's not plain object. +// +// This is a workaround to make the existing testharness and testcase keep +// working even after lazy getters are moved to plain `lazy` object. +const cachedPlainObject = new Set(); +function updateGlobalOrObject(object) { + // Given this function modifies the prototype, and the following + // condition doesn't meet on the second call, cache the result. + if (cachedPlainObject.has(object)) { + return global; + } + + if (Object.getPrototypeOf(object).constructor.name !== "Object") { + return object; + } + + cachedPlainObject.add(object); + Object.setPrototypeOf(object, global); + return global; +} + +const TEST_GLOBAL = { + JSWindowActorParent, + JSWindowActorChild, + AboutReaderParent: { + addMessageListener: (messageName, listener) => {}, + removeMessageListener: (messageName, listener) => {}, + }, + AboutWelcomeTelemetry: class { + submitGleanPingForPing() {} + }, + AddonManager: { + getActiveAddons() { + return Promise.resolve({ addons: [], fullData: false }); + }, + }, + AppConstants: { + MOZILLA_OFFICIAL: true, + MOZ_APP_VERSION: "69.0a1", + isChinaRepack() { + return false; + }, + isPlatformAndVersionAtMost() { + return false; + }, + platform: "win", + }, + ASRouterPreferences: { + console: new FakeConsoleAPI({ + maxLogLevel: "off", // set this to "debug" or "all" to get more ASRouter logging in tests + prefix: "ASRouter", + }), + }, + AWScreenUtils: { + evaluateTargetingAndRemoveScreens() { + return true; + }, + async removeScreens() { + return true; + }, + evaluateScreenTargeting() { + return true; + }, + }, + BrowserUtils: { + sendToDeviceEmailsSupported() { + return true; + }, + }, + UpdateUtils: { getUpdateChannel() {} }, + BasePromiseWorker: class { + constructor() { + this.ExceptionHandlers = []; + } + post() {} + }, + browserSearchRegion: "US", + BrowserWindowTracker: { getTopWindow() {} }, + ChromeUtils: { + defineLazyGetter(object, name, f) { + updateGlobalOrObject(object)[name] = f(); + }, + defineModuleGetter: updateGlobalOrObject, + defineESModuleGetters: updateGlobalOrObject, + generateQI() { + return {}; + }, + import() { + return global; + }, + importESModule() { + return global; + }, + }, + ClientEnvironment: { + get userId() { + return "foo123"; + }, + }, + Components: { + Constructor(classId) { + switch (classId) { + case "@mozilla.org/referrer-info;1": + return function (referrerPolicy, sendReferrer, originalReferrer) { + this.referrerPolicy = referrerPolicy; + this.sendReferrer = sendReferrer; + this.originalReferrer = originalReferrer; + }; + } + return function () {}; + }, + isSuccessCode: () => true, + }, + ConsoleAPI: FakeConsoleAPI, + // NB: These are functions/constructors + // eslint-disable-next-line object-shorthand + ContentSearchUIController: function () {}, + // eslint-disable-next-line object-shorthand + ContentSearchHandoffUIController: function () {}, + Cc: { + "@mozilla.org/browser/nav-bookmarks-service;1": { + addObserver() {}, + getService() { + return this; + }, + removeObserver() {}, + SOURCES: {}, + TYPE_BOOKMARK: {}, + }, + "@mozilla.org/browser/nav-history-service;1": { + addObserver() {}, + executeQuery() {}, + getNewQuery() {}, + getNewQueryOptions() {}, + getService() { + return this; + }, + insert() {}, + markPageAsTyped() {}, + removeObserver() {}, + }, + "@mozilla.org/io/string-input-stream;1": { + createInstance() { + return {}; + }, + }, + "@mozilla.org/security/hash;1": { + createInstance() { + return { + init() {}, + updateFromStream() {}, + finish() { + return "0"; + }, + }; + }, + }, + "@mozilla.org/updates/update-checker;1": { createInstance() {} }, + "@mozilla.org/widget/useridleservice;1": { + getService() { + return { + idleTime: 0, + addIdleObserver() {}, + removeIdleObserver() {}, + }; + }, + }, + "@mozilla.org/streamConverters;1": { + getService() { + return this; + }, + }, + "@mozilla.org/network/stream-loader;1": { + createInstance() { + return {}; + }, + }, + }, + Ci: { + nsICryptoHash: {}, + nsIReferrerInfo: { UNSAFE_URL: 5 }, + nsITimer: { TYPE_ONE_SHOT: 1 }, + nsIWebProgressListener: { LOCATION_CHANGE_SAME_DOCUMENT: 1 }, + nsIDOMWindow: Object, + nsITrackingDBService: { + TRACKERS_ID: 1, + TRACKING_COOKIES_ID: 2, + CRYPTOMINERS_ID: 3, + FINGERPRINTERS_ID: 4, + SOCIAL_ID: 5, + }, + nsICookieBannerService: { + MODE_DISABLED: 0, + MODE_REJECT: 1, + MODE_REJECT_OR_ACCEPT: 2, + MODE_UNSET: 3, + }, + }, + Cu: { + importGlobalProperties() {}, + now: () => window.performance.now(), + cloneInto: o => JSON.parse(JSON.stringify(o)), + }, + console: { + ...console, + error() {}, + }, + dump() {}, + EveryWindow: { + registerCallback: (id, init, uninit) => {}, + unregisterCallback: id => {}, + }, + setTimeout: window.setTimeout.bind(window), + clearTimeout: window.clearTimeout.bind(window), + fetch() {}, + // eslint-disable-next-line object-shorthand + Image: function () {}, // NB: This is a function/constructor + IOUtils: { + writeJSON() { + return Promise.resolve(0); + }, + readJSON() { + return Promise.resolve({}); + }, + read() { + return Promise.resolve(new Uint8Array()); + }, + makeDirectory() { + return Promise.resolve(0); + }, + write() { + return Promise.resolve(0); + }, + exists() { + return Promise.resolve(0); + }, + remove() { + return Promise.resolve(0); + }, + stat() { + return Promise.resolve(0); + }, + }, + NewTabUtils: { + activityStreamProvider: { + getTopFrecentSites: () => [], + executePlacesQuery: async (sql, options) => ({ sql, options }), + }, + }, + OS: { + File: { + writeAtomic() {}, + makeDir() {}, + stat() {}, + Error: {}, + read() {}, + exists() {}, + remove() {}, + removeEmptyDir() {}, + }, + Path: { + join() { + return "/"; + }, + }, + Constants: { + Path: { + localProfileDir: "/", + }, + }, + }, + PathUtils: { + join(...parts) { + return parts[parts.length - 1]; + }, + joinRelative(...parts) { + return parts[parts.length - 1]; + }, + getProfileDir() { + return Promise.resolve("/"); + }, + getLocalProfileDir() { + return Promise.resolve("/"); + }, + }, + PlacesUtils: { + get bookmarks() { + return TEST_GLOBAL.Cc["@mozilla.org/browser/nav-bookmarks-service;1"]; + }, + get history() { + return TEST_GLOBAL.Cc["@mozilla.org/browser/nav-history-service;1"]; + }, + observers: { + addListener() {}, + removeListener() {}, + }, + }, + Preferences: FakePrefs, + PrivateBrowsingUtils: { + isBrowserPrivate: () => false, + isWindowPrivate: () => false, + permanentPrivateBrowsing: false, + }, + DownloadsViewUI: { + getDisplayName: () => "filename.ext", + getSizeWithUnits: () => "1.5 MB", + }, + FileUtils: { + // eslint-disable-next-line object-shorthand + File: function () {}, // NB: This is a function/constructor + }, + Region: { + home: "US", + REGION_TOPIC: "browser-region-updated", + }, + Services: { + dirsvc: { + get: () => ({ parent: { parent: { path: "appPath" } } }), + }, + env: { + set: () => undefined, + }, + locale: { + get appLocaleAsBCP47() { + return "en-US"; + }, + negotiateLanguages() {}, + }, + urlFormatter: { formatURL: str => str, formatURLPref: str => str }, + mm: { + addMessageListener: (msg, cb) => this.receiveMessage(), + removeMessageListener() {}, + }, + obs: { + addObserver() {}, + removeObserver() {}, + notifyObservers() {}, + }, + telemetry: { + setEventRecordingEnabled: () => {}, + recordEvent: eventDetails => {}, + scalarSet: () => {}, + keyedScalarAdd: () => {}, + }, + uuid: { + generateUUID() { + return "{foo-123-foo}"; + }, + }, + console: { logStringMessage: () => {} }, + prefs: new FakensIPrefService(), + tm: { + dispatchToMainThread: cb => cb(), + idleDispatchToMainThread: cb => cb(), + }, + eTLD: { + getBaseDomain({ spec }) { + return spec.match(/\/([^/]+)/)[1]; + }, + getBaseDomainFromHost(host) { + return host.match(/.*?(\w+\.\w+)$/)[1]; + }, + getPublicSuffix() {}, + }, + io: { + newURI: spec => ({ + mutate: () => ({ + setRef: ref => ({ + finalize: () => ({ + ref, + spec, + }), + }), + }), + spec, + }), + }, + search: { + init() { + return Promise.resolve(); + }, + getVisibleEngines: () => + Promise.resolve([{ identifier: "google" }, { identifier: "bing" }]), + defaultEngine: { + identifier: "google", + searchForm: + "https://www.google.com/search?q=&ie=utf-8&oe=utf-8&client=firefox-b", + aliases: ["@google"], + }, + defaultPrivateEngine: { + identifier: "bing", + searchForm: "https://www.bing.com", + aliases: ["@bing"], + }, + getEngineByAlias: async () => null, + }, + scriptSecurityManager: { + createNullPrincipal() {}, + getSystemPrincipal() {}, + }, + wm: { + getMostRecentWindow: () => window, + getMostRecentBrowserWindow: () => window, + getEnumerator: () => [], + }, + ww: { registerNotification() {}, unregisterNotification() {} }, + appinfo: { appBuildID: "20180710100040", version: "69.0a1" }, + scriptloader: { loadSubScript: () => {} }, + startup: { + getStartupInfo() { + return { + process: { + getTime() { + return 1588010448000; + }, + }, + }; + }, + }, + }, + XPCOMUtils: { + defineLazyGlobalGetters: updateGlobalOrObject, + defineLazyModuleGetters: updateGlobalOrObject, + defineLazyServiceGetter: updateGlobalOrObject, + defineLazyServiceGetters: updateGlobalOrObject, + defineLazyPreferenceGetter(object, name) { + updateGlobalOrObject(object)[name] = ""; + }, + generateQI() { + return {}; + }, + }, + EventEmitter, + ShellService: { + doesAppNeedPin: () => false, + isDefaultBrowser: () => true, + }, + FilterExpressions: { + eval() { + return Promise.resolve(false); + }, + }, + RemoteSettings, + Localization: class { + async formatMessages(stringsIds) { + return Promise.resolve( + stringsIds.map(({ id, args }) => ({ value: { string_id: id, args } })) + ); + } + async formatValue(stringId) { + return Promise.resolve(stringId); + } + }, + FxAccountsConfig: { + promiseConnectAccountURI(id) { + return Promise.resolve(id); + }, + }, + FX_MONITOR_OAUTH_CLIENT_ID: "fake_client_id", + ExperimentAPI: { + getExperiment() {}, + getExperimentMetaData() {}, + getRolloutMetaData() {}, + }, + NimbusFeatures: { + glean: { + getVariable() {}, + }, + newtab: { + getVariable() {}, + getAllVariables() {}, + onUpdate() {}, + offUpdate() {}, + }, + pocketNewtab: { + getVariable() {}, + getAllVariables() {}, + onUpdate() {}, + offUpdate() {}, + }, + cookieBannerHandling: { + getVariable() {}, + }, + }, + TelemetryEnvironment: { + setExperimentActive() {}, + currentEnvironment: { + profile: { + creationDate: 16587, + }, + settings: {}, + }, + }, + TelemetryStopwatch: { + start: () => {}, + finish: () => {}, + }, + Sampling: { + ratioSample(seed, ratios) { + return Promise.resolve(0); + }, + }, + BrowserHandler: { + get kiosk() { + return false; + }, + }, + TelemetrySession: { + getMetadata(reason) { + return { + reason, + sessionId: "fake_session_id", + }; + }, + }, + PageThumbs: { + addExpirationFilter() {}, + removeExpirationFilter() {}, + }, + Logger: FakeLogger, + getFxAccountsSingleton() {}, + AboutNewTab: {}, + Glean: { + newtab: { + opened: { + record() {}, + }, + closed: { + record() {}, + }, + locale: { + set() {}, + }, + newtabCategory: { + set() {}, + }, + homepageCategory: { + set() {}, + }, + blockedSponsors: { + set() {}, + }, + sovAllocation: { + set() {}, + }, + }, + newtabSearch: { + enabled: { + set() {}, + }, + }, + newtabHandoffPreference: { + enabled: { + set() {}, + }, + }, + pocket: { + enabled: { + set() {}, + }, + impression: { + record() {}, + }, + isSignedIn: { + set() {}, + }, + sponsoredStoriesEnabled: { + set() {}, + }, + click: { + record() {}, + }, + save: { + record() {}, + }, + topicClick: { + record() {}, + }, + shim: { + set() {}, + }, + }, + topsites: { + enabled: { + set() {}, + }, + sponsoredEnabled: { + set() {}, + }, + impression: { + record() {}, + }, + click: { + record() {}, + }, + rows: { + set() {}, + }, + showPrivacyClick: { + record() {}, + }, + dismiss: { + record() {}, + }, + prefChanged: { + record() {}, + }, + sponsoredTilesConfigured: { + set() {}, + }, + sponsoredTilesReceived: { + set() {}, + }, + }, + topSites: { + pingType: { + set() {}, + }, + position: { + set() {}, + }, + source: { + set() {}, + }, + tileId: { + set() {}, + }, + reportingUrl: { + set() {}, + }, + advertiser: { + set() {}, + }, + contextId: { + set() {}, + }, + }, + }, + GleanPings: { + newtab: { + submit() {}, + }, + topSites: { + submit() {}, + }, + spoc: { + submit() {}, + }, + }, + Utils: { + SERVER_URL: "bogus://foo", + }, +}; +overrider.set(TEST_GLOBAL); + +describe("activity-stream", () => { + after(() => overrider.restore()); + files.forEach(file => req(file)); +}); diff --git a/browser/components/newtab/test/unit/utils.js b/browser/components/newtab/test/unit/utils.js new file mode 100644 index 0000000000..22069b8635 --- /dev/null +++ b/browser/components/newtab/test/unit/utils.js @@ -0,0 +1,406 @@ +/** + * GlobalOverrider - Utility that allows you to override properties on the global object. + * See unit-entry.js for example usage. + */ +export class GlobalOverrider { + constructor() { + this.originalGlobals = new Map(); + this.sandbox = sinon.createSandbox(); + } + + /** + * _override - Internal method to override properties on the global object. + * The first time a given key is overridden, we cache the original + * value in this.originalGlobals so that later it can be restored. + * + * @param {string} key The identifier of the property + * @param {any} value The value to which the property should be reassigned + */ + _override(key, value) { + if (!this.originalGlobals.has(key)) { + this.originalGlobals.set(key, global[key]); + } + global[key] = value; + } + + /** + * set - Override a given property, or all properties on an object + * + * @param {string|object} key If a string, the identifier of the property + * If an object, a number of properties and values to which they should be reassigned. + * @param {any} value The value to which the property should be reassigned + * @return {type} description + */ + set(key, value) { + if (!value && typeof key === "object") { + const overrides = key; + Object.keys(overrides).forEach(k => this._override(k, overrides[k])); + } else { + this._override(key, value); + } + return value; + } + + /** + * reset - Reset the global sandbox, so all state on spies, stubs etc. is cleared. + * You probably want to call this after each test. + */ + reset() { + this.sandbox.reset(); + } + + /** + * restore - Restore the global sandbox and reset all overriden properties to + * their original values. You should call this after all tests have completed. + */ + restore() { + this.sandbox.restore(); + this.originalGlobals.forEach((value, key) => { + global[key] = value; + }); + } +} + +/** + * A map of mocked preference names and values, used by `FakensIPrefBranch`, + * `FakensIPrefService`, and `FakePrefs`. + * + * Tests should add entries to this map for any preferences they'd like to set, + * and remove any entries during teardown for preferences that shouldn't be + * shared between tests. + */ +export const FAKE_GLOBAL_PREFS = new Map(); + +/** + * Very simple fake for the most basic semantics of nsIPrefBranch. Lots of + * things aren't yet supported. Feel free to add them in. + * + * @param {Object} args - optional arguments + * @param {Function} args.initHook - if present, will be called back + * inside the constructor. Typically used from tests + * to save off a pointer to the created instance so that + * stubs and spies can be inspected by the test code. + */ +export class FakensIPrefBranch { + PREF_INVALID = "invalid"; + PREF_INT = "integer"; + PREF_BOOL = "boolean"; + PREF_STRING = "string"; + + constructor(args) { + if (args) { + if ("initHook" in args) { + args.initHook.call(this); + } + if (args.defaultBranch) { + this.prefs = new Map(); + } else { + this.prefs = FAKE_GLOBAL_PREFS; + } + } else { + this.prefs = FAKE_GLOBAL_PREFS; + } + this._prefBranch = {}; + this.observers = new Map(); + } + addObserver(prefix, callback) { + this.observers.set(prefix, callback); + } + removeObserver(prefix, callback) { + this.observers.delete(prefix, callback); + } + setStringPref(prefName, value) { + this.set(prefName, value); + } + getStringPref(prefName, defaultValue) { + return this.get(prefName, defaultValue); + } + setBoolPref(prefName, value) { + this.set(prefName, value); + } + getBoolPref(prefName) { + return this.get(prefName); + } + setIntPref(prefName, value) { + this.set(prefName, value); + } + getIntPref(prefName) { + return this.get(prefName); + } + setCharPref(prefName, value) { + this.set(prefName, value); + } + getCharPref(prefName) { + return this.get(prefName); + } + clearUserPref(prefName) { + this.prefs.delete(prefName); + } + get(prefName, defaultValue) { + let value = this.prefs.get(prefName); + return typeof value === "undefined" ? defaultValue : value; + } + getPrefType(prefName) { + let value = this.prefs.get(prefName); + switch (typeof value) { + case "number": + return this.PREF_INT; + + case "boolean": + return this.PREF_BOOL; + + case "string": + return this.PREF_STRING; + + default: + return this.PREF_INVALID; + } + } + set(prefName, value) { + this.prefs.set(prefName, value); + + // Trigger all observers for prefixes of the changed pref name. This matches + // the semantics of `nsIPrefBranch`. + let observerPrefixes = [...this.observers.keys()].filter(prefix => + prefName.startsWith(prefix) + ); + for (let observerPrefix of observerPrefixes) { + this.observers.get(observerPrefix)("", "", prefName); + } + } + getChildList(prefix) { + return [...this.prefs.keys()].filter(prefName => + prefName.startsWith(prefix) + ); + } + prefHasUserValue(prefName) { + return this.prefs.has(prefName); + } + prefIsLocked(prefName) { + return false; + } +} + +/** + * A fake `Services.prefs` implementation that extends `FakensIPrefBranch` + * with methods specific to `nsIPrefService`. + */ +export class FakensIPrefService extends FakensIPrefBranch { + getBranch() {} + getDefaultBranch(prefix) { + return { + setBoolPref() {}, + setIntPref() {}, + setStringPref() {}, + clearUserPref() {}, + }; + } +} + +/** + * Very simple fake for the most basic semantics of Preferences.sys.mjs. + * Extends FakensIPrefBranch. + */ +export class FakePrefs extends FakensIPrefBranch { + observe(prefName, callback) { + super.addObserver(prefName, callback); + } + ignore(prefName, callback) { + super.removeObserver(prefName, callback); + } + observeBranch(listener) {} + ignoreBranch(listener) {} + set(prefName, value) { + this.prefs.set(prefName, value); + + // Trigger observers for just the changed pref name, not any of its + // prefixes. This matches the semantics of `Preferences.sys.mjs`. + if (this.observers.has(prefName)) { + this.observers.get(prefName)(value); + } + } +} + +/** + * Slimmed down version of toolkit/modules/EventEmitter.sys.mjs + */ +export function EventEmitter() {} +EventEmitter.decorate = function (objectToDecorate) { + let emitter = new EventEmitter(); + objectToDecorate.on = emitter.on.bind(emitter); + objectToDecorate.off = emitter.off.bind(emitter); + objectToDecorate.once = emitter.once.bind(emitter); + objectToDecorate.emit = emitter.emit.bind(emitter); +}; +EventEmitter.prototype = { + on(event, listener) { + if (!this._eventEmitterListeners) { + this._eventEmitterListeners = new Map(); + } + if (!this._eventEmitterListeners.has(event)) { + this._eventEmitterListeners.set(event, []); + } + this._eventEmitterListeners.get(event).push(listener); + }, + off(event, listener) { + if (!this._eventEmitterListeners) { + return; + } + let listeners = this._eventEmitterListeners.get(event); + if (listeners) { + this._eventEmitterListeners.set( + event, + listeners.filter( + l => l !== listener && l._originalListener !== listener + ) + ); + } + }, + once(event, listener) { + return new Promise(resolve => { + let handler = (_, first, ...rest) => { + this.off(event, handler); + if (listener) { + listener(event, first, ...rest); + } + resolve(first); + }; + + handler._originalListener = listener; + this.on(event, handler); + }); + }, + // All arguments to this method will be sent to listeners + emit(event, ...args) { + if ( + !this._eventEmitterListeners || + !this._eventEmitterListeners.has(event) + ) { + return; + } + let originalListeners = this._eventEmitterListeners.get(event); + for (let listener of this._eventEmitterListeners.get(event)) { + // If the object was destroyed during event emission, stop + // emitting. + if (!this._eventEmitterListeners) { + break; + } + // If listeners were removed during emission, make sure the + // event handler we're going to fire wasn't removed. + if ( + originalListeners === this._eventEmitterListeners.get(event) || + this._eventEmitterListeners.get(event).some(l => l === listener) + ) { + try { + listener(event, ...args); + } catch (ex) { + // error with a listener + } + } + } + }, +}; + +export function FakePerformance() {} +FakePerformance.prototype = { + marks: new Map(), + now() { + return window.performance.now(); + }, + timing: { navigationStart: 222222.123 }, + get timeOrigin() { + return 10000.234; + }, + // XXX assumes type == "mark" + getEntriesByName(name, type) { + if (this.marks.has(name)) { + return this.marks.get(name); + } + return []; + }, + callsToMark: 0, + + /** + * @note The "startTime" for each mark is simply the number of times mark + * has been called in this object. + */ + mark(name) { + let markObj = { + name, + entryType: "mark", + startTime: ++this.callsToMark, + duration: 0, + }; + + if (this.marks.has(name)) { + this.marks.get(name).push(markObj); + return; + } + + this.marks.set(name, [markObj]); + }, +}; + +/** + * addNumberReducer - a simple dummy reducer for testing that adds a number + */ +export function addNumberReducer(prevState = 0, action) { + return action.type === "ADD" ? prevState + action.data : prevState; +} + +export class FakeConsoleAPI { + static LOG_LEVELS = { + all: Number.MIN_VALUE, + debug: 2, + log: 3, + info: 3, + clear: 3, + trace: 3, + timeEnd: 3, + time: 3, + assert: 3, + group: 3, + groupEnd: 3, + profile: 3, + profileEnd: 3, + dir: 3, + dirxml: 3, + warn: 4, + error: 5, + off: Number.MAX_VALUE, + }; + + constructor({ prefix = "", maxLogLevel = "all" } = {}) { + this.prefix = prefix; + this.prefixStr = prefix ? `${prefix}: ` : ""; + this.maxLogLevel = maxLogLevel; + + for (const level of Object.keys(FakeConsoleAPI.LOG_LEVELS)) { + // eslint-disable-next-line no-console + if (typeof console[level] === "function") { + this[level] = this.shouldLog(level) + ? this._log.bind(this, level) + : () => {}; + } + } + } + shouldLog(level) { + return ( + FakeConsoleAPI.LOG_LEVELS[this.maxLogLevel] <= + FakeConsoleAPI.LOG_LEVELS[level] + ); + } + _log(level, ...args) { + console[level](this.prefixStr, ...args); // eslint-disable-line no-console + } +} + +export class FakeLogger extends FakeConsoleAPI { + constructor() { + super({ + // Don't use a prefix because the first instance gets cached and reused by + // other consumers that would otherwise pass their own identifying prefix. + maxLogLevel: "off", // Change this to "debug" or "all" to get more logging in tests + }); + } +} |