-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathevent-test-utils.ts
More file actions
496 lines (438 loc) · 14.4 KB
/
event-test-utils.ts
File metadata and controls
496 lines (438 loc) · 14.4 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import { type Locator, type Page, expect } from "@playwright/test";
type SomedaySection = "week" | "month";
const LOCAL_DB_NAME = "compass-local";
// Shared timeout for form operations - use a single reasonable timeout instead of short retries
const FORM_TIMEOUT = 10000;
/**
* Dispatch a keyboard shortcut to the document.
* Uses the same event properties as the app's internal pressKey utility.
*/
const pressShortcut = async (page: Page, key: string) => {
await page.evaluate((shortcut) => {
document.dispatchEvent(
new KeyboardEvent("keydown", {
key: shortcut,
bubbles: true,
cancelable: true,
composed: true,
}),
);
document.dispatchEvent(
new KeyboardEvent("keyup", {
key: shortcut,
bubbles: true,
cancelable: true,
composed: true,
}),
);
}, key);
};
/**
* Retry an action until a condition is met.
* Consolidates the retry pattern used throughout the test utils.
*/
const retryUntil = async (
page: Page,
action: () => Promise<void>,
waitFor: Locator,
options: { maxAttempts?: number; perAttemptTimeout?: number } = {},
) => {
const { maxAttempts = 3, perAttemptTimeout = 3000 } = options;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
await action();
try {
await waitFor.waitFor({ state: "visible", timeout: perAttemptTimeout });
return;
} catch {
if (attempt === maxAttempts - 1) {
throw new Error(
`Action failed after ${maxAttempts} attempts. ` +
"Expected target element to become visible.",
);
}
await page.waitForTimeout(200);
}
}
};
const ensureWeekView = async (page: Page) => {
const weekViewButton = page.getByRole("button", {
name: /select view, currently week/i,
});
if (await weekViewButton.isVisible()) {
return;
}
const viewButton = page
.getByRole("button", { name: /select view, currently/i })
.first();
await viewButton.waitFor({ state: "visible", timeout: 5000 });
await viewButton.click();
await page.getByRole("option", { name: "Week" }).click();
await page.waitForURL((url) => url.pathname === "/week", { timeout: 10000 });
// Verify we actually switched to Week view
await weekViewButton.waitFor({ state: "visible", timeout: 5000 });
};
const blurActiveElement = async (page: Page) => {
await page.evaluate(() => {
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
});
};
/** Returns a locator for the form's title input */
const getFormTitleInput = (page: Page) =>
page.getByRole("form").getByPlaceholder("Title");
const waitForCalendarShell = async (page: Page) => {
await page
.getByRole("button", { name: /select view, currently/i })
.first()
.waitFor({
state: "visible",
timeout: 15000,
});
};
const reloadCalendarView = async (
page: Page,
options: { openSidebar?: boolean } = {},
) => {
await page.reload({ waitUntil: "domcontentloaded" });
await waitForCalendarShell(page);
await ensureWeekView(page);
await page.locator("#mainGrid").waitFor({ state: "visible", timeout: 15000 });
if (options.openSidebar) {
await ensureSidebarOpen(page);
}
};
const expectWithCalendarRecovery = async (
page: Page,
assertion: () => Promise<void>,
options: { openSidebar?: boolean } = {},
) => {
try {
await assertion();
} catch {
await reloadCalendarView(page, options);
await assertion();
}
};
const clearClientAuthState = async (page: Page) => {
await page.evaluate(() => {
localStorage.removeItem("compass.auth");
});
};
export const createEventTitle = (prefix: string) => `${prefix} ${Date.now()}`;
export const updateEventTitle = (prefix: string) =>
`${prefix} Updated ${Date.now()}`;
export const prepareCalendarPage = async (page: Page) => {
await page.goto("/week", { waitUntil: "domcontentloaded" });
await waitForCalendarShell(page);
await clearClientAuthState(page);
await resetLocalEventDb(page);
await page.reload({ waitUntil: "domcontentloaded" });
await waitForCalendarShell(page);
await ensureWeekView(page);
await page.locator("#mainGrid").waitFor({ state: "visible", timeout: 15000 });
await blurActiveElement(page);
await page.locator("#mainGrid").focus();
};
export const resetLocalEventDb = async (page: Page) => {
await page.evaluate(async (dbName) => {
const clearStore = async (
db: IDBDatabase,
storeName: string,
): Promise<void> => {
if (!db.objectStoreNames.contains(storeName)) {
return;
}
await new Promise<void>((resolve) => {
const transaction = db.transaction(storeName, "readwrite");
const clearRequest = transaction.objectStore(storeName).clear();
clearRequest.onerror = () => resolve();
transaction.oncomplete = () => resolve();
transaction.onerror = () => resolve();
transaction.onabort = () => resolve();
});
};
await new Promise<void>((resolve) => {
const deleteRequest = indexedDB.deleteDatabase(dbName);
deleteRequest.onsuccess = () => resolve();
deleteRequest.onerror = () => resolve();
deleteRequest.onblocked = () => {
// If the app still has an open connection, fall back to clearing stores
// so tests still start from a clean state.
const openRequest = indexedDB.open(dbName);
openRequest.onerror = () => resolve();
openRequest.onsuccess = async () => {
const db = openRequest.result;
await clearStore(db, "events");
await clearStore(db, "tasks");
db.close();
resolve();
};
};
});
}, LOCAL_DB_NAME);
};
export const ensureSidebarOpen = async (page: Page) => {
const sidebar = page.locator("#sidebar");
if (!(await sidebar.isVisible())) {
await blurActiveElement(page);
await page.locator("#mainGrid").focus();
await pressShortcut(page, "[");
await expect(sidebar).toBeVisible();
}
};
export const clickGridCenter = async (page: Page, locator: Locator) => {
await locator.scrollIntoViewIfNeeded();
const box = await locator.boundingBox();
if (!box) {
throw new Error("Expected grid element to be visible for interaction.");
}
const x = box.x + box.width * 0.3;
const y = box.y + box.height * 0.3;
await page.mouse.move(x, y);
await page.mouse.down();
// Allow draft state to settle before mouseup so the form can open reliably.
await page.waitForTimeout(175);
await page.mouse.up();
};
/**
* Fills the event form title and submits via the Save control (role=tab, name Save).
* Keyboard shortcuts for submit (Enter / Mod+Enter) are not driven here: Playwright’s
* synthesized keyboard events are unreliable in headless Chromium on Linux CI; the Save
* tab matches the accessible UI and stays stable. Shortcut submit is covered in
* EventForm unit tests.
*/
export const fillTitleAndSaveEventForm = async (page: Page, title: string) => {
const titleInput = getFormTitleInput(page);
await expect(titleInput).toBeVisible({ timeout: FORM_TIMEOUT });
await titleInput.fill(title);
const saveTab = page.getByRole("form").getByRole("tab", { name: "Save" });
await saveTab.scrollIntoViewIfNeeded();
// Save often sits below the fold; Playwright click() can still refuse when the tab is
// outside the viewport (CI). Dispatch the DOM click so React handlers run reliably.
await saveTab.evaluate((el) => {
(el as HTMLElement).click();
});
await titleInput.waitFor({ state: "hidden", timeout: FORM_TIMEOUT });
};
export const openTimedEventFormWithMouse = async (page: Page) => {
const draftEvent = page.locator('#mainGrid .active[role="button"]').first();
await retryUntil(
page,
async () => {
if (await draftEvent.isVisible().catch(() => false)) {
await draftEvent.click({ force: true });
return;
}
await clickGridCenter(page, page.locator("#mainGrid"));
try {
await draftEvent.waitFor({ state: "visible", timeout: 1000 });
await draftEvent.click({ force: true });
} catch {
// Retry will create a fresh draft if the preview did not appear yet.
}
},
getFormTitleInput(page),
{ maxAttempts: 4, perAttemptTimeout: 4000 },
);
};
export const openAllDayEventFormWithMouse = async (page: Page) => {
await retryUntil(
page,
() => clickGridCenter(page, page.locator("#allDayRow")),
getFormTitleInput(page),
);
};
export const openSomedayEventFormWithMouse = async (
page: Page,
section: SomedaySection,
) => {
await ensureSidebarOpen(page);
const plusButtons = page
.locator("#sidebar")
.getByRole("button", { name: "+" });
const targetIndex = section === "week" ? 0 : 1;
await plusButtons.nth(targetIndex).click();
};
export const openTimedEventFormWithKeyboard = async (page: Page) => {
await retryUntil(
page,
async () => {
await blurActiveElement(page);
await page.locator("#mainGrid").focus();
await pressShortcut(page, "c");
},
getFormTitleInput(page),
);
};
export const openAllDayEventFormWithKeyboard = async (page: Page) => {
await retryUntil(
page,
async () => {
await blurActiveElement(page);
await page.locator("#mainGrid").focus();
await pressShortcut(page, "a");
},
getFormTitleInput(page),
);
};
export const openSomedayEventFormWithKeyboard = async (page: Page) => {
await blurActiveElement(page);
await ensureSidebarOpen(page);
await page.keyboard.press("Shift+w");
const somedayForm = page.locator('form[name="Someday Event Form"]');
await somedayForm.waitFor({ state: "visible", timeout: FORM_TIMEOUT });
await somedayForm.getByPlaceholder("Title").waitFor({
state: "visible",
timeout: FORM_TIMEOUT,
});
};
export const openEventForEditingWithKeyboard = async (
page: Page,
eventTitle: string,
) => {
const eventButton = await findEventButton(page, eventTitle);
if (!eventButton) {
throw new Error(
`Unable to locate event "${eventTitle}" for keyboard editing.`,
);
}
await eventButton.scrollIntoViewIfNeeded();
await eventButton.focus();
const box = await eventButton.boundingBox();
if (box) {
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
}
const titleInput = getFormTitleInput(page);
await retryUntil(
page,
async () => {
await eventButton.focus();
await page.keyboard.press("Enter");
// Also try space if Enter didn't work
if (!(await titleInput.isVisible().catch(() => false))) {
await page.keyboard.press(" ");
}
},
titleInput,
{ maxAttempts: 4 },
);
};
export const openEventForEditingWithMouse = async (
page: Page,
eventTitle: string,
) => {
const eventButton = await findEventButton(page, eventTitle);
if (!eventButton) {
throw new Error(
`Unable to locate event "${eventTitle}" for mouse editing.`,
);
}
await retryUntil(
page,
async () => {
await page.waitForTimeout(200);
await eventButton.click({ force: true });
},
getFormTitleInput(page),
);
};
const findEventButton = async (
page: Page,
eventTitle: string,
): Promise<Locator | null> => {
const containers = [
page.locator("#mainGrid"),
page.locator("#allDayRow"),
page.locator("#sidebar"),
];
for (const container of containers) {
const eventButtons = container
.locator('[data-event-id][role="button"]')
.filter({ hasText: eventTitle });
const buttonCount = await eventButtons.count();
if (buttonCount > 0) {
return eventButtons.nth(buttonCount - 1);
}
}
const sidebarButton = page
.locator("#sidebar")
.getByRole("button", { name: eventTitle });
if ((await sidebarButton.count()) > 0) {
return sidebarButton.first();
}
const allDayButton = page
.locator("#allDayRow")
.getByRole("button", { name: eventTitle });
if ((await allDayButton.count()) > 0) {
return allDayButton.last();
}
const timedButton = page.locator("#mainGrid").getByRole("button", {
name: eventTitle,
});
if ((await timedButton.count()) > 0) {
return timedButton.last();
}
const activeButton = page.locator(".active", { hasText: eventTitle });
return (await activeButton.count()) > 0 ? activeButton.first() : null;
};
export const deleteEventWithMouse = async (page: Page) => {
const form = page.getByRole("form");
await expect(form).toBeVisible();
page.once("dialog", (dialog) => dialog.accept());
await form.getByLabel("Open actions menu").click();
await page.getByRole("menuitem", { name: /delete/i }).click();
};
export const deleteEventWithKeyboard = async (page: Page) => {
await getFormTitleInput(page).waitFor({ timeout: FORM_TIMEOUT });
page.once("dialog", (dialog) => dialog.accept());
await page.keyboard.press("Delete");
};
export const expectTimedEventVisible = async (page: Page, title: string) => {
await expectWithCalendarRecovery(page, async () => {
await expect(
page.locator("#mainGrid").getByRole("button", { name: title }),
).toBeVisible({ timeout: 3000 });
});
};
export const expectAllDayEventVisible = async (page: Page, title: string) => {
await expectWithCalendarRecovery(page, async () => {
await expect(
page.locator("#allDayRow").getByRole("button", { name: title }),
).toBeVisible({ timeout: 3000 });
});
};
export const expectSomedayEventVisible = async (page: Page, title: string) =>
expectWithCalendarRecovery(
page,
async () => {
await expect(
page.locator("#sidebar").getByRole("button", { name: title }),
).toBeVisible({ timeout: 3000 });
},
{ openSidebar: true },
);
export const expectTimedEventMissing = async (page: Page, title: string) => {
await expectWithCalendarRecovery(page, async () => {
await expect(
page.locator("#mainGrid").getByRole("button", { name: title }),
).toHaveCount(0, { timeout: 3000 });
});
};
export const expectAllDayEventMissing = async (page: Page, title: string) => {
await expectWithCalendarRecovery(page, async () => {
await expect(
page.locator("#allDayRow").getByRole("button", { name: title }),
).toHaveCount(0, { timeout: 3000 });
});
};
export const expectSomedayEventMissing = async (page: Page, title: string) =>
expectWithCalendarRecovery(
page,
async () => {
await expect(
page.locator("#sidebar").getByRole("button", { name: title }),
).toHaveCount(0, { timeout: 3000 });
},
{ openSidebar: true },
);