forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdonation-modal.spec.ts
379 lines (328 loc) · 13.9 KB
/
donation-modal.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { execSync } from 'child_process';
import { test, expect, type Page } from '@playwright/test';
import { addGrowthbookCookie } from './utils/add-growthbook-cookie';
import { clearEditor, focusEditor } from './utils/editor';
const slowExpect = expect.configure({ timeout: 25000 });
const completeFrontEndCert = async (page: Page, number?: number) => {
await page.goto(
`/learn/front-end-development-libraries/front-end-development-libraries-projects/build-a-random-quote-machine`
);
const projects = [
'random-quote-machine',
'markdown-previewer',
'drum-machine',
'javascript-calculator',
'25--5-clock'
];
const loopNumber = number || projects.length;
for (let i = 0; i < loopNumber; i++) {
await page.waitForURL(
`/learn/front-end-development-libraries/front-end-development-libraries-projects/build-a-${projects[i]}`
);
await page
.getByRole('textbox', { name: 'solution' })
.fill('https://codepen.io/camperbot/full/oNvPqqo');
await page
.getByRole('button', { name: "I've completed this challenge" })
.click();
await page
.getByRole('button', { name: 'Submit and go to next challenge' })
.click();
}
};
const challenges = [
{
url: '/learn/javascript-algorithms-and-data-structures/basic-javascript/comment-your-javascript-code',
solution: `// some comment\n/* some comment */`
},
{
url: '/learn/javascript-algorithms-and-data-structures/basic-javascript/declare-javascript-variables',
solution: 'var myName;'
},
{
url: '/learn/javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator',
solution: `// Setup\nvar a;\n\n// Only change code below this line\na = 7;`
},
{
url: '/learn/javascript-algorithms-and-data-structures/basic-javascript/assigning-the-value-of-one-variable-to-another',
solution: `// Setup\nvar a;\na = 7;\nvar b;\n\n// Only change code below this line\nb = a;`
},
{
url: '/learn/javascript-algorithms-and-data-structures/basic-javascript/initializing-variables-with-the-assignment-operator',
solution: 'var a = 9;'
},
{
url: '/learn/javascript-algorithms-and-data-structures/basic-javascript/declare-string-variables',
solution: `var myFirstName = 'foo';\nvar myLastName = 'bar';`
},
{
url: '/learn/javascript-algorithms-and-data-structures/basic-javascript/understanding-uninitialized-variables',
solution: `// Only change code below this line\nvar a = 5;\nvar b = 10;\nvar c = 'I am a';\n// Only change code above this line\n\na = a + 1;\nb = b + 5;\nc = c + " String!";`
},
{
url: '/learn/javascript-algorithms-and-data-structures/basic-javascript/understanding-case-sensitivity-in-variables',
solution: `// Variable declarations\nvar studlyCapVar;\nvar properCamelCase;\nvar titleCaseOver;\n\n// Variable assignments\nstudlyCapVar = 10;\nproperCamelCase = "A String";\ntitleCaseOver = 9000;`
},
{
url: '/learn/javascript-algorithms-and-data-structures/basic-javascript/explore-differences-between-the-var-and-let-keywords',
solution: `let catName = "Oliver";\nlet catSound = "Meow!";`
},
{
url: '/learn/javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword',
solution: `const FCC = "freeCodeCamp";\n// Change this line\nlet fact = "is cool!";\n// Change this line\nfact = "is awesome!";\nconsole.log(FCC, fact);\n// Change this line`
}
];
const completeChallenges = async ({
page,
browserName,
isMobile,
number
}: {
page: Page;
browserName: string;
isMobile: boolean;
number: number;
}) => {
await page.goto(challenges[0].url);
for (const challenge of challenges.slice(0, number)) {
await page.waitForURL(challenge.url);
await focusEditor({ page, isMobile });
await clearEditor({ page, browserName });
await page.evaluate(
async contents => await navigator.clipboard.writeText(contents),
challenge.solution
);
await page.keyboard.press('ControlOrMeta+V');
await page.getByRole('button', { name: 'Run' }).click();
await expect(
page.getByRole('dialog').filter({ hasText: 'Basic Javascript' })
).toBeVisible(); // completion dialog
await page.getByRole('button', { name: 'Submit' }).click();
}
};
test.skip(
({ browserName }) => browserName !== 'chromium',
'Only chromium allows us to use the clipboard API.'
);
test.describe('Donation modal display', () => {
test.beforeEach(async ({ context }) => {
await addGrowthbookCookie({ context, variation: 'A' });
});
test('should display the content correctly and disable close when the animation is not complete', async ({
page,
browserName,
isMobile,
context
}) => {
await context.grantPermissions(['clipboard-read', 'clipboard-write']);
test.setTimeout(40000);
await completeChallenges({ page, browserName, isMobile, number: 3 });
const donationModal = page
.getByRole('dialog')
.filter({ hasText: 'Become a Supporter' });
await expect(donationModal).toBeVisible();
await expect(
donationModal.getByText(
'This is a 20 second animated advertisement to encourage campers to become supporters of freeCodeCamp. The animation starts with a teddy bear who becomes a supporter. As a result, distracting pop-ups disappear and the bear gets to complete all of its goals. Then, it graduates and becomes an education super hero helping people around the world.'
)
).toBeVisible();
await expect(donationModal.getByTestId('donation-animation')).toBeVisible();
await expect(donationModal.getByText('Become a Supporter')).toBeVisible();
await expect(donationModal.getByText('Remove distractions')).toBeVisible();
await expect(
donationModal.getByText('Reach your goals faster')
).toBeVisible();
await expect(
donationModal.getByText('Help millions of people learn')
).toBeVisible();
// Ensure that the modal cannot be closed by pressing the Escape key
await page.keyboard.press('Escape');
await expect(donationModal).toBeVisible();
// Second part of the modal.
// Use `slowExpect` as we need to wait 20s for this part to show up.
await slowExpect(
donationModal.getByRole('img', {
name: 'Illustration of an adorable teddy bear wearing a graduation cap and flying with a Supporter badge.'
})
).toBeVisible();
await expect(
donationModal.getByRole('heading', {
name: 'Support us'
})
).toBeVisible();
await expect(
donationModal
.getByRole('listitem')
.filter({ hasText: 'Help us build more certifications' })
).toBeVisible();
await expect(
donationModal
.getByRole('listitem')
.filter({ hasText: 'Remove donation popups' })
).toBeVisible();
await expect(
donationModal
.getByRole('listitem')
.filter({ hasText: 'Help millions of people learn' })
).toBeVisible();
await expect(
donationModal.getByRole('button', { name: 'Become a Supporter' })
).toBeVisible();
await expect(
donationModal.getByRole('button', { name: 'Ask me later' })
).toBeVisible();
});
});
test.describe('Donation modal appearance logic - New user', () => {
test.use({ storageState: 'playwright/.auth/development-user.json' });
test.beforeEach(async ({ context }) => {
await addGrowthbookCookie({ context, variation: 'B' });
});
test.beforeEach(() => {
execSync('node ./tools/scripts/seed/seed-demo-user');
});
test.afterAll(() => {
execSync('node ./tools/scripts/seed/seed-demo-user --certified-user');
});
test('should not appear if the user has less than 10 completed challenges in total and has just completed 3 challenges', async ({
page,
browserName,
isMobile,
context
}) => {
await context.grantPermissions(['clipboard-read', 'clipboard-write']);
// Development user doesn't have any completed challenges, we are completing the first 3.
await completeChallenges({ page, browserName, isMobile, number: 3 });
const donationModal = page
.getByRole('dialog')
.filter({ hasText: 'Become a Supporter' });
await expect(donationModal).toBeHidden();
});
test('should appear if the user has less than 10 completed challenges in total and has just completed 10 challenges', async ({
page,
isMobile,
browserName,
context
}) => {
await context.grantPermissions(['clipboard-read', 'clipboard-write']);
test.setTimeout(50000);
await completeChallenges({ page, isMobile, browserName, number: 10 });
const donationModal = page
.getByRole('dialog')
.filter({ hasText: 'Become a Supporter' });
await expect(donationModal).toBeVisible();
await expect(
donationModal.getByText(
'This is a 20 second animated advertisement to encourage campers to become supporters of freeCodeCamp. The animation starts with a teddy bear who becomes a supporter. As a result, distracting pop-ups disappear and the bear gets to complete all of its goals. Then, it graduates and becomes an education super hero helping people around the world.'
)
).toBeVisible();
// Second part of the modal.
// Use `slowExpect` as we need to wait 20s for this part to show up.
await slowExpect(
donationModal.getByRole('heading', { name: 'Support us' })
).toBeVisible();
await donationModal.getByRole('button', { name: 'Ask me later' }).click();
// Ensure that the close state has been registered before ending the test.
// The modal will show up on another page/test otherwise.
await expect(donationModal).toBeHidden();
});
test('should not appear if the user has just completed a new block but has less than 10 completed challenges', async ({
page
}) => {
test.setTimeout(40000);
await completeFrontEndCert(page);
const donationModal = page
.getByRole('dialog')
.filter({ hasText: 'Become a Supporter' });
await expect(donationModal).toBeHidden();
});
});
test.describe('Donation modal appearance logic - Certified user claiming a new block', () => {
test.use({ storageState: 'playwright/.auth/certified-user.json' });
execSync('node ./tools/scripts/seed/seed-demo-user --almost-certified-user');
test('should appear if the user has just completed a new block, and should not appear if the user re-submits the projects of the block', async ({
page,
context
}) => {
await context.grantPermissions(['clipboard-read', 'clipboard-write']);
test.setTimeout(40000);
await completeFrontEndCert(page, 1);
const donationModal = page
.getByRole('dialog')
.filter({ hasText: 'Become a Supporter' });
await expect(donationModal).toBeVisible();
await expect(
donationModal.getByText(
'This is a 20 second animated advertisement to encourage campers to become supporters of freeCodeCamp. The animation starts with a teddy bear who becomes a supporter. As a result, distracting pop-ups disappear and the bear gets to complete all of its goals. Then, it graduates and becomes an education super hero helping people around the world.'
)
).toBeVisible();
// Second part of the modal.
// Use `slowExpect` as we need to wait 20s for this part to show up.
await slowExpect(
donationModal.getByText(
'Nicely done. You just completed Front End Development Libraries Projects.'
)
).toBeVisible();
await donationModal.getByRole('button', { name: 'Ask me later' }).click();
await expect(donationModal).toBeHidden();
await completeFrontEndCert(page, 1);
await expect(donationModal).toBeHidden();
});
});
test.describe('Donation modal appearance logic - Certified user', () => {
test.beforeEach(async ({ context }) => {
await addGrowthbookCookie({ context, variation: 'A' });
});
test('should appear if the user has completed 3 challenges and has more than 10 completed challenges in total', async ({
page,
browserName,
isMobile,
context
}) => {
await context.grantPermissions(['clipboard-read', 'clipboard-write']);
test.setTimeout(40000);
// Certified user already has more than 10 completed challenges, we are just completing 3 more.
await completeChallenges({ page, isMobile, browserName, number: 3 });
const donationModal = page
.getByRole('dialog')
.filter({ hasText: 'Become a Supporter' });
await expect(donationModal).toBeVisible();
await expect(
donationModal.getByText(
'This is a 20 second animated advertisement to encourage campers to become supporters of freeCodeCamp. The animation starts with a teddy bear who becomes a supporter. As a result, distracting pop-ups disappear and the bear gets to complete all of its goals. Then, it graduates and becomes an education super hero helping people around the world.'
)
).toBeVisible();
// Second part of the modal.
// Use `slowExpect` as we need to wait 20s for this part to show up.
await slowExpect(
donationModal.getByRole('heading', { name: 'Support us' })
).toBeVisible();
await donationModal.getByRole('button', { name: 'Ask me later' }).click();
// Ensure that the close state has been registered before ending the test.
// The modal will show up on another page/test otherwise.
await expect(donationModal).toBeHidden();
});
});
test.describe('Donation modal appearance logic - Donor user', () => {
test.beforeAll(() => {
execSync(
'node ./tools/scripts/seed/seed-demo-user --certified-user --set-true isDonating'
);
});
test.afterAll(() => {
execSync('node ./tools/scripts/seed/seed-demo-user --certified-user');
});
test('should not appear', async ({
page,
browserName,
isMobile,
context
}) => {
await context.grantPermissions(['clipboard-read', 'clipboard-write']);
await completeChallenges({ page, browserName, isMobile, number: 3 });
const donationModal = page
.getByRole('dialog')
.filter({ hasText: 'Become a Supporter' });
await expect(donationModal).toBeHidden();
});
});