-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathofficial-site.spec.ts
More file actions
310 lines (255 loc) · 10.8 KB
/
official-site.spec.ts
File metadata and controls
310 lines (255 loc) · 10.8 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
import { expect, type Page, test } from "@playwright/test";
const BASE = "http://localhost:8080/";
test("Open documentation", async ({ page }) => {
await page.goto(BASE);
// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/SQLPage.*/);
// open the submenu
await page.getByText("Documentation", { exact: true }).first().click();
const components = ["form", "map", "chart", "button"];
for (const component of components) {
await expect(
page.getByRole("link", { name: component }).first(),
).toBeVisible();
}
});
test("chart", async ({ page }) => {
await page.goto(`${BASE}/documentation.sql?component=chart#component`);
await expect(page.getByText("Loading...")).not.toBeVisible();
await expect(page.locator(".apexcharts-canvas").first()).toBeVisible();
});
test("chart supports hiding legend", async ({ page }) => {
await page.goto(`${BASE}/documentation.sql?component=chart#component`);
const expensesChart = page.locator(".card", {
has: page.getByRole("heading", { name: "Expenses" }),
});
await expect(expensesChart.locator(".apexcharts-canvas")).toBeVisible();
await expect(expensesChart.locator(".apexcharts-legend")).toBeHidden();
});
test("map", async ({ page }) => {
await page.goto(`${BASE}/documentation.sql?component=map#component`);
await expect(page.getByText("Loading...")).not.toBeVisible();
await expect(page.locator(".leaflet-marker-icon").first()).toBeVisible();
});
test("form example", async ({ page }) => {
await page.goto(`${BASE}/examples/multistep-form`);
// Single selection matching the value or label
await page.getByLabel("From").selectOption("Paris");
await page.getByText("Next").click();
await page.getByLabel(/\bTo\b/).selectOption("Mexico");
await page.getByText("Next").click();
await page.getByLabel("Number of Adults").fill("1");
await page.getByText("Next").click();
await page.getByLabel("Passenger 1 (adult)").fill("John Doe");
await page.getByText("Book the flight").click();
await expect(page.getByText("John Doe").first()).toBeVisible();
});
test("File upload", async ({ page }) => {
await page.goto(`${BASE}/your-first-sql-website`);
await page.getByRole("button", { name: "Examples", exact: true }).click();
await page.getByText("File uploads").click();
const my_svg = '<svg><text y="20">Hello World</text></svg>';
// @ts-ignore
const buffer = Buffer.from(my_svg);
await page.getByLabel("Picture").setInputFiles({
name: "small.svg",
mimeType: "image/svg+xml",
buffer,
});
await page.getByRole("button", { name: "Upload picture" }).click();
await expect(
page.locator("img[src^=data]").first().getAttribute("src"),
).resolves.toBe(`data:image/svg+xml;base64,${buffer.toString("base64")}`);
});
test("Authentication example", async ({ page }) => {
await page.goto(`${BASE}/examples/authentication/login.sql`);
await expect(page.locator("h1", { hasText: "Authentication" })).toBeVisible();
const usernameInput = page.getByLabel("Username");
const passwordInput = page.getByLabel("Password");
const loginButton = page.getByRole("button", { name: "Log in" });
await expect(usernameInput).toBeVisible();
await expect(passwordInput).toBeVisible();
await expect(loginButton).toBeVisible();
await usernameInput.fill("admin");
await passwordInput.fill("admin");
await loginButton.click();
await expect(page.getByText("You are logged in as admin")).toBeVisible();
});
test("table filtering", async ({ page }) => {
await page.goto(`${BASE}/documentation.sql?component=table`);
const tableSection = page.locator(".card", {
has: page.getByRole("cell", { name: "Chart", exact: true }),
});
const searchInput = tableSection.getByPlaceholder("Search…");
await searchInput.fill("chart");
const chartCell = tableSection.getByRole("cell", { name: "Chart" });
await expect(chartCell).toBeVisible();
await expect(chartCell).toHaveClass(/\b_col_name\b/);
await expect(chartCell).toHaveCSS("vertical-align", "middle");
await expect(
tableSection.getByRole("cell", { name: "Table" }),
).not.toBeVisible();
});
test("table sorting", async ({ page }) => {
await page.goto(`${BASE}/documentation.sql?component=table`);
const tableSection = page.locator(".table-responsive", {
has: page.getByRole("cell", { name: "31456" }),
});
// Test numeric sorting on id column
await tableSection.getByRole("button", { name: "id" }).click();
let ids = await tableSection.locator("td.id").allInnerTexts();
let numericIds = ids.map((id) => Number.parseInt(id));
const sortedIds = [...numericIds].sort((a, b) => a - b);
expect(numericIds).toEqual(sortedIds);
// Test reverse sorting
await tableSection.getByRole("button", { name: "id" }).click();
ids = await tableSection.locator("td.id").allInnerTexts();
numericIds = ids.map((id) => Number.parseInt(id));
const reverseSortedIds = [...numericIds].sort((a, b) => b - a);
expect(numericIds).toEqual(reverseSortedIds);
// Test amount in stock column sorting
await tableSection.getByRole("button", { name: "Amount in stock" }).click();
const amounts = await tableSection.locator("td.Amount").allInnerTexts();
const numericAmounts = amounts.map((amount) =>
Number.parseInt(amount.replace(/[^0-9]/g, "")),
);
const sortedAmounts = [...numericAmounts].sort((a, b) => a - b);
expect(numericAmounts).toEqual(sortedAmounts);
});
async function checkNoConsoleErrors(page: Page, component: string) {
const errors: string[] = [];
page.on("console", (msg) => {
if (msg.type() === "error") {
errors.push(msg.text());
}
});
await page.goto(`${BASE}/documentation.sql?component=${component}`);
await page.waitForLoadState();
expect(errors).toHaveLength(0);
}
test("no console errors on table page", async ({ page }) => {
await checkNoConsoleErrors(page, "table");
});
test("no console errors on chart page", async ({ page }) => {
await checkNoConsoleErrors(page, "chart");
});
test("no console errors on map page", async ({ page }) => {
await checkNoConsoleErrors(page, "map");
});
test("no console errors on card page", async ({ page }) => {
await checkNoConsoleErrors(page, "card");
});
test("CSP issues unique nonces per request", async ({ page }) => {
const csp1 = await (await page.goto(BASE)).headerValue(
"content-security-policy",
);
const csp2 = await (await page.reload()).headerValue(
"content-security-policy",
);
expect(csp1, `check if ${csp1} != ${csp2}`).not.toEqual(csp2);
});
test("form component documentation", async ({ page }) => {
await page.goto(`${BASE}/component.sql?component=form`);
// Find the form that contains radio buttons for component selection
const componentForm = page.locator("form", {
has: page.getByRole("radio", { name: "Chart" }),
});
// the form should be visible
await expect(componentForm).toBeVisible();
// Check that "form" is the first and default selected option
const mapRadio = componentForm.getByRole("radio", { name: "Map" });
await expect(mapRadio).toHaveValue("map");
await expect(mapRadio).toBeChecked();
// Select "Chart" option and submit
await componentForm.getByLabel("Chart").click({ force: true });
await componentForm.getByRole("button", { name: "Submit" }).click();
// Verify we're on the chart documentation page
await expect(
page.getByRole("heading", { name: /chart/i, level: 1 }),
).toBeVisible();
});
test("modal", async ({ page }) => {
await page.goto(`${BASE}/documentation.sql?component=modal#component`);
// get the button that opens the modal
const openButton = page.getByRole("button", { name: "Open a simple modal" });
await openButton.click();
const modal = page.getByRole("dialog", { label: "A modal box" });
await expect(modal).toBeVisible();
// close the modal
await page.keyboard.press("Escape");
await expect(modal).not.toBeVisible();
await openButton.click();
await expect(modal).toBeVisible();
await modal.getByRole("button", { label: "Close" }).first().click();
await expect(modal).not.toBeVisible();
});
test("table action buttons - edit_url and delete_url", async ({ page }) => {
await page.goto(`${BASE}/documentation.sql?component=table`);
const tableSection = page.locator(".table-responsive", {
has: page.getByRole("cell", { name: "PharmaCo" }),
});
const editButton = tableSection.getByTitle("Edit").first();
await expect(editButton).toBeVisible();
await expect(editButton).toHaveAttribute("href", /action=edit&update_id=\d+/);
const deleteButton = tableSection.getByTitle("Delete").first();
await expect(deleteButton).toBeVisible();
await expect(deleteButton).toHaveAttribute(
"href",
/action=delete&delete_id=\d+/,
);
});
test("table action buttons - custom_actions", async ({ page }) => {
await page.goto(`${BASE}/documentation.sql?component=table`);
const tableSection = page.locator(".table-responsive", {
has: page.getByRole("cell", { name: "PharmaCo" }),
});
const historyButton = tableSection
.getByTitle("View Standard History")
.first();
await expect(historyButton).toBeVisible();
await expect(historyButton).toHaveAttribute(
"href",
/action=history&standard_id=\d+/,
);
});
test("table action buttons - _sqlpage_actions", async ({ page }) => {
await page.goto(`${BASE}/documentation.sql?component=table`);
const tableSection = page.locator(".table-responsive", {
has: page.getByRole("cell", { name: "PharmaCo" }),
});
const pdfButtons = tableSection.getByTitle("View Presentation");
await expect(pdfButtons.first()).toBeVisible();
await expect(pdfButtons).toHaveCount(3);
const firstPdfButton = pdfButtons.first();
await expect(firstPdfButton).toHaveAttribute(
"href",
"https://sql-page.com/pgconf/2024-sqlpage-badass.pdf",
);
const setInUseButton = tableSection.getByTitle("Set In Use");
await expect(setInUseButton).toBeVisible();
await expect(setInUseButton).toHaveAttribute(
"href",
/action=set_in_use&standard_id=32/,
);
const retireButton = tableSection.getByTitle("Retire Standard");
await expect(retireButton).toBeVisible();
await expect(retireButton).toHaveAttribute(
"href",
/action=retire&standard_id=33/,
);
});
test("table action buttons - disabled action", async ({ page }) => {
await page.goto(`${BASE}/documentation.sql?component=table`);
const tableSection = page.locator(".table-responsive", {
has: page.getByRole("cell", { name: "PharmaCo" }),
});
const viewPresentationButtons = tableSection.getByTitle("View Presentation");
await expect(viewPresentationButtons).toHaveCount(3);
const actionColumnButtons = tableSection.locator(
"td._col_Action a[data-action='Action']",
);
await expect(actionColumnButtons).toHaveCount(3);
const emptyActionButton = actionColumnButtons.last();
await expect(emptyActionButton).toHaveAttribute("href", "null");
await expect(emptyActionButton).toHaveAttribute("title", "Action");
});