Skip to content

Commit

Permalink
cleanup chrome.storage.session mock (#8559)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduardo Fungairino <[email protected]>
  • Loading branch information
grahamlangford and fungairino authored Jun 6, 2024
1 parent a2f8f2f commit 18c5348
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 40 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 0 additions & 24 deletions src/mv3/SessionStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 3 additions & 11 deletions src/mv3/SessionStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* @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";
Expand All @@ -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<Value extends JsonValue> {
constructor(
Expand Down Expand Up @@ -97,8 +89,8 @@ export class SessionMap<Value extends JsonValue> {
}

/**
* 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<Value extends OmitIndexSignature<JsonValue>> {
Expand Down

0 comments on commit 18c5348

Please sign in to comment.