readest / apps /readest-app /vitest.setup.ts
dlxj
init
4e1096a
// localStorage mock
if (typeof window !== 'undefined' && !window.localStorage) {
const storage: Record<string, string> = {};
window.localStorage = {
getItem: (key: string) => storage[key] || null,
setItem: (key: string, value: string) => {
storage[key] = value;
},
removeItem: (key: string) => {
delete storage[key];
},
clear: () => {
Object.keys(storage).forEach((key) => delete storage[key]);
},
get length() {
return Object.keys(storage).length;
},
key: (index: number) => {
const keys = Object.keys(storage);
return keys[index] || null;
},
} as Storage;
} else if (typeof window !== 'undefined' && window.localStorage && !window.localStorage.getItem) {
// If localStorage exists but getItem is not a function, replace it
const storage: Record<string, string> = {};
window.localStorage = {
getItem: (key: string) => storage[key] || null,
setItem: (key: string, value: string) => {
storage[key] = value;
},
removeItem: (key: string) => {
delete storage[key];
},
clear: () => {
Object.keys(storage).forEach((key) => delete storage[key]);
},
get length() {
return Object.keys(storage).length;
},
key: (index: number) => {
const keys = Object.keys(storage);
return keys[index] || null;
},
} as Storage;
}
// matchMedia mock
if (typeof window !== 'undefined' && !window.matchMedia) {
window.matchMedia = (query: string) =>
({
matches: false,
media: query,
onchange: null,
addEventListener: () => {},
removeEventListener: () => {},
addListener: () => {},
removeListener: () => {},
dispatchEvent: () => false,
}) as MediaQueryList;
}