@defra/interactive-map 0.0.56-alpha → 0.0.58-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/dist/esm/im-core.js +1 -1
  2. package/dist/umd/im-core.js +1 -1
  3. package/dist/umd/index.js +1 -1
  4. package/docs/api/button-definition.md +11 -0
  5. package/docs/api/context.md +2 -0
  6. package/docs/api.md +3 -1
  7. package/docs/assets/css/docusaurus.scss +7 -1
  8. package/docs/plugins/draw.md +326 -121
  9. package/package.json +1 -1
  10. package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
  11. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  12. package/plugins/datasets/dist/umd/index.js +1 -1
  13. package/plugins/datasets/src/initialise/DatasetsInit.jsx +6 -17
  14. package/plugins/datasets/src/initialise/DatasetsInit.test.jsx +3 -43
  15. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  16. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  17. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  18. package/plugins/draw/dist/esm/index.js +1 -1
  19. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  20. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  21. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  22. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  23. package/plugins/draw/dist/umd/index.js +1 -1
  24. package/plugins/draw/src/DrawInit.jsx +9 -6
  25. package/plugins/draw/src/DrawInit.test.jsx +15 -0
  26. package/plugins/draw/src/adapterEvents.js +82 -44
  27. package/plugins/draw/src/adapters/adapterContract.test.js +3 -3
  28. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +18 -10
  29. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +31 -22
  30. package/plugins/draw/src/adapters/maplibre/drawEvents.js +2 -1
  31. package/plugins/draw/src/adapters/maplibre/modes/drawMode/clickHandlers.js +13 -12
  32. package/plugins/draw/src/adapters/maplibre/modes/drawMode/clickHandlers.test.js +5 -5
  33. package/plugins/draw/src/adapters/maplibre/modes/drawMode/undoHandlers.js +1 -1
  34. package/plugins/draw/src/adapters/maplibre/modes/drawMode/undoHandlers.test.js +2 -2
  35. package/plugins/draw/src/adapters/maplibre/modes/drawPointMode.js +6 -9
  36. package/plugins/draw/src/adapters/maplibre/modes/drawPointMode.test.js +4 -4
  37. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.js +6 -9
  38. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.test.js +1 -1
  39. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/undoHandlers.js +8 -14
  40. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/undoHandlers.test.js +37 -8
  41. package/plugins/draw/src/adapters/maplibre/modes/editPointMode.js +11 -1
  42. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/undoHandlers.js +14 -19
  43. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/undoHandlers.test.js +41 -4
  44. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode.js +12 -1
  45. package/plugins/draw/src/adapters/maplibre/utils/editModeEvents.js +3 -1
  46. package/plugins/draw/src/adapters/maplibre/utils/geometryValidation.js +9 -10
  47. package/plugins/draw/src/adapters/maplibre/utils/geometryValidation.test.js +5 -24
  48. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +10 -7
  49. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +12 -7
  50. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.js +5 -0
  51. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.test.js +7 -3
  52. package/plugins/draw/src/adapters/openlayers/draw/DrawMode.js +11 -11
  53. package/plugins/draw/src/adapters/openlayers/draw/DrawMode.test.js +15 -15
  54. package/plugins/draw/src/adapters/openlayers/edit/EditMode.js +21 -28
  55. package/plugins/draw/src/adapters/openlayers/edit/EditMode.test.js +26 -5
  56. package/plugins/draw/src/adapters/openlayers/edit/selectionState.js +5 -6
  57. package/plugins/draw/src/adapters/openlayers/edit/selectionState.test.js +2 -12
  58. package/plugins/draw/src/adapters/openlayers/edit/undoOps.js +9 -0
  59. package/plugins/draw/src/adapters/openlayers/edit/undoOps.test.js +7 -0
  60. package/plugins/draw/src/adapters/openlayers/point/drawPointMode.js +6 -9
  61. package/plugins/draw/src/adapters/openlayers/point/drawPointMode.test.js +3 -3
  62. package/plugins/draw/src/adapters/openlayers/point/editPointMode.js +18 -9
  63. package/plugins/draw/src/adapters/openlayers/point/editPointMode.test.js +29 -4
  64. package/plugins/draw/src/adapters/openlayers/point/pointOps.js +3 -2
  65. package/plugins/draw/src/adapters/openlayers/point/pointOps.test.js +6 -0
  66. package/plugins/draw/src/adapters/openlayers/point/pointSelectionState.js +3 -4
  67. package/plugins/draw/src/adapters/openlayers/point/pointSelectionState.test.js +2 -12
  68. package/plugins/draw/src/api/createNewShape.js +4 -4
  69. package/plugins/draw/src/api/createNewShape.test.js +1 -1
  70. package/plugins/draw/src/api/editFeature.js +8 -11
  71. package/plugins/draw/src/api/editFeature.test.js +33 -22
  72. package/plugins/draw/src/api/newLine.test.js +3 -3
  73. package/plugins/draw/src/api/newPoint.js +4 -5
  74. package/plugins/draw/src/api/newPoint.test.js +11 -3
  75. package/plugins/draw/src/api/newPolygon.test.js +11 -3
  76. package/plugins/draw/src/api/recheckGeometry.js +32 -0
  77. package/plugins/draw/src/api/recheckGeometry.test.js +55 -0
  78. package/plugins/draw/src/api/setGeometry.js +33 -0
  79. package/plugins/draw/src/api/setGeometry.test.js +48 -0
  80. package/plugins/draw/src/api/split.js +26 -22
  81. package/plugins/draw/src/api/split.test.js +38 -18
  82. package/plugins/draw/src/events.js +269 -77
  83. package/plugins/draw/src/events.test.js +779 -34
  84. package/plugins/draw/src/hooks/useSpatialList.js +10 -11
  85. package/plugins/draw/src/hooks/useSpatialList.test.js +3 -3
  86. package/plugins/draw/src/manifest.js +30 -15
  87. package/plugins/draw/src/manifest.test.js +91 -9
  88. package/plugins/draw/src/reducer.js +31 -9
  89. package/plugins/draw/src/reducer.test.js +35 -3
  90. package/plugins/draw/src/utils/drawModes.js +27 -0
  91. package/plugins/draw/src/utils/drawModes.test.js +27 -0
  92. package/plugins/draw/src/utils/needsDrawTarget.js +1 -5
  93. package/plugins/draw/src/utils/needsDrawTarget.test.js +1 -11
  94. package/plugins/draw/src/utils/touchTarget.js +34 -26
  95. package/plugins/draw/src/utils/touchTarget.test.js +32 -20
  96. package/plugins/draw/src/validation/commitChecks.js +187 -0
  97. package/plugins/draw/src/validation/commitChecks.test.js +209 -0
  98. package/plugins/draw/src/validation/liveDrawChecks.js +11 -11
  99. package/plugins/draw/src/validation/liveDrawChecks.test.js +37 -37
  100. package/plugins/draw/src/validation/liveStroke.js +6 -6
  101. package/plugins/draw/src/validation/liveStroke.test.js +27 -27
  102. package/plugins/draw/src/validation/rules.js +16 -14
  103. package/plugins/draw/src/validation/rules.test.js +5 -5
  104. package/plugins/draw/src/validation/validateGeometry.js +58 -31
  105. package/plugins/draw/src/validation/validateGeometry.test.js +68 -44
  106. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  107. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  108. package/plugins/interact/dist/umd/index.js +1 -1
  109. package/plugins/interact/src/hooks/useInteractionHandlers.js +3 -5
  110. package/plugins/interact/src/hooks/useInteractionHandlers.test.js +4 -3
  111. package/plugins/interact/src/hooks/useSpatialList.js +16 -17
  112. package/plugins/interact/src/hooks/useSpatialList.test.js +9 -9
  113. package/plugins/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
  114. package/plugins/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
  115. package/plugins/map-styles/src/MapStyles.jsx +2 -2
  116. package/providers/maplibre/dist/esm/index.js +1 -1
  117. package/providers/maplibre/dist/umd/index.js +1 -1
  118. package/providers/maplibre/src/index.js +11 -2
  119. package/providers/maplibre/src/index.test.js +23 -1
  120. package/providers/maplibre/src/utils/detectModuleWorker.js +8 -19
  121. package/providers/maplibre/src/utils/detectModuleWorker.test.js +19 -23
  122. package/providers/maplibre/src/utils/workerUrl.js +12 -0
  123. package/providers/maplibre/src/utils/workerUrl.test.js +14 -1
  124. package/src/App/components/MapButton/MapButton.jsx +6 -0
  125. package/src/App/components/MapButton/MapButton.test.jsx +10 -0
  126. package/src/App/components/Markers/Markers.jsx +3 -5
  127. package/src/App/components/Markers/Markers.test.jsx +9 -11
  128. package/src/App/components/Panel/Panel.jsx +2 -9
  129. package/src/App/hooks/useInterfaceAPI.js +16 -4
  130. package/src/App/hooks/useInterfaceAPI.test.js +66 -1
  131. package/src/App/renderer/mapButtons.js +2 -1
  132. package/src/App/renderer/mapButtons.test.js +9 -0
  133. package/src/App/store/MapProvider.jsx +3 -0
  134. package/src/App/store/MapProvider.test.jsx +24 -0
  135. package/src/config/mapSizeScales.js +3 -0
  136. package/src/types.js +4 -0
  137. package/src/utils/focusAfterPanelClose.js +9 -0
  138. package/src/utils/focusAfterPanelClose.test.js +40 -0
@@ -1,4 +1,5 @@
1
1
  import { attachEvents } from './events.js'
2
+ import { SLOW_CHECK_MS } from './validation/commitChecks.js'
2
3
 
3
4
  jest.useFakeTimers()
4
5
 
@@ -10,9 +11,10 @@ const setup = (overrides = {}) => {
10
11
  done: jest.fn(),
11
12
  cancel: jest.fn(),
12
13
  add: jest.fn(),
14
+ delete: jest.fn(),
13
15
  get: jest.fn(() => ({ id: 'F' })),
14
16
  undo: jest.fn(),
15
- setGeometryValid: jest.fn(),
17
+ setCanFinish: jest.fn(),
16
18
  setInvalid: jest.fn(),
17
19
  deleteVertex: jest.fn(),
18
20
  nudgeSelectedVertex: jest.fn(),
@@ -27,13 +29,14 @@ const setup = (overrides = {}) => {
27
29
  const mapProvider = { draw }
28
30
  const eventBus = { emit: jest.fn() }
29
31
  const hints = { show: jest.fn() }
32
+ const announce = jest.fn()
30
33
  const buttonConfig = { drawDone: {}, drawCancel: {}, drawUndo: {}, drawDeletePoint: {}, drawSnap: {}, ...overrides.buttonConfig }
31
34
  const appState = { layoutRefs: { viewportRef: { current: 'viewport' } }, interfaceType: 'mouse' }
32
35
  const appConfig = { id: 'app' }
33
- const mapState = { mapSize: 'medium' }
36
+ const mapState = { mapSizeScale: 1.5 }
34
37
 
35
- const detach = attachEvents({ appState, appConfig, mapState, pluginState, mapProvider, buttonConfig, eventBus, hints })
36
- return { draw, dispatch, pluginState, mapProvider, eventBus, hints, buttonConfig, detach }
38
+ const detach = attachEvents({ appState, appConfig, mapState, pluginState, mapProvider, buttonConfig, eventBus, hints, announce })
39
+ return { draw, dispatch, pluginState, mapProvider, eventBus, hints, announce, buttonConfig, detach }
37
40
  }
38
41
 
39
42
  const drawHandler = (draw, event) => draw.on.mock.calls.find(([name]) => name === event)[1]
@@ -239,67 +242,67 @@ describe('geometrychange validation', () => {
239
242
  test('ignores preview payloads that carry no phase', () => {
240
243
  const { draw, dispatch } = setup()
241
244
  drawHandler(draw, 'geometrychange')({ coordinates: [[0, 0], [1, 1]] })
242
- expect(dispatch).not.toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: expect.anything() })
245
+ expect(dispatch).not.toHaveBeenCalledWith({ type: 'SET_VALIDATION_STATUS', payload: expect.anything() })
243
246
  })
244
247
 
245
248
  test('opens the gate for a valid geometry', () => {
246
249
  const { draw, dispatch } = setup()
247
- drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'commit-add', vertexIndex: 3 })
248
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: true })
250
+ drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'change', vertexIndex: 3 })
251
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_VALIDATION_STATUS', payload: 'valid' })
249
252
  })
250
253
 
251
254
  test('gates a self-intersecting shape while drawing', () => {
252
255
  const { draw, dispatch, eventBus, hints } = setup()
253
- drawHandler(draw, 'geometrychange')({ feature: bowtieFeature, phase: 'commit-add', vertexIndex: 3 })
254
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: false })
255
- expect(draw.setGeometryValid).toHaveBeenCalledWith(false)
256
+ drawHandler(draw, 'geometrychange')({ feature: bowtieFeature, phase: 'change', vertexIndex: 3 })
257
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_VALIDATION_STATUS', payload: 'invalid' })
258
+ expect(draw.setCanFinish).toHaveBeenCalledWith(false)
256
259
  expect(eventBus.emit).toHaveBeenCalledWith('draw:geometryinvalid', expect.objectContaining({ reason: expect.stringMatching(/intersect/i) }))
257
260
  expect(hints.show).toHaveBeenCalledWith(expect.stringMatching(/intersect/i))
258
261
  })
259
262
 
260
263
  test('gates a part-drawn shape below the minimum vertex count, but skips the hint — an incomplete shape is expected, not a mistake', () => {
261
264
  const { draw, dispatch, eventBus, hints } = setup()
262
- drawHandler(draw, 'geometrychange')({ feature: partDrawnFeature, phase: 'commit-add', vertexIndex: 1 })
263
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: false })
265
+ drawHandler(draw, 'geometrychange')({ feature: partDrawnFeature, phase: 'change', vertexIndex: 1 })
266
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_VALIDATION_STATUS', payload: 'invalid' })
264
267
  expect(eventBus.emit).toHaveBeenCalledWith('draw:geometryinvalid', expect.objectContaining({ reason: expect.stringMatching(/at least 3 points/) }))
265
268
  expect(hints.show).not.toHaveBeenCalled()
266
269
  })
267
270
 
268
271
  test('gates a zero-area shape while drawing', () => {
269
272
  const { draw, dispatch } = setup()
270
- drawHandler(draw, 'geometrychange')({ feature: collinearFeature, phase: 'commit-add', vertexIndex: 2 })
271
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: false })
273
+ drawHandler(draw, 'geometrychange')({ feature: collinearFeature, phase: 'change', vertexIndex: 2 })
274
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_VALIDATION_STATUS', payload: 'invalid' })
272
275
  })
273
276
 
274
277
  test('gates a self-intersecting move in edit mode (never reverts)', () => {
275
278
  const { draw, dispatch, eventBus } = setup()
276
279
  draw.getMode.mockReturnValue('edit_vertex')
277
- drawHandler(draw, 'geometrychange')({ feature: bowtieFeature, phase: 'commit-move', vertexIndex: 2 })
278
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: false })
280
+ drawHandler(draw, 'geometrychange')({ feature: bowtieFeature, phase: 'change', vertexIndex: 2 })
281
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_VALIDATION_STATUS', payload: 'invalid' })
279
282
  expect(eventBus.emit).toHaveBeenCalledWith('draw:geometryinvalid', expect.objectContaining({ reason: expect.stringMatching(/intersect/i) }))
280
283
  })
281
284
 
282
285
  test('keeps a valid edit move (gate open)', () => {
283
286
  const { draw, dispatch } = setup()
284
287
  draw.getMode.mockReturnValue('edit_vertex')
285
- drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'commit-move', vertexIndex: 1 })
286
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: true })
288
+ drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'change', vertexIndex: 1 })
289
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_VALIDATION_STATUS', payload: 'valid' })
287
290
  })
288
291
 
289
292
  test('applies the per-session user validator as a gate', () => {
290
293
  const { draw, dispatch, eventBus, hints } = setup()
291
- draw._geometryValidator = () => ({ valid: false, reason: 'too big' })
292
- drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'commit-add', vertexIndex: 3 })
293
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: false })
294
+ draw._commitValidator = () => ({ valid: false, reason: 'too big' })
295
+ drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'change', vertexIndex: 3 })
296
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_VALIDATION_STATUS', payload: 'invalid' })
294
297
  expect(eventBus.emit).toHaveBeenCalledWith('draw:geometryinvalid', expect.objectContaining({ reason: 'too big' }))
295
298
  expect(hints.show).toHaveBeenCalledWith('too big')
296
299
  })
297
300
 
298
301
  test('a user validator that vetoes with no reason (plain `false`) still emits, but skips the hint', () => {
299
302
  const { draw, dispatch, eventBus, hints } = setup()
300
- draw._geometryValidator = () => false
301
- drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'commit-add', vertexIndex: 3 })
302
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: false })
303
+ draw._commitValidator = () => false
304
+ drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'change', vertexIndex: 3 })
305
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_VALIDATION_STATUS', payload: 'invalid' })
303
306
  expect(eventBus.emit).toHaveBeenCalledWith('draw:geometryinvalid', expect.objectContaining({ reason: null }))
304
307
  expect(hints.show).not.toHaveBeenCalled()
305
308
  })
@@ -307,29 +310,32 @@ describe('geometrychange validation', () => {
307
310
  test('drives the invalid stroke from committed validity in edit mode only', () => {
308
311
  const { draw } = setup()
309
312
  draw.getMode.mockReturnValue('draw_polygon')
310
- drawHandler(draw, 'geometrychange')({ feature: bowtieFeature, phase: 'commit-add', vertexIndex: 3 })
313
+ drawHandler(draw, 'geometrychange')({ feature: bowtieFeature, phase: 'change', vertexIndex: 3 })
311
314
  expect(draw.setInvalid).not.toHaveBeenCalled() // draw mode: the adapter's live check owns the stroke
312
315
 
313
316
  draw.getMode.mockReturnValue('edit_vertex')
314
- drawHandler(draw, 'geometrychange')({ feature: bowtieFeature, phase: 'commit-move', vertexIndex: 2 })
317
+ drawHandler(draw, 'geometrychange')({ feature: bowtieFeature, phase: 'change', vertexIndex: 2 })
315
318
  expect(draw.setInvalid).toHaveBeenCalledWith(true)
316
319
  })
317
320
 
318
321
  test('drives the invalid stroke from committed validity in a point edit too', () => {
319
322
  const { draw } = setup()
320
323
  draw.getMode.mockReturnValue('edit_point')
321
- drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'commit-move', vertexIndex: 0 })
324
+ drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'change', vertexIndex: 0 })
322
325
  expect(draw.setInvalid).toHaveBeenCalledWith(false)
323
326
  })
324
327
 
325
- test('a live validity flip (edit drag) drives the Done gate', () => {
328
+ test('a live validity flip (edit drag) drives the Done gate alongside the commit check', () => {
326
329
  const { draw, dispatch } = setup()
327
330
  drawHandler(draw, 'validitychange')({ valid: false, reason: 'Shape must not intersect itself' })
328
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: false })
329
- expect(draw.setGeometryValid).toHaveBeenCalledWith(false)
331
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_PREVIEW_VALID', payload: false })
330
332
 
331
333
  drawHandler(draw, 'validitychange')({ valid: true, reason: null })
332
- expect(dispatch).toHaveBeenCalledWith({ type: 'SET_GEOMETRY_VALID', payload: true })
334
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_PREVIEW_VALID', payload: true })
335
+ // A live flip never writes the commit-level gate, so it can't enable Done while a
336
+ // commit check is still pending or failing.
337
+ expect(dispatch).not.toHaveBeenCalledWith(expect.objectContaining({ type: 'SET_VALIDATION_STATUS' }))
338
+ expect(draw.setCanFinish).not.toHaveBeenCalled()
333
339
  })
334
340
 
335
341
  test('a live placement-veto flip drives the Add point gate', () => {
@@ -359,9 +365,748 @@ describe('geometrychange validation', () => {
359
365
  const { draw } = setup()
360
366
  draw.getMode.mockReturnValue('draw_polygon')
361
367
  const validator = jest.fn(() => true)
362
- draw._geometryValidator = validator
363
- drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'commit-add', vertexIndex: 3 })
364
- expect(validator).toHaveBeenCalledWith({ feature: squareFeature, phase: 'commit-add', vertexIndex: 3, mode: 'draw_polygon' })
368
+ draw._commitValidator = validator
369
+ drawHandler(draw, 'geometrychange')({ feature: squareFeature, phase: 'change', vertexIndex: 3 })
370
+ expect(validator).toHaveBeenCalledWith(
371
+ { feature: squareFeature, phase: 'change', vertexIndex: 3, mode: 'draw_polygon' },
372
+ { signal: expect.any(AbortSignal), revision: expect.any(Number) }
373
+ )
374
+ })
375
+ })
376
+
377
+ describe('async geometry checks', () => {
378
+ const square = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]] } }
379
+ const otherSquare = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[0, 0], [0, 2], [2, 2], [2, 0], [0, 0]]] } }
380
+
381
+ // A promise the test settles itself, so replies can be made to arrive in any order
382
+ const deferred = () => {
383
+ const handles = {}
384
+ handles.promise = new Promise((resolve, reject) => Object.assign(handles, { resolve, reject }))
385
+ return handles
386
+ }
387
+ const flush = () => Promise.resolve()
388
+ const commit = (draw, feature, phase = 'change') => drawHandler(draw, 'geometrychange')({ feature, phase, vertexIndex: 0 })
389
+ const slow = () => jest.advanceTimersByTime(SLOW_CHECK_MS)
390
+ const lastStatus = (dispatch) => dispatch.mock.calls.filter(([a]) => a.type === 'SET_VALIDATION_STATUS').map(([a]) => a.payload).pop()
391
+
392
+ test('marks a check as pending, then applies its result', async () => {
393
+ const { draw, dispatch } = setup()
394
+ const reply = deferred()
395
+ draw.getMode.mockReturnValue('edit_vertex')
396
+ draw._commitValidator = () => reply.promise
397
+
398
+ commit(draw, square)
399
+ expect(lastStatus(dispatch)).toBe('pending')
400
+ expect(draw.setInvalid).not.toHaveBeenCalled() // pending doesn't change the stroke
401
+
402
+ reply.resolve({ valid: true })
403
+ await flush()
404
+ expect(lastStatus(dispatch)).toBe('valid')
405
+ expect(draw.setCanFinish).toHaveBeenLastCalledWith(true)
406
+ expect(draw.setInvalid).toHaveBeenLastCalledWith(false)
407
+ })
408
+
409
+ test('a reply for an older change never changes the gate', async () => {
410
+ const { draw, dispatch, eventBus } = setup()
411
+ const first = deferred()
412
+ const second = deferred()
413
+ draw.getMode.mockReturnValue('edit_vertex')
414
+ draw._commitValidator = jest.fn()
415
+ .mockReturnValueOnce(first.promise)
416
+ .mockReturnValueOnce(second.promise)
417
+
418
+ commit(draw, square)
419
+ commit(draw, otherSquare)
420
+ second.resolve({ valid: true })
421
+ await flush()
422
+ expect(lastStatus(dispatch)).toBe('valid')
423
+
424
+ // The first change's reply arrives late, and says invalid — it's dropped
425
+ first.resolve({ valid: false, reason: 'overlaps a parcel' })
426
+ await flush()
427
+ expect(lastStatus(dispatch)).toBe('valid')
428
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:geometryinvalid', expect.anything())
429
+ })
430
+
431
+ test('a new change aborts the previous check', () => {
432
+ const { draw } = setup()
433
+ const signals = []
434
+ draw.getMode.mockReturnValue('edit_vertex')
435
+ draw._commitValidator = (_event, { signal }) => { signals.push(signal); return new Promise(() => {}) }
436
+
437
+ commit(draw, square)
438
+ commit(draw, otherSquare)
439
+ expect(signals[0].aborted).toBe(true)
440
+ expect(signals[1].aborted).toBe(false)
441
+ })
442
+
443
+ test('passes an increasing revision with each change', () => {
444
+ const { draw } = setup()
445
+ const revisions = []
446
+ draw._commitValidator = (_event, { revision }) => { revisions.push(revision); return true }
447
+ commit(draw, square, 'change')
448
+ commit(draw, otherSquare, 'change')
449
+ expect(revisions[1]).toBeGreaterThan(revisions[0])
450
+ })
451
+
452
+ test('a rejected check keeps Done blocked without reporting the shape invalid', async () => {
453
+ const { draw, dispatch, eventBus } = setup()
454
+ const reply = deferred()
455
+ draw._commitValidator = () => reply.promise
456
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
457
+
458
+ commit(draw, square, 'change')
459
+ reply.reject(new Error('503'))
460
+ await flush()
461
+ await flush()
462
+ expect(lastStatus(dispatch)).toBe('error')
463
+ expect(draw.setCanFinish).toHaveBeenLastCalledWith(false)
464
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:geometryinvalid', expect.anything())
465
+ console.warn.mockRestore()
466
+ })
467
+
468
+ test('a callback that throws is an error result, not a crash', () => {
469
+ const { draw, dispatch } = setup()
470
+ draw._commitValidator = () => { throw new Error('boom') }
471
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
472
+
473
+ expect(() => commit(draw, square, 'change')).not.toThrow()
474
+ expect(lastStatus(dispatch)).toBe('error')
475
+ console.warn.mockRestore()
476
+ })
477
+
478
+ test('a synchronous callback never shows the pending state', () => {
479
+ const { draw, dispatch } = setup()
480
+ draw._commitValidator = () => true
481
+ commit(draw, square, 'change')
482
+ expect(dispatch).not.toHaveBeenCalledWith({ type: 'SET_VALIDATION_STATUS', payload: 'pending' })
483
+ expect(lastStatus(dispatch)).toBe('valid')
484
+ })
485
+
486
+ test('the built-in rules run first, so a failing shape never reaches the callback', () => {
487
+ const { draw } = setup()
488
+ const validator = jest.fn(() => true)
489
+ draw._commitValidator = validator
490
+ const partDrawn = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[0, 0], [1, 0], [0, 0]]] } }
491
+ commit(draw, partDrawn, 'change')
492
+ expect(validator).not.toHaveBeenCalled()
493
+ })
494
+
495
+ test('cancelling aborts a check in flight and drops its reply', async () => {
496
+ const { draw, dispatch, buttonConfig } = setup()
497
+ const reply = deferred()
498
+ let signal
499
+ draw._commitValidator = (_event, ctx) => { signal = ctx.signal; return reply.promise }
500
+
501
+ commit(draw, square, 'change')
502
+ buttonConfig.drawCancel.onClick()
503
+ expect(signal.aborted).toBe(true)
504
+
505
+ dispatch.mockClear()
506
+ reply.resolve({ valid: true })
507
+ await flush()
508
+ expect(dispatch).not.toHaveBeenCalledWith(expect.objectContaining({ type: 'SET_VALIDATION_STATUS' }))
509
+ })
510
+
511
+ test('a commit arriving after the session has ended is ignored', () => {
512
+ const { draw } = setup()
513
+ const validator = jest.fn(() => true)
514
+ draw._commitValidator = validator
515
+ draw.getMode.mockReturnValue('disabled')
516
+ commit(draw, square, 'change')
517
+ expect(validator).not.toHaveBeenCalled()
518
+ })
519
+
520
+ test('finishing reuses a passing result for the same shape, without checking again', () => {
521
+ const { draw, eventBus } = setup()
522
+ const validator = jest.fn(() => true)
523
+ draw._commitValidator = validator
524
+ // A draw-mode commit carries the placed vertices as an open ring
525
+ const placed = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0]]] } }
526
+ commit(draw, placed, 'change')
527
+ expect(validator).toHaveBeenCalledTimes(1)
528
+
529
+ drawHandler(draw, 'create')({ id: 'new', ...square })
530
+ expect(validator).toHaveBeenCalledTimes(1)
531
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'new' }))
532
+ })
533
+
534
+ test('finishing a different shape checks it with phase create', () => {
535
+ const { draw } = setup()
536
+ const validator = jest.fn(() => true)
537
+ draw._commitValidator = validator
538
+ drawHandler(draw, 'create')({ id: 'new', ...square })
539
+ expect(validator).toHaveBeenCalledWith(expect.objectContaining({ phase: 'create' }), expect.anything())
540
+ })
541
+
542
+ test('a point placed while its check is pending is held, then created once the check passes', async () => {
543
+ const { draw, dispatch, eventBus } = setup()
544
+ const reply = deferred()
545
+ draw.getMode.mockReturnValue('draw_point')
546
+ draw._commitValidator = () => reply.promise
547
+ const point = { id: 'p1', type: 'Feature', geometry: { type: 'Point', coordinates: [0, 0] } }
548
+ draw.get.mockReturnValue(point)
549
+
550
+ drawHandler(draw, 'create')(point)
551
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_HOLDING', payload: true })
552
+ jest.runAllTimers()
553
+ expect(draw.changeMode).not.toHaveBeenCalledWith('edit_point', expect.anything())
554
+
555
+ reply.resolve(true)
556
+ await flush()
557
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', point)
558
+ })
559
+
560
+ test('a held point whose check fails reopens in point edit mode', async () => {
561
+ const { draw, eventBus } = setup()
562
+ const reply = deferred()
563
+ draw.getMode.mockReturnValue('draw_point')
564
+ draw._commitValidator = () => reply.promise
565
+ const point = { id: 'p1', type: 'Feature', geometry: { type: 'Point', coordinates: [0, 0] } }
566
+ draw.get.mockReturnValue(point)
567
+
568
+ drawHandler(draw, 'create')(point)
569
+ jest.runAllTimers()
570
+ reply.resolve({ valid: false, reason: 'inside another parcel' })
571
+ await flush()
572
+ jest.runAllTimers()
573
+ expect(draw.changeMode).toHaveBeenCalledWith('edit_point', expect.objectContaining({ featureId: 'p1' }))
574
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:reopened', expect.objectContaining({ status: 'invalid', reason: 'inside another parcel' }))
575
+ })
576
+
577
+ test('a point that fails its check reopens in point edit mode', () => {
578
+ const { draw, eventBus } = setup()
579
+ draw.getMode.mockReturnValue('draw_point')
580
+ draw._commitValidator = () => ({ valid: false, reason: 'inside another parcel' })
581
+ const point = { id: 'p1', type: 'Feature', geometry: { type: 'Point', coordinates: [0, 0] } }
582
+
583
+ drawHandler(draw, 'create')(point)
584
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:geometryinvalid', expect.objectContaining({ reason: 'inside another parcel', phase: 'create' }))
585
+ jest.runAllTimers()
586
+ expect(draw.changeMode).toHaveBeenCalledWith('edit_point', expect.objectContaining({ featureId: 'p1' }))
587
+ })
588
+
589
+ describe('screen reader announcements', () => {
590
+ test('a check that settles straight away announces nothing new', () => {
591
+ const { draw, announce } = setup()
592
+ draw._commitValidator = () => ({ valid: false, reason: 'too big' })
593
+ commit(draw, square, 'change')
594
+ draw._commitValidator = () => true
595
+ commit(draw, otherSquare, 'change')
596
+ expect(announce).not.toHaveBeenCalled()
597
+ })
598
+
599
+ test('announces an async check starting, then passing', async () => {
600
+ const { draw, announce } = setup()
601
+ const reply = deferred()
602
+ draw._commitValidator = () => reply.promise
603
+ commit(draw, square, 'change')
604
+ expect(announce).not.toHaveBeenCalled()
605
+ slow()
606
+ expect(announce).toHaveBeenLastCalledWith('Checking shape', 'action')
607
+ reply.resolve(true)
608
+ await flush()
609
+ expect(announce).toHaveBeenLastCalledWith('Shape check passed', 'action')
610
+ })
611
+
612
+ test('announces a failure reason before the hint, so the hint does not repeat it', async () => {
613
+ const { draw, announce, hints } = setup()
614
+ const reply = deferred()
615
+ draw._commitValidator = () => reply.promise
616
+ commit(draw, square, 'change')
617
+ slow()
618
+ reply.resolve({ valid: false, reason: 'Overlaps a neighbouring parcel' })
619
+ await flush()
620
+ expect(announce).toHaveBeenLastCalledWith('Overlaps a neighbouring parcel', 'action')
621
+ expect(hints.show).toHaveBeenCalledWith('Overlaps a neighbouring parcel')
622
+ expect(announce.mock.invocationCallOrder.at(-1)).toBeLessThan(hints.show.mock.invocationCallOrder[0])
623
+ })
624
+
625
+ test('announces a failure with no reason, and an error', async () => {
626
+ const { draw, announce } = setup()
627
+ const first = deferred()
628
+ const second = deferred()
629
+ draw._commitValidator = jest.fn().mockReturnValueOnce(first.promise).mockReturnValueOnce(second.promise)
630
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
631
+
632
+ commit(draw, square, 'change')
633
+ slow()
634
+ first.resolve(false)
635
+ await flush()
636
+ expect(announce).toHaveBeenLastCalledWith('Shape check failed', 'action')
637
+
638
+ commit(draw, otherSquare, 'change')
639
+ slow()
640
+ second.reject(new Error('503'))
641
+ await flush()
642
+ await flush()
643
+ expect(announce).toHaveBeenLastCalledWith('Shape could not be checked', 'action')
644
+ console.warn.mockRestore()
645
+ })
646
+
647
+ test('a quick async check announces nothing and never reads "Checking…"', async () => {
648
+ const { draw, dispatch, announce } = setup()
649
+ const reply = deferred()
650
+ draw._commitValidator = () => reply.promise
651
+ commit(draw, square, 'change')
652
+ expect(lastStatus(dispatch)).toBe('pending')
653
+ reply.resolve(true)
654
+ await flush()
655
+ slow()
656
+ expect(announce).not.toHaveBeenCalled()
657
+ expect(dispatch).not.toHaveBeenCalledWith({ type: 'SET_CHECK_SLOW' })
658
+ })
659
+
660
+ test('a slow check is marked as slow', () => {
661
+ const { draw, dispatch } = setup()
662
+ draw._commitValidator = () => new Promise(() => {})
663
+ commit(draw, square, 'change')
664
+ slow()
665
+ expect(dispatch).toHaveBeenLastCalledWith({ type: 'SET_CHECK_SLOW' })
666
+ })
667
+
668
+ test('a result\u2019s own announcement replaces draw\u2019s, and its hint', () => {
669
+ const { draw, announce, hints, eventBus } = setup()
670
+ draw._commitValidator = () => ({ valid: false, reason: 'overlap', announcement: 'Overlaps parcel 12. See the issues panel.' })
671
+ commit(draw, square, 'change')
672
+ expect(announce).toHaveBeenLastCalledWith('Overlaps parcel 12. See the issues panel.', 'action')
673
+ expect(hints.show).not.toHaveBeenCalled()
674
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:geometryinvalid', expect.objectContaining({ reason: 'overlap', source: 'callback' }))
675
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:geometryinvalid', expect.objectContaining({ announcement: expect.anything() }))
676
+ })
677
+
678
+ test('an announcement is made for a passing result, and for the edit-start check', () => {
679
+ const { draw, announce } = setup()
680
+ draw._commitValidator = () => ({ valid: true, announcement: 'Area 2 hectares' })
681
+ commit(draw, square, 'change')
682
+ expect(announce).toHaveBeenLastCalledWith('Area 2 hectares', 'action')
683
+
684
+ draw._commitValidator = () => ({ valid: false, announcement: 'Already overlaps parcel 12' })
685
+ draw.getMode.mockReturnValue('edit_vertex')
686
+ commit(draw, otherSquare, 'edit-start')
687
+ expect(announce).toHaveBeenLastCalledWith('Already overlaps parcel 12', 'action')
688
+ })
689
+
690
+ test('a placement veto with an announcement is announced, without a hint', () => {
691
+ const { draw, announce, hints, eventBus } = setup()
692
+ drawHandler(draw, 'placementblocked')({ feature: square, reason: 'outside', source: 'callback', announcement: 'Outside the holding', phase: 'place', mode: 'draw_polygon', vertexIndex: 0 })
693
+ expect(announce).toHaveBeenCalledWith('Outside the holding', 'action')
694
+ expect(hints.show).not.toHaveBeenCalled()
695
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:geometryinvalid', expect.objectContaining({ reason: 'outside', source: 'callback' }))
696
+ })
697
+ })
698
+
699
+ test('draw:geometryinvalid says whether a built-in rule or onGeometryChange failed', () => {
700
+ const { draw, eventBus } = setup()
701
+ draw._commitValidator = () => ({ valid: false, reason: 'overlap' })
702
+ commit(draw, square, 'change')
703
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:geometryinvalid', expect.objectContaining({ reason: 'overlap', source: 'callback' }))
704
+ draw.getMode.mockReturnValue('edit_vertex')
705
+ const bowtie = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[0, 0], [1, 1], [1, 0], [0, 1], [0, 0]]] } }
706
+ commit(draw, bowtie, 'change')
707
+ expect(eventBus.emit).toHaveBeenLastCalledWith('draw:geometryinvalid', expect.objectContaining({ source: 'rules' }))
708
+ })
709
+
710
+ test('reopening emits draw:reopened with the check\u2019s status and reason', () => {
711
+ const { draw, eventBus } = setup()
712
+ draw._commitValidator = () => ({ valid: false, reason: 'inside another parcel' })
713
+ draw.get.mockReturnValue({ id: 'new', ...square })
714
+ drawHandler(draw, 'create')({ id: 'new', ...square })
715
+ jest.runAllTimers()
716
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:reopened', { feature: { id: 'new', ...square }, status: 'invalid', reason: 'inside another parcel' })
717
+ })
718
+
719
+ test('cancelling a reopened shape discards it rather than keeping it', () => {
720
+ const { draw, eventBus, buttonConfig } = setup({ pluginState: { feature: { id: 'new' }, tempFeature: { id: 'new' } } })
721
+ draw._commitValidator = () => false
722
+ drawHandler(draw, 'create')({ id: 'new', ...square })
723
+ jest.runAllTimers()
724
+ draw.getMode.mockReturnValue('edit_vertex')
725
+
726
+ buttonConfig.drawCancel.onClick()
727
+ expect(draw.add).not.toHaveBeenCalled()
728
+ expect(draw.cancel).toHaveBeenCalled()
729
+ expect(draw.delete).toHaveBeenCalledWith('new')
730
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:cancelled', { id: 'new' })
731
+ })
732
+
733
+ describe('pressing Done while editing with a check pending', () => {
734
+ const editing = () => {
735
+ const context = setup()
736
+ context.draw.getMode.mockReturnValue('edit_vertex')
737
+ return context
738
+ }
739
+
740
+ test('waits for the check, then finishes the edit if it passes', async () => {
741
+ const { draw, dispatch, buttonConfig } = editing()
742
+ const reply = deferred()
743
+ draw._commitValidator = () => reply.promise
744
+ commit(draw, square, 'change')
745
+
746
+ buttonConfig.drawDone.onClick()
747
+ expect(draw.done).not.toHaveBeenCalled()
748
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_HOLDING', payload: true })
749
+
750
+ // Still waiting once the check is slow, when Done reads "Checking…"
751
+ slow()
752
+ expect(dispatch).toHaveBeenLastCalledWith({ type: 'SET_CHECK_SLOW' })
753
+ expect(draw.done).not.toHaveBeenCalled()
754
+
755
+ reply.resolve(true)
756
+ await flush()
757
+ expect(draw.done).toHaveBeenCalledTimes(1)
758
+ })
759
+
760
+ test('stays in edit mode if the check fails', async () => {
761
+ const { draw, dispatch, buttonConfig, hints } = editing()
762
+ const reply = deferred()
763
+ draw._commitValidator = () => reply.promise
764
+ commit(draw, square, 'change')
765
+ buttonConfig.drawDone.onClick()
766
+
767
+ reply.resolve({ valid: false, reason: 'overlap' })
768
+ await flush()
769
+ expect(draw.done).not.toHaveBeenCalled()
770
+ expect(dispatch).toHaveBeenLastCalledWith({ type: 'SET_HOLDING', payload: false })
771
+ expect(hints.show).toHaveBeenCalledWith('overlap')
772
+ })
773
+
774
+ test('stops waiting if the shape changes again, even when the new check passes', () => {
775
+ const { draw, dispatch, buttonConfig } = editing()
776
+ draw._commitValidator = () => new Promise(() => {})
777
+ commit(draw, square, 'change')
778
+ buttonConfig.drawDone.onClick()
779
+
780
+ draw._commitValidator = () => true
781
+ commit(draw, otherSquare, 'change')
782
+ expect(draw.done).not.toHaveBeenCalled()
783
+ expect(dispatch).toHaveBeenLastCalledWith({ type: 'SET_HOLDING', payload: false })
784
+ })
785
+
786
+ test('finishes straight away when the check has already passed', () => {
787
+ const { draw, buttonConfig } = editing()
788
+ draw._commitValidator = () => true
789
+ commit(draw, square, 'change')
790
+ buttonConfig.drawDone.onClick()
791
+ expect(draw.done).toHaveBeenCalledTimes(1)
792
+ })
793
+
794
+ test('ending the session while waiting drops the wait, so a later check doesn\u2019t act on it', () => {
795
+ const { draw, dispatch, buttonConfig } = editing()
796
+ draw._commitValidator = () => new Promise(() => {})
797
+ commit(draw, square, 'change')
798
+ buttonConfig.drawDone.onClick()
799
+ buttonConfig.drawCancel.onClick()
800
+
801
+ dispatch.mockClear()
802
+ draw._commitValidator = () => true
803
+ commit(draw, otherSquare, 'change')
804
+ expect(dispatch).not.toHaveBeenCalledWith({ type: 'SET_HOLDING', payload: false })
805
+ expect(draw.done).not.toHaveBeenCalled()
806
+ })
807
+
808
+ test('cancelling while waiting keeps the edited feature', () => {
809
+ const { draw, buttonConfig } = editing()
810
+ draw._commitValidator = () => new Promise(() => {})
811
+ commit(draw, square, 'change')
812
+ buttonConfig.drawDone.onClick()
813
+
814
+ buttonConfig.drawCancel.onClick()
815
+ expect(draw.delete).not.toHaveBeenCalled()
816
+ expect(draw.add).toHaveBeenCalledWith({ id: 'F' })
817
+ })
818
+ })
819
+
820
+ describe('pressing Done while editing when the last result asked for recheckOnFinish', () => {
821
+ const editing = (finishCheck) => {
822
+ const context = setup()
823
+ context.draw.getMode.mockReturnValue('edit_vertex')
824
+ context.draw._commitValidator = jest.fn(({ phase }) =>
825
+ phase === 'edit-finish' ? finishCheck() : { valid: true, recheckOnFinish: true })
826
+ commit(context.draw, square, 'change')
827
+ return context
828
+ }
829
+
830
+ test('checks the shape again with phase edit-finish, then finishes the edit if it passes', async () => {
831
+ const reply = deferred()
832
+ const { draw, dispatch, buttonConfig } = editing(() => reply.promise)
833
+
834
+ buttonConfig.drawDone.onClick()
835
+ expect(draw._commitValidator).toHaveBeenLastCalledWith(
836
+ expect.objectContaining({ phase: 'edit-finish', mode: 'edit_vertex', feature: square }), expect.anything())
837
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_HOLDING', payload: true })
838
+ expect(draw.done).not.toHaveBeenCalled()
839
+
840
+ reply.resolve({ valid: true, recheckOnFinish: true }) // not asked again on the finishing check
841
+ await flush()
842
+ expect(draw._commitValidator).toHaveBeenCalledTimes(2)
843
+ expect(draw.done).toHaveBeenCalledTimes(1)
844
+ })
845
+
846
+ test('finishes straight away when the finishing check answers synchronously', () => {
847
+ const { draw, buttonConfig } = editing(() => true)
848
+ buttonConfig.drawDone.onClick()
849
+ expect(draw.done).toHaveBeenCalledTimes(1)
850
+ })
851
+
852
+ test('stays in edit mode if the finishing check fails', () => {
853
+ const { draw, dispatch, buttonConfig, hints } = editing(() => ({ valid: false, reason: 'overlap' }))
854
+ buttonConfig.drawDone.onClick()
855
+ expect(draw.done).not.toHaveBeenCalled()
856
+ expect(dispatch).toHaveBeenLastCalledWith({ type: 'SET_HOLDING', payload: false })
857
+ expect(hints.show).toHaveBeenCalledWith('overlap')
858
+ })
859
+
860
+ test('a pending change that asks for it is checked again once it passes', async () => {
861
+ const { draw, buttonConfig } = setup()
862
+ draw.getMode.mockReturnValue('edit_vertex')
863
+ const reply = deferred()
864
+ draw._commitValidator = jest.fn(({ phase }) => (phase === 'edit-finish' ? true : reply.promise))
865
+ commit(draw, square, 'change')
866
+ buttonConfig.drawDone.onClick()
867
+
868
+ reply.resolve({ valid: true, recheckOnFinish: true })
869
+ await flush()
870
+ expect(draw._commitValidator).toHaveBeenLastCalledWith(expect.objectContaining({ phase: 'edit-finish' }), expect.anything())
871
+ expect(draw.done).toHaveBeenCalledTimes(1)
872
+ })
873
+ })
874
+
875
+ describe('finishing while the check is pending', () => {
876
+ test('a pending check doesn\u2019t block the finishing gestures, but a failed one does', async () => {
877
+ const { draw } = setup()
878
+ const reply = deferred()
879
+ draw._commitValidator = () => reply.promise
880
+ commit(draw, square, 'change')
881
+ expect(draw.setCanFinish).toHaveBeenLastCalledWith(true)
882
+ reply.resolve(false)
883
+ await flush()
884
+ expect(draw.setCanFinish).toHaveBeenLastCalledWith(false)
885
+ })
886
+
887
+ test('a held shape is marked as holding, so Done waits', () => {
888
+ const { draw, dispatch } = setup()
889
+ draw._commitValidator = () => new Promise(() => {})
890
+ commit(draw, square, 'change')
891
+ drawHandler(draw, 'create')({ id: 'new', ...square })
892
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_HOLDING', payload: true })
893
+ })
894
+
895
+ test('holds the shape, then creates it once the check passes', async () => {
896
+ const { draw, dispatch, announce, eventBus } = setup()
897
+ const reply = deferred()
898
+ draw._commitValidator = jest.fn(() => reply.promise)
899
+ draw.get.mockReturnValue({ id: 'new', ...square })
900
+ commit(draw, square, 'change')
901
+ drawHandler(draw, 'create')({ id: 'new', ...square })
902
+ expect(draw._commitValidator).toHaveBeenCalledTimes(1) // the pending check is reused
903
+
904
+ jest.runAllTimers()
905
+ expect(draw.changeMode).toHaveBeenCalledWith('disabled')
906
+ expect(draw.changeMode).not.toHaveBeenCalledWith('edit_vertex', expect.anything())
907
+ expect(lastStatus(dispatch)).toBe('pending')
908
+
909
+ reply.resolve(true)
910
+ await flush()
911
+ expect(announce).toHaveBeenLastCalledWith('Shape check passed', 'action')
912
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_MODE', payload: null })
913
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', { id: 'new', ...square })
914
+ })
915
+
916
+ test('reopens a held shape in edit mode when its check fails', async () => {
917
+ const { draw, eventBus, hints } = setup()
918
+ const reply = deferred()
919
+ draw._commitValidator = () => reply.promise
920
+ draw.get.mockReturnValue({ id: 'new', ...square })
921
+ commit(draw, square, 'change')
922
+ drawHandler(draw, 'create')({ id: 'new', ...square })
923
+ jest.runAllTimers()
924
+
925
+ reply.resolve({ valid: false, reason: 'inside another parcel' })
926
+ await flush()
927
+ expect(hints.show).toHaveBeenCalledWith('inside another parcel')
928
+ jest.runAllTimers()
929
+ expect(draw.changeMode).toHaveBeenCalledWith('edit_vertex', expect.objectContaining({ featureId: 'new' }))
930
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:reopened', expect.objectContaining({ status: 'invalid', reason: 'inside another parcel' }))
931
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:created', expect.anything())
932
+ })
933
+
934
+ test('cancelling a held shape discards it and drops its check', async () => {
935
+ const { draw, eventBus, buttonConfig } = setup()
936
+ const reply = deferred()
937
+ draw._commitValidator = () => reply.promise
938
+ commit(draw, square, 'change')
939
+ drawHandler(draw, 'create')({ id: 'new', ...square })
940
+ jest.runAllTimers()
941
+ draw.getMode.mockReturnValue('disabled')
942
+
943
+ buttonConfig.drawCancel.onClick()
944
+ expect(draw.delete).toHaveBeenCalledWith('new')
945
+ reply.resolve(true)
946
+ await flush()
947
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:created', expect.anything())
948
+ })
949
+ })
950
+
951
+ describe('recheckOnFinish', () => {
952
+ const drawing = () => {
953
+ const context = setup()
954
+ context.draw.get.mockReturnValue({ id: 'new', ...square })
955
+ return context
956
+ }
957
+ // Skips the real check while drawing, as a consumer would
958
+ const consumer = (check) => jest.fn((event) => (event.phase === 'create' ? check(event) : { valid: true, recheckOnFinish: true }))
959
+
960
+ test('a finished shape is checked again with phase create', () => {
961
+ const { draw, eventBus } = drawing()
962
+ draw._commitValidator = consumer(() => true)
963
+ commit(draw, square, 'change')
964
+ drawHandler(draw, 'create')({ id: 'new', ...square })
965
+ expect(draw._commitValidator).toHaveBeenCalledTimes(2)
966
+ expect(draw._commitValidator).toHaveBeenLastCalledWith(expect.objectContaining({ phase: 'create' }), expect.anything())
967
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'new' }))
968
+ })
969
+
970
+ test('a single async check on create holds the shape, then creates it', async () => {
971
+ const { draw, eventBus } = drawing()
972
+ const reply = deferred()
973
+ draw._commitValidator = consumer(() => reply.promise)
974
+ commit(draw, square, 'change')
975
+ drawHandler(draw, 'create')({ id: 'new', ...square })
976
+ jest.runAllTimers()
977
+ expect(draw.changeMode).not.toHaveBeenCalledWith('edit_vertex', expect.anything())
978
+
979
+ reply.resolve(true)
980
+ await flush()
981
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'new' }))
982
+ })
983
+
984
+ test('a pending result that turns out to want a recheck is checked again once it settles', async () => {
985
+ const { draw, eventBus } = drawing()
986
+ const reply = deferred()
987
+ draw._commitValidator = jest.fn()
988
+ .mockReturnValueOnce(reply.promise) // the last change while drawing, still pending at finish
989
+ .mockReturnValueOnce({ valid: false, reason: 'overlap' }) // the create check
990
+ commit(draw, square, 'change')
991
+ drawHandler(draw, 'create')({ id: 'new', ...square })
992
+ jest.runAllTimers()
993
+
994
+ reply.resolve({ valid: true, recheckOnFinish: true })
995
+ await flush()
996
+ expect(draw._commitValidator).toHaveBeenLastCalledWith(expect.objectContaining({ phase: 'create' }), expect.anything())
997
+ jest.runAllTimers()
998
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:reopened', expect.objectContaining({ status: 'invalid', reason: 'overlap' }))
999
+ })
1000
+
1001
+ test('is ignored on the create check itself', () => {
1002
+ const { draw, eventBus } = drawing()
1003
+ draw._commitValidator = jest.fn(() => ({ valid: true, recheckOnFinish: true }))
1004
+ drawHandler(draw, 'create')({ id: 'new', ...square })
1005
+ expect(draw._commitValidator).toHaveBeenCalledTimes(1)
1006
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'new' }))
1007
+ })
1008
+ })
1009
+
1010
+ describe('onGeometryFinish', () => {
1011
+ const drawing = () => {
1012
+ const context = setup()
1013
+ context.draw.get.mockReturnValue({ id: 'new', ...square })
1014
+ context.draw._commitValidator = jest.fn(() => undefined)
1015
+ return context
1016
+ }
1017
+
1018
+ test('checks a finished new shape with phase create, instead of reusing onGeometryChange', () => {
1019
+ const { draw, eventBus } = drawing()
1020
+ draw._finishValidator = jest.fn(() => true)
1021
+ commit(draw, square, 'change')
1022
+ drawHandler(draw, 'create')({ id: 'new', ...square })
1023
+ expect(draw._commitValidator).toHaveBeenCalledTimes(1)
1024
+ expect(draw._finishValidator).toHaveBeenCalledWith(expect.objectContaining({ phase: 'create', feature: { id: 'new', ...square } }), expect.anything())
1025
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'new' }))
1026
+ })
1027
+
1028
+ test('holds the shape while it runs, then reopens it if it fails', async () => {
1029
+ const { draw, eventBus } = drawing()
1030
+ const reply = deferred()
1031
+ draw._finishValidator = () => reply.promise
1032
+ commit(draw, square, 'change')
1033
+ drawHandler(draw, 'create')({ id: 'new', ...square })
1034
+ jest.runAllTimers()
1035
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:created', expect.anything())
1036
+
1037
+ reply.resolve({ valid: false, reason: 'overlap' })
1038
+ await flush()
1039
+ jest.runAllTimers()
1040
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:reopened', expect.objectContaining({ status: 'invalid', reason: 'overlap' }))
1041
+ })
1042
+
1043
+ test('runs on Done while editing with phase edit-finish, then finishes the edit if it passes', async () => {
1044
+ const { draw, buttonConfig } = drawing()
1045
+ draw.getMode.mockReturnValue('edit_vertex')
1046
+ const reply = deferred()
1047
+ draw._finishValidator = jest.fn(() => reply.promise)
1048
+ commit(draw, square, 'change')
1049
+
1050
+ buttonConfig.drawDone.onClick()
1051
+ expect(draw._finishValidator).toHaveBeenCalledWith(expect.objectContaining({ phase: 'edit-finish', mode: 'edit_vertex' }), expect.anything())
1052
+ expect(draw.done).not.toHaveBeenCalled()
1053
+
1054
+ reply.resolve(true)
1055
+ await flush()
1056
+ expect(draw._finishValidator).toHaveBeenCalledTimes(1)
1057
+ expect(draw.done).toHaveBeenCalledTimes(1)
1058
+ })
1059
+
1060
+ test('keeps an edit open if it fails', () => {
1061
+ const { draw, buttonConfig } = drawing()
1062
+ draw.getMode.mockReturnValue('edit_vertex')
1063
+ draw._finishValidator = () => false
1064
+ commit(draw, square, 'change')
1065
+ buttonConfig.drawDone.onClick()
1066
+ expect(draw.done).not.toHaveBeenCalled()
1067
+ })
1068
+
1069
+ test('doesn’t wait for a pending onGeometryChange before running', () => {
1070
+ const { draw, buttonConfig } = drawing()
1071
+ draw.getMode.mockReturnValue('edit_vertex')
1072
+ draw._commitValidator = () => new Promise(() => {})
1073
+ draw._finishValidator = jest.fn(() => true)
1074
+ commit(draw, square, 'change')
1075
+ buttonConfig.drawDone.onClick()
1076
+ expect(draw._finishValidator).toHaveBeenCalledTimes(1)
1077
+ expect(draw.done).toHaveBeenCalledTimes(1)
1078
+ })
1079
+ })
1080
+
1081
+ test('survives the handlers being rebuilt mid-check', async () => {
1082
+ const first = setup()
1083
+ const { draw, eventBus } = first
1084
+ const reply = deferred()
1085
+ draw._commitValidator = () => reply.promise
1086
+ commit(draw, square, 'change')
1087
+ drawHandler(draw, 'create')({ id: 'new', ...square })
1088
+ jest.runAllTimers()
1089
+
1090
+ // DrawInit re-attaches on every plugin state change
1091
+ first.detach()
1092
+ draw.on.mockClear()
1093
+ const dispatch = jest.fn()
1094
+ attachEvents({
1095
+ appState: { layoutRefs: { viewportRef: { current: 'viewport' } } },
1096
+ appConfig: { id: 'app' },
1097
+ mapState: {},
1098
+ pluginState: { dispatch },
1099
+ mapProvider: { draw },
1100
+ buttonConfig: { drawDone: {}, drawCancel: {}, drawUndo: {} },
1101
+ eventBus,
1102
+ hints: { show: jest.fn() }
1103
+ })
1104
+
1105
+ // The held shape is created by the new handlers once its check passes
1106
+ reply.resolve(true)
1107
+ await flush()
1108
+ expect(lastStatus(dispatch)).toBe('valid')
1109
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'F' }))
365
1110
  })
366
1111
  })
367
1112