-
Notifications
You must be signed in to change notification settings - Fork 386
/
codegrade_mvp.test.js
313 lines (309 loc) · 13.6 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
import React from 'react'
import { render, fireEvent, screen, waitFor } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'
import { setupServer, getHandlers } from './backend/mock-server'
import App, { resetStore } from './frontend/components/App'
jest.setTimeout(750) // default 5000 too long for Codegrade
const waitForOptions = { timeout: 150 }
const queryOptions = { exact: false }
const renderApp = ui => {
resetStore()
window.history.pushState({}, 'Test page', '/')
return render(ui)
}
let server
beforeAll(() => {
server = setupServer(...getHandlers())
server.listen()
})
afterAll(() => {
server.close()
})
beforeEach(() => {
renderApp(<App />)
})
afterEach(() => {
server.resetHandlers(...getHandlers())
})
// links
const wheelLink = () => document.querySelector('#wheelLink')
const quizLink = () => document.querySelector('#quizLink')
const formLink = () => document.querySelector('#formLink')
// wheel screen
const cogs = () => document.querySelectorAll('#wheel .cog')
const clockwiseBtn = () => document.querySelector('#clockwiseBtn')
const counterClockwiseBtn = () => document.querySelector('#counterClockwiseBtn')
// quiz screen
const submitAnswerBtn = () => document.querySelector('#submitAnswerBtn')
// form screen
const submitNewQuizBtn = () => document.querySelector('#submitNewQuizBtn')
const newQuestionInput = () => document.querySelector('#newQuestion')
const newTrueAnswerInput = () => document.querySelector('#newTrueAnswer')
const newFalseAnswerInput = () => document.querySelector('#newFalseAnswer')
// wheel test helper
const testCogs = activeIdx => {
expect(cogs()).toHaveLength(6)
cogs().forEach((cog, idx) => {
if (idx === activeIdx) {
expect(cog.textContent).toBe('B')
expect(cog.className).toMatch(/active/)
} else {
expect(cog.textContent).toBeFalsy()
expect(cog.className).not.toMatch(/active/)
}
})
}
// text contents
const WhatIsClosure = 'What is a closure'
const AFunction = 'A function plus its bindings'
const AnElephant = 'Clearly some kind of elephant'
const WhatIsPromise = 'What is a promise'
const AValue = 'A value representing a future result'
const WhatIsModule = 'An ES6 module is a'
const AFile = 'JS file'
const ThatIsCorrect = 'That was the correct answer'
const ThatIsAShame = 'That was the incorrect answer'
describe('Advanced State Sprint Challenge Submission', () => {
describe('[WHEEL SCREEN]', () => {
test(`[1] Clicking on the clockwise button:
- the next cog to have the text content capital "B"
- the next cog to have the class name "active"
- the next cog after the sixth to be the first
`, async () => {
testCogs(0)
fireEvent.click(clockwiseBtn())
testCogs(1)
fireEvent.click(clockwiseBtn())
testCogs(2)
fireEvent.click(clockwiseBtn())
testCogs(3)
fireEvent.click(clockwiseBtn())
testCogs(4)
fireEvent.click(clockwiseBtn())
testCogs(5)
fireEvent.click(clockwiseBtn())
testCogs(0)
})
test(`[2] Clicking on the COUNTER clockwise button:
- the previous cog to have the text content capital "B"
- the previous cog to have the class name "active"
- the previous cog before the first to be the sixth
`, async () => {
fireEvent.click(counterClockwiseBtn())
testCogs(5)
fireEvent.click(counterClockwiseBtn())
testCogs(4)
fireEvent.click(counterClockwiseBtn())
testCogs(3)
fireEvent.click(counterClockwiseBtn())
testCogs(2)
fireEvent.click(counterClockwiseBtn())
testCogs(1)
fireEvent.click(counterClockwiseBtn())
testCogs(0)
fireEvent.click(counterClockwiseBtn())
testCogs(5)
})
})
describe('[QUIZ SCREEN]', () => {
beforeEach(() => {
fireEvent.click(quizLink())
})
test(`[3] Navigating to the Quiz screen:
- Loads the first question & answers asynchronously`, async () => {
let question = await screen.findByText(WhatIsClosure, queryOptions, waitForOptions)
let answerA = screen.queryByText(AFunction, queryOptions)
let answerB = screen.queryByText(AnElephant, queryOptions)
expect(question).toBeInTheDocument()
expect(answerA).toBeInTheDocument()
expect(answerB).toBeInTheDocument()
})
test(`[4] Navigating to the Quiz screen:
- The "Submit answer" button should be disabled`, async () => {
await screen.findByText(WhatIsClosure, queryOptions, waitForOptions)
expect(submitAnswerBtn()).toBeDisabled()
})
test(`[5] Selecting an answer adds the correct class name:
- Adds the "selected" class name to the selected answer
- Removes the "selected" class name from the other answer`, async () => {
await screen.findByText(WhatIsClosure, queryOptions, waitForOptions)
let answerA = screen.queryByText(AFunction, queryOptions)
let answerB = screen.queryByText(AnElephant, queryOptions)
fireEvent.click(answerA.querySelector('button'))
expect(answerA.className).toMatch(/selected/)
expect(answerB.className).not.toMatch(/selected/)
fireEvent.click(answerB.querySelector('button'))
expect(answerA.className).not.toMatch(/selected/)
expect(answerB.className).toMatch(/selected/)
})
test(`[6] Selecting an answer adds the correct text to its button:
- Changes the label of the button from "Select" to "SELECTED"
- Turns the label of the unselected from "SELECTED" to "Select"`, async () => {
await screen.findByText(WhatIsClosure, queryOptions, waitForOptions)
let answerA = screen.queryByText(AFunction, queryOptions)
let answerB = screen.queryByText(AnElephant, queryOptions)
fireEvent.click(answerA.querySelector('button'))
expect(answerA.textContent).toMatch(/SELECTED/)
expect(answerB.textContent).not.toMatch(/SELECTED/)
fireEvent.click(answerB.querySelector('button'))
expect(answerA.textContent).not.toMatch(/SELECTED/)
expect(answerB.textContent).toMatch(/SELECTED/)
})
test(`[7] Selecting and submitting an answer:
- Loads the next quiz from the API`, async () => {
await screen.findByText(WhatIsClosure, queryOptions, waitForOptions)
let answerA = screen.queryByText(AFunction, queryOptions)
fireEvent.click(answerA.querySelector('button'))
fireEvent.click(submitAnswerBtn())
await screen.findByText(WhatIsPromise, queryOptions, waitForOptions)
})
test(`[8] Selecting and submitting a correct answer:
- Puts the proper success message at the top of the page`, async () => {
await screen.findByText(WhatIsClosure, queryOptions, waitForOptions)
let answerA = screen.queryByText(AFunction, queryOptions)
fireEvent.click(answerA.querySelector('button'))
fireEvent.click(submitAnswerBtn())
await screen.findByText(ThatIsCorrect, queryOptions)
})
test(`[9] Selecting and submitting an incorrect answer:
- Submitting puts the proper failure message at the top of the page`, async () => {
await screen.findByText(WhatIsClosure, queryOptions, waitForOptions)
let answerB = screen.queryByText(AnElephant, queryOptions)
fireEvent.click(answerB.querySelector('button'))
fireEvent.click(submitAnswerBtn())
await screen.findByText(ThatIsAShame, queryOptions)
})
})
describe('[FORM SCREEN]', () => {
beforeEach(() => {
fireEvent.click(formLink())
})
test('[10] Typing in inputs changes their value', async () => {
fireEvent.change(newQuestionInput(), { target: { value: 'foo' } })
await waitFor(() => {
expect(newQuestionInput()).toHaveValue('foo')
}, waitForOptions)
fireEvent.change(newTrueAnswerInput(), { target: { value: 'bar' } })
await waitFor(() => {
expect(newTrueAnswerInput()).toHaveValue('bar')
}, waitForOptions)
fireEvent.change(newFalseAnswerInput(), { target: { value: 'baz' } })
await waitFor(() => {
expect(newFalseAnswerInput()).toHaveValue('baz')
}, waitForOptions)
})
test(`[11] The submit button is disabled until all inputs have values more than one character
in length after trimming leading and trailing whitespace
`, async () => {
expect(submitNewQuizBtn()).toBeDisabled()
fireEvent.change(newQuestionInput(), { target: { value: 'question' } })
await waitFor(() => {
expect(submitNewQuizBtn()).toBeDisabled()
}, waitForOptions)
fireEvent.change(newTrueAnswerInput(), { target: { value: 'true' } })
await waitFor(() => {
expect(submitNewQuizBtn()).toBeDisabled()
}, waitForOptions)
fireEvent.change(newFalseAnswerInput(), { target: { value: 'false' } })
await waitFor(() => {
expect(submitNewQuizBtn()).toBeEnabled()
}, waitForOptions)
fireEvent.change(newQuestionInput(), { target: { value: ' ' } })
await waitFor(() => {
expect(submitNewQuizBtn()).toBeDisabled()
}, waitForOptions)
fireEvent.change(newQuestionInput(), { target: { value: ' question ' } })
await waitFor(() => {
expect(submitNewQuizBtn()).toBeEnabled()
}, waitForOptions)
fireEvent.change(newTrueAnswerInput(), { target: { value: ' ' } })
await waitFor(() => {
expect(submitNewQuizBtn()).toBeDisabled()
}, waitForOptions)
fireEvent.change(newTrueAnswerInput(), { target: { value: ' true ' } })
await waitFor(() => {
expect(submitNewQuizBtn()).toBeEnabled()
}, waitForOptions)
})
// This test has become unreliable so it is skipped. Learner: test manually in Chrome that submitting works!
test.skip(`[12] Successful submit of new quiz (TEST MANUALLY IN CHROME!)
- Displays the correct success message at the top of the screen
- Empties out the form
`, async () => {
fireEvent.change(newQuestionInput(), { target: { value: 'foobarbaz?' } })
fireEvent.change(newTrueAnswerInput(), { target: { value: 'bar' } })
fireEvent.change(newFalseAnswerInput(), { target: { value: 'baz' } })
fireEvent.click(submitNewQuizBtn())
await screen.findByText('Congrats: "foobarbaz?" is a great question!', queryOptions, waitForOptions)
expect(newQuestionInput()).toHaveValue('')
expect(newTrueAnswerInput()).toHaveValue('')
expect(newFalseAnswerInput()).toHaveValue('')
})
// This test has become unreliable so it is skipped. Learner: test manually in Chrome that submitting works!
test.skip(`[13] Successful submit of new quiz (TEST MANUALLY IN CHROME!)
- Adds the quiz to the roster of quizzes
`, async () => {
fireEvent.change(newQuestionInput(), { target: { value: 'foobarbaz?' } })
fireEvent.change(newTrueAnswerInput(), { target: { value: 'bar' } })
fireEvent.change(newFalseAnswerInput(), { target: { value: 'baz' } })
fireEvent.click(submitNewQuizBtn())
await screen.findByText('Congrats: "foobarbaz?" is a great question!', queryOptions, waitForOptions)
fireEvent.click(quizLink())
await screen.findByText(WhatIsClosure, queryOptions, waitForOptions)
let answerA = screen.queryByText(AFunction, queryOptions)
fireEvent.click(answerA.querySelector('button'))
fireEvent.click(submitAnswerBtn())
await screen.findByText(WhatIsPromise, queryOptions, waitForOptions)
screen.getByText(ThatIsCorrect, queryOptions)
answerA = screen.queryByText(AValue, queryOptions)
fireEvent.click(answerA.querySelector('button'))
fireEvent.click(submitAnswerBtn())
await screen.findByText(WhatIsModule, queryOptions, waitForOptions)
screen.getByText(ThatIsCorrect, queryOptions)
answerA = screen.queryByText(AFile, queryOptions)
fireEvent.click(answerA.querySelector('button'))
fireEvent.click(submitAnswerBtn())
await screen.findByText('foobarbaz?', queryOptions, waitForOptions)
})
})
describe('[APP STATE]', () => {
test(`[14] The state of the wheel survives route changes:
- Moving the wheel, navigating away and back, should keep the position of the "B"
`, async () => {
testCogs(0)
fireEvent.click(clockwiseBtn())
testCogs(1)
fireEvent.click(formLink())
fireEvent.click(wheelLink())
testCogs(1)
})
test(`[15] The state of the quiz survives route changes:
- Selecting an answer, navigating away and back, should keep the selected answer
- Navigating away and back shouldn't cause a new quiz to be fetched from the API
`, async () => {
fireEvent.click(quizLink())
await screen.findByText(WhatIsClosure, queryOptions, waitForOptions)
let answerB = screen.queryByText(AnElephant, queryOptions)
fireEvent.click(answerB.querySelector('button'))
expect(answerB.textContent).toMatch(/SELECTED/)
fireEvent.click(formLink())
fireEvent.click(quizLink())
answerB = screen.queryByText(AnElephant, queryOptions)
fireEvent.click(answerB.querySelector('button'))
expect(answerB.textContent).toMatch(/SELECTED/)
})
test(`[16] The state of the form survives route changes
- Filling out the form, navigating away and back, the entered data should survive
`, async () => {
fireEvent.click(formLink())
fireEvent.change(newQuestionInput(), { target: { value: 'foo' } })
fireEvent.change(newTrueAnswerInput(), { target: { value: 'bar' } })
fireEvent.change(newFalseAnswerInput(), { target: { value: 'baz' } })
fireEvent.click(wheelLink())
fireEvent.click(formLink())
expect(newQuestionInput()).toHaveValue('foo')
expect(newTrueAnswerInput()).toHaveValue('bar')
expect(newFalseAnswerInput()).toHaveValue('baz')
})
})
})