-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-setup.ts
More file actions
206 lines (184 loc) · 5.57 KB
/
Copy pathtest-setup.ts
File metadata and controls
206 lines (184 loc) · 5.57 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import "@testing-library/jest-dom";
import React from "react";
import dayjs from "@/dayjs";
import "jest-axe/extend-expect";
// Define mocks using vi.hoisted to ensure they're available during hoisting
const logViewerMock = vi.hoisted(() => ({
LogViewer: ({ data, toolbar, onScroll, _hasLineNumbers = false, _scrollToRow }) => {
const handleScroll = (event) => {
// Use the custom event detail if available, otherwise use default value
const scrollOffsetToBottom = event?.detail?.scrollOffsetToBottom ?? 100;
onScroll?.({ scrollOffsetToBottom });
};
return React.createElement(
"div",
{
role: "log",
"data-testid": "log-viewer",
onScroll: handleScroll,
},
toolbar,
React.createElement("pre", null, data)
);
},
LogViewerSearch: ({ placeholder }) =>
React.createElement("input", {
type: "text",
placeholder: placeholder,
}),
}));
const codeEditorMock = vi.hoisted(() => ({
CodeEditor: ({
code,
onChange,
_language,
height,
_isReadOnly,
_isDownloadEnabled,
_isCopyEnabled,
_isLanguageLabelVisible,
customControls,
_isDarkTheme,
...props
}) => {
return React.createElement(
"div",
{ "data-testid": "code-editor", style: { height }, ...props },
React.createElement("pre", { "data-testid": "code-editor-content" }, code),
customControls &&
React.createElement(
"div",
{ "data-testid": "code-editor-custom-controls" },
customControls
),
onChange &&
React.createElement("textarea", {
"data-testid": "code-editor-textarea",
value: code,
onChange: (event) => onChange((event.target as HTMLTextAreaElement).value),
style: { display: "none" },
})
);
},
CodeEditorControl: ({ onClick, icon, "aria-label": ariaLabel, tooltipProps, ...props }) =>
React.createElement(
"button",
{
onClick,
"aria-label": ariaLabel,
"data-tooltip": tooltipProps?.content,
...props,
},
icon
),
Language: {
markdown: "markdown",
json: "json",
xml: "xml",
python: "python",
yaml: "yaml",
javascript: "javascript",
typescript: "typescript",
},
}));
// Composer mocks - must use function (not arrow) for Vitest 4 constructor compatibility
const linkShapeMock = vi.hoisted(() => ({
LinkShape: vi.fn().mockImplementation(function () {
return {
source: vi.fn().mockReturnThis(),
target: vi.fn().mockReturnThis(),
router: vi.fn().mockReturnThis(),
connector: vi.fn().mockReturnThis(),
set: vi.fn().mockReturnThis(),
addTo: vi.fn().mockReturnThis(),
};
}),
}));
// Fix timezone to Europe/Brussels for all tests so date-related assertions are deterministic.
vi.spyOn(dayjs.tz, "guess").mockReturnValue("Europe/Brussels");
// jsdom does not implement scrollIntoView; provide a no-op so components that call it
// (e.g. to bring a newly expanded/focused row into view) don't crash in tests.
if (!window.HTMLElement.prototype.scrollIntoView) {
window.HTMLElement.prototype.scrollIntoView = vi.fn();
}
// Mock window.matchMedia
Object.defineProperty(window, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: query === "(prefers-color-scheme: light)",
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// JointJS mock - only define if not already defined
if (!window.SVGAngle) {
Object.defineProperty(window, "SVGAngle", {
value: vi.fn(),
});
}
// Set window size for tests
Object.defineProperty(window, "innerWidth", {
writable: true,
value: 1300,
});
Object.defineProperty(window, "innerHeight", {
writable: true,
value: 800,
});
// Set test timeout
vi.setConfig({ testTimeout: 25000 });
// Collect console warnings and errors
const consoleIssues: Array<{ type: "warn" | "error"; message: string }> = [];
// Store original console methods
const originalWarn = console.warn;
const originalError = console.error;
// Helper function to format console args
const formatConsoleArgs = (args: unknown[]) => {
return args
.map((arg) => {
if (typeof arg === "object" && arg !== null) {
try {
return JSON.stringify(arg, null, 2);
} catch (_error) {
return `[${typeof arg}: Circular or complex object]`;
}
}
return String(arg);
})
.join(" ");
};
// Setup console spies
vi.spyOn(console, "warn").mockImplementation((...args) => {
consoleIssues.push({
type: "warn",
message: formatConsoleArgs(args),
});
originalWarn.apply(console, args);
});
vi.spyOn(console, "error").mockImplementation((...args) => {
consoleIssues.push({
type: "error",
message: formatConsoleArgs(args),
});
originalError.apply(console, args);
});
// Mock @patternfly/react-log-viewer
vi.mock("@patternfly/react-log-viewer", () => logViewerMock);
// Mock @patternfly/react-code-editor
vi.mock("@patternfly/react-code-editor", () => codeEditorMock);
// Mock Composer LinkShape (used by linkUtils, initializeCanvasFromInstance, and other Composer tests)
vi.mock("@/UI/Components/Composer/UI/JointJsShapes/LinkShape", () => linkShapeMock);
// Mock mermaid
vi.mock("mermaid", () => ({
default: {
initialize: vi.fn(),
render: vi.fn().mockResolvedValue({ svg: "<svg>Mock Mermaid Diagram</svg>" }),
run: vi.fn().mockResolvedValue(undefined),
init: vi.fn(),
},
}));