From c42ca7ac00b51c63129d161cbcd11122fb6ebc03 Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Wed, 19 Jul 2023 11:44:08 +0200 Subject: [PATCH] Move newtab-tracker-stats tests to esbuild --- .../{legacy => background}/newtab-tracker-stats.js | 13 +++++++++---- unit-test/helpers/mock-browser-api.js | 10 ++++++++-- unit-test/inject-chrome-shim.js | 10 ++++++++-- 3 files changed, 25 insertions(+), 8 deletions(-) rename unit-test/{legacy => background}/newtab-tracker-stats.js (96%) diff --git a/unit-test/legacy/newtab-tracker-stats.js b/unit-test/background/newtab-tracker-stats.js similarity index 96% rename from unit-test/legacy/newtab-tracker-stats.js rename to unit-test/background/newtab-tracker-stats.js index be7141eb9a..83183cbdb0 100644 --- a/unit-test/legacy/newtab-tracker-stats.js +++ b/unit-test/background/newtab-tracker-stats.js @@ -1,6 +1,5 @@ const { NewTabTrackerStats } = require('../../shared/js/background/newtab-tracker-stats') const { TrackerStats } = require('../../shared/js/background/classes/tracker-stats') -const browserWrapper = require('../../shared/js/background/wrapper') const constants = require('../../shared/data/constants') const testTDS = require('../data/tds.json') const { dataFormatSchema } = require('../../shared/js/newtab/schema') @@ -116,6 +115,11 @@ describe('NewTabTrackerStats', () => { describe('sending data', () => { beforeEach(() => { jasmine.clock().install() + // @ts-ignore + while (chrome.storage.local._setCalls.length > 0) { + // @ts-ignore + chrome.storage.local._setCalls.pop() + } }) afterEach(() => { jasmine.clock().uninstall() @@ -127,7 +131,8 @@ describe('sending data', () => { // @ts-ignore newtab.assignTopCompanies(testTDS.entities) const sendSpy = spyOn(newtab, '_publish') - const syncSpy = spyOn(browserWrapper, 'syncToStorage') + // @ts-ignore + const syncSpy = chrome.storage.local._setCalls const now = 1673473220560 @@ -145,8 +150,8 @@ describe('sending data', () => { expect(sendSpy).toHaveBeenCalledTimes(1) // assert that values were synced to storage - expect(syncSpy).toHaveBeenCalledTimes(1) - expect(syncSpy).toHaveBeenCalledWith({ + expect(syncSpy.length).toBe(1) + expect(syncSpy[0]).toEqual({ [NewTabTrackerStats.storageKey]: { stats: { current: { diff --git a/unit-test/helpers/mock-browser-api.js b/unit-test/helpers/mock-browser-api.js index f312e4ac96..c22f141abd 100644 --- a/unit-test/helpers/mock-browser-api.js +++ b/unit-test/helpers/mock-browser-api.js @@ -1,10 +1,16 @@ globalThis.browser = { storage: { local: { - set: () => {}, + set: (value) => { + browser.storage.local._setCalls.push(value) + }, get: () => { return {} - } + }, + _setCalls: [] + }, + managed: { + get: () => {} } }, browserAction: { diff --git a/unit-test/inject-chrome-shim.js b/unit-test/inject-chrome-shim.js index 6d8e9ead00..8e23eb733a 100644 --- a/unit-test/inject-chrome-shim.js +++ b/unit-test/inject-chrome-shim.js @@ -1,10 +1,16 @@ const chrome = { storage: { local: { - set: () => {}, + set: (value) => { + chrome.storage.local._setCalls.push(value) + }, get: () => { return {} - } + }, + _setCalls: [] + }, + managed: { + get: () => {} } }, browserAction: {