-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.ts
229 lines (197 loc) · 6.68 KB
/
index.test.ts
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
import * as path from 'node:path';
import { Liquid } from 'liquidjs';
import {
type PleasantestUtils,
devices,
getAccessibilityTree,
withBrowser,
} from 'pleasantest';
const iPhone = devices['iPhone 11'];
// This particular example uses Liquid to load templates with data,
// but that is not needed for Pleasantest.
// You can use any way you'd like to load content into the browser.
const engine = new Liquid({
root: path.join(__dirname, 'templates'),
extname: '.html.liquid',
});
const renderMenu = async ({
utils,
data,
initJS = true,
}: {
utils: PleasantestUtils;
data: MenuData;
initJS?: boolean;
}) => {
const html = `${await engine.renderFile(
'index',
data,
)}<main><p>${mainContentText}</p></main>`;
await utils.injectHTML(html);
await utils.loadCSS('./index.css');
if (initJS) {
await utils.runJS(`
import { init } from '.'
init()
`);
}
};
interface MenuSection {
name: string;
link: string;
description: string;
}
interface MenuData {
companyName: string;
menuSections: MenuSection[];
}
const aboutText = 'This company was founded a long time ago';
const productsText = 'These are the products plz buy them';
const mainContentText = 'This is the main content';
const data: MenuData = {
companyName: 'Company',
menuSections: [
{
name: 'Products',
link: '/products',
description: productsText,
},
{
name: 'About',
link: '/about',
description: aboutText,
},
],
};
test(
'Renders desktop menus',
withBrowser(async ({ screen, utils, user }) => {
await renderMenu({ utils, data, initJS: false });
// Menu content should be hidden
await expect(await screen.getByText(aboutText)).not.toBeVisible();
// Before JS initializes the menus should be links
expect(await getAccessibilityTree(await screen.getByRole('navigation')))
.toMatchInlineSnapshot(`
navigation
heading "Company" (level=1)
link "Company"
text "Company"
list
listitem
link "Products"
text "Products"
listitem
link "About"
text "About"
listitem
link "Log In"
text "Log In"
`);
await utils.runJS(`
import { init } from '.'
init()
`);
// The menus should be upgraded to buttons,
const aboutBtn = await screen.getByRole('button', { name: /about/i });
expect(await getAccessibilityTree(await screen.getByRole('navigation')))
.toMatchInlineSnapshot(`
navigation
heading "Company" (level=1)
link "Company"
text "Company"
list
listitem
button "Products"
listitem
button "About"
listitem
link "Log In"
text "Log In"
`);
// Login should still be a link, since it does not trigger a menu to open
const loginBtn = await screen.getByRole('link', { name: /log in/i });
await expect(loginBtn).toHaveAttribute('href');
// Open the "about" menu and check its contents
await user.click(aboutBtn);
await expect(await screen.getByText(aboutText)).toBeVisible();
}),
);
test(
'Desktop menus toggle open/closed',
withBrowser(async ({ screen, utils, page, user }) => {
await renderMenu({ utils, data });
const aboutBtn = await screen.getByRole('button', { name: /about/i });
const productsBtn = await screen.getByRole('button', { name: /products/i });
// First click: opens about menu
await user.click(aboutBtn);
await expect(await screen.getByText(aboutText)).toBeVisible();
// Second click: closes about menu
await user.click(aboutBtn);
await expect(await screen.getByText(aboutText)).not.toBeVisible();
// Open products menu
await user.click(productsBtn);
await expect(await screen.getByText(productsText)).toBeVisible();
// Clicking about button should close products menu and open about menu
await user.click(aboutBtn);
await expect(await screen.getByText(productsText)).not.toBeVisible();
await expect(await screen.getByText(aboutText)).toBeVisible();
// Click near the bottom of the screen (outside the menu), and the menu should close
await page.mouse.click(page.viewport()!.width / 2, page.viewport()!.height);
await expect(await screen.getByText(aboutText)).not.toBeVisible();
}),
);
test(
'Renders toggle-able menu on small screens',
withBrowser({ device: iPhone }, async ({ screen, utils, user, page }) => {
await renderMenu({ utils, data, initJS: false });
// Menu content should be hidden
await expect(await screen.getByText(aboutText)).not.toBeVisible();
// Menu buttons should be hidden (into the menu)
const aboutButtons = await screen.queryAllByText(/about/i);
for (const aboutButton of aboutButtons) {
await expect(aboutButton).not.toBeVisible();
}
// You could also do:
// await Promise.all(aboutButtons.map((about) => expect(about).not.toBeVisible()));
const toggleMenuBtn = await screen.getByRole('button', {
name: /show menu/i,
});
await utils.runJS(`
import { init } from '.'
init()
`);
// Open menu
await user.click(toggleMenuBtn);
// The sub-menu buttons should be visible
const aboutBtn = await screen.getByRole('button', { name: /about/i });
await screen.getByRole('button', {
name: /products/i,
});
// Accessible text should have changed in toggle button icon
expect(await toggleMenuBtn.evaluate((el) => el.textContent)).toMatch(
/hide menu/i,
);
await expect(await screen.getByText(aboutText)).not.toBeVisible();
// First click: opens about menu
await user.click(aboutBtn);
await expect(await screen.getByText(aboutText)).toBeVisible();
// Second click: closes about menu
await user.click(aboutBtn);
await expect(await screen.getByText(aboutText)).not.toBeVisible();
// Open about menu again
await user.click(aboutBtn);
await expect(await screen.getByText(aboutText)).toBeVisible();
// Close the _outer_ menu, which should also close the about menu
await user.click(toggleMenuBtn);
await expect(await screen.getByText(aboutText)).not.toBeVisible();
// Accessible text should have changed in toggle button icon
expect(await toggleMenuBtn.evaluate((el) => el.textContent)).toMatch(
/show menu/i,
);
// Open outer menu again
await user.click(toggleMenuBtn);
// Click near the bottom of the screen (outside the menu), and the menu should close
await page.mouse.click(page.viewport()!.width / 2, page.viewport()!.height);
await expect(aboutBtn).not.toBeVisible();
}),
);