Skip to content

Commit 4ae7fd8

Browse files
Add easting and northing tests location map client tests
1 parent f37f9c0 commit 4ae7fd8

2 files changed

Lines changed: 126 additions & 8 deletions

File tree

jest.config.cjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ module.exports = {
5151
'@defra/forms-model/.*',
5252
'nanoid', // Supports ESM only
5353
'slug', // Supports ESM only
54-
'@defra/hapi-tracing' // Supports ESM only|
54+
'@defra/hapi-tracing', // Supports ESM only
55+
'geodesy' // Supports ESM only|
5556
].join('|')}/)`
5657
],
5758
testTimeout: 10000,

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

Lines changed: 124 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Location Maps Client JS', () => {
6464
<div class="govuk-form-group">
6565
<label class="govuk-label" for="cbkDgE__latitude">Latitude</label>
6666
<div class="govuk-input__wrapper">
67-
<input class="govuk-input govuk-input--width-10" id="cbkDgE__latitude" name="cbkDgE__latitude" type="text" value="53.2392016" inputmode="text">
67+
<input class="govuk-input govuk-input--width-10" id="cbkDgE__latitude" name="cbkDgE__latitude" type="text" inputmode="text">
6868
<div class="govuk-input__suffix" aria-hidden="true">°</div>
6969
</div>
7070
</div>
@@ -75,7 +75,7 @@ describe('Location Maps Client JS', () => {
7575
Longitude
7676
</label>
7777
<div class="govuk-input__wrapper">
78-
<input class="govuk-input govuk-input--width-10" id="cbkDgE__longitude" name="cbkDgE__longitude" type="text" value="-2.5734104" inputmode="text">
78+
<input class="govuk-input govuk-input--width-10" id="cbkDgE__longitude" name="cbkDgE__longitude" type="text" inputmode="text">
7979
<div class="govuk-input__suffix" aria-hidden="true">°</div>
8080
</div>
8181
</div>
@@ -116,11 +116,6 @@ describe('Location Maps Client JS', () => {
116116
expect.any(Function)
117117
)
118118

119-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
120-
const onInteractMarkerChange = onMock.mock.calls[1][1]
121-
expect(typeof onInteractMarkerChange).toBe('function')
122-
onInteractMarkerChange({ coords: [0, 0] })
123-
124119
const inputs = document.body.querySelectorAll('input.govuk-input')
125120
expect(inputs).toHaveLength(2)
126121

@@ -133,9 +128,131 @@ describe('Location Maps Client JS', () => {
133128
longInput.value = '-2.421975'
134129
longInput.dispatchEvent(new window.Event('change'))
135130

131+
// Expect it to update twice as when both fields are valid
132+
expect(addMarkerMock).toHaveBeenCalledTimes(1)
133+
expect(flyToMock).toHaveBeenCalledTimes(1)
134+
135+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
136+
const onInteractMarkerChange = onMock.mock.calls[1][1]
137+
expect(typeof onInteractMarkerChange).toBe('function')
138+
onInteractMarkerChange({ coords: [-2.1478238, 54.155676] })
139+
})
140+
141+
test('initMaps only applies when there are location components on the page', () => {
142+
const locations = document.querySelectorAll('.app-location-field')
143+
144+
// Remove any locations for the test
145+
locations.forEach((location) => {
146+
location.remove()
147+
})
148+
149+
expect(() => initMaps()).not.toThrow()
150+
expect(onMock).not.toHaveBeenCalled()
151+
})
152+
153+
test('initMaps only applies when there are supported location components on the page', () => {
154+
const locations = document.querySelectorAll('.app-location-field')
155+
156+
// Reset the location type of each component
157+
locations.forEach((location) => {
158+
location.setAttribute('data-locationtype', 'unknowntype')
159+
})
160+
161+
expect(() => initMaps()).not.toThrow()
162+
expect(onMock).not.toHaveBeenCalled()
163+
})
164+
})
165+
})
166+
167+
describe('Easting northing component', () => {
168+
beforeEach(() => {
169+
document.body.innerHTML = `
170+
<form method="post" novalidate="">
171+
<div class="govuk-form-group app-location-field " data-locationtype="eastingnorthingfield">
172+
<fieldset class="govuk-fieldset" aria-describedby="cbkDgE-hint">
173+
<legend class="govuk-fieldset__legend govuk-fieldset__legend--l">
174+
What is your easting and northing
175+
</legend>
176+
<div id="cbkDgE-hint" class="govuk-hint">
177+
For example. Easting: 248741, Northing: 63688
178+
</div>
179+
<div class="govuk-grid-row app-location-field-inputs">
180+
<div class="govuk-grid-column-one-third">
181+
<div class="govuk-form-group">
182+
<label class="govuk-label" for="cbkDgE__easting">Easting</label>
183+
<div class="govuk-input__wrapper">
184+
<input class="govuk-input govuk-input--width-10" id="cbkDgE__easting" name="cbkDgE__easting" type="text" inputmode="text">
185+
</div>
186+
</div>
187+
</div>
188+
<div class="govuk-grid-column-one-third">
189+
<div class="govuk-form-group">
190+
<label class="govuk-label" for="cbkDgE__northing">
191+
Northing
192+
</label>
193+
<div class="govuk-input__wrapper">
194+
<input class="govuk-input govuk-input--width-10" id="cbkDgE__northing" name="cbkDgE__northing" type="text" inputmode="text">
195+
</div>
196+
</div>
197+
</div>
198+
</div>
199+
</fieldset>
200+
</div>
201+
<div class="govuk-button-group">
202+
<button type="submit" data-prevent-double-click="true" class="govuk-button" data-module="govuk-button" data-govuk-button-init="">Continue</button>
203+
</div>
204+
</form>
205+
`
206+
})
207+
208+
describe('Map initialisation', () => {
209+
test('initMaps easting northing initializes without errors when DOM elements are present', () => {
210+
expect(() => initMaps()).not.toThrow()
211+
expect(onMock).toHaveBeenLastCalledWith(
212+
'map:ready',
213+
expect.any(Function)
214+
)
215+
216+
const onMapReady = onMock.mock.calls[0][1]
217+
expect(typeof onMapReady).toBe('function')
218+
219+
// Manually invoke onMapReady callback
220+
const flyToMock = jest.fn()
221+
onMapReady({
222+
map: {
223+
flyTo: flyToMock
224+
}
225+
})
226+
227+
expect(addPanelMock).toHaveBeenCalledWith('info', expect.any(Object))
228+
229+
expect(onMock).toHaveBeenLastCalledWith(
230+
'interact:markerchange',
231+
expect.any(Function)
232+
)
233+
234+
const inputs = document.body.querySelectorAll('input.govuk-input')
235+
expect(inputs).toHaveLength(2)
236+
237+
const eastingInput = /** @type {HTMLInputElement} */ (inputs[0])
238+
const northingInput = /** @type {HTMLInputElement} */ (inputs[1])
239+
240+
eastingInput.value = '380779'
241+
eastingInput.dispatchEvent(new window.Event('change'))
242+
243+
northingInput.value = '462222'
244+
northingInput.dispatchEvent(new window.Event('change'))
245+
136246
// Expect it to update once, only when both fields are valid
137247
expect(addMarkerMock).toHaveBeenCalledTimes(1)
138248
expect(flyToMock).toHaveBeenCalledTimes(1)
249+
250+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
251+
const onInteractMarkerChange = onMock.mock.calls[1][1]
252+
expect(typeof onInteractMarkerChange).toBe('function')
253+
onInteractMarkerChange({
254+
coords: [-2.147823, 54.155676]
255+
})
139256
})
140257

141258
test('initMaps only applies when there are location components on the page', () => {

0 commit comments

Comments
 (0)