forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotkeys.spec.ts
145 lines (124 loc) · 4.67 KB
/
hotkeys.spec.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
import { test, expect, type Page } from '@playwright/test';
import translations from '../client/i18n/locales/english/translations.json';
import { authedRequest } from './utils/request';
import { getEditors } from './utils/editor';
import { alertToBeVisible } from './utils/alerts';
const links = {
basicJS1:
'/learn/javascript-algorithms-and-data-structures/basic-javascript/comment-your-javascript-code',
basicJS2:
'/learn/javascript-algorithms-and-data-structures/basic-javascript/declare-javascript-variables',
frontEnd1:
'/learn/front-end-development-libraries/front-end-development-libraries-projects/build-a-random-quote-machine',
frontEnd2:
'/learn/front-end-development-libraries/front-end-development-libraries-projects/build-a-markdown-previewer',
backEnd1:
'/learn/back-end-development-and-apis/back-end-development-and-apis-projects/timestamp-microservice',
backEnd2:
'learn/back-end-development-and-apis/back-end-development-and-apis-projects/request-header-parser-microservice',
video1:
'/learn/python-for-everybody/python-for-everybody/introduction-why-program',
video2:
'/learn/python-for-everybody/python-for-everybody/introduction-hardware-architecture'
};
const titles = {
basicJS1: /Comment Your JavaScript Code/,
basicJS2: /Declare JavaScript Variables/,
frontEnd2: /Build a Markdown Previewer/,
backEnd2: /Request Header Parser Microservice/,
video2: /Introduction: Hardware Architecture/
};
type PageId = keyof typeof titles;
// The hotkeys are attached to specific elements, so we need to wait for the
// wrapper to be focused before we can test the hotkeys.
const waitUntilListening = async (page: Page) =>
await expect(page.locator('#editor-layout')).toBeFocused();
// This is a hack to work around the fact that the page isn't always hydrated
// with the new content when the URL changes.
const waitUntilHydrated = async (page: Page, pageId: PageId) => {
await page.waitForURL(links[pageId]);
await expect(page).toHaveTitle(titles[pageId]);
await waitUntilListening(page);
};
test.beforeAll(async ({ request }) => {
await authedRequest({
request,
endpoint: 'update-my-keyboard-shortcuts',
method: 'put',
data: {
keyboardShortcuts: false
}
});
});
test.beforeEach(async ({ page }) => {
await page.goto('/settings');
const keyboardShortcutGroup = page.getByRole('group', {
name: translations.settings.labels['keyboard-shortcuts']
});
await keyboardShortcutGroup
.getByRole('button', { name: translations.buttons.on, exact: true })
.click();
// wait for the client to register the change:
await alertToBeVisible(page, translations.flash['keyboard-shortcut-updated']);
});
test.afterEach(
async ({ request }) =>
await authedRequest({
request,
method: 'put',
endpoint: 'update-my-keyboard-shortcuts',
data: {
keyboardShortcuts: false
}
})
);
// TODO: handle keyboard shortcuts on mobile
test.skip(({ isMobile }) => isMobile, 'Only test on desktop');
test('User can use shortcuts in and around the editor', async ({ page }) => {
await page.goto(links.basicJS1);
await expect(getEditors(page)).toBeFocused();
await getEditors(page).press('Escape');
await expect(getEditors(page)).not.toBeFocused();
await page.keyboard.press('n');
await waitUntilHydrated(page, 'basicJS2');
await page.keyboard.press('p');
await waitUntilHydrated(page, 'basicJS1');
await page.keyboard.press('e');
await expect(getEditors(page)).toBeFocused();
await getEditors(page).press('Escape');
await page.keyboard.press('r');
await expect(page.locator('.instructions-panel')).toBeFocused();
});
test('User can use shortcuts to navigate between frontend projects', async ({
page
}) => {
await page.goto(links.frontEnd1);
await waitUntilListening(page);
await page.keyboard.press('Escape');
await page.keyboard.press('n');
await waitUntilHydrated(page, 'frontEnd2');
await page.keyboard.press('p');
await page.waitForURL(links.frontEnd1);
});
test('User can use shortcuts to navigate between backend projects', async ({
page
}) => {
await page.goto(links.backEnd1);
await waitUntilListening(page);
await page.keyboard.press('Escape');
await page.keyboard.press('n');
await waitUntilHydrated(page, 'backEnd2');
await page.keyboard.press('p');
await page.waitForURL(links.backEnd1);
});
test('User can use shortcuts to navigate between video-based challenges', async ({
page
}) => {
await page.goto(links.video1);
await waitUntilListening(page);
await page.keyboard.press('Escape');
await page.keyboard.press('n');
await waitUntilHydrated(page, 'video2');
await page.keyboard.press('p');
await page.waitForURL(links.video1);
});