fix(web): ensure localStorage mock exists for tests
All checks were successful
ci/woodpecker/push/web Pipeline was successful
All checks were successful
ci/woodpecker/push/web Pipeline was successful
This commit is contained in:
@@ -24,3 +24,34 @@ Object.defineProperty(window, "matchMedia", {
|
||||
dispatchEvent: () => false,
|
||||
}),
|
||||
});
|
||||
|
||||
// Ensure localStorage exists with a full Storage API for tests.
|
||||
if (!window.localStorage || typeof window.localStorage.clear !== "function") {
|
||||
let store: Record<string, string> = {};
|
||||
const storageMock: Storage = {
|
||||
getItem: (key: string): string | null => store[key] ?? null,
|
||||
setItem: (key: string, value: string): void => {
|
||||
store[key] = value;
|
||||
},
|
||||
removeItem: (key: string): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
||||
delete store[key];
|
||||
},
|
||||
clear: (): void => {
|
||||
store = {};
|
||||
},
|
||||
get length(): number {
|
||||
return Object.keys(store).length;
|
||||
},
|
||||
key: (index: number): string | null => {
|
||||
const keys = Object.keys(store);
|
||||
return keys[index] ?? null;
|
||||
},
|
||||
};
|
||||
|
||||
Object.defineProperty(window, "localStorage", {
|
||||
value: storageMock,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user