Skip to content

Commit 0d1b687

Browse files
committed
fix: refactored tests (#4068)
1 parent 97e2bbd commit 0d1b687

File tree

2 files changed

+167
-48
lines changed

2 files changed

+167
-48
lines changed
+59-42
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
import { expect, test } from "@playwright/test";
22
import {
3-
filter_regex,
3+
filterRegex,
44
getFirstElementTextLocator,
5+
testFilterBubbles,
6+
testFilterCounts,
7+
testFilterPersistence,
58
testFilterPresence,
69
} from "../testFunctions";
710
import { anvilFilters, anvilTabs, anvilTabTestOrder } from "./anvil-tabs";
811

912
test.describe.configure({ mode: "parallel" });
13+
const filter_index_list = [3, 4, 5, 7, 6, 2];
1014

1115
test("Check that all filters exist on the Datasets tab and are clickable", async ({
1216
page,
1317
}) => {
14-
await testFilterPresence(page, anvilTabs.datasets);
18+
await testFilterPresence(page, anvilTabs.datasets, anvilFilters);
1519
});
1620

1721
test("Check that all filters exist on the Donors tab and are clickable", async ({
1822
page,
1923
}) => {
20-
await testFilterPresence(page, anvilTabs.donors);
24+
await testFilterPresence(page, anvilTabs.donors, anvilFilters);
2125
});
2226

2327
test("Check that all filters exist on the BioSamples tab and are clickable", async ({
2428
page,
2529
}) => {
26-
await testFilterPresence(page, anvilTabs.biosamples);
30+
await testFilterPresence(page, anvilTabs.biosamples, anvilFilters);
2731
});
2832

2933
test("Check that all filters exist on the Activities tab and are clickable", async ({
3034
page,
3135
}) => {
32-
await testFilterPresence(page, anvilTabs.activities);
36+
await testFilterPresence(page, anvilTabs.activities, anvilFilters);
3337
});
3438

3539
test("Check that all filters exist on the Files tab and are clickable", async ({
3640
page,
3741
}) => {
38-
await testFilterPresence(page, anvilTabs.files);
42+
await testFilterPresence(page, anvilTabs.files, anvilFilters);
3943
});
4044

4145
test("Check that the first filter on the Datasets tab creates at least one checkbox, and that checking up to the first five does not cause an error and does not cause there to be no entries in the table", async ({
@@ -51,9 +55,7 @@ test("Check that the first filter on the Datasets tab creates at least one check
5155
await page
5256
.getByRole("button")
5357
.getByText(
54-
filter_regex(
55-
anvilFilters[Math.floor(Math.random() * anvilFilters.length)]
56-
)
58+
filterRegex(anvilFilters[Math.floor(Math.random() * anvilFilters.length)])
5759
)
5860
.click();
5961
// Expect all checkboxes to be unchecked initially and to work properly
@@ -70,39 +72,54 @@ test("Check that the first filter on the Datasets tab creates at least one check
7072
await expect(getFirstElementTextLocator(page, 0)).toBeVisible();
7173
});
7274

73-
test("Check that filter checkboxes are persistent across pages", async ({
75+
test("Check that filter checkboxes are persistent across pages on an arbitrary filter", async ({
7476
page,
7577
}) => {
76-
// Randomly select a filter
77-
const test_filter =
78-
anvilFilters[Math.floor(Math.random() * anvilFilters.length)];
79-
// Start on the first tab in the test order (should be files)
80-
await page.goto(anvilTabs[anvilTabTestOrder[0]].url);
81-
await expect(
82-
page.getByRole("tab").getByText(anvilTabs.datasets.tabName)
83-
).toBeVisible();
84-
// Select the first checkbox on the test filter
85-
await page.getByText(filter_regex(test_filter)).click();
86-
await expect(page.getByRole("checkbox").first()).not.toBeChecked();
87-
await page.getByRole("checkbox").first().click();
88-
await expect(page.getByRole("checkbox").first()).toBeChecked();
89-
await page.locator("body").click();
90-
// Expect at least some text to still be visible
91-
await expect(getFirstElementTextLocator(page, 0)).toBeVisible();
92-
// For each tab, check that the selected filter is still checked
93-
for (const tab of anvilTabTestOrder.slice(1)) {
94-
await page.getByRole("tab").getByText(anvilTabs[tab].tabName).click();
95-
await expect(page.getByText(filter_regex(test_filter))).toBeVisible();
96-
await page.getByText(filter_regex(test_filter)).click();
97-
await expect(page.getByRole("checkbox").first()).toBeChecked();
98-
await page.locator("body").click();
99-
}
100-
// Return to the start tab and confirm that the filter stays checked and that some content is visible
101-
await page
102-
.getByRole("tab")
103-
.getByText(anvilTabs[anvilTabTestOrder[0]].tabName)
104-
.click();
105-
await expect(getFirstElementTextLocator(page, 0)).toBeVisible();
106-
await page.getByText(filter_regex(test_filter)).click();
107-
await expect(page.getByRole("checkbox").first()).toBeChecked();
78+
await testFilterPersistence(
79+
page,
80+
anvilFilters[3],
81+
anvilTabTestOrder.map((x) => anvilTabs[x])
82+
);
83+
});
84+
85+
test("Check that filter menu counts match actual counts on the Datasets tab", async ({
86+
page,
87+
}) => {
88+
await testFilterCounts(
89+
page,
90+
anvilTabs.datasets,
91+
filter_index_list.map((x) => anvilFilters[x]),
92+
25
93+
);
94+
});
95+
96+
test("Check that filter menu counts match actual counts on the Activities tab", async ({
97+
page,
98+
}) => {
99+
await testFilterCounts(
100+
page,
101+
anvilTabs.activities,
102+
filter_index_list.map((x) => anvilFilters[x]),
103+
25
104+
);
105+
});
106+
107+
test("Check that the blue filter bubbles match the selected filter for an arbitrary filter on the Files tab", async ({
108+
page,
109+
}) => {
110+
await testFilterBubbles(
111+
page,
112+
anvilTabs.files,
113+
filter_index_list.map((x) => anvilFilters[x])
114+
);
115+
});
116+
117+
test("Check that the blue filter bubbles match the selected filter for an arbitrary filter on the BioSamples tab", async ({
118+
page,
119+
}) => {
120+
await testFilterBubbles(
121+
page,
122+
anvilTabs.biosamples,
123+
filter_index_list.map((x) => anvilFilters[x])
124+
);
108125
});

explorer/e2e/testFunctions.ts

+108-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { expect, Locator, Page } from "@playwright/test";
2-
import { anvilFilters } from "./anvil/anvil-tabs";
32
import { TabDescription } from "./testInterfaces";
43

54
/* eslint-disable sonarjs/no-duplicate-string -- ignoring duplicate strings here */
@@ -216,25 +215,128 @@ export async function testPreSelectedColumns(
216215
}
217216
}
218217

219-
export const filter_regex = (filter: string): RegExp =>
218+
export const filterRegex = (filter: string): RegExp =>
220219
new RegExp(filter + "\\s+\\([0-9]+\\)\\s*");
221220

222221
export async function testFilterPresence(
223222
page: Page,
224-
tab: TabDescription
223+
tab: TabDescription,
224+
filters: string[]
225225
): Promise<void> {
226226
// Goto the selected tab
227227
await page.goto(tab.url);
228228
await expect(page.getByRole("tab").getByText(tab.tabName)).toBeVisible();
229-
for (const filter of anvilFilters) {
229+
for (const filter of filters) {
230230
// Check that each filter is visible and clickable
231-
await expect(page.getByText(filter_regex(filter))).toBeVisible();
232-
await page.getByText(filter_regex(filter)).click();
231+
await expect(page.getByText(filterRegex(filter))).toBeVisible();
232+
await page.getByText(filterRegex(filter)).click();
233+
await expect(page.getByRole("checkbox").first()).toBeVisible();
233234
await expect(page.getByRole("checkbox").first()).not.toBeChecked();
234235
// Check that clicking out of the filter menu causes it to disappear
235236
await page.locator("body").click();
236237
await expect(page.getByRole("checkbox")).toHaveCount(0);
237238
}
238239
}
239240

241+
export const getNamedFilterButton = (
242+
page: Page,
243+
filterName: string
244+
): Locator => {
245+
return page
246+
.getByRole("button")
247+
.filter({ has: page.getByRole("checkbox"), hasText: filterName });
248+
};
249+
export const getFirstFilterButton = (page: Page): Locator => {
250+
return page
251+
.getByRole("button")
252+
.filter({ has: page.getByRole("checkbox") })
253+
.first();
254+
};
255+
256+
export async function testFilterPersistence(
257+
page: Page,
258+
testFilter: string,
259+
tabOrder: TabDescription[]
260+
): Promise<void> {
261+
// Start on the first tab in the test order (should be files)
262+
await page.goto(tabOrder[0].url);
263+
// Select the first checkbox on the test filter
264+
await page.getByText(filterRegex(testFilter)).click();
265+
const to_select = await getFirstFilterButton(page);
266+
await expect(to_select.getByRole("checkbox")).not.toBeChecked();
267+
await to_select.getByRole("checkbox").setChecked(true);
268+
const filterName = (await to_select.innerText()).split("\n")[0]; //MAY NEED TO ADD SOME CHECKING MECHANISM HERE
269+
await expect(to_select.getByRole("checkbox")).toBeChecked();
270+
await page.locator("body").click();
271+
// Expect at least some text to still be visible
272+
await expect(getFirstElementTextLocator(page, 0)).toBeVisible();
273+
// For each tab, check that the selected filter is still checked
274+
for (const tab of tabOrder.slice(1)) {
275+
await page.getByRole("tab").getByText(tab.tabName).click();
276+
await expect(page.getByText(filterRegex(testFilter))).toBeVisible();
277+
await page.getByText(filterRegex(testFilter)).click();
278+
const previously_selected = getNamedFilterButton(page, filterName);
279+
await expect(previously_selected.getByRole("checkbox")).toBeChecked();
280+
await page.locator("body").click();
281+
}
282+
// Return to the start tab and confirm that the filter stays checked and that some content is visible
283+
await page.getByRole("tab").getByText(tabOrder[0].tabName).click();
284+
await expect(getFirstElementTextLocator(page, 0)).toBeVisible();
285+
await page.getByText(filterRegex(testFilter)).click();
286+
const previously_selected = getFirstFilterButton(page);
287+
await expect(previously_selected).toContainText(filterName, {
288+
useInnerText: true,
289+
});
290+
await expect(previously_selected.getByRole("checkbox").first()).toBeChecked();
291+
}
292+
293+
export async function testFilterCounts(
294+
page: Page,
295+
tab: TabDescription,
296+
filters: string[],
297+
elements_per_page: number
298+
): Promise<void> {
299+
await page.goto(tab.url);
300+
// For each arbitrarily selected filter
301+
for (const filter of filters) {
302+
// Select the filter
303+
await page.getByText(filterRegex(filter)).click();
304+
// Get the number associated with the first filter button, and select it
305+
const filter_button = getFirstFilterButton(page);
306+
const filterNumber = Number(
307+
(await filter_button.innerText()).split("\n")[1]
308+
);
309+
await filter_button.getByRole("checkbox").setChecked(true);
310+
//
311+
await page.locator("body").click();
312+
const firstNumber =
313+
filterNumber <= elements_per_page ? filterNumber : elements_per_page;
314+
await expect(
315+
page.getByText("Results 1 - " + firstNumber + " of " + filterNumber)
316+
).toBeVisible();
317+
}
318+
}
319+
320+
export async function testFilterBubbles(
321+
page: Page,
322+
tab: TabDescription,
323+
filters: string[]
324+
): Promise<void> {
325+
page.goto(tab.url);
326+
for (const filter of filters) {
327+
await page.getByText(filterRegex(filter)).click();
328+
const firstFilterButton = getFirstFilterButton(page);
329+
const firstFilterName = (await firstFilterButton.innerText()).split(
330+
"\n"
331+
)[0];
332+
await firstFilterButton.getByRole("checkbox").setChecked(true);
333+
await page.locator("body").click();
334+
const filterBlueButton = page
335+
.getByRole("button")
336+
.getByText(firstFilterName);
337+
await expect(filterBlueButton).toBeVisible();
338+
await filterBlueButton.click();
339+
await expect(filterBlueButton).toHaveCount(0);
340+
}
341+
}
240342
/* eslint-enable sonarjs/no-duplicate-string -- Checking duplicate strings again*/

0 commit comments

Comments
 (0)