-
Notifications
You must be signed in to change notification settings - Fork 4
/
LocationForm.spec.js
379 lines (294 loc) · 10.1 KB
/
LocationForm.spec.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
import { shallowMount, createLocalVue } from '@vue/test-utils'
import axios from 'axios'
import LocationResult from 'location/models/LocationResult'
import LocationForm from 'location/components/LocationForm'
const localVue = createLocalVue()
describe('location/components/LocationForm', () => {
beforeEach(() => {
axios.get.mockClear()
})
it('fills in the user location data from the server', (done) => {
axios.mockResponseOnce('get', {
city: 'Chicago',
state: 'IL',
country: 'US',
})
const myWrapper = shallowMount(LocationForm, {
localVue,
propsData: {
scopeName: 'student',
},
})
setImmediate(() => {
expect(axios.get).toHaveBeenCalledWith('/student/current_location')
expect(myWrapper.vm.city).toEqual('Chicago')
expect(myWrapper.vm.state).toEqual('IL')
expect(myWrapper.vm.country).toEqual('US')
expect(myWrapper.find('#location_city').element.value).toEqual('Chicago')
expect(myWrapper.find('#location_state').element.value).toEqual('IL')
done()
})
})
it('accepts an optional accountId prop for the current location request', (done) => {
shallowMount(LocationForm, {
localVue,
propsData: {
accountId: 1,
scopeName: 'admin',
},
})
setImmediate(() => {
expect(axios.get).toHaveBeenCalledWith('/admin/current_location?account_id=1')
done()
})
})
it('accepts an optional teamId prop for the current location request', (done) => {
shallowMount(LocationForm, {
localVue,
propsData: {
teamId: 1,
scopeName: 'admin',
},
})
setImmediate(() => {
expect(axios.get).toHaveBeenCalledWith('/admin/current_location?team_id=1')
done()
})
})
describe('suggestions table', () => {
let wrapper
let handleSuggestionClickSpy
beforeEach(() => {
handleSuggestionClickSpy = jest.fn(() => {})
wrapper = shallowMount(LocationForm, {
localVue,
propsData: {
teamId: 1,
scopeName: 'admin',
},
methods: {
handleSuggestionClick: handleSuggestionClickSpy,
},
})
wrapper.setData({
suggestions: [
{ id: 1, city: 'Kansas City', state: 'Missouri', country: 'United States' },
{ id: 2, city: 'KC', state: 'MO', country: 'United States' },
{ id: 3, city: null, state: null, country: null },
],
})
})
it('appears if there are suggestions present', () => {
const suggestionsTable = wrapper.find({ ref: 'suggestionsTable' })
expect(suggestionsTable.exists()).toBe(true)
const suggestions = wrapper.findAll('.suggestion')
expect(suggestions.length).toEqual(3)
})
it('is not visible if there are no suggestions present', async () => {
wrapper.setData({ suggestions: [] })
await wrapper.vm.$nextTick()
const suggestionsTable = wrapper.find({ ref: 'suggestionsTable' })
expect(suggestionsTable.exists()).toBe(false)
const suggestions = wrapper.findAll('.suggestion')
expect(suggestions.length).toEqual(0)
})
it('contains a row for each suggestion present', () => {
const suggestions = wrapper.findAll('.suggestion')
let suggestionText = suggestions.at(0).text()
expect(suggestionText).toContain('Kansas City')
expect(suggestionText).toContain('Missouri')
expect(suggestionText).toContain('United States')
suggestionText = suggestions.at(1).text()
expect(suggestionText).toContain('KC')
expect(suggestionText).toContain('MO')
expect(suggestionText).toContain('United States')
suggestionText = suggestions.at(2).text()
expect(suggestionText).toContain('(no city)')
expect(suggestionText).toContain('(no state/province)')
expect(suggestionText).toContain('(no country)')
})
it('calls handleSuggestionClick when a suggestion row is clicked', () => {
const suggestions = wrapper.findAll('.suggestion')
suggestions.at(0).element.click()
expect(handleSuggestionClickSpy).toHaveBeenCalledTimes(1)
expect(handleSuggestionClickSpy).toHaveBeenCalledWith({
id: 1,
city: 'Kansas City',
state: 'Missouri',
country: 'United States',
})
suggestions.at(1).element.click()
expect(handleSuggestionClickSpy).toHaveBeenCalledTimes(2)
expect(handleSuggestionClickSpy).toHaveBeenCalledWith({
id: 2,
city: 'KC',
state: 'MO',
country: 'United States',
})
suggestions.at(2).element.click()
expect(handleSuggestionClickSpy).toHaveBeenCalledTimes(3)
expect(handleSuggestionClickSpy).toHaveBeenCalledWith({
id: 3,
city: null,
state: null,
country: null,
})
})
})
describe('saved locations table', () => {
let wrapper
beforeEach(() => {
wrapper = shallowMount(LocationForm, {
localVue,
propsData: {
teamId: 1,
scopeName: 'admin',
},
})
wrapper.setData({
savedLocation: {
id: 1,
city: 'Kansas City',
state: 'Missouri',
country: 'United States'
},
})
})
it('appears if there is a saved location present', () => {
const savedLocationTable = wrapper.find({ ref: 'savedLocationTable' })
expect(savedLocationTable.exists()).toBe(true)
const savedLocationText = wrapper.find({ ref: 'savedLocationTableRow' })
.text()
expect(savedLocationText).toContain('Kansas City')
expect(savedLocationText).toContain('Missouri')
expect(savedLocationText).toContain('United States')
})
it('is not visible if there is no saved location', async () => {
wrapper.setData({ savedLocation: null })
await wrapper.vm.$nextTick()
const savedLocationTable = wrapper.find({ ref: 'savedLocationTable' })
expect(savedLocationTable.exists()).toBe(false)
const savedLocationRow = wrapper.find({ ref: 'savedLocationTableRow' })
expect(savedLocationRow.exists()).toBe(false)
})
})
describe('methods', () => {
describe('handleSuggestionClick', () => {
let wrapper
let handleSubmitSpy
const suggestion = {
id: 1,
city: 'Kansas City',
state: 'Missouri',
country: 'United States'
}
beforeEach(() => {
handleSubmitSpy = jest.fn(() => {})
wrapper = shallowMount(LocationForm, {
localVue,
propsData: {
teamId: 1,
scopeName: 'admin',
},
methods: {
handleSubmit: handleSubmitSpy,
},
})
})
it('sets the city, state, and country information in the component data', () => {
wrapper.vm.handleSuggestionClick(suggestion)
expect(wrapper.vm.city).toEqual(suggestion.city)
expect(wrapper.vm.state).toEqual(suggestion.state)
expect(wrapper.vm.country).toEqual(suggestion.country)
expect(wrapper.vm.countryConfirmed).toBe(true)
})
it('calls handleSubmit', () => {
expect(handleSubmitSpy).not.toHaveBeenCalled()
wrapper.vm.handleSuggestionClick(suggestion)
expect(handleSubmitSpy).toHaveBeenCalledTimes(1)
})
})
})
describe('full integration suite for single result', () => {
const suggestionsResults = [
{
id: 'd7070f84',
state_code: 'MO',
state: 'Missouri',
latitude: 39.0997265,
longitude: -94.5785667,
city: 'Kansas City',
country_code: 'US',
country: 'United States'
},
]
let wrapper
beforeEach(() => {
axios.get.mockClear()
axios.post.mockClear()
axios.patch.mockClear()
})
it('it makes a GET request to populate the initial location', () => {
axios.mockResponseOnce('get', {
city: 'Chicago',
state: 'IL',
country: 'US',
})
expect(axios.get).not.toHaveBeenCalled()
wrapper = shallowMount(LocationForm, {
localVue,
propsData: {
teamId: 1,
scopeName: 'student',
handleConfirm: jest.fn(() => {}),
},
})
expect(axios.get).toHaveBeenCalledWith('/student/current_location?team_id=1')
})
it('sends a search request to the back-end using input data, returning one result', (done) => {
axios.mockResponseOnce('patch', {
results: suggestionsResults,
})
const handleOKResponseSpy = jest.spyOn(wrapper.vm, 'handleOKResponse')
const handleErrorResponseSpy = jest.spyOn(wrapper.vm, 'handleErrorResponse')
const suggestion = new LocationResult(suggestionsResults[0])
expect(axios.patch).not.toHaveBeenCalled()
wrapper.setData({
city: 'Kansas City',
state: 'Missouri',
country: 'United States',
})
wrapper.vm.handleSubmit()
expect(axios.patch).toHaveBeenCalledWith(
wrapper.vm.patchLocationEndpoint,
wrapper.vm.params
)
setImmediate(() => {
expect(wrapper.vm.status).toEqual(200)
expect(wrapper.vm.savedLocation).toEqual(suggestion)
expect(wrapper.vm.city).toEqual(suggestion.city)
expect(wrapper.vm.state).toEqual(suggestion.state)
expect(wrapper.vm.country).toEqual(suggestion.country)
expect(wrapper.vm.countryConfirmed).toBe(true)
expect(wrapper.vm.suggestions.length).toEqual(0)
expect(handleOKResponseSpy).toHaveBeenCalledTimes(1)
expect(handleErrorResponseSpy).toHaveBeenCalledTimes(0)
done()
})
})
it('saved the final saved location to the database via another XHR request', (done) => {
axios.mockResponseOnce('post', {})
const handleConfirmSpy = jest.spyOn(wrapper.vm, 'handleConfirm')
expect(axios.post).not.toHaveBeenCalled()
wrapper.vm.handleSubmit()
expect(axios.post).toHaveBeenCalledWith(
wrapper.vm.patchLocationEndpoint,
wrapper.vm.params
)
setImmediate(() => {
expect(handleConfirmSpy).toHaveBeenCalledTimes(1)
done()
})
})
})
})