From 18c5348f9993a890b0329d4414353c6b9461727c Mon Sep 17 00:00:00 2001 From: Graham Langford <30706330+grahamlangford@users.noreply.github.com> Date: Thu, 6 Jun 2024 12:20:50 -0500 Subject: [PATCH] cleanup chrome.storage.session mock (#8559) Co-authored-by: Eduardo Fungairino --- package-lock.json | 8 ++++---- package.json | 2 +- src/mv3/SessionStorage.test.ts | 24 ------------------------ src/mv3/SessionStorage.ts | 14 +++----------- 4 files changed, 8 insertions(+), 40 deletions(-) diff --git a/package-lock.json b/package-lock.json index 75240c10ee..a9c5a9f9f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -242,7 +242,7 @@ "jest-environment-jsdom": "^29.7.0", "jest-extended": "^4.0.2", "jest-location-mock": "^2.0.0", - "jest-webextension-mock": "^3.8.16", + "jest-webextension-mock": "^3.9.0", "jsdom": "^24.0.0", "jsdom-testing-mocks": "^1.13.0", "knip": "^5.16.0", @@ -20806,9 +20806,9 @@ } }, "node_modules/jest-webextension-mock": { - "version": "3.8.16", - "resolved": "https://registry.npmjs.org/jest-webextension-mock/-/jest-webextension-mock-3.8.16.tgz", - "integrity": "sha512-bFEaRBuF+QZkPsprJfCXGuTTCvBm368y9ComlwIu30Z1ia2B+cHqHr45qABgRvTwb0eojbhsawJliqhiRypQwA==", + "version": "3.9.0", + "resolved": "https://registry.npmjs.org/jest-webextension-mock/-/jest-webextension-mock-3.9.0.tgz", + "integrity": "sha512-cknPFLO3cD2GwsgpVWwcsW+s2CoKKsEHCDNDw1ZNrA5+9R28/g4UOC2CEPc+Z+MwvoNjvIsEsowJy7L6MldBYg==", "dev": true }, "node_modules/jest-worker": { diff --git a/package.json b/package.json index c808922c36..5de9253513 100644 --- a/package.json +++ b/package.json @@ -268,7 +268,7 @@ "jest-environment-jsdom": "^29.7.0", "jest-extended": "^4.0.2", "jest-location-mock": "^2.0.0", - "jest-webextension-mock": "^3.8.16", + "jest-webextension-mock": "^3.9.0", "jsdom": "^24.0.0", "jsdom-testing-mocks": "^1.13.0", "knip": "^5.16.0", diff --git a/src/mv3/SessionStorage.test.ts b/src/mv3/SessionStorage.test.ts index a9fbe64b16..ff91dd319b 100644 --- a/src/mv3/SessionStorage.test.ts +++ b/src/mv3/SessionStorage.test.ts @@ -17,30 +17,6 @@ import { SessionMap, SessionValue } from "./SessionStorage"; -const _map = new Map(); - -// Workaround until https://github.com/RickyMarou/jest-webextension-mock/issues/6 is resolved -browser.storage.session = { - get: jest.fn(async (key: string) => ({ [key]: _map.get(key) })), - set: jest.fn(async (obj) => { - _map.set(...Object.entries(obj)[0]!); - }), - remove: jest.fn(async (key) => { - _map.delete(key); - }), - clear: jest.fn(), - onChanged: { - addListener: jest.fn(), - removeListener: jest.fn(), - hasListener: jest.fn(), - hasListeners: jest.fn(), - }, -}; - -beforeEach(() => { - _map.clear(); -}); - test("SessionMap", async () => { const map = new SessionMap("jester", import.meta.url); await expect(map.get("alpha")).resolves.toBeUndefined(); diff --git a/src/mv3/SessionStorage.ts b/src/mv3/SessionStorage.ts index 7cba28b977..881aa61dcc 100644 --- a/src/mv3/SessionStorage.ts +++ b/src/mv3/SessionStorage.ts @@ -15,13 +15,6 @@ * along with this program. If not, see . */ -/** - * @file Store data in a Map with fallback to storage.session, if present. - * The alternative would be to use `chrome.storage.local` as a polyfill, but - * then we'd have to manually keep track of this data and clean it up, - * potentially leaving data behind. - */ - import { expectContext } from "@/utils/expectContext"; import { type OmitIndexSignature, type JsonValue } from "type-fest"; import { type ManualStorageKey } from "@/utils/storageUtils"; @@ -36,8 +29,7 @@ function validateContext(): void { } /** - * MV3-compatible Map-like storage, this helps transition to chrome.storage.session - * and provide some type safety. + * Wrapper for chrome.storage.session with added type safety. */ export class SessionMap { constructor( @@ -97,8 +89,8 @@ export class SessionMap { } /** - * MV3-compatible single-value storage. - * This helps transition to chrome.storage.session and provide some type safety. + * Single-value storage leveraging chrome.storage.session. + * Adds some additional type safety. */ // "OmitIndexSignature" is because of https://github.com/sindresorhus/type-fest/issues/815 export class SessionValue> {