|
1 | 1 | /// <reference types="vitest/globals" /> |
2 | 2 | import "@testing-library/jest-dom/vitest"; |
3 | | -import { userEvent } from "@testing-library/user-event"; |
4 | | -import { version } from "svelte/package.json"; |
5 | 3 |
|
6 | | -export const SVELTE_VERSION = Number.parseInt(version.split(".")[0], 10); |
7 | | -export const isSvelte3 = SVELTE_VERSION === 3; |
8 | | -export const isSvelte4 = SVELTE_VERSION === 4; |
9 | | -export const isSvelte5 = SVELTE_VERSION === 5; |
10 | | - |
11 | | -// Mock scrollIntoView since it's not implemented in JSDOM |
12 | | -Element.prototype.scrollIntoView = vi.fn(); |
13 | | - |
14 | | -// Mock ResizeObserver since it's not implemented in JSDOM |
15 | | -class ResizeObserverMock { |
16 | | - callback: ResizeObserverCallback; |
17 | | - elements: Element[]; |
18 | | - |
19 | | - constructor(callback: ResizeObserverCallback) { |
20 | | - this.callback = callback; |
21 | | - this.elements = []; |
22 | | - } |
23 | | - |
24 | | - observe(element: Element) { |
25 | | - this.elements.push(element); |
26 | | - this.callback( |
27 | | - [ |
28 | | - { |
29 | | - target: element, |
30 | | - contentRect: { height: 100 } as DOMRectReadOnly, |
31 | | - borderBoxSize: [], |
32 | | - contentBoxSize: [], |
33 | | - devicePixelContentBoxSize: [], |
34 | | - }, |
35 | | - ], |
36 | | - this, |
37 | | - ); |
38 | | - } |
39 | | - |
40 | | - unobserve(element: Element) { |
41 | | - this.elements = this.elements.filter((el) => el !== element); |
42 | | - } |
43 | | - |
44 | | - disconnect() { |
45 | | - this.elements = []; |
46 | | - } |
47 | | -} |
48 | | - |
49 | | -global.ResizeObserver = ResizeObserverMock; |
50 | | - |
51 | | -if (typeof DataTransfer === "undefined") { |
52 | | - class DataTransferMock { |
53 | | - items: DataTransferItemList; |
54 | | - files: FileList = [] as unknown as FileList; |
55 | | - private fileList: File[] = []; |
56 | | - |
57 | | - constructor() { |
58 | | - this.items = { |
59 | | - add: (file: File) => { |
60 | | - this.fileList.push(file); |
61 | | - this.updateFiles(); |
62 | | - return null as unknown as DataTransferItem; |
63 | | - }, |
64 | | - length: 0, |
65 | | - } as unknown as DataTransferItemList; |
66 | | - |
67 | | - this.updateFiles(); |
68 | | - } |
69 | | - |
70 | | - private updateFiles() { |
71 | | - const fileList = Object.create(Array.prototype); |
72 | | - this.fileList.forEach((file, index) => { |
73 | | - fileList[index] = file; |
74 | | - }); |
75 | | - fileList.length = this.fileList.length; |
76 | | - fileList.item = (index: number) => this.fileList[index] || null; |
77 | | - |
78 | | - fileList[Symbol.iterator] = function* () { |
79 | | - for (let i = 0; i < this.length; i++) { |
80 | | - yield this[i]; |
81 | | - } |
82 | | - }; |
83 | | - |
84 | | - this.files = fileList as FileList; |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - global.DataTransfer = DataTransferMock as unknown as typeof DataTransfer; |
89 | | -} |
90 | | - |
91 | | -export const user = userEvent.setup(); |
92 | | - |
93 | | -export const setupLocalStorageMock = () => { |
94 | | - let localStorageMock: { [key: string]: string } = {}; |
95 | | - let originalLocalStorage: Storage; |
96 | | - |
97 | | - beforeEach(() => { |
98 | | - originalLocalStorage = global.localStorage; |
99 | | - localStorageMock = {}; |
100 | | - global.localStorage = { |
101 | | - getItem: vi.fn((key) => localStorageMock[key] || null), |
102 | | - setItem: vi.fn((key, value) => { |
103 | | - localStorageMock[key] = value; |
104 | | - }), |
105 | | - removeItem: vi.fn((key) => { |
106 | | - delete localStorageMock[key]; |
107 | | - }), |
108 | | - clear: vi.fn(() => { |
109 | | - localStorageMock = {}; |
110 | | - }), |
111 | | - length: 0, |
112 | | - key: vi.fn(), |
113 | | - }; |
114 | | - }); |
115 | | - |
116 | | - afterEach(() => { |
117 | | - global.localStorage = originalLocalStorage; |
118 | | - localStorage.clear(); |
119 | | - vi.restoreAllMocks(); |
120 | | - localStorageMock = {}; |
121 | | - }); |
122 | | - |
123 | | - return { |
124 | | - setMockItem: (key: string, value: string) => { |
125 | | - localStorageMock[key] = value; |
126 | | - }, |
127 | | - getMockItem: (key: string) => localStorageMock[key], |
128 | | - }; |
129 | | -}; |
| 4 | +export { |
| 5 | + isSvelte3, |
| 6 | + isSvelte4, |
| 7 | + isSvelte5, |
| 8 | + SVELTE_VERSION, |
| 9 | + setupLocalStorageMock, |
| 10 | + user, |
| 11 | +} from "../tests/utils/setup-shared"; |
0 commit comments