meisiyu | 1d4aade | 2025-06-02 20:10:36 +0800 | [diff] [blame^] | 1 | import '@testing-library/jest-dom'; |
| 2 | |
| 3 | // 全局测试配置 |
| 4 | global.console = { |
| 5 | ...console, |
| 6 | // 在测试中禁用console.log以减少噪音 |
| 7 | log: jest.fn(), |
| 8 | debug: jest.fn(), |
| 9 | info: jest.fn(), |
| 10 | warn: jest.fn(), |
| 11 | error: jest.fn(), |
| 12 | }; |
| 13 | |
| 14 | // Mock window.matchMedia |
| 15 | Object.defineProperty(window, 'matchMedia', { |
| 16 | writable: true, |
| 17 | value: jest.fn().mockImplementation(query => ({ |
| 18 | matches: false, |
| 19 | media: query, |
| 20 | onchange: null, |
| 21 | addListener: jest.fn(), // deprecated |
| 22 | removeListener: jest.fn(), // deprecated |
| 23 | addEventListener: jest.fn(), |
| 24 | removeEventListener: jest.fn(), |
| 25 | dispatchEvent: jest.fn(), |
| 26 | })), |
| 27 | }); |
| 28 | |
| 29 | // Mock ResizeObserver |
| 30 | global.ResizeObserver = jest.fn().mockImplementation(() => ({ |
| 31 | observe: jest.fn(), |
| 32 | unobserve: jest.fn(), |
| 33 | disconnect: jest.fn(), |
| 34 | })); |
| 35 | |
| 36 | // Mock IntersectionObserver |
| 37 | global.IntersectionObserver = jest.fn().mockImplementation(() => ({ |
| 38 | observe: jest.fn(), |
| 39 | unobserve: jest.fn(), |
| 40 | disconnect: jest.fn(), |
| 41 | })); |
| 42 | |
| 43 | // Mock localStorage |
| 44 | const localStorageMock: Storage = { |
| 45 | getItem: jest.fn(), |
| 46 | setItem: jest.fn(), |
| 47 | removeItem: jest.fn(), |
| 48 | clear: jest.fn(), |
| 49 | length: 0, |
| 50 | key: jest.fn(), |
| 51 | }; |
| 52 | global.localStorage = localStorageMock; |
| 53 | global.sessionStorage = localStorageMock; |