This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
utils.js
102 lines (96 loc) · 2.87 KB
/
utils.js
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
const { expect } = require('@playwright/test')
const openFilters = async (page) => {
const filterButtonSelector =
'[aria-controls="filter-sidebar"], [aria-controls="filter-modal"]'
const isPressed = async () =>
await page.getAttribute(filterButtonSelector, 'aria-pressed')
if ((await isPressed()) !== 'true') {
await page.click(filterButtonSelector)
expect(await isPressed()).toEqual('true')
}
}
const assertCheckboxCheckedStatus = async (page, label, checked = true) => {
const checkbox = page.locator(`label:has-text("${label}")`)
if (checked) {
await expect(checkbox).toBeChecked()
} else {
await expect(checkbox).not.toBeChecked()
}
}
/**
* Replace all the thumbnail requests with a single sample image
*/
const mockThumbnails = async (context) => {
await context.route(
'https://api.openverse.engineering/v1/thumbs/**',
(route) =>
route.fulfill({
path: 'test/e2e/resources/sample_image.jpg',
headers: { 'Access-Control-Allow-Origin': '*' },
})
)
}
/**
* Replace all the image search requests with mock data
*/
const mockImageSearch = async (context) => {
// Serve mock data on all image search requests
await context.route(
'https://api.openverse.engineering/v1/images/?***',
(route) =>
route.fulfill({
path: 'test/e2e/resources/mock_image_data.json',
headers: { 'Access-Control-Allow-Origin': '*' },
})
)
}
/**
* Replace all the image search requests with mock data
*/
const mockAudioSearch = async (context) => {
// Serve mock data on all image search requests
await context.route(
'https://api.openverse.engineering/v1/audio/?***',
(route) =>
route.fulfill({
path: 'test/e2e/resources/mock_audio_data.json',
headers: { 'Access-Control-Allow-Origin': '*' },
})
)
}
const mockImages = async (context) => {
await context.route(/\.(png|jpeg|jpg|svg)$/, (route) => route.abort())
}
const changeContentType = async (page, to) => {
await page.click(
`button[aria-controls="content-switcher-popover"], button[aria-controls="content-switcher-modal"]`
)
await page.click(`button[role="radio"]:has-text("${to}")`)
}
/**
* Finds a button with a popup to the left of the filters button which doesn't have a 'menu' label
* @param page
* @returns {Promise<string>}
*/
const currentContentType = async (page) => {
const contentSwitcherButton = await page.locator(
`button[aria-controls="content-switcher-popover"], button[aria-controls="content-switcher-modal"]`
)
return contentSwitcherButton.textContent()
}
const mockAllSearch = async (context) => {
await Promise.all([
mockAudioSearch(context),
mockImageSearch(context),
mockThumbnails(context),
mockImages(context),
])
}
module.exports = {
openFilters,
changeContentType,
currentContentType,
assertCheckboxCheckedStatus,
mockAllSearch,
mockThumbnails,
}