-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathBackupCreateFlow.spec.tsx
More file actions
349 lines (307 loc) · 9.42 KB
/
Copy pathBackupCreateFlow.spec.tsx
File metadata and controls
349 lines (307 loc) · 9.42 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import React from "react";
import { render } from "test-utils/testing-library";
import { screen, waitFor, fireEvent } from "@testing-library/react";
import { MockedProvider } from "@apollo/client/testing";
import BackupCreateFlow from "renderer/pages/backup/BackupCreateFlow";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import gql from "graphql-tag";
import legacyDownload from "js-file-download";
vi.mock("js-file-download");
// Mock environment
vi.mock("shared/environment", () => ({
default: {
isElectron: true,
},
}));
// Mock compatibility checks
vi.mock("renderer/compatibility/checks", () => ({
default: {
hasFilesystemApi: true,
},
}));
describe("<BackupCreateFlow />", () => {
it("should render the component", () => {
render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
// Component should render without crashing
expect(screen.getByRole("button")).toBeInTheDocument();
});
it("should display folder icon button", () => {
const { container } = render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const folderIcon = container.querySelector(".anticon-folder-open");
expect(folderIcon).toBeInTheDocument();
});
it("should render initial state with action buttons", () => {
render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
// Should have at least one button rendered
const buttons = screen.getAllByRole("button");
expect(buttons.length).toBeGreaterThan(0);
});
it("should render without crashing when no directory is selected", () => {
const { container } = render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
expect(container).toBeInTheDocument();
});
it("should display SD Card selection UI", () => {
const { container } = render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const cards = container.querySelectorAll(".ant-card");
expect(cards.length).toBeGreaterThanOrEqual(1);
});
it("should have proper flex layout", () => {
const { container } = render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.firstChild).toHaveStyle({
display: "flex",
flexDirection: "column",
});
});
it("should render FolderOpenTwoTone icon", () => {
const { container } = render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const icon = container.querySelector(".anticon-folder-open");
expect(icon).toBeInTheDocument();
});
it("should display Select SD Card button when no directory is selected", () => {
render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
const buttons = screen.getAllByRole("button");
expect(buttons).toBeTruthy();
expect(buttons.length).toBeGreaterThan(0);
});
it("should render Card components", () => {
const { container } = render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const cardElements = container.querySelectorAll(".ant-card");
expect(cardElements.length).toBeGreaterThan(0);
});
it("should have gap styling in main container", () => {
const { container } = render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const mainDiv = container.firstChild as HTMLElement;
expect(mainDiv).toHaveStyle({ gap: "16px" });
});
it("should contain Typography components", () => {
const { container } = render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const textElements = container.querySelectorAll(".ant-typography");
expect(textElements).toBeTruthy();
});
it("should render without GraphQL errors", async () => {
const { container } = render(
<MockedProvider mocks={[]} addTypename={false}>
<BackupCreateFlow />
</MockedProvider>
);
await waitFor(() => {
expect(container).toBeInTheDocument();
});
});
it("should have interactive button elements", () => {
render(
<MockedProvider mocks={[]}>
<BackupCreateFlow />
</MockedProvider>
);
const buttons = screen.getAllByRole("button");
buttons.forEach((button) => {
expect(button).toBeInstanceOf(HTMLElement);
});
});
});
describe("<BackupCreateFlow /> sequential individual download", () => {
let downloadedFiles: string[];
const mockLegacyDownload = vi.mocked(legacyDownload);
beforeEach(() => {
downloadedFiles = [];
mockLegacyDownload.mockImplementation((data: unknown, filename: string) => {
downloadedFiles.push(filename);
});
});
afterEach(() => {
vi.restoreAllMocks();
});
it("should download all selected individual model files, not just the first 10", async () => {
const directoryId = "test-dir-id";
const modelCount = 11;
const modelNames = Array.from(
{ length: modelCount },
(_, i) => `model${i + 1}`
);
const base64 = Buffer.from("content").toString("base64");
const mocks = [
{
request: {
query: gql`
mutation PickSdcardDirectory {
pickSdcardDirectory {
id
}
}
`,
},
result: {
data: {
pickSdcardDirectory: { id: directoryId },
},
},
},
{
request: {
query: gql`
query SdcardDirectoryInfo($directoryId: ID!) {
sdcardModelsDirectory(id: $directoryId) {
id
name
isValid
hasLabels
pack {
target
version
}
}
}
`,
variables: { directoryId },
},
result: {
data: {
sdcardModelsDirectory: {
id: directoryId,
name: "MODELS",
isValid: true,
hasLabels: false,
pack: null,
},
},
},
},
{
request: {
query: gql`
query SdcardModelsWithNames($directoryId: ID!) {
sdcardModelsWithNames(directoryId: $directoryId) {
fileName
displayName
}
}
`,
variables: { directoryId },
},
result: {
data: {
sdcardModelsWithNames: modelNames.map((name) => ({
fileName: name,
displayName: `Model ${name}`,
})),
},
},
},
{
request: {
query: gql`
mutation DownloadIndividualModels(
$directoryId: ID!
$selectedModels: [String!]!
$includeLabels: Boolean
) {
downloadIndividualModels(
directoryId: $directoryId
selectedModels: $selectedModels
includeLabels: $includeLabels
) {
fileName
base64Data
}
}
`,
variables: {
directoryId,
selectedModels: modelNames,
includeLabels: false,
},
},
result: {
data: {
downloadIndividualModels: modelNames.map((name) => ({
fileName: `${name}.yml`,
base64Data: base64,
})),
},
},
},
];
render(
<MockedProvider mocks={mocks} addTypename={false}>
<BackupCreateFlow />
</MockedProvider>
);
// Trigger SD card selection
fireEvent.click(screen.getByText("Select SD Card"));
// Wait for Apollo mutation response and models to load
await waitFor(
() => {
expect(screen.getByText("Select all")).toBeInTheDocument();
},
{ timeout: 3000 }
);
// Select all models
fireEvent.click(screen.getByText("Select all"));
// Switch to individual .yml format
fireEvent.click(screen.getByText("Individual .yml files"));
// Trigger backup download
fireEvent.click(screen.getByRole("button", { name: /create backup/i }));
// All 11 files should be downloaded. Chromium blocks simultaneous
// programmatic downloads above ~10; the sequential approach (one per
// 200ms in production, instant in tests) avoids that entirely.
await waitFor(() => {
expect(downloadedFiles).toHaveLength(modelCount);
});
// Verify each model file was included
modelNames.forEach((name) => {
expect(downloadedFiles).toContain(`${name}.yml`);
});
});
});