forked from bloominstituteoftechnology/W_S7_Challenge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmvpA.test.js
246 lines (227 loc) · 9.29 KB
/
mvpA.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
/*
Codegrade Setup
1- Global setup script
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - && sudo apt-get install -y nodejs; cg-jest install; npm i -g [email protected]
2- Per-student setup script
mv $FIXTURES/* . && npm install
3- Levels
3A- Codegrade tests
cg-jest run -- mvpA.test.js --runInBand --forceExit
3B Learner tests
cg-jest run -- mvpB.test.js --runInBand --forceExit
4- Fixtures
mvpA.test.js
*/
import React from 'react'
import App from './frontend/components/App'
import { BrowserRouter as Router } from 'react-router-dom'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import '@testing-library/jest-dom'
import server from './backend/mock-server'
jest.setTimeout(750) // default 5000 too long for Codegrade
const waitForOptions = { timeout: 250 }
const queryOptions = { exact: false }
const renderApp = ui => {
window.localStorage.clear()
window.history.pushState({}, 'Test page', '/')
return render(ui)
}
beforeAll(() => { server.listen() })
afterAll(() => { server.close() })
beforeEach(() => { renderApp(<Router><App /></Router>) })
afterEach(() => { server.resetHandlers() })
describe('Sprint 7 Challenge Codegrade Tests', () => {
describe('App routing', () => {
test('[1] <App /> Renders without crashing', () => {
// screen.debug()
})
test('[2] The <a>Home</a> and <a>Order</a> links navigate to "/" and "/order"', () => {
expect(document.location.pathname).toBe('/')
fireEvent.click(screen.getByText('Order', queryOptions))
expect(document.location.pathname).toBe('/order')
fireEvent.click(screen.getByText('Home', queryOptions))
expect(document.location.pathname).toBe('/')
})
test('[3] Renders the <Home /> component on path "/"', () => {
screen.getByText('Welcome to Bloom Pizza', queryOptions)
})
test('[4] Renders the <Form /> component on path "/order"', () => {
fireEvent.click(screen.getByText('Order', queryOptions))
screen.getByText('Order Your Pizza', queryOptions)
})
test('[5] Clicking on the pizza image navigates to "/order"', () => {
fireEvent.click(screen.getByAltText('order-pizza'))
expect(document.location.pathname).toBe('/order')
})
})
let name, size, pepperoni, peppers, pineapple, mushrooms, ham, submit
function getFormElements() {
name = document.querySelector('#fullName')
size = document.querySelector('#size')
pepperoni = document.querySelectorAll('input[type=checkbox]')[0]
peppers = document.querySelectorAll('input[type=checkbox]')[1]
pineapple = document.querySelectorAll('input[type=checkbox]')[2]
mushrooms = document.querySelectorAll('input[type=checkbox]')[3]
ham = document.querySelectorAll('input[type=checkbox]')[4]
submit = document.querySelector('input[type=submit]')
}
function getValues() { // eslint-disable-line
return {
name: name.value,
size: size.value,
pepperoni: pepperoni.checked,
peppers: peppers.checked,
pineapple: pineapple.checked,
mushrooms: mushrooms.checked,
ham: ham.checked,
submitDisabled: submit.disabled,
}
}
describe('Form submission success', () => {
beforeEach(() => {
fireEvent.click(screen.getByText('Order', queryOptions))
getFormElements()
})
test('[6] Successful order with no toppings renders correct message', async () => {
await waitFor(() => {
fireEvent.change(name, { target: { value: 'Mollusk' } })
fireEvent.change(size, { target: { value: 'L' } })
}, waitForOptions)
await waitFor(() => expect(submit).toBeEnabled())
await waitFor(() => {
fireEvent.click(submit)
}, waitForOptions)
await waitFor(() => {
screen.getByText('Thank you for your order, Mollusk!', queryOptions)
screen.getByText('Your large pizza', queryOptions)
screen.getByText('with no toppings', queryOptions)
}, waitForOptions)
})
test('[7] Successful order with some toppings renders correct message', async () => {
await waitFor(() => {
fireEvent.change(name, { target: { value: 'Fish' } })
fireEvent.change(size, { target: { value: 'S' } })
fireEvent.click(pepperoni)
fireEvent.click(pineapple)
fireEvent.click(ham)
}, waitForOptions)
await waitFor(() => expect(submit).toBeEnabled())
await waitFor(() => {
fireEvent.click(submit)
}, waitForOptions)
await waitFor(() => {
screen.getByText('Thank you for your order, Fish!', queryOptions)
screen.getByText('Your small pizza', queryOptions)
screen.getByText('with 3 toppings', queryOptions)
}, waitForOptions)
})
test('[8] A successful order clears the form', async () => {
await waitFor(() => {
fireEvent.change(name, { target: { value: 'Fish' } })
fireEvent.change(size, { target: { value: 'S' } })
}, waitForOptions)
await waitFor(() => expect(submit).toBeEnabled())
await waitFor(() => {
fireEvent.click(submit)
}, waitForOptions)
await waitFor(() => {
screen.getByText('Thank you', queryOptions)
}, waitForOptions)
await waitFor(() => {
expect(name.value).toBeFalsy()
expect(size.value).toBeFalsy()
expect(pepperoni.checked).toBeFalsy()
expect(peppers.checked).toBeFalsy()
expect(pineapple.checked).toBeFalsy()
expect(mushrooms.checked).toBeFalsy()
expect(ham.checked).toBeFalsy()
})
})
})
describe('Form validation', () => {
beforeEach(() => {
fireEvent.click(screen.getByText('Order', queryOptions))
getFormElements()
})
test('[9] Submit only enables if `fullName` and `size` pass validation', async () => {
expect(submit).toBeDisabled() // initial state
await waitFor(() => {
fireEvent.change(name, { target: { value: '123' } })
fireEvent.change(size, { target: { value: 'S' } })
}, waitForOptions)
await waitFor(() => expect(submit).toBeEnabled())
await waitFor(() => {
fireEvent.change(name, { target: { value: '12' } }) // BAD VALUE
fireEvent.change(size, { target: { value: 'S' } })
}, waitForOptions)
await waitFor(() => expect(submit).toBeDisabled())
await waitFor(() => {
fireEvent.change(name, { target: { value: '123' } })
fireEvent.change(size, { target: { value: 'M' } })
}, waitForOptions)
await waitFor(() => expect(submit).toBeEnabled())
await waitFor(() => {
fireEvent.change(name, { target: { value: '123' } })
fireEvent.change(size, { target: { value: 'X' } }) // BAD VALUE
}, waitForOptions)
await waitFor(() => expect(submit).toBeDisabled())
await waitFor(() => {
fireEvent.change(name, { target: { value: '123' } })
fireEvent.change(size, { target: { value: 'L' } })
}, waitForOptions)
await waitFor(() => expect(submit).toBeEnabled())
})
test('[10] Validation of `fullName` renders correct error message', async () => {
const validationError = 'full name must be at least 3 characters'
await waitFor(() => {
fireEvent.change(name, { target: { value: '1' } }) // BAD VALUE
}, waitForOptions)
await waitFor(() => screen.getByText(validationError, queryOptions))
await waitFor(() => {
fireEvent.change(name, { target: { value: '123' } })
}, waitForOptions)
await waitFor(() => {
expect(screen.queryByText(validationError, queryOptions)).not.toBeInTheDocument()
}, waitForOptions)
await waitFor(() => {
fireEvent.change(name, { target: { value: ' 12 ' } }) // BAD VALUE (trying to fool validation with whitespace)
}, waitForOptions)
await waitFor(() => screen.getByText(validationError, queryOptions))
await waitFor(() => {
fireEvent.change(name, { target: { value: '1234' } })
}, waitForOptions)
await waitFor(() => {
expect(screen.queryByText(validationError, queryOptions)).not.toBeInTheDocument()
}, waitForOptions)
})
test('[11] Validation of `size` renders correct error message', async () => {
const validationError = 'size must be S or M or L'
await waitFor(() => {
fireEvent.change(size, { target: { value: 'S' } })
}, waitForOptions)
await waitFor(() => {
expect(screen.queryByText(validationError, queryOptions)).not.toBeInTheDocument()
}, waitForOptions)
await waitFor(() => {
fireEvent.change(size, { target: { value: '' } }) // BAD VALUE
}, waitForOptions)
await waitFor(() => screen.getByText(validationError, queryOptions))
await waitFor(() => {
fireEvent.change(size, { target: { value: 'M' } })
}, waitForOptions)
await waitFor(() => {
expect(screen.queryByText(validationError, queryOptions)).not.toBeInTheDocument()
}, waitForOptions)
await waitFor(() => {
fireEvent.change(size, { target: { value: 'X' } }) // BAD VALUE
}, waitForOptions)
await waitFor(() => screen.getByText(validationError, queryOptions))
await waitFor(() => {
fireEvent.change(size, { target: { value: 'L' } })
}, waitForOptions)
await waitFor(() => {
expect(screen.queryByText(validationError, queryOptions)).not.toBeInTheDocument()
}, waitForOptions)
})
})
})