-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcontent-actions.spec.ts
More file actions
802 lines (688 loc) · 28.1 KB
/
Copy pathcontent-actions.spec.ts
File metadata and controls
802 lines (688 loc) · 28.1 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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/**
* Content Actions E2E Tests
*
* Tests content lifecycle actions that go beyond basic CRUD:
* - Schedule / unschedule for future publishing
* - Duplicate content
* - Soft delete (trash) and restore from trash
* - Permanent delete
* - Discard draft changes (revert to published version)
*
* Uses the seeded "posts" collection which supports drafts and revisions.
*/
import type { Locator, Page } from "@playwright/test";
import { test, expect } from "../fixtures";
// ---------- regex patterns ----------
const SCHEDULE_API_PATTERN = /\/api\/content\/posts\/[A-Z0-9]+\/schedule/;
const DUPLICATE_API_PATTERN = /\/api\/content\/posts\/[A-Z0-9]+\/duplicate/;
const DISCARD_DRAFT_API_PATTERN = /\/api\/content\/posts\/[A-Z0-9]+\/discard-draft/;
const RESTORE_API_PATTERN = /\/api\/content\/posts\/[A-Z0-9]+\/restore/;
const PERMANENT_DELETE_API_PATTERN = /\/api\/content\/posts\/[A-Z0-9]+\/permanent/;
// Button/tab label patterns
const TRASH_TAB_LABEL = /Trash/i;
const RESTORE_LABEL = /Restore/i;
const PERM_DELETE_LABEL = /Permanently delete/i;
// ---------- helpers ----------
function apiHeaders(token: string, baseUrl: string) {
return {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"X-EmDash-Request": "1",
Origin: baseUrl,
};
}
function localDateKey(date: Date): string {
return [
date.getFullYear(),
String(date.getMonth() + 1).padStart(2, "0"),
String(date.getDate()).padStart(2, "0"),
].join("-");
}
async function selectTomorrow(dialog: Locator) {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
await dialog.locator(`[data-day="${localDateKey(tomorrow)}"] button`).click();
}
async function fillTime(page: Page, dialog: Locator, value: `${string}:${string}`) {
const [hour = "", minute = ""] = value.split(":");
const hour24 = Number(hour);
const displayHour = String(hour24 % 12 || 12).padStart(2, "0");
await dialog.getByRole("textbox", { name: "Hour" }).fill(displayHour);
await dialog.getByRole("textbox", { name: "Minute" }).fill(minute);
const desiredPeriod = hour24 >= 12 ? "PM" : "AM";
const period = dialog.getByRole("combobox", { name: "Period" });
if (!(await period.textContent())?.includes(desiredPeriod)) {
await period.click();
await page.getByRole("option", { name: desiredPeriod, exact: true }).click();
}
}
/** Create a post via API and return its ID */
async function createPost(
baseUrl: string,
headers: Record<string, string>,
title: string,
slug: string,
): Promise<string> {
const res = await fetch(`${baseUrl}/_emdash/api/content/posts`, {
method: "POST",
headers,
body: JSON.stringify({ data: { title }, slug }),
});
const json: any = await res.json();
return json.data?.item?.id ?? json.data?.id;
}
/** Publish a post via API */
async function publishPost(
baseUrl: string,
headers: Record<string, string>,
id: string,
): Promise<void> {
await fetch(`${baseUrl}/_emdash/api/content/posts/${id}/publish`, {
method: "POST",
headers,
body: JSON.stringify({}),
});
}
/** Soft-delete a post via API (move to trash) */
async function trashPost(
baseUrl: string,
headers: Record<string, string>,
id: string,
): Promise<void> {
await fetch(`${baseUrl}/_emdash/api/content/posts/${id}`, {
method: "DELETE",
headers,
});
}
/** Clean up a post — trash then permanently delete, ignoring errors */
async function cleanupPost(
baseUrl: string,
headers: Record<string, string>,
id: string,
): Promise<void> {
await fetch(`${baseUrl}/_emdash/api/content/posts/${id}`, {
method: "DELETE",
headers,
}).catch(() => {});
await fetch(`${baseUrl}/_emdash/api/content/posts/${id}/permanent`, {
method: "DELETE",
headers,
}).catch(() => {});
}
// ==========================================================================
// Schedule / Unschedule
// ==========================================================================
test.describe("Schedule content", () => {
let headers: Record<string, string>;
let baseUrl: string;
let postId: string;
let postSlug: string;
test.beforeEach(async ({ admin, serverInfo }) => {
await admin.devBypassAuth();
baseUrl = serverInfo.baseUrl;
headers = apiHeaders(serverInfo.token, baseUrl);
// Create a fresh draft post for scheduling tests
postSlug = `schedule-test-${Date.now()}`;
postId = await createPost(baseUrl, headers, "Schedule Test Post", postSlug);
});
test.afterEach(async () => {
await cleanupPost(baseUrl, headers, postId);
});
test("schedule a draft post for future publishing", async ({ admin, page }) => {
await admin.goToEditContent("posts", postId);
await admin.waitForLoading();
// Verify we're on the edit page with our post
await expect(page.locator("#field-title")).toHaveValue("Schedule Test Post");
const publishButton = page.getByRole("button", { name: "Publish", exact: true });
await expect(publishButton).toBeVisible({ timeout: 5000 });
await publishButton.click();
await page.getByRole("menuitem", { name: /Schedule publication/ }).click();
const dialog = page.getByRole("dialog", { name: "Schedule publication" });
await expect(dialog).toBeVisible({ timeout: 5000 });
await selectTomorrow(dialog);
await fillTime(page, dialog, "14:43");
const minuteInput = dialog.getByRole("textbox", { name: "Minute" });
await expect(minuteInput).toHaveValue("43");
// Submit from the time field and wait for the API response.
const scheduleResponse = page.waitForResponse(
(res) =>
SCHEDULE_API_PATTERN.test(res.url()) &&
res.request().method() === "POST" &&
res.status() === 200,
{ timeout: 10000 },
);
await minuteInput.press("Enter");
await scheduleResponse;
// A toast confirming scheduling should appear
await expect(page.getByRole("heading", { name: "Scheduled" })).toBeVisible({ timeout: 5000 });
await expect(page.getByText("First publication", { exact: true })).toBeVisible({
timeout: 5000,
});
const scheduledButton = page.getByRole("button", { name: "Scheduled", exact: true });
await expect(scheduledButton).toBeVisible();
await scheduledButton.click();
await expect(page.getByRole("menuitem", { name: /Change schedule/ })).toBeVisible();
await expect(page.getByRole("menuitem", { name: /Remove schedule/ })).toBeVisible();
});
test("keeps publishing choices usable in a 320-pixel viewport", async ({ admin, page }) => {
await admin.goToEditContent("posts", postId);
await admin.waitForLoading();
await page.setViewportSize({ width: 320, height: 576 });
const publishButton = page.getByRole("button", { name: "Publish", exact: true });
await publishButton.focus();
await page.keyboard.press("Enter");
await page.keyboard.press("ArrowDown");
await expect(page.locator('[role="menuitem"][data-highlighted]')).toHaveCount(1);
await expect(page.getByText("Choose when this draft goes live", { exact: true })).toHaveCount(
0,
);
await page.keyboard.press("Escape");
await expect(publishButton).toBeFocused();
await publishButton.click();
const menu = page.getByRole("menu", { name: "Publish" });
const menuBox = await menu.boundingBox();
expect(menuBox).not.toBeNull();
expect(menuBox!.x).toBeGreaterThanOrEqual(0);
expect(menuBox!.x + menuBox!.width).toBeLessThanOrEqual(320);
await page.getByRole("menuitem", { name: /Schedule publication/ }).click();
const dialog = page.getByRole("dialog", { name: "Schedule publication" });
const dialogBox = await dialog.boundingBox();
expect(dialogBox).not.toBeNull();
expect(dialogBox!.x).toBeGreaterThanOrEqual(0);
expect(dialogBox!.x + dialogBox!.width).toBeLessThanOrEqual(320);
expect(dialogBox!.y).toBeGreaterThanOrEqual(0);
expect(dialogBox!.y + dialogBox!.height).toBeLessThanOrEqual(576);
const timeSpacing = await dialog.getByRole("group", { name: "Time" }).evaluate((fieldset) => {
const legend = fieldset.querySelector("legend")!;
const picker = legend.nextElementSibling!;
return picker.getBoundingClientRect().top - legend.getBoundingClientRect().bottom;
});
expect(timeSpacing).toBeGreaterThanOrEqual(7);
expect(timeSpacing).toBeLessThanOrEqual(9);
const calendarBox = await dialog.locator(".rdp-root").boundingBox();
expect(calendarBox).not.toBeNull();
const calendarStart = calendarBox!.x - dialogBox!.x;
const calendarEnd = dialogBox!.x + dialogBox!.width - (calendarBox!.x + calendarBox!.width);
expect(Math.abs(calendarStart - calendarEnd)).toBeLessThan(2);
await expect(dialog.getByRole("button", { name: /Tomorrow at/ })).toHaveCount(0);
await expect(dialog.getByRole("button", { name: /Next .* at/ })).toHaveCount(0);
expect(await dialog.evaluate((element) => element.scrollWidth <= element.clientWidth)).toBe(
true,
);
expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(
320,
);
});
test("unschedule a scheduled post", async ({ admin, page }) => {
// Schedule the post via API first
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
await fetch(`${baseUrl}/_emdash/api/content/posts/${postId}/schedule`, {
method: "POST",
headers,
body: JSON.stringify({ scheduledAt: tomorrow.toISOString() }),
});
await admin.goToEditContent("posts", postId);
await admin.waitForLoading();
await expect(page.getByText("First publication", { exact: true })).toBeVisible({
timeout: 5000,
});
// Click unschedule and wait for API response
const unscheduleResponse = page.waitForResponse(
(res) =>
SCHEDULE_API_PATTERN.test(res.url()) &&
res.request().method() === "DELETE" &&
res.status() === 200,
{ timeout: 10000 },
);
await page.getByRole("button", { name: "Scheduled", exact: true }).click();
await page.getByRole("menuitem", { name: /Remove schedule/ }).click();
await unscheduleResponse;
await expect(page.getByText("Draft version", { exact: true })).toBeVisible({
timeout: 5000,
});
await expect(page.getByRole("button", { name: "Publish", exact: true })).toBeVisible();
});
test("keeps the live version public while a draft update is scheduled and removed", async ({
admin,
page,
}) => {
await publishPost(baseUrl, headers, postId);
await admin.goToEditContent("posts", postId);
await admin.waitForLoading();
const title = page.locator("#field-title");
await expect(title).toHaveValue("Schedule Test Post");
await title.fill("Scheduled Draft Update");
await admin.clickSave();
await admin.waitForSaveComplete();
await expect(page.getByText("Live version", { exact: true })).toBeVisible();
await expect(page.getByText("Draft changes", { exact: true })).toBeVisible();
let publicResponse = await fetch(`${baseUrl}/posts/${postSlug}`);
expect(publicResponse.ok).toBe(true);
let publicHtml = await publicResponse.text();
expect(publicHtml).toContain("Schedule Test Post");
expect(publicHtml).not.toContain("Scheduled Draft Update");
const publicationDateTrigger = page.getByRole("button", {
name: /Change publication date:/,
});
const publishingSummary = page.getByRole("group", { name: "Publishing summary" });
const createdAndUpdatedTrigger = publishingSummary.getByRole("button", {
name: "Created and updated",
});
const statusIconBox = await publishingSummary.locator("svg").first().boundingBox();
const publicationLabelBox = await publicationDateTrigger
.getByText("Publication date", { exact: true })
.boundingBox();
const createdAndUpdatedLabelBox = await createdAndUpdatedTrigger
.locator("span")
.filter({ hasText: /^Created and updated$/ })
.last()
.boundingBox();
const publicationButtonBox = await publicationDateTrigger.boundingBox();
const createdAndUpdatedButtonBox = await createdAndUpdatedTrigger.boundingBox();
for (const box of [
statusIconBox,
publicationLabelBox,
createdAndUpdatedLabelBox,
publicationButtonBox,
createdAndUpdatedButtonBox,
]) {
expect(box).not.toBeNull();
}
expect(Math.abs(publicationLabelBox!.x - statusIconBox!.x)).toBeLessThanOrEqual(1);
expect(Math.abs(createdAndUpdatedLabelBox!.x - statusIconBox!.x)).toBeLessThanOrEqual(1);
expect(statusIconBox!.x - publicationButtonBox!.x).toBeGreaterThanOrEqual(7);
expect(statusIconBox!.x - createdAndUpdatedButtonBox!.x).toBeGreaterThanOrEqual(7);
const publicationDateLayout = await publicationDateTrigger.evaluate((element) => {
(element as HTMLElement).style.width = "296px";
const label = element.querySelector(".text-kumo-subtle")!;
const value = element.querySelector("time")!;
const lineCount = (target: Element) => {
const range = document.createRange();
range.selectNodeContents(target);
return new Set(Array.from(range.getClientRects(), (rect) => Math.round(rect.top))).size;
};
return {
height: element.getBoundingClientRect().height,
labelLines: lineCount(label),
valueLines: lineCount(value),
};
});
expect(publicationDateLayout).toEqual({ height: 36, labelLines: 1, valueLines: 1 });
await publicationDateTrigger.click();
const publicationDateDialog = page.getByRole("dialog", {
name: "Change publication date",
});
await expect(
publicationDateDialog.getByText("Change the recorded date for the live version.", {
exact: true,
}),
).toHaveCount(0);
await expect(publicationDateDialog.getByRole("textbox", { name: "Hour" })).toBeVisible();
await expect(publicationDateDialog.getByRole("textbox", { name: "Minute" })).toBeVisible();
expect(await publicationDateDialog.locator('input[type="time"]').count()).toBe(0);
await publicationDateDialog.getByRole("button", { name: "Cancel", exact: true }).click();
await expect(publicationDateTrigger).toBeFocused();
const publishChanges = page.getByRole("button", { name: "Publish changes", exact: true });
const publishChangesLayout = await publishChanges.evaluate((element) => {
const button = element.getBoundingClientRect();
const caret = element.querySelector("svg")!.getBoundingClientRect();
const label = element.querySelector("span.truncate")!.getBoundingClientRect();
return {
labelCenterOffset: Math.abs(
button.left + button.width / 2 - (label.left + label.width / 2),
),
trailingGap: button.right - caret.right,
};
});
expect(publishChangesLayout.labelCenterOffset).toBeLessThanOrEqual(1);
expect(publishChangesLayout.trailingGap).toBeLessThanOrEqual(16);
await publishChanges.click();
await page.getByRole("menuitem", { name: /Schedule changes/ }).click();
const dialog = page.getByRole("dialog", { name: "Schedule changes" });
await expect(
dialog.getByText("Choose when these changes replace the live version.", { exact: true }),
).toBeVisible();
await selectTomorrow(dialog);
await fillTime(page, dialog, "09:00");
const scheduleResponse = page.waitForResponse(
(res) =>
SCHEDULE_API_PATTERN.test(res.url()) &&
res.request().method() === "POST" &&
res.status() === 200,
{ timeout: 10000 },
);
await dialog.getByRole("button", { name: "Schedule changes", exact: true }).click();
await scheduleResponse;
await expect(page.getByRole("button", { name: "Scheduled update", exact: true })).toBeVisible({
timeout: 5000,
});
await expect(
page.getByText("Visitors see the published version until the scheduled update", {
exact: true,
}),
).toBeVisible();
await expect(page.getByText("Draft changes", { exact: true })).toBeVisible();
publicResponse = await fetch(`${baseUrl}/posts/${postSlug}`);
publicHtml = await publicResponse.text();
expect(publicHtml).toContain("Schedule Test Post");
expect(publicHtml).not.toContain("Scheduled Draft Update");
await page.getByRole("button", { name: "Scheduled update", exact: true }).click();
const unscheduleResponse = page.waitForResponse(
(res) =>
SCHEDULE_API_PATTERN.test(res.url()) &&
res.request().method() === "DELETE" &&
res.status() === 200,
{ timeout: 10000 },
);
await page.getByRole("menuitem", { name: /Remove schedule/ }).click();
await unscheduleResponse;
await expect(page.getByRole("button", { name: "Publish changes", exact: true })).toBeVisible({
timeout: 5000,
});
await expect(
page.getByText("Ready to publish now or schedule for later", { exact: true }),
).toBeVisible();
publicResponse = await fetch(`${baseUrl}/posts/${postSlug}`);
publicHtml = await publicResponse.text();
expect(publicHtml).toContain("Schedule Test Post");
expect(publicHtml).not.toContain("Scheduled Draft Update");
});
});
// ==========================================================================
// Duplicate
// ==========================================================================
test.describe("Duplicate content", () => {
let headers: Record<string, string>;
let baseUrl: string;
let postId: string;
let duplicateId: string | undefined;
test.beforeEach(async ({ admin, serverInfo }) => {
await admin.devBypassAuth();
baseUrl = serverInfo.baseUrl;
headers = apiHeaders(serverInfo.token, baseUrl);
postId = await createPost(
baseUrl,
headers,
"Duplicate Source Post",
`dup-source-${Date.now()}`,
);
});
test.afterEach(async () => {
await cleanupPost(baseUrl, headers, postId);
if (duplicateId) {
await cleanupPost(baseUrl, headers, duplicateId);
}
});
test("duplicate a post from the content list", async ({ admin, page }) => {
await admin.goToContent("posts");
await admin.waitForLoading();
// Find the row for our post and click the duplicate button
const row = page.locator("tr", { hasText: "Duplicate Source Post" });
await expect(row).toBeVisible({ timeout: 5000 });
const duplicateResponse = page.waitForResponse(
(res) =>
DUPLICATE_API_PATTERN.test(res.url()) &&
res.request().method() === "POST" &&
(res.status() === 200 || res.status() === 201),
{ timeout: 10000 },
);
await row.getByRole("button", { name: "Duplicate Duplicate Source Post" }).click();
const response = await duplicateResponse;
const body = await response.json();
duplicateId = body.data?.item?.id ?? body.data?.id;
// Wait for the list to refresh
await admin.waitForLoading();
// A copy should now appear in the list (typically with "(Copy)" suffix or similar)
// Reload to ensure fresh data
await page.reload();
await admin.waitForShell();
await admin.waitForLoading();
// The duplicate should exist -- verify via API since the title pattern may vary
expect(duplicateId).toBeTruthy();
const getRes = await fetch(`${baseUrl}/_emdash/api/content/posts/${duplicateId}`, {
headers,
});
expect(getRes.ok).toBe(true);
});
});
// ==========================================================================
// Trash (soft delete) and Restore
// ==========================================================================
test.describe("Trash and restore content", () => {
let headers: Record<string, string>;
let baseUrl: string;
let postId: string;
test.beforeEach(async ({ admin, serverInfo }) => {
await admin.devBypassAuth();
baseUrl = serverInfo.baseUrl;
headers = apiHeaders(serverInfo.token, baseUrl);
postId = await createPost(baseUrl, headers, "Trash Test Post", `trash-test-${Date.now()}`);
});
test.afterEach(async () => {
await cleanupPost(baseUrl, headers, postId);
});
test("move a post to trash from the content list", async ({ admin, page }) => {
await admin.goToContent("posts");
await admin.waitForLoading();
// Find the row and click the trash button
const row = page.locator("tr", { hasText: "Trash Test Post" });
await expect(row).toBeVisible({ timeout: 5000 });
await row.getByRole("button", { name: "Move Trash Test Post to trash" }).click();
// A confirmation dialog should appear
const dialog = page.locator('[role="dialog"]').filter({ hasText: "Move to Trash?" });
await expect(dialog).toBeVisible({ timeout: 5000 });
await expect(dialog.locator("text=Trash Test Post")).toBeVisible();
// Confirm the deletion
const deleteResponse = page.waitForResponse(
(res) =>
res.url().includes(`/api/content/posts/${postId}`) &&
res.request().method() === "DELETE" &&
res.status() === 200,
{ timeout: 10000 },
);
await dialog.getByRole("button", { name: "Move to Trash" }).click();
await deleteResponse;
// The dialog should close
await expect(dialog).not.toBeVisible({ timeout: 5000 });
// The post should no longer appear in the "All" tab
await admin.waitForLoading();
await expect(page.locator("tr", { hasText: "Trash Test Post" })).not.toBeVisible({
timeout: 5000,
});
});
test("restore a trashed post from the trash tab", async ({ admin, page }) => {
// Trash the post via API first
await trashPost(baseUrl, headers, postId);
await admin.goToContent("posts");
await admin.waitForLoading();
// Switch to the Trash tab
const trashTab = page.getByRole("tab", { name: TRASH_TAB_LABEL });
await expect(trashTab).toBeVisible({ timeout: 5000 });
await trashTab.click();
// Wait for trashed items to load
await admin.waitForLoading();
// The trashed post should appear
const trashedRow = page.locator("tr", { hasText: "Trash Test Post" });
await expect(trashedRow).toBeVisible({ timeout: 10000 });
// Click the restore button
const restoreResponse = page.waitForResponse(
(res) =>
RESTORE_API_PATTERN.test(res.url()) &&
res.request().method() === "POST" &&
res.status() === 200,
{ timeout: 10000 },
);
await trashedRow.getByRole("button", { name: RESTORE_LABEL }).click();
await restoreResponse;
// Wait for the list to refresh
await admin.waitForLoading();
// Switch back to the All tab
const allTab = page.getByRole("tab", { name: "All" });
await allTab.click();
await admin.waitForLoading();
// The post should be back in the main list
await expect(page.locator("tr", { hasText: "Trash Test Post" })).toBeVisible({
timeout: 10000,
});
});
});
// ==========================================================================
// Permanent delete
// ==========================================================================
test.describe("Permanent delete", () => {
let headers: Record<string, string>;
let baseUrl: string;
let postId: string;
test.beforeEach(async ({ admin, serverInfo }) => {
await admin.devBypassAuth();
baseUrl = serverInfo.baseUrl;
headers = apiHeaders(serverInfo.token, baseUrl);
postId = await createPost(baseUrl, headers, "Permanent Delete Post", `perm-del-${Date.now()}`);
// Trash it first -- permanent delete only works on trashed items
await trashPost(baseUrl, headers, postId);
});
test("permanently delete a trashed post", async ({ admin, page }) => {
await admin.goToContent("posts");
await admin.waitForLoading();
// Switch to the Trash tab
const trashTab = page.getByRole("tab", { name: TRASH_TAB_LABEL });
await trashTab.click();
await admin.waitForLoading();
// The trashed post should appear
const trashedRow = page.locator("tr", { hasText: "Permanent Delete Post" });
await expect(trashedRow).toBeVisible({ timeout: 10000 });
// Click the permanent delete button (trash icon in trash view)
await trashedRow.getByRole("button", { name: PERM_DELETE_LABEL }).click();
// A confirmation dialog should appear
const dialog = page.locator('[role="dialog"]').filter({ hasText: "Delete Permanently?" });
await expect(dialog).toBeVisible({ timeout: 5000 });
await expect(dialog.locator("text=Permanent Delete Post")).toBeVisible();
// Confirm permanent deletion
const permanentDeleteResponse = page.waitForResponse(
(res) =>
PERMANENT_DELETE_API_PATTERN.test(res.url()) &&
res.request().method() === "DELETE" &&
res.status() === 200,
{ timeout: 10000 },
);
await dialog.getByRole("button", { name: "Delete Permanently" }).click();
await permanentDeleteResponse;
// The dialog should close
await expect(dialog).not.toBeVisible({ timeout: 5000 });
// The post should disappear from the trash
await admin.waitForLoading();
await expect(page.locator("tr", { hasText: "Permanent Delete Post" })).not.toBeVisible({
timeout: 5000,
});
// Verify via API that the post is truly gone
const getRes = await fetch(`${baseUrl}/_emdash/api/content/posts/${postId}`, { headers });
expect(getRes.status).toBe(404);
});
});
// ==========================================================================
// Discard draft changes
// ==========================================================================
test.describe("Discard draft changes", () => {
let headers: Record<string, string>;
let baseUrl: string;
let postId: string;
test.beforeEach(async ({ admin, serverInfo }) => {
await admin.devBypassAuth();
baseUrl = serverInfo.baseUrl;
headers = apiHeaders(serverInfo.token, baseUrl);
// Create and publish a post
postId = await createPost(
baseUrl,
headers,
"Published Original Title",
`discard-draft-${Date.now()}`,
);
await publishPost(baseUrl, headers, postId);
});
test.afterEach(async () => {
await cleanupPost(baseUrl, headers, postId);
});
test("discard draft reverts to the published version", async ({ admin, page }) => {
// Navigate to the editor and make changes to create a draft
await admin.goToEditContent("posts", postId);
await admin.waitForLoading();
// Verify the published title
const titleInput = page.locator("#field-title");
await expect(titleInput).toHaveValue("Published Original Title");
// Edit the title
await titleInput.fill("Draft Modified Title");
// Save to create a draft revision
await admin.clickSave();
await admin.waitForSaveComplete();
await expect(page.getByText("Draft changes", { exact: true })).toBeVisible({ timeout: 5000 });
// The "Discard changes" button should be visible
const discardButton = page.getByRole("button", { name: "Discard changes" });
await expect(discardButton).toBeVisible({ timeout: 5000 });
await discardButton.click();
// A confirmation dialog should appear
const dialog = page.locator('[role="dialog"]').filter({ hasText: "Discard draft changes?" });
await expect(dialog).toBeVisible({ timeout: 5000 });
// Confirm discarding the draft
const discardResponse = page.waitForResponse(
(res) =>
DISCARD_DRAFT_API_PATTERN.test(res.url()) &&
res.request().method() === "POST" &&
res.status() === 200,
{ timeout: 10000 },
);
await dialog.getByRole("button", { name: "Discard changes" }).click();
await discardResponse;
// Wait for the page to update
await admin.waitForLoading();
// The title should revert to the published version
await expect(titleInput).toHaveValue("Published Original Title", { timeout: 10000 });
await expect(page.getByText("Draft changes", { exact: true })).not.toBeVisible({
timeout: 5000,
});
// The "Discard changes" button should also be gone
await expect(discardButton).not.toBeVisible({ timeout: 5000 });
});
});
// ==========================================================================
// Trash from the editor
// ==========================================================================
test.describe("Trash from editor", () => {
let headers: Record<string, string>;
let baseUrl: string;
let postId: string;
test.beforeEach(async ({ admin, serverInfo }) => {
await admin.devBypassAuth();
baseUrl = serverInfo.baseUrl;
headers = apiHeaders(serverInfo.token, baseUrl);
postId = await createPost(baseUrl, headers, "Editor Trash Post", `editor-trash-${Date.now()}`);
});
test.afterEach(async () => {
await cleanupPost(baseUrl, headers, postId);
});
test("move a post to trash from the editor sidebar", async ({ admin, page }) => {
await admin.goToEditContent("posts", postId);
await admin.waitForLoading();
// The "Move to Trash" button should be in the sidebar
const trashButton = page.getByRole("button", { name: "Move to Trash" });
await expect(trashButton).toBeVisible({ timeout: 5000 });
await trashButton.click();
// A confirmation dialog should appear
const dialog = page.locator('[role="dialog"]').filter({ hasText: "Move to Trash?" });
await expect(dialog).toBeVisible({ timeout: 5000 });
// Confirm trashing
const deleteResponse = page.waitForResponse(
(res) =>
res.url().includes(`/api/content/posts/${postId}`) &&
res.request().method() === "DELETE" &&
res.status() === 200,
{ timeout: 10000 },
);
await dialog.getByRole("button", { name: "Move to Trash" }).click();
await deleteResponse;
// Should navigate back to the content list (or show a confirmation)
// Verify the post is trashed via API
const getRes = await fetch(`${baseUrl}/_emdash/api/content/posts/${postId}`, { headers });
expect(getRes.status).toBe(404);
});
});