Skip to content

Commit bff24c7

Browse files
Add map tests with initial field values
1 parent 4ae7fd8 commit bff24c7

1 file changed

Lines changed: 93 additions & 1 deletion

File tree

test/client/javascripts/location-map.test.js

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('Location Maps Client JS', () => {
128128
longInput.value = '-2.421975'
129129
longInput.dispatchEvent(new window.Event('change'))
130130

131-
// Expect it to update twice as when both fields are valid
131+
// Expect it to update once, only when both fields are valid
132132
expect(addMarkerMock).toHaveBeenCalledTimes(1)
133133
expect(flyToMock).toHaveBeenCalledTimes(1)
134134

@@ -138,6 +138,52 @@ describe('Location Maps Client JS', () => {
138138
onInteractMarkerChange({ coords: [-2.1478238, 54.155676] })
139139
})
140140

141+
test('initMaps with initial values', () => {
142+
const inputs = document.body.querySelectorAll('input.govuk-input')
143+
expect(inputs).toHaveLength(2)
144+
145+
const latInput = /** @type {HTMLInputElement} */ (inputs[0])
146+
const longInput = /** @type {HTMLInputElement} */ (inputs[1])
147+
148+
// Set some initial values prior to initMaps
149+
latInput.value = '53.743697'
150+
longInput.value = '-1.522781'
151+
152+
expect(() => initMaps()).not.toThrow()
153+
expect(onMock).toHaveBeenLastCalledWith(
154+
'map:ready',
155+
expect.any(Function)
156+
)
157+
158+
const onMapReady = onMock.mock.calls[0][1]
159+
expect(typeof onMapReady).toBe('function')
160+
161+
// Manually invoke onMapReady callback
162+
const flyToMock = jest.fn()
163+
onMapReady({
164+
map: {
165+
flyTo: flyToMock
166+
}
167+
})
168+
169+
expect(addPanelMock).toHaveBeenCalledWith('info', expect.any(Object))
170+
171+
expect(onMock).toHaveBeenLastCalledWith(
172+
'interact:markerchange',
173+
expect.any(Function)
174+
)
175+
176+
latInput.value = '53.825564'
177+
latInput.dispatchEvent(new window.Event('change'))
178+
179+
longInput.value = '-2.421975'
180+
longInput.dispatchEvent(new window.Event('change'))
181+
182+
// Expect it to update twice as both fields are already valid
183+
expect(addMarkerMock).toHaveBeenCalledTimes(2)
184+
expect(flyToMock).toHaveBeenCalledTimes(2)
185+
})
186+
141187
test('initMaps only applies when there are location components on the page', () => {
142188
const locations = document.querySelectorAll('.app-location-field')
143189

@@ -255,6 +301,52 @@ describe('Location Maps Client JS', () => {
255301
})
256302
})
257303

304+
test('initMaps with initial values', () => {
305+
const inputs = document.body.querySelectorAll('input.govuk-input')
306+
expect(inputs).toHaveLength(2)
307+
308+
const eastingInput = /** @type {HTMLInputElement} */ (inputs[0])
309+
const northingInput = /** @type {HTMLInputElement} */ (inputs[1])
310+
311+
// Set some initial values prior to initMaps
312+
eastingInput.value = '431571'
313+
northingInput.value = '427585'
314+
315+
expect(() => initMaps()).not.toThrow()
316+
expect(onMock).toHaveBeenLastCalledWith(
317+
'map:ready',
318+
expect.any(Function)
319+
)
320+
321+
const onMapReady = onMock.mock.calls[0][1]
322+
expect(typeof onMapReady).toBe('function')
323+
324+
// Manually invoke onMapReady callback
325+
const flyToMock = jest.fn()
326+
onMapReady({
327+
map: {
328+
flyTo: flyToMock
329+
}
330+
})
331+
332+
expect(addPanelMock).toHaveBeenCalledWith('info', expect.any(Object))
333+
334+
expect(onMock).toHaveBeenLastCalledWith(
335+
'interact:markerchange',
336+
expect.any(Function)
337+
)
338+
339+
eastingInput.value = '380779'
340+
eastingInput.dispatchEvent(new window.Event('change'))
341+
342+
northingInput.value = '462222'
343+
northingInput.dispatchEvent(new window.Event('change'))
344+
345+
// Expect it to update twice as both fields are already valid
346+
expect(addMarkerMock).toHaveBeenCalledTimes(2)
347+
expect(flyToMock).toHaveBeenCalledTimes(2)
348+
})
349+
258350
test('initMaps only applies when there are location components on the page', () => {
259351
const locations = document.querySelectorAll('.app-location-field')
260352

0 commit comments

Comments
 (0)