import '@testing-library/jest-dom';
import 'fake-indexeddb/auto';
import { theme } from 'antd';
import i18n from 'i18next';
import { enableMapSet, enablePatches } from 'immer';
import React from 'react';
import { beforeEach, vi } from 'vitest';
import chat from '@/locales/default/chat';
import common from '@/locales/default/common';
import discover from '@/locales/default/discover';
import home from '@/locales/default/home';
import oauth from '@/locales/default/oauth';
class TestMemoryStorage implements Storage {
private readonly store = new Map<string, string>();
get length() {
return this.store.size;
}
clear() {
this.store.clear();
}
getItem(key: string) {
return this.store.get(key) ?? null;
}
key(index: number) {
return Array.from(this.store.keys())[index] ?? null;
}
removeItem(key: string) {
this.store.delete(key);
}
setItem(key: string, value: string) {
this.store.set(key, String(value));
}
}
const installTestStorage = () => {
const localStorage = new TestMemoryStorage();
const sessionStorage = new TestMemoryStorage();
Object.defineProperties(globalThis, {
Storage: { configurable: true, value: TestMemoryStorage, writable: true },
localStorage: { configurable: true, value: localStorage, writable: true },
sessionStorage: { configurable: true, value: sessionStorage, writable: true },
});
if (typeof globalThis.window !== 'undefined') {
Object.defineProperties(window, {
localStorage: { configurable: true, value: localStorage, writable: true },
sessionStorage: { configurable: true, value: sessionStorage, writable: true },
});
}
};
enablePatches();
enableMapSet();
vi.mock('@lobehub/analytics/react', () => ({
useAnalytics: () => ({
analytics: {
track: vi.fn(),
},
}),
}));
vi.mock('@/auth', () => ({
auth: {
api: {
getSession: vi.fn().mockResolvedValue(null),
},
},
}));
if (typeof globalThis.window === 'undefined') {
const { Crypto } = await import('@peculiar/webcrypto');
Object.defineProperty(globalThis, 'crypto', {
value: new Crypto(),
writable: true,
});
}
installTestStorage();
beforeEach(installTestStorage);
theme.defaultConfig.hashed = false;
await i18n.init({
defaultNS: 'common',
fallbackLng: 'zh-CN',
interpolation: { escapeValue: false },
lng: 'zh-CN',
ns: ['common', 'chat', 'discover', 'home', 'oauth'],
resources: {
'zh-CN': {
chat,
common,
discover,
home,
oauth,
},
},
});
(globalThis as any).React = React;