-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegrade_mvp.test.js
450 lines (441 loc) · 17.1 KB
/
codegrade_mvp.test.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import server from './backend/mock-server'
import React from 'react'
import AppFunctional from './frontend/components/AppFunctional'
import AppClass from './frontend/components/AppClass'
import { render, fireEvent, screen } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'
jest.setTimeout(1000) // default 5000 too long for Codegrade
const waitForOptions = { timeout: 100 }
const queryOptions = { exact: false }
let up, down, left, right, reset, submit
let squares, coordinates, steps, message, email
const updateStatelessSelectors = document => {
up = document.querySelector('#up')
down = document.querySelector('#down')
left = document.querySelector('#left')
right = document.querySelector('#right')
reset = document.querySelector('#reset')
submit = document.querySelector('#submit')
}
const updateStatefulSelectors = document => {
squares = document.querySelectorAll('.square')
coordinates = document.querySelector('#coordinates')
steps = document.querySelector('#steps')
message = document.querySelector('#message')
email = document.querySelector('#email')
}
const testSquares = (squares, activeIdx) => {
squares.forEach((square, idx) => {
if (idx === activeIdx) {
expect(square.textContent).toBe('B')
expect(square.className).toMatch(/active/)
} else {
expect(square.textContent).toBeFalsy()
expect(square.className).not.toMatch(/active/)
}
})
}
test('AppFunctional is a functional component, Review how to build a functional component, including useState and passing props.', () => {
expect(
AppFunctional.prototype &&
AppFunctional.prototype.isReactComponent
).not.toBeTruthy()
})
test('AppClass is a class-based component, Review how to build a class-based component, such as using “extends”, and constructors', () => {
expect(
AppClass.prototype &&
AppClass.prototype.isReactComponent
).toBeTruthy()
});
[AppFunctional, AppClass].forEach((Component, idx) => {
const label = idx === 0 ? 'FUNCTIONAL' : 'CLASS-BASED'
describe(`${label}`, () => {
beforeAll(() => { server.listen() })
afterAll(() => { server.close() })
beforeEach(() => {
render(<Component />)
updateStatelessSelectors(document)
updateStatefulSelectors(document)
})
afterEach(() => {
server.resetHandlers()
document.body.innerHTML = ''
})
describe(`[A ${label}] Active Square, Review how to set a class name and use ternary statements, as well as how to set, manipulate, and read pieces of state. Also review how to handle user interaction.`, () => {
test(`[A1 ${label}] Actions: none (Initial State of <App />)
Active Square should be index 4`, () => {
testSquares(squares, 4)
})
test(`[A2 ${label}] Actions: up
Active Square should be index 1`, () => {
fireEvent.click(up)
testSquares(squares, 1)
})
test(`[A3 ${label}] Actions: up, up
Active Square should be index 1`, () => {
fireEvent.click(up)
fireEvent.click(up)
testSquares(squares, 1)
})
test(`[A4 ${label}] Actions: up, left
Active Square should be index 0`, () => {
fireEvent.click(up)
fireEvent.click(left)
testSquares(squares, 0)
})
test(`[A5 ${label}] Actions: up, left, left
Active Square should be index 0`, () => {
fireEvent.click(up)
fireEvent.click(left)
fireEvent.click(left)
testSquares(squares, 0)
})
test(`[A6 ${label}] Actions: up, right
Active Square should be index 2`, () => {
fireEvent.click(up)
fireEvent.click(right)
testSquares(squares, 2)
})
test(`[A7 ${label}] Actions: up, right, right
Active Square should be index 2`, () => {
fireEvent.click(up)
fireEvent.click(right)
fireEvent.click(right)
testSquares(squares, 2)
})
test(`[A8 ${label}] Actions: right
Active Square should be index 5`, () => {
fireEvent.click(right)
testSquares(squares, 5)
})
test(`[A9 ${label}] Actions: right, right
Active Square should be index 5`, () => {
fireEvent.click(right)
fireEvent.click(right)
testSquares(squares, 5)
})
test(`[A10 ${label}] Actions: right, down
Active Square should be index 8`, () => {
fireEvent.click(right)
fireEvent.click(down)
testSquares(squares, 8)
})
test(`[A11 ${label}] Actions: right, down, down
Active Square should be index 8`, () => {
fireEvent.click(right)
fireEvent.click(down)
fireEvent.click(down)
testSquares(squares, 8)
})
test(`[A12 ${label}] Actions: down, left
Active Square should be index 6`, () => {
fireEvent.click(down)
fireEvent.click(left)
testSquares(squares, 6)
})
test(`[A13 ${label}] Actions: down, down, left, left
Active Square should be index 6`, () => {
fireEvent.click(down)
fireEvent.click(down)
fireEvent.click(left)
fireEvent.click(left)
testSquares(squares, 6)
})
})
describe(`[B ${label}] Coordinates Readout, Review how to set, manipulate, and display pieces of state, and handle user interaction.`, () => {
test(`[B1] Actions: none (Initial State of <App />)
Coordinates should be (2,2)`, () => {
expect(coordinates.textContent).toMatch(/\(2.*2\)$/)
})
test(`[B2 ${label}] Actions: up
Coordinates should be (2,1)`, () => {
fireEvent.click(up)
expect(coordinates.textContent).toMatch(/\(2.*1\)$/)
})
test(`[B3 ${label}] Actions: up, up
Coordinates should be (2,1)`, () => {
fireEvent.click(up)
fireEvent.click(up)
expect(coordinates.textContent).toMatch(/\(2.*1\)$/)
})
test(`[B4 ${label}] Actions: up, left
Coordinates should be (1,1)`, () => {
fireEvent.click(up)
fireEvent.click(left)
expect(coordinates.textContent).toMatch(/\(1.*1\)$/)
})
test(`[B5 ${label}] Actions: up, left, left
Coordinates should be (1,1)`, () => {
fireEvent.click(up)
fireEvent.click(left)
fireEvent.click(left)
expect(coordinates.textContent).toMatch(/\(1.*1\)$/)
})
test(`[B6 ${label}] Actions: up, right
Coordinates should be (3,1)`, () => {
fireEvent.click(up)
fireEvent.click(right)
expect(coordinates.textContent).toMatch(/\(3.*1\)$/)
})
test(`[B7 ${label}] Actions: up, right, right
Coordinates should be (3,1)`, () => {
fireEvent.click(up)
fireEvent.click(right)
fireEvent.click(right)
expect(coordinates.textContent).toMatch(/\(3.*1\)$/)
})
test(`[B8 ${label}] Actions: right
Coordinates should be (3,2)`, () => {
fireEvent.click(right)
expect(coordinates.textContent).toMatch(/\(3.*2\)$/)
})
test(`[B9 ${label}] Actions: right, right
Coordinates should be (3,2)`, () => {
fireEvent.click(right)
fireEvent.click(right)
expect(coordinates.textContent).toMatch(/\(3.*2\)$/)
})
test(`[B10 ${label}] Actions: right, down
Coordinates should be (3,3)`, () => {
fireEvent.click(right)
fireEvent.click(down)
expect(coordinates.textContent).toMatch(/\(3.*3\)$/)
})
test(`[B11 ${label}] Actions: right, down, down
Coordinates should be (3,3)`, () => {
fireEvent.click(right)
fireEvent.click(down)
fireEvent.click(down)
expect(coordinates.textContent).toMatch(/\(3.*3\)$/)
})
test(`[B12 ${label}] Actions: down, left
Coordinates should be (1,3)`, () => {
fireEvent.click(down)
fireEvent.click(left)
expect(coordinates.textContent).toMatch(/\(1.*3\)$/)
})
test(`[B13 ${label}] Actions: down, down, left, left
Coordinates should be (1,3)`, () => {
fireEvent.click(down)
fireEvent.click(down)
fireEvent.click(left)
fireEvent.click(left)
expect(coordinates.textContent).toMatch(/\(1.*3\)$/)
})
})
describe(`[C ${label}] Limit Reached Message, Review how to set, manipulate, and display pieces of state, and handle user interaction.`, () => {
test(`[C1 ${label}] Actions: none (Initial State of <App />)
Limit reached message should be empty`, () => {
expect(message.textContent).toBeFalsy()
})
test(`[C2 ${label}] Actions: up
Limit reached message should be empty`, () => {
fireEvent.click(up)
expect(message.textContent).toBeFalsy()
})
test(`[C3 ${label}] Actions: up, up
Limit reached message should be "You can't go up"`, () => {
fireEvent.click(up)
fireEvent.click(up)
expect(message.textContent).toBe("You can't go up")
})
test(`[C4 ${label}] Actions: up, left
Limit reached message should be empty`, () => {
fireEvent.click(up)
fireEvent.click(left)
expect(message.textContent).toBeFalsy()
})
test(`[C5 ${label}] Actions: up, left, left
Limit reached message should be "You can't go left"`, () => {
fireEvent.click(up)
fireEvent.click(left)
fireEvent.click(left)
expect(message.textContent).toBe("You can't go left")
})
test(`[C6 ${label}] Actions: up, right
Limit reached message should be empty`, () => {
fireEvent.click(up)
fireEvent.click(right)
expect(message.textContent).toBeFalsy()
})
test(`[C7 ${label}] Actions: up, right, right
Limit reached message should be "You can't go right"`, () => {
fireEvent.click(up)
fireEvent.click(right)
fireEvent.click(right)
expect(message.textContent).toBe("You can't go right")
})
test(`[C8 ${label}] Actions: right
Limit reached message should be empty`, () => {
fireEvent.click(right)
expect(message.textContent).toBeFalsy()
})
test(`[C9 ${label}] Actions: right, right
Limit reached message should be (3,2)`, () => {
fireEvent.click(right)
fireEvent.click(right)
expect(message.textContent).toBe("You can't go right")
})
test(`[C10 ${label}] Actions: right, down
Limit reached message should be empty`, () => {
fireEvent.click(right)
fireEvent.click(down)
expect(message.textContent).toBeFalsy()
})
test(`[C11 ${label}] Actions: right, down, down
Limit reached message should be "You can't go down"`, () => {
fireEvent.click(right)
fireEvent.click(down)
fireEvent.click(down)
expect(message.textContent).toBe("You can't go down")
})
test(`[C12 ${label}] Actions: down, left
Limit reached message should be empty`, () => {
fireEvent.click(down)
fireEvent.click(left)
expect(message.textContent).toBeFalsy()
})
test(`[C13 ${label}] Actions: down, down, left, left
Limit reached message should be "You can't go left"`, () => {
fireEvent.click(down)
fireEvent.click(down)
fireEvent.click(left)
fireEvent.click(left)
expect(message.textContent).toBe("You can't go left")
})
})
describe(`[D ${label}] Steps Counter, Review how to set, manipulate, and display pieces of state, and handle user interaction.`, () => {
test(`[D1 ${label}] Steps counter works correctly`, () => {
expect(steps.textContent).toBe("You moved 0 times")
fireEvent.click(up)
fireEvent.click(up)
fireEvent.click(left)
expect(steps.textContent).toBe("You moved 2 times")
fireEvent.click(right)
fireEvent.click(right)
expect(steps.textContent).toBe("You moved 4 times")
fireEvent.click(down)
fireEvent.click(down)
fireEvent.click(down)
expect(steps.textContent).toBe("You moved 6 times")
fireEvent.click(left)
fireEvent.click(left)
fireEvent.click(left)
expect(steps.textContent).toBe("You moved 8 times")
})
test(`[D2 ${label}] Steps counter handles a single step gracefully`, () => {
fireEvent.click(up)
expect(steps.textContent).toBe("You moved 1 time")
fireEvent.click(up)
expect(steps.textContent).toBe("You moved 1 time")
})
})
describe(`[E ${label}] Reset Button, Review how to set, manipulate, and display pieces of state, and handle user interaction.`, () => {
test(`[E1 ${label}] Active Square is reset`, () => {
fireEvent.click(up)
fireEvent.click(up)
fireEvent.click(left)
testSquares(squares, 0)
fireEvent.click(reset)
testSquares(squares, 4)
})
test(`[E2 ${label}] Coordinates are reset`, () => {
fireEvent.click(up)
fireEvent.click(up)
fireEvent.click(left)
expect(coordinates.textContent).toMatch(/\(1.*1\)$/)
fireEvent.click(reset)
expect(coordinates.textContent).toMatch(/\(2.*2\)$/)
})
test(`[E3 ${label}] Message is reset`, () => {
fireEvent.click(up)
fireEvent.click(up)
expect(message.textContent).toBe("You can't go up")
fireEvent.click(reset)
expect(message.textContent).toBeFalsy()
})
test(`[E4 ${label}] Steps are reset`, () => {
fireEvent.click(up)
fireEvent.click(up)
fireEvent.click(left)
expect(steps.textContent).toBe("You moved 2 times")
fireEvent.click(reset)
expect(steps.textContent).toBe("You moved 0 times")
})
test(`[E5 ${label}] Email input is reset`, () => {
fireEvent.change(email, { target: { value: 'lady@gaga.com' } })
expect(email).toHaveValue('lady@gaga.com')
fireEvent.click(reset)
expect(email.value).toBeFalsy()
})
})
describe(`[F ${label}] Submit Button`, () => {
test(`[F1 ${label}] Actions: up, type email, submit
Success message is correct`, async () => {
fireEvent.click(up)
fireEvent.change(email, { target: { value: 'lady@gaga.com' } })
fireEvent.click(submit)
await screen.findByText('lady win #31', queryOptions, waitForOptions)
})
test(`[F2 ${label}] Actions: down, down, type email, submit
Success message is correct`, async () => {
fireEvent.click(down)
fireEvent.click(down)
fireEvent.change(email, { target: { value: 'lady@gaga.com' } })
fireEvent.click(submit)
await screen.findByText('lady win #43', queryOptions, waitForOptions)
})
test(`[F3 ${label}] Actions: up, down, left, right, type email, submit
Success message is correct`, async () => {
fireEvent.click(up)
fireEvent.click(down)
fireEvent.click(left)
fireEvent.click(right)
fireEvent.change(email, { target: { value: 'lady@gaga.com' } })
fireEvent.click(submit)
await screen.findByText('lady win #73', queryOptions, waitForOptions)
})
test(`[F4 ${label}] Actions: down, right, submit
Error message on no email is correct`, async () => {
fireEvent.click(down)
fireEvent.click(right)
fireEvent.click(submit)
await screen.findByText('Ouch: email is required', queryOptions, waitForOptions)
})
test(`[F5 ${label}] Actions: down, right, type invalid email, submit
Error message on invalid email is correct`, async () => {
fireEvent.click(down)
fireEvent.click(right)
fireEvent.change(email, { target: { value: 'bad@email' } })
fireEvent.click(submit)
await screen.findByText('Ouch: email must be a valid email', queryOptions, waitForOptions)
})
test(`[F6 ${label}] Actions: down, right, type foo@bar.baz email, submit
Error message on banned email is correct`, async () => {
fireEvent.click(down)
fireEvent.click(right)
fireEvent.change(email, { target: { value: 'foo@bar.baz' } })
fireEvent.click(submit)
await screen.findByText('foo@bar.baz failure #71', queryOptions, waitForOptions)
})
test(`[F7 ${label}] Actions: left, type valid email, submit
Submitting resets the email input`, async () => {
fireEvent.click(left)
fireEvent.change(email, { target: { value: 'lady@gaga.com' } })
fireEvent.click(submit)
await screen.findByText('lady win #29', queryOptions, waitForOptions)
expect(email.value).toBeFalsy()
})
test(`[F8 ${label}] Actions: up, right, type valid email, submit
Submitting does not reset coordinates nor steps`, async () => {
fireEvent.click(up)
fireEvent.click(right)
fireEvent.change(email, { target: { value: 'lady@gaga.com' } })
fireEvent.click(submit)
await screen.findByText('lady win #49', queryOptions, waitForOptions)
expect(coordinates.textContent).toMatch(/\(3.*1\)$/)
expect(steps.textContent).toBe('You moved 2 times')
})
})
})
})