@@ -87,98 +87,114 @@ describe('Location Maps Client JS', () => {
8787 document . body . innerHTML = ''
8888 } )
8989
90- test ( 'initMaps initializes without errors when DOM elements are present' , ( ) => {
91- expect ( ( ) => initMaps ( ) ) . not . toThrow ( )
92- expect ( onMock ) . toHaveBeenLastCalledWith ( 'map:ready' , expect . any ( Function ) )
93-
94- const onMapReady = onMock . mock . calls [ 0 ] [ 1 ]
95- expect ( typeof onMapReady ) . toBe ( 'function' )
96-
97- // Manually invoke onMapReady callback
98- const flyToMock = jest . fn ( )
99- onMapReady ( {
100- map : {
101- flyTo : flyToMock
102- }
103- } )
90+ describe ( 'Map initialisation' , ( ) => {
91+ test ( 'initMaps initializes without errors when DOM elements are present' , ( ) => {
92+ expect ( ( ) => initMaps ( ) ) . not . toThrow ( )
93+ expect ( onMock ) . toHaveBeenLastCalledWith ( 'map:ready' , expect . any ( Function ) )
94+
95+ const onMapReady = onMock . mock . calls [ 0 ] [ 1 ]
96+ expect ( typeof onMapReady ) . toBe ( 'function' )
97+
98+ // Manually invoke onMapReady callback
99+ const flyToMock = jest . fn ( )
100+ onMapReady ( {
101+ map : {
102+ flyTo : flyToMock
103+ }
104+ } )
104105
105- expect ( addPanelMock ) . toHaveBeenCalledWith ( 'info' , expect . any ( Object ) )
106+ expect ( addPanelMock ) . toHaveBeenCalledWith ( 'info' , expect . any ( Object ) )
106107
107- expect ( onMock ) . toHaveBeenLastCalledWith (
108- 'interact:markerchange' ,
109- expect . any ( Function )
110- )
108+ expect ( onMock ) . toHaveBeenLastCalledWith (
109+ 'interact:markerchange' ,
110+ expect . any ( Function )
111+ )
111112
112- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
113- const onInteractMarkerChange = onMock . mock . calls [ 1 ] [ 1 ]
114- expect ( typeof onInteractMarkerChange ) . toBe ( 'function' )
115- onInteractMarkerChange ( { coords : [ 0 , 0 ] } )
113+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
114+ const onInteractMarkerChange = onMock . mock . calls [ 1 ] [ 1 ]
115+ expect ( typeof onInteractMarkerChange ) . toBe ( 'function' )
116+ onInteractMarkerChange ( { coords : [ 0 , 0 ] } )
116117
117- const inputs = document . body . querySelectorAll ( 'input.govuk-input' )
118- expect ( inputs ) . toHaveLength ( 2 )
118+ const inputs = document . body . querySelectorAll ( 'input.govuk-input' )
119+ expect ( inputs ) . toHaveLength ( 2 )
119120
120- const latInput = /** @type {HTMLInputElement } */ ( inputs [ 0 ] )
121- const longInput = /** @type {HTMLInputElement } */ ( inputs [ 1 ] )
121+ const latInput = /** @type {HTMLInputElement } */ ( inputs [ 0 ] )
122+ const longInput = /** @type {HTMLInputElement } */ ( inputs [ 1 ] )
122123
123- latInput . value = '53.825564'
124- latInput . dispatchEvent ( new window . Event ( 'change' ) )
124+ latInput . value = '53.825564'
125+ latInput . dispatchEvent ( new window . Event ( 'change' ) )
125126
126- longInput . value = '-2.421975'
127- longInput . dispatchEvent ( new window . Event ( 'change' ) )
127+ longInput . value = '-2.421975'
128+ longInput . dispatchEvent ( new window . Event ( 'change' ) )
129+
130+ // Expect it to update once, only when both fields are valid
131+ expect ( addMarkerMock ) . toHaveBeenCalledTimes ( 1 )
132+ expect ( flyToMock ) . toHaveBeenCalledTimes ( 1 )
133+ } )
128134
129- // Expect it to update once, only when both fields are valid
130- expect ( addMarkerMock ) . toHaveBeenCalledTimes ( 1 )
131- expect ( flyToMock ) . toHaveBeenCalledTimes ( 1 )
135+ test ( 'initMaps only applies when there are location components on the page' , ( ) => {
136+ const locations = document . querySelectorAll ( '.app-location-field' )
137+
138+ // Remove any locations for the test
139+ locations . forEach ( ( location ) => {
140+ location . remove ( )
141+ } )
142+
143+ expect ( ( ) => initMaps ( ) ) . not . toThrow ( )
144+ expect ( onMock ) . not . toHaveBeenCalled ( )
145+ } )
132146 } )
133147
134- test ( 'form does submit when submitter is the main form button' , ( ) => {
135- const preventDefault = jest . fn ( )
148+ describe ( 'Form submit event propagation' , ( ) => {
149+ test ( 'form does submit when submitter is the main form button' , ( ) => {
150+ const preventDefault = jest . fn ( )
136151
137- initMaps ( )
152+ initMaps ( )
138153
139- const form = document . body . querySelector ( 'form' )
140- if ( ! form ) {
141- throw new TypeError ( 'Unexpected empty form' )
142- }
154+ const form = document . body . querySelector ( 'form' )
155+ if ( ! form ) {
156+ throw new TypeError ( 'Unexpected empty form' )
157+ }
143158
144- const buttons = Array . from ( form . querySelectorAll ( 'button' ) )
145- const onFormSubmit = formSubmitFactory ( buttons )
159+ const buttons = Array . from ( form . querySelectorAll ( 'button' ) )
160+ const onFormSubmit = formSubmitFactory ( buttons )
146161
147- const e = /** @type {SubmitEvent } */ (
148- /** @type {unknown } */ ( {
149- submitter : buttons . at ( 0 ) ,
150- preventDefault
151- } )
152- )
162+ const e = /** @type {SubmitEvent } */ (
163+ /** @type {unknown } */ ( {
164+ submitter : buttons . at ( 0 ) ,
165+ preventDefault
166+ } )
167+ )
153168
154- onFormSubmit ( e )
169+ onFormSubmit ( e )
155170
156- expect ( preventDefault ) . toHaveBeenCalledTimes ( 0 )
157- } )
171+ expect ( preventDefault ) . toHaveBeenCalledTimes ( 0 )
172+ } )
158173
159- test ( 'form does not submit unless it is the main form button' , ( ) => {
160- const preventDefault = jest . fn ( )
174+ test ( 'form does not submit unless it is the main form button' , ( ) => {
175+ const preventDefault = jest . fn ( )
161176
162- initMaps ( )
177+ initMaps ( )
163178
164- const form = document . body . querySelector ( 'form' )
165- if ( ! form ) {
166- throw new TypeError ( 'Unexpected empty form' )
167- }
179+ const form = document . body . querySelector ( 'form' )
180+ if ( ! form ) {
181+ throw new TypeError ( 'Unexpected empty form' )
182+ }
168183
169- const buttons = Array . from ( form . querySelectorAll ( 'button' ) )
170- const onFormSubmit = formSubmitFactory ( buttons )
184+ const buttons = Array . from ( form . querySelectorAll ( 'button' ) )
185+ const onFormSubmit = formSubmitFactory ( buttons )
171186
172- const e = /** @type {SubmitEvent } */ (
173- /** @type {unknown } */ ( {
174- submitter : null ,
175- preventDefault
176- } )
177- )
187+ const e = /** @type {SubmitEvent } */ (
188+ /** @type {unknown } */ ( {
189+ submitter : null ,
190+ preventDefault
191+ } )
192+ )
178193
179- onFormSubmit ( e )
194+ onFormSubmit ( e )
180195
181- expect ( preventDefault ) . toHaveBeenCalledTimes ( 1 )
196+ expect ( preventDefault ) . toHaveBeenCalledTimes ( 1 )
197+ } )
182198 } )
183199
184200 describe ( 'Tile request transformer' , ( ) => {
0 commit comments