-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathworkspace-home.spec.ts
More file actions
325 lines (265 loc) · 10.4 KB
/
Copy pathworkspace-home.spec.ts
File metadata and controls
325 lines (265 loc) · 10.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
import { test, expect } from "./fixtures/auth";
import type { Locator } from "@playwright/test";
/**
* Returns the "All Pages" container using its data-testid attribute.
*/
function getAllPagesSection(main: Locator): Locator {
return main.locator('[data-testid="wh-all-pages"]');
}
/**
* Returns the page-list buttons inside the "All Pages" section only.
* Excludes buttons from "Recently Visited" and the filter/sort toolbar.
*/
function getPageListItems(main: Locator): Locator {
const section = getAllPagesSection(main);
return section.locator("button.text-left.text-sm");
}
/**
* Collects visible page titles from the page list (up to `max` items).
*/
async function collectTitles(
items: Locator,
max = 20,
): Promise<string[]> {
const count = Math.min(await items.count(), max);
const titles: string[] = [];
for (let i = 0; i < count; i++) {
const text = await items.nth(i).locator("span.flex-1").textContent();
titles.push((text ?? "Untitled").trim());
}
return titles;
}
/**
* Waits for the workspace home page to fully load inside #main-content.
*/
async function waitForWorkspaceHome(main: Locator): Promise<void> {
const filterInput = main.getByTestId("wh-filter-input");
await expect(filterInput).toBeVisible({ timeout: 15_000 });
}
test.describe("Workspace home page interactions", () => {
test("clicking 'New Page' creates a page and navigates to it", async ({
authenticatedPage: page,
}) => {
const main = page.locator("#main-content");
const newPageButton = main.getByTestId("wh-new-page-btn");
await expect(newPageButton).toBeVisible({ timeout: 15_000 });
await newPageButton.click();
// Should navigate to the new page — URL contains a UUID page ID
await page.waitForURL(
(url) => /\/[^/]+\/[0-9a-f-]{36}/.test(url.pathname),
{ timeout: 15_000 },
);
// The editor should be visible (contenteditable area or title input)
const editor = page
.locator('[contenteditable="true"]')
.or(page.locator('input[aria-label="Page title"]'));
await expect(editor.first()).toBeVisible({ timeout: 10_000 });
});
test("typing in the filter input filters the page list by title", async ({
authenticatedPage: page,
}) => {
const main = page.locator("#main-content");
await waitForWorkspaceHome(main);
const filterInput = main.getByTestId("wh-filter-input");
const pageItems = getPageListItems(main);
await expect(pageItems.first()).toBeVisible({ timeout: 10_000 });
const initialCount = await pageItems.count();
if (initialCount < 2) {
test.skip(true, "Need at least 2 pages to test filtering");
return;
}
// Find a page with a non-empty, non-"Untitled" title for a meaningful filter
let searchTerm = "";
const titleCount = Math.min(initialCount, 20);
for (let i = 0; i < titleCount; i++) {
const text = await pageItems
.nth(i)
.locator("span.flex-1")
.textContent();
const trimmed = (text ?? "").trim();
if (trimmed && trimmed !== "Untitled") {
searchTerm = trimmed;
break;
}
}
if (!searchTerm) {
// Fall back to "Untitled" if all pages are untitled
searchTerm = "Untitled";
}
// Type the filter
await filterInput.fill(searchTerm);
// Wait for the list to update — filtered count should be <= initial
await expect(async () => {
const filteredCount = await pageItems.count();
expect(filteredCount).toBeGreaterThan(0);
expect(filteredCount).toBeLessThanOrEqual(initialCount);
}).toPass({ timeout: 5_000 });
// Verify the first few visible items contain the filter text
const titles = await collectTitles(pageItems, 5);
for (const title of titles) {
expect(title.toLowerCase()).toContain(searchTerm.toLowerCase());
}
});
test("changing the sort dropdown reorders the page list", async ({
authenticatedPage: page,
}) => {
const main = page.locator("#main-content");
await waitForWorkspaceHome(main);
const sortTrigger = main.getByTestId("wh-sort-dropdown");
const pageItems = getPageListItems(main);
await expect(pageItems.first()).toBeVisible({ timeout: 10_000 });
const initialCount = await pageItems.count();
if (initialCount < 2) {
test.skip(true, "Need at least 2 pages to test sorting");
return;
}
// Collect titles in default order (Last modified)
const titlesDefault = await collectTitles(pageItems, 10);
// Switch to "Title A-Z"
await sortTrigger.click();
await page.getByRole("option", { name: "Title A-Z" }).click();
// Wait for the list to stabilize
await expect(async () => {
const count = await pageItems.count();
expect(count).toBe(initialCount);
}).toPass({ timeout: 5_000 });
// Collect titles in A-Z order
const titlesAZ = await collectTitles(pageItems, 10);
// Verify A-Z order: each consecutive pair should be in ascending order
for (let i = 1; i < titlesAZ.length; i++) {
expect(titlesAZ[i - 1].localeCompare(titlesAZ[i])).toBeLessThanOrEqual(0);
}
// Switch to "Title Z-A"
await sortTrigger.click();
await page.getByRole("option", { name: "Title Z-A" }).click();
await expect(async () => {
const count = await pageItems.count();
expect(count).toBe(initialCount);
}).toPass({ timeout: 5_000 });
// Collect titles in Z-A order
const titlesZA = await collectTitles(pageItems, 10);
// Verify Z-A order: each consecutive pair should be in descending order
for (let i = 1; i < titlesZA.length; i++) {
expect(titlesZA[i - 1].localeCompare(titlesZA[i])).toBeGreaterThanOrEqual(
0,
);
}
// Verify A-Z and Z-A are different orderings (not all identical titles)
const allSame = titlesAZ.every((t) => t === titlesAZ[0]);
if (!allSame) {
expect(titlesAZ).not.toEqual(titlesZA);
}
});
test("clicking a page in the list navigates to that page", async ({
authenticatedPage: page,
}) => {
const main = page.locator("#main-content");
await waitForWorkspaceHome(main);
const pageItems = getPageListItems(main);
await expect(pageItems.first()).toBeVisible({ timeout: 10_000 });
const count = await pageItems.count();
if (count === 0) {
test.skip(true, "No pages available to navigate to");
return;
}
const urlBefore = page.url();
// Click the first page item
await pageItems.first().click();
// Should navigate to the page — URL contains a UUID page ID
await page.waitForURL(
(url) => /\/[^/]+\/[0-9a-f-]{36}/.test(url.pathname),
{ timeout: 15_000 },
);
expect(page.url()).not.toBe(urlBefore);
// The page should render — either an editor or a database view
const editor = page.locator('[contenteditable="true"]');
const dbView = page.locator('[data-testid="db-view-tabs"]');
const titleInput = page.locator('input[aria-label="Page title"]');
await expect(editor.or(dbView).or(titleInput).first()).toBeVisible({
timeout: 10_000,
});
});
test("'Clear filter' button appears for non-matching filter and resets the list", async ({
authenticatedPage: page,
}) => {
const main = page.locator("#main-content");
await waitForWorkspaceHome(main);
const filterInput = main.getByTestId("wh-filter-input");
const pageItems = getPageListItems(main);
await expect(pageItems.first()).toBeVisible({ timeout: 10_000 });
const initialCount = await pageItems.count();
if (initialCount === 0) {
test.skip(true, "No pages available to test clear filter");
return;
}
// Type a filter that matches nothing
await filterInput.fill("zzz_nonexistent_page_xyz_12345");
// The "No matches" state should appear with a "Clear filter" button
const clearButton = main.getByRole("button", { name: /clear filter/i });
await expect(clearButton).toBeVisible({ timeout: 5_000 });
await expect(main.locator("text=No matches")).toBeVisible();
// Click "Clear filter"
await clearButton.click();
// The filter input should be cleared
await expect(filterInput).toHaveValue("");
// The page list should be restored
await expect(async () => {
const restoredCount = await pageItems.count();
expect(restoredCount).toBe(initialCount);
}).toPass({ timeout: 5_000 });
});
test("'Recently Visited' section shows pages the user has visited", async ({
authenticatedPage: page,
}) => {
const main = page.locator("#main-content");
await waitForWorkspaceHome(main);
const pageItems = getPageListItems(main);
await expect(pageItems.first()).toBeVisible({ timeout: 10_000 });
const count = await pageItems.count();
if (count === 0) {
test.skip(true, "No pages available to visit");
return;
}
// Get the title of the page we'll visit
const visitedTitle = await pageItems
.first()
.locator("span.flex-1")
.textContent();
// Click the page to visit it (this records a page_visit server-side)
await pageItems.first().click();
// Wait for the page to load
await page.waitForURL(
(url) => /\/[^/]+\/[0-9a-f-]{36}/.test(url.pathname),
{ timeout: 15_000 },
);
const editor = page.locator('[contenteditable="true"]');
const dbView = page.locator('[data-testid="db-view-tabs"]');
const titleInput = page.locator('input[aria-label="Page title"]');
await expect(editor.or(dbView).or(titleInput).first()).toBeVisible({
timeout: 10_000,
});
// Navigate back to the workspace home
const pathSegments = new URL(page.url()).pathname
.split("/")
.filter(Boolean);
const workspaceSlug = pathSegments[0];
await page.goto(`/${workspaceSlug}`);
// Wait for the workspace home to reload
await waitForWorkspaceHome(main);
// The "Recently Visited" section should be visible
const recentHeading = main.locator("h2").filter({
hasText: /recently visited/i,
});
await expect(recentHeading).toBeVisible({ timeout: 10_000 });
// The visited page should appear in the recently visited list
if (visitedTitle && visitedTitle.trim()) {
const trimmedTitle = visitedTitle.trim();
// The recently visited section is the parent div of the heading
const recentContainer = recentHeading.locator("..").first();
const recentItem = recentContainer.locator("button", {
hasText: trimmedTitle,
});
await expect(recentItem.first()).toBeVisible({ timeout: 5_000 });
}
});
});