-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetupTests.ts
More file actions
111 lines (98 loc) · 3.14 KB
/
setupTests.ts
File metadata and controls
111 lines (98 loc) · 3.14 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import '@testing-library/jest-dom/vitest';
import { cleanup } from '@testing-library/react';
import { ChangeEvent, createElement } from 'react';
import { afterAll, afterEach, beforeAll, vi } from 'vitest';
// Mock Monaco Editor
vi.mock('monaco-editor', () => ({
editor: {
IStandaloneThemeData: {},
IMarker: {},
IStandaloneEditorConstructionOptions: {},
},
}));
// Mock Monaco Editor React
vi.mock('@monaco-editor/react', () => ({
Editor: vi.fn(({ value, onChange, theme, onValidate }) => {
// Simulate Monaco Editor validation behavior by calling onValidate after mount
if (onValidate) {
// Use queueMicrotask to ensure validation happens after render
queueMicrotask(() => {
// Call onValidate with empty markers array (valid JSON) or with errors if invalid
try {
if (value) {
JSON.parse(value);
onValidate([]); // Valid JSON - no markers
} else {
onValidate([]); // Empty value - no markers
}
} catch {
// Invalid JSON - return error markers
onValidate([
{
severity: 8, // Error severity
message: 'Invalid JSON',
startLineNumber: 1,
startColumn: 1,
endLineNumber: 1,
endColumn: 1,
},
]);
}
});
}
return createElement(
'div',
{ role: 'textbox', 'aria-label': 'JSON Editor' },
createElement('textarea', {
value: value || '',
onChange: (e: ChangeEvent<HTMLTextAreaElement>) =>
onChange?.(e.target.value),
'data-theme': theme,
'aria-label': 'JSON content',
}),
);
}),
}));
afterEach(() => {
cleanup();
});
let originalIntersectionObserver: typeof IntersectionObserver;
let originalResizeObserver: typeof ResizeObserver;
beforeAll(() => {
if (!document.elementFromPoint) {
document.elementFromPoint = vi.fn(() => document.createElement('div'));
}
originalIntersectionObserver = globalThis.IntersectionObserver;
originalResizeObserver = globalThis.ResizeObserver;
class IntersectionObserverMock implements IntersectionObserver {
readonly root: Element | Document | null = null;
readonly rootMargin: string = '';
readonly thresholds: number[] = [];
callback: IntersectionObserverCallback;
constructor(callback: IntersectionObserverCallback) {
this.callback = callback;
}
observe = vi.fn();
unobserve = vi.fn();
disconnect = vi.fn();
takeRecords = vi.fn();
}
class ResizeObserverMock implements ResizeObserver {
callback: ResizeObserverCallback;
constructor(callback: ResizeObserverCallback) {
this.callback = callback;
}
observe = vi.fn();
unobserve = vi.fn();
disconnect = vi.fn();
}
globalThis.IntersectionObserver =
IntersectionObserverMock as unknown as typeof IntersectionObserver;
globalThis.ResizeObserver =
ResizeObserverMock as unknown as typeof ResizeObserver;
});
afterAll(() => {
vi.restoreAllMocks();
globalThis.IntersectionObserver = originalIntersectionObserver;
globalThis.ResizeObserver = originalResizeObserver;
});