-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ts
More file actions
91 lines (79 loc) · 2.41 KB
/
Copy pathsetup.ts
File metadata and controls
91 lines (79 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import '@testing-library/jest-dom';
import { expect, afterEach, vi } from 'vitest';
import { cleanup } from '@testing-library/react';
import '@testing-library/jest-dom/vitest';
import { toHaveNoViolations } from 'jest-axe';
// Cleanup after each test
afterEach(() => {
cleanup();
localStorage.clear();
});
// Mock localStorage
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] || null,
setItem: (key: string, value: string) => {
store[key] = value.toString();
},
removeItem: (key: string) => {
delete store[key];
},
clear: () => {
store = {};
},
};
})();
Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
});
// Mock fetch for API calls with proper response structure
global.fetch = vi.fn((url: string) => {
// Extract key from URL pattern /api/data/:key
const match = url.match(/\/api\/data\/([^/?]+)/);
const key = match ? match[1] : null;
// Get value from localStorage for the key
const value = key ? localStorage.getItem(key) : null;
// API expects { data: <value> } structure
const responseData = value ? { data: JSON.parse(value) } : { data: null };
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve(responseData),
text: () => Promise.resolve(JSON.stringify(responseData)),
} as Response);
}) as unknown as typeof fetch;
// Mock window.matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// Mock window.matchMedia for prefers-reduced-motion
const mockMatchMedia = (query: string) => {
const isReducedMotion = query === '(prefers-reduced-motion: reduce)';
return {
matches: isReducedMotion ? false : query === '(prefers-reduced-motion: no-preference)',
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
};
};
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation(mockMatchMedia),
});
// Add jest-axe matchers to expect
expect.extend(toHaveNoViolations);