forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusername-change.spec.ts
187 lines (165 loc) · 6.24 KB
/
username-change.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
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
import { execSync } from 'child_process';
import { test, expect } from '@playwright/test';
import translations from '../client/i18n/locales/english/translations.json';
test.afterAll(() => {
// change the name back to the original
execSync('node ./tools/scripts/seed/seed-demo-user --certified-user');
});
const settingsObject = {
usernamePlaceholder: '{{username}}',
usernameUpdateToLowerCase: 'quincy',
usernameUpdateToUpperCase: 'Mrugesh',
usernameAvailable: 'Sem',
usernameAvailablePressingEnter: 'Oliver',
usernameNotAvailable: 'Twaha',
usernameInvalid: 'user!',
usernameTooShort: 'us',
certifiedUsername: 'certifieduser',
testUser: 'testuser',
errorCode: '404'
};
test.describe('Username Settings Validation', () => {
test.beforeEach(async ({ page }) => {
execSync('node ./tools/scripts/seed/seed-demo-user --certified-user');
await page.goto(`/certifieduser`);
if (!process.env.CI) {
await page
.getByRole('button', { name: 'Preview custom 404 page' })
.click();
}
await page.getByRole('button', { name: 'Edit my profile' }).click();
});
test('Should display Username Input and Save Button', async ({ page }) => {
const inputLabel = page.getByLabel(translations.settings.labels.username);
const saveButton = page.getByRole('button', {
name: translations.settings.labels.username
});
await expect(inputLabel).toBeVisible();
await expect(saveButton).toBeVisible();
});
test('Should handle Reserved Username Error', async ({ page }) => {
const inputLabel = page.getByLabel(translations.settings.labels.username);
await inputLabel.fill(settingsObject.errorCode);
await expect(
page.getByText(
translations.settings.username['is a reserved error code'].replace(
settingsObject.usernamePlaceholder,
settingsObject.errorCode
)
)
).toBeVisible();
});
test('Should handle Invalid Username Error', async ({ page }) => {
const inputLabel = page.getByLabel(translations.settings.labels.username);
await inputLabel.fill(settingsObject.usernameInvalid);
await expect(
page.getByText(
translations.settings.username['contains invalid characters'].replace(
settingsObject.usernamePlaceholder,
settingsObject.usernameInvalid
)
)
).toBeVisible();
});
test('Should handle Unavailable Username Error', async ({ page }) => {
const inputLabel = page.getByLabel(translations.settings.labels.username);
await inputLabel.fill(settingsObject.usernameNotAvailable);
await expect(
page.getByText(
translations.settings.username['unavailable'].replace(
settingsObject.usernamePlaceholder,
settingsObject.usernameNotAvailable
)
)
).toBeVisible();
});
test('Should handle Too Short Username Error', async ({ page }) => {
const inputLabel = page.getByLabel(translations.settings.labels.username);
await inputLabel.fill(settingsObject.usernameTooShort);
await expect(
page.getByText(
translations.settings.username['is too short'].replace(
settingsObject.usernamePlaceholder,
settingsObject.usernameTooShort
)
)
).toBeVisible();
});
test('Should save valid Username', async ({ page }) => {
const inputLabel = page.getByLabel(translations.settings.labels.username);
const saveButton = page.getByRole('button', {
name: translations.settings.labels.username
});
const flashText = translations.flash['username-updated'].replace(
settingsObject.usernamePlaceholder,
settingsObject.usernameAvailable
);
await inputLabel.fill(settingsObject.usernameAvailable);
await expect(saveButton).not.toBeDisabled();
await saveButton.click();
await expect(
page.getByRole('alert').filter({ hasText: flashText }).first()
).toBeVisible();
});
test('should update username in lowercase and reflect in the UI', async ({
page
}) => {
const inputLabel = page.getByLabel(translations.settings.labels.username);
const saveButton = page.getByRole('button', {
name: translations.settings.labels.username
});
const flashText = translations.flash['username-updated'].replace(
settingsObject.usernamePlaceholder,
settingsObject.usernameUpdateToLowerCase
);
await inputLabel.fill(settingsObject.usernameUpdateToLowerCase);
await expect(saveButton).not.toBeDisabled();
await saveButton.click();
await expect(
page.getByRole('alert').filter({ hasText: flashText }).first()
).toBeVisible();
});
test('should update username in uppercase and reflect in the UI', async ({
page
}) => {
const inputLabel = page.getByLabel(translations.settings.labels.username);
const saveButton = page.getByRole('button', {
name: translations.settings.labels.username
});
const flashText = translations.flash['username-updated'].replace(
settingsObject.usernamePlaceholder,
settingsObject.usernameUpdateToUpperCase
);
await inputLabel.fill(settingsObject.usernameUpdateToUpperCase);
await expect(saveButton).not.toBeDisabled();
await saveButton.click();
await expect(
page.getByRole('alert').filter({ hasText: flashText }).first()
).toBeVisible();
});
test('should update username by pressing enter', async ({ page }) => {
const inputLabel = page.getByLabel(translations.settings.labels.username);
await inputLabel.fill(settingsObject.testUser);
const flashText = translations.flash['username-updated'].replace(
settingsObject.usernamePlaceholder,
settingsObject.testUser
);
await expect(
page.getByText(translations.settings.username.available)
).toBeVisible();
await inputLabel.press('Enter');
await expect(
page.getByRole('alert').filter({ hasText: flashText }).first()
).toBeVisible();
});
test('should not be able to update username to the same username', async ({
page
}) => {
const inputLabel = page.getByLabel(translations.settings.labels.username);
const saveButton = page.getByRole('button', {
name: translations.settings.labels.username
});
await inputLabel.fill(settingsObject.certifiedUsername);
await expect(saveButton).toBeDisabled();
});
});