-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetupTests.ts
More file actions
140 lines (121 loc) · 3.88 KB
/
Copy pathsetupTests.ts
File metadata and controls
140 lines (121 loc) · 3.88 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import '@testing-library/jest-dom/vitest';
import { cleanup } from '@testing-library/react';
import { ChangeEvent, createElement } from 'react';
import { afterAll, afterEach, beforeAll, vi } from 'vitest';
const hasDom = typeof window !== 'undefined' && typeof document !== 'undefined';
if (hasDom) {
// Mock Monaco Editor
vi.doMock('monaco-editor', () => ({
editor: {
IStandaloneThemeData: {},
IMarker: {},
IStandaloneEditorConstructionOptions: {},
},
}));
// Mock Monaco Editor React
vi.doMock('@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 | undefined;
let originalResizeObserver: typeof ResizeObserver | undefined;
beforeAll(() => {
if (!hasDom) {
return;
}
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 scrollMargin: 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();
if (!hasDom) {
return;
}
const globalWithOptionalObservers = globalThis as {
IntersectionObserver?: typeof IntersectionObserver;
ResizeObserver?: typeof ResizeObserver;
};
if (originalIntersectionObserver) {
globalWithOptionalObservers.IntersectionObserver =
originalIntersectionObserver;
} else {
delete globalWithOptionalObservers.IntersectionObserver;
}
if (originalResizeObserver) {
globalWithOptionalObservers.ResizeObserver = originalResizeObserver;
} else {
delete globalWithOptionalObservers.ResizeObserver;
}
});