@defra/interactive-map 0.0.58-alpha → 0.0.59-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 (58) hide show
  1. package/dist/esm/im-core.js +1 -1
  2. package/dist/umd/im-core.js +1 -1
  3. package/docs/plugins/draw.md +95 -13
  4. package/package.json +1 -1
  5. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  6. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  7. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  8. package/plugins/draw/dist/esm/index.js +1 -1
  9. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  10. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  11. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  12. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  13. package/plugins/draw/dist/umd/index.js +1 -1
  14. package/plugins/draw/src/adapterEvents.js +2 -0
  15. package/plugins/draw/src/adapters/adapterContract.test.js +1 -0
  16. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +15 -3
  17. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -0
  18. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.js +8 -19
  19. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.test.js +20 -0
  20. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.js +6 -17
  21. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.test.js +19 -0
  22. package/plugins/draw/src/adapters/maplibre/utils/editModeEvents.js +29 -1
  23. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +13 -3
  24. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +28 -0
  25. package/plugins/draw/src/api/addFeature.js +9 -6
  26. package/plugins/draw/src/api/addFeature.test.js +37 -10
  27. package/plugins/draw/src/api/editFeature.js +1 -1
  28. package/plugins/draw/src/api/recheckGeometry.js +1 -1
  29. package/plugins/draw/src/api/recheckGeometry.test.js +2 -2
  30. package/plugins/draw/src/api/setProperties.js +39 -0
  31. package/plugins/draw/src/api/setProperties.test.js +74 -0
  32. package/plugins/draw/src/api/setStyle.js +16 -19
  33. package/plugins/draw/src/api/setStyle.test.js +17 -2
  34. package/plugins/draw/src/api/split.js +7 -4
  35. package/plugins/draw/src/api/split.test.js +25 -2
  36. package/plugins/draw/src/events.js +95 -53
  37. package/plugins/draw/src/events.test.js +425 -6
  38. package/plugins/draw/src/manifest.js +6 -4
  39. package/plugins/draw/src/manifest.test.js +20 -26
  40. package/plugins/draw/src/reducer.js +7 -0
  41. package/plugins/draw/src/reducer.test.js +11 -0
  42. package/plugins/draw/src/utils/flattenStyleProperties.js +54 -2
  43. package/plugins/draw/src/utils/flattenStyleProperties.test.js +74 -1
  44. package/plugins/draw/src/utils/isPlainObject.js +14 -0
  45. package/plugins/draw/src/utils/isPlainObject.test.js +24 -0
  46. package/plugins/draw/src/utils/mergeFeatureProperties.js +45 -0
  47. package/plugins/draw/src/utils/mergeFeatureProperties.test.js +48 -0
  48. package/plugins/draw/src/utils/stripInternalProperties.js +3 -2
  49. package/plugins/draw/src/validation/commitChecks.js +29 -11
  50. package/plugins/draw/src/validation/commitChecks.test.js +67 -11
  51. package/plugins/draw/src/validation/validateGeometry.js +12 -5
  52. package/plugins/draw/src/validation/validateGeometry.test.js +18 -0
  53. package/src/App/store/MapProvider.jsx +1 -0
  54. package/src/App/store/MapProvider.test.jsx +25 -0
  55. package/src/App/store/mapActionsMap.js +6 -0
  56. package/src/App/store/mapActionsMap.test.js +7 -0
  57. package/src/App/store/mapReducer.js +2 -0
  58. package/src/App/store/mapReducer.test.js +2 -0
@@ -1,5 +1,6 @@
1
1
  import { attachEvents } from './events.js'
2
- import { SLOW_CHECK_MS } from './validation/commitChecks.js'
2
+ import { SLOW_CHECK_MS, getCommitChecks } from './validation/commitChecks.js'
3
+ import { recheckGeometry } from './api/recheckGeometry.js'
3
4
 
4
5
  jest.useFakeTimers()
5
6
 
@@ -387,7 +388,7 @@ describe('async geometry checks', () => {
387
388
  const flush = () => Promise.resolve()
388
389
  const commit = (draw, feature, phase = 'change') => drawHandler(draw, 'geometrychange')({ feature, phase, vertexIndex: 0 })
389
390
  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
+ const lastStatus = (dispatch) => dispatch.mock.calls.filter(([action]) => action.type === 'SET_VALIDATION_STATUS').map(([action]) => action.payload).pop()
391
392
 
392
393
  test('marks a check as pending, then applies its result', async () => {
393
394
  const { draw, dispatch } = setup()
@@ -662,7 +663,7 @@ describe('async geometry checks', () => {
662
663
  draw._commitValidator = () => new Promise(() => {})
663
664
  commit(draw, square, 'change')
664
665
  slow()
665
- expect(dispatch).toHaveBeenLastCalledWith({ type: 'SET_CHECK_SLOW' })
666
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_CHECK_SLOW' })
666
667
  })
667
668
 
668
669
  test('a result\u2019s own announcement replaces draw\u2019s, and its hint', () => {
@@ -749,7 +750,7 @@ describe('async geometry checks', () => {
749
750
 
750
751
  // Still waiting once the check is slow, when Done reads "Checking…"
751
752
  slow()
752
- expect(dispatch).toHaveBeenLastCalledWith({ type: 'SET_CHECK_SLOW' })
753
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_CHECK_SLOW' })
753
754
  expect(draw.done).not.toHaveBeenCalled()
754
755
 
755
756
  reply.resolve(true)
@@ -1066,16 +1067,143 @@ describe('async geometry checks', () => {
1066
1067
  expect(draw.done).not.toHaveBeenCalled()
1067
1068
  })
1068
1069
 
1069
- test('doesn’t wait for a pending onGeometryChange before running', () => {
1070
+ test('doesn’t wait for a pending onGeometryChange before running, and cancels it', () => {
1070
1071
  const { draw, buttonConfig } = drawing()
1071
1072
  draw.getMode.mockReturnValue('edit_vertex')
1072
- draw._commitValidator = () => new Promise(() => {})
1073
+ let changeSignal
1074
+ draw._commitValidator = (_event, { signal }) => { changeSignal = signal; return new Promise(() => {}) }
1073
1075
  draw._finishValidator = jest.fn(() => true)
1074
1076
  commit(draw, square, 'change')
1075
1077
  buttonConfig.drawDone.onClick()
1078
+ expect(changeSignal.aborted).toBe(true)
1076
1079
  expect(draw._finishValidator).toHaveBeenCalledTimes(1)
1077
1080
  expect(draw.done).toHaveBeenCalledTimes(1)
1078
1081
  })
1082
+
1083
+ test('cancels a pending onGeometryChange when a new shape is finished', () => {
1084
+ const { draw, eventBus } = drawing()
1085
+ let changeSignal
1086
+ draw._commitValidator = (_event, { signal }) => { changeSignal = signal; return new Promise(() => {}) }
1087
+ draw._finishValidator = jest.fn(() => true)
1088
+ commit(draw, square, 'change')
1089
+ drawHandler(draw, 'create')({ id: 'new', ...square })
1090
+ expect(changeSignal.aborted).toBe(true)
1091
+ expect(draw._finishValidator).toHaveBeenCalledWith(expect.objectContaining({ phase: 'create' }), expect.anything())
1092
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'new' }))
1093
+ })
1094
+ })
1095
+
1096
+ describe('Done while the next check runs', () => {
1097
+ const canFinish = (dispatch) => dispatch.mock.calls.filter(([action]) => action.type === 'SET_CAN_FINISH').map(([action]) => action.payload).pop()
1098
+ const partDrawn = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[0, 0], [1, 0], [0, 0]]] } }
1099
+
1100
+ test('stays disabled while an existing feature’s edit-start check runs, then follows its result', async () => {
1101
+ const passing = setup()
1102
+ passing.draw.getMode.mockReturnValue('edit_vertex')
1103
+ const passes = deferred()
1104
+ getCommitChecks(passing.draw).run({ feature: square, context: { phase: 'edit-start', mode: 'edit_vertex' }, consumerCheck: () => passes.promise })
1105
+ expect(canFinish(passing.dispatch)).toBe(false)
1106
+ expect(passing.draw.setCanFinish).toHaveBeenLastCalledWith(false)
1107
+ passes.resolve(true)
1108
+ await flush()
1109
+ expect(canFinish(passing.dispatch)).toBe(true)
1110
+
1111
+ const failing = setup()
1112
+ failing.draw.getMode.mockReturnValue('edit_vertex')
1113
+ const fails = deferred()
1114
+ getCommitChecks(failing.draw).run({ feature: square, context: { phase: 'edit-start', mode: 'edit_vertex' }, consumerCheck: () => fails.promise })
1115
+ fails.resolve(false)
1116
+ await flush()
1117
+ // Never enabled on the way to disabled
1118
+ expect(failing.dispatch.mock.calls.filter(([action]) => action.type === 'SET_CAN_FINISH').map(([action]) => action.payload)).toEqual([false, false])
1119
+ })
1120
+
1121
+ test('stays disabled after a failed result, and enables only once the next check passes', async () => {
1122
+ const { draw, dispatch } = setup()
1123
+ draw.getMode.mockReturnValue('edit_vertex')
1124
+ draw._commitValidator = () => false
1125
+ commit(draw, square)
1126
+ const reply = deferred()
1127
+ draw._commitValidator = () => reply.promise
1128
+ commit(draw, otherSquare)
1129
+ expect(lastStatus(dispatch)).toBe('pending')
1130
+ expect(canFinish(dispatch)).toBe(false)
1131
+ expect(draw.setCanFinish).toHaveBeenLastCalledWith(false)
1132
+
1133
+ reply.resolve(true)
1134
+ await flush()
1135
+ expect(canFinish(dispatch)).toBe(true)
1136
+ expect(draw.setCanFinish).toHaveBeenLastCalledWith(true)
1137
+ })
1138
+
1139
+ test('stays disabled after an error', () => {
1140
+ const { draw, dispatch } = setup()
1141
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
1142
+ draw._commitValidator = () => { throw new Error('503') }
1143
+ commit(draw, square)
1144
+ draw._commitValidator = () => new Promise(() => {})
1145
+ commit(draw, otherSquare)
1146
+ expect(canFinish(dispatch)).toBe(false)
1147
+ console.warn.mockRestore()
1148
+ })
1149
+
1150
+ test('stays enabled after a passing result', () => {
1151
+ const { draw, dispatch } = setup()
1152
+ draw._commitValidator = () => true
1153
+ commit(draw, square)
1154
+ draw._commitValidator = () => new Promise(() => {})
1155
+ commit(draw, otherSquare)
1156
+ expect(canFinish(dispatch)).toBe(true)
1157
+ })
1158
+
1159
+ test('isn\u2019t held back by a built-in rule failure, such as too few points', () => {
1160
+ const { draw, dispatch } = setup()
1161
+ draw._commitValidator = () => new Promise(() => {})
1162
+ commit(draw, partDrawn)
1163
+ expect(canFinish(dispatch)).toBe(false)
1164
+ commit(draw, square)
1165
+ expect(canFinish(dispatch)).toBe(true)
1166
+ })
1167
+
1168
+ test('a check cancelled by a newer change doesn\u2019t change it', () => {
1169
+ const { draw, dispatch } = setup()
1170
+ draw._commitValidator = () => false
1171
+ commit(draw, square)
1172
+ draw._commitValidator = () => new Promise(() => {})
1173
+ commit(draw, otherSquare)
1174
+ commit(draw, square)
1175
+ expect(canFinish(dispatch)).toBe(false)
1176
+ })
1177
+
1178
+ test('a new shape that onGeometryFinish failed keeps that result when it reopens', () => {
1179
+ const { draw, dispatch } = setup()
1180
+ draw.get.mockReturnValue({ id: 'new', ...square })
1181
+ draw._commitValidator = () => true
1182
+ draw._finishValidator = () => ({ valid: false, reason: 'overlap' })
1183
+ commit(draw, square)
1184
+ drawHandler(draw, 'create')({ id: 'new', ...square })
1185
+ jest.runAllTimers()
1186
+ expect(canFinish(dispatch)).toBe(false)
1187
+
1188
+ draw.getMode.mockReturnValue('edit_vertex')
1189
+ draw._commitValidator = () => new Promise(() => {})
1190
+ commit(draw, otherSquare)
1191
+ expect(canFinish(dispatch)).toBe(false)
1192
+ })
1193
+
1194
+ test('an edit that onGeometryFinish failed keeps that result', () => {
1195
+ const { draw, dispatch, buttonConfig } = setup()
1196
+ draw.getMode.mockReturnValue('edit_vertex')
1197
+ draw._commitValidator = () => true
1198
+ draw._finishValidator = () => false
1199
+ commit(draw, square)
1200
+ buttonConfig.drawDone.onClick()
1201
+ expect(draw.done).not.toHaveBeenCalled()
1202
+
1203
+ draw._commitValidator = () => new Promise(() => {})
1204
+ commit(draw, otherSquare)
1205
+ expect(canFinish(dispatch)).toBe(false)
1206
+ })
1079
1207
  })
1080
1208
 
1081
1209
  test('survives the handlers being rebuilt mid-check', async () => {
@@ -1108,6 +1236,297 @@ describe('async geometry checks', () => {
1108
1236
  expect(lastStatus(dispatch)).toBe('valid')
1109
1237
  expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'F' }))
1110
1238
  })
1239
+
1240
+ describe('properties from onGeometryFinish', () => {
1241
+ // A store for the one shape, so a properties write shows up in draw.get
1242
+ const drawing = () => {
1243
+ const context = setup()
1244
+ const { draw } = context
1245
+ let stored = { id: 'new', ...square, properties: { landUse: 'arable' } }
1246
+ draw.get.mockImplementation(() => stored)
1247
+ draw.setProperties = jest.fn((id, properties) => { stored = { ...stored, properties: { ...stored.properties, ...properties } } })
1248
+ draw._commitValidator = jest.fn(() => undefined)
1249
+ return context
1250
+ }
1251
+ const finishedWith = (properties) => ({ id: 'new', ...square, properties })
1252
+ const drawrules = { drawRules: { checked: false, reason: 'error' } }
1253
+
1254
+ test('are merged into a new shape before draw:created', () => {
1255
+ const { draw, eventBus } = drawing()
1256
+ draw._finishValidator = () => ({ valid: true, properties: drawrules })
1257
+ commit(draw, square, 'change')
1258
+ drawHandler(draw, 'create')(draw.get())
1259
+ expect(draw.setProperties).toHaveBeenCalledWith('new', drawrules)
1260
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', finishedWith({ landUse: 'arable', ...drawrules }))
1261
+ })
1262
+
1263
+ test('are merged into a held shape once its finishing check passes', async () => {
1264
+ const { draw, eventBus } = drawing()
1265
+ const reply = deferred()
1266
+ draw._finishValidator = () => reply.promise
1267
+ commit(draw, square, 'change')
1268
+ drawHandler(draw, 'create')(draw.get())
1269
+ jest.runAllTimers()
1270
+ reply.resolve({ valid: true, properties: drawrules })
1271
+ await flush()
1272
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', finishedWith({ landUse: 'arable', ...drawrules }))
1273
+ })
1274
+
1275
+ test('are merged into an edited shape before draw:edited, replacing earlier values', () => {
1276
+ const { draw, eventBus, buttonConfig } = drawing()
1277
+ draw.getMode.mockReturnValue('edit_vertex')
1278
+ draw._finishValidator = () => ({ valid: true, properties: { drawRules: { checked: true } } })
1279
+ draw.setProperties('new', drawrules)
1280
+ commit(draw, square, 'change')
1281
+ buttonConfig.drawDone.onClick()
1282
+ expect(draw.done).toHaveBeenCalled()
1283
+ drawHandler(draw, 'editfinish')(draw.get()) // the adapter's EDIT_FINISH, fired by done()
1284
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:edited', finishedWith({ landUse: 'arable', drawRules: { checked: true } }))
1285
+ })
1286
+
1287
+ test('are merged into a reopened shape that then passes, reported as created', () => {
1288
+ const { draw, eventBus, buttonConfig } = drawing()
1289
+ draw._finishValidator = ({ phase }) => (phase === 'create' ? false : { valid: true, properties: drawrules })
1290
+ commit(draw, square, 'change')
1291
+ drawHandler(draw, 'create')(draw.get())
1292
+ jest.runAllTimers()
1293
+ draw.getMode.mockReturnValue('edit_vertex')
1294
+ commit(draw, square, 'change') // Done stays disabled until the reopened shape is changed
1295
+ buttonConfig.drawDone.onClick()
1296
+ drawHandler(draw, 'editfinish')(draw.get())
1297
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', finishedWith({ landUse: 'arable', ...drawrules }))
1298
+ })
1299
+
1300
+ test('are ignored on a failing result', () => {
1301
+ const { draw } = drawing()
1302
+ draw._finishValidator = () => ({ valid: false, reason: 'overlap', properties: drawrules })
1303
+ commit(draw, square, 'change')
1304
+ drawHandler(draw, 'create')(draw.get())
1305
+ expect(draw.setProperties).not.toHaveBeenCalled()
1306
+ })
1307
+
1308
+ test('are ignored from onGeometryChange, with or without an onGeometryFinish', () => {
1309
+ const { draw, eventBus } = drawing()
1310
+ draw._commitValidator = () => ({ valid: true, properties: drawrules })
1311
+ commit(draw, square, 'change')
1312
+ drawHandler(draw, 'create')(draw.get())
1313
+ expect(draw.setProperties).not.toHaveBeenCalled()
1314
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', finishedWith({ landUse: 'arable' }))
1315
+ })
1316
+
1317
+ test('are ignored when the finished shape differs from the one checked', () => {
1318
+ const { draw, buttonConfig } = drawing()
1319
+ draw.getMode.mockReturnValue('edit_vertex')
1320
+ draw._finishValidator = () => ({ valid: true, properties: drawrules })
1321
+ commit(draw, square, 'change')
1322
+ buttonConfig.drawDone.onClick()
1323
+ drawHandler(draw, 'editfinish')({ id: 'new', ...otherSquare, properties: {} })
1324
+ expect(draw.setProperties).not.toHaveBeenCalled()
1325
+ })
1326
+ })
1327
+
1328
+ describe('a shape that fails its finishing check can always be finished or cancelled', () => {
1329
+ const canFinish = (dispatch) => dispatch.mock.calls.filter(([action]) => action.type === 'SET_CAN_FINISH').map(([action]) => action.payload).pop()
1330
+ const isGeometryOf = (feature, other) => JSON.stringify(feature.geometry) === JSON.stringify(other.geometry)
1331
+
1332
+ // A new shape whose onGeometryChange passed, finished with the given onGeometryFinish
1333
+ const finishNewShape = (onGeometryFinish, overrides) => {
1334
+ const context = setup(overrides)
1335
+ const { draw } = context
1336
+ draw.get.mockReturnValue({ id: 'new', ...square })
1337
+ draw._commitValidator = jest.fn(() => true)
1338
+ draw._finishValidator = onGeometryFinish
1339
+ commit(draw, square)
1340
+ drawHandler(draw, 'create')({ id: 'new', ...square })
1341
+ jest.runAllTimers()
1342
+ draw.getMode.mockReturnValue('edit_vertex')
1343
+ return context
1344
+ }
1345
+
1346
+ test('reopens with Done disabled, which stays disabled while edits fail, then finishes once fixed', () => {
1347
+ const { draw, dispatch, eventBus, buttonConfig } = finishNewShape(({ feature }) => isGeometryOf(feature, otherSquare))
1348
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:reopened', expect.objectContaining({ status: 'invalid' }))
1349
+ expect(canFinish(dispatch)).toBe(false)
1350
+ expect(draw.setInvalid).toHaveBeenLastCalledWith(true)
1351
+
1352
+ draw._commitValidator = () => ({ valid: false, reason: 'overlap' })
1353
+ commit(draw, square)
1354
+ expect(canFinish(dispatch)).toBe(false)
1355
+
1356
+ draw._commitValidator = () => true
1357
+ commit(draw, otherSquare)
1358
+ expect(canFinish(dispatch)).toBe(true)
1359
+ buttonConfig.drawDone.onClick()
1360
+ expect(draw.done).toHaveBeenCalledTimes(1)
1361
+ drawHandler(draw, 'editfinish')({ id: 'new', ...otherSquare })
1362
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'new' }))
1363
+ })
1364
+
1365
+ test('keeps the edit open with Done disabled when only onGeometryFinish still fails it', () => {
1366
+ const { draw, dispatch, buttonConfig } = finishNewShape(() => false)
1367
+
1368
+ commit(draw, otherSquare)
1369
+ expect(canFinish(dispatch)).toBe(true)
1370
+ buttonConfig.drawDone.onClick()
1371
+ expect(draw.done).not.toHaveBeenCalled()
1372
+ expect(canFinish(dispatch)).toBe(false)
1373
+ expect(dispatch).toHaveBeenLastCalledWith({ type: 'SET_HOLDING', payload: false })
1374
+ })
1375
+
1376
+ test('reopens an errored check with Done disabled, and recheckGeometry enables it once it passes', () => {
1377
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
1378
+ const { draw, dispatch, eventBus } = finishNewShape(() => { throw new Error('503') })
1379
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:reopened', expect.objectContaining({ status: 'error' }))
1380
+ expect(canFinish(dispatch)).toBe(false)
1381
+
1382
+ expect(recheckGeometry({ mapProvider: { draw } })).toBe(true)
1383
+ expect(canFinish(dispatch)).toBe(true)
1384
+ expect(draw.setInvalid).toHaveBeenLastCalledWith(false)
1385
+ console.warn.mockRestore()
1386
+ })
1387
+
1388
+ test('Cancel discards a reopened shape whose next check never replies', () => {
1389
+ const { draw, dispatch, eventBus, buttonConfig } = finishNewShape(() => false, { pluginState: { tempFeature: { id: 'new' } } })
1390
+
1391
+ draw._commitValidator = () => new Promise(() => {})
1392
+ commit(draw, otherSquare)
1393
+ expect(canFinish(dispatch)).toBe(false)
1394
+ buttonConfig.drawCancel.onClick()
1395
+ expect(draw.delete).toHaveBeenCalledWith('new')
1396
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:cancelled', expect.anything())
1397
+ })
1398
+
1399
+ test('Cancel discards a new shape held for an onGeometryFinish that never replies', () => {
1400
+ const { draw, eventBus, buttonConfig } = setup()
1401
+ draw.get.mockReturnValue({ id: 'new', ...square })
1402
+ draw._commitValidator = () => true
1403
+ draw._finishValidator = () => new Promise(() => {})
1404
+ commit(draw, square)
1405
+ drawHandler(draw, 'create')({ id: 'new', ...square })
1406
+ jest.runAllTimers()
1407
+ draw.getMode.mockReturnValue('disabled')
1408
+
1409
+ buttonConfig.drawCancel.onClick()
1410
+ expect(draw.delete).toHaveBeenCalledWith('new')
1411
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:cancelled', expect.anything())
1412
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:created', expect.anything())
1413
+ })
1414
+ })
1415
+
1416
+ describe('a passing onGeometryFinish result that asks to reopen a new shape', () => {
1417
+ const canFinish = (dispatch) => dispatch.mock.calls.filter(([action]) => action.type === 'SET_CAN_FINISH').map(([action]) => action.payload).pop()
1418
+ const suggestions = { drawRules: { suggestions: 1 } }
1419
+
1420
+ // A store for the one shape, so a properties write shows up in draw.get
1421
+ const drawing = (overrides) => {
1422
+ const context = setup(overrides)
1423
+ const { draw } = context
1424
+ let stored = { id: 'new', ...square, properties: {} }
1425
+ draw.get.mockImplementation(() => stored)
1426
+ draw.setGeometry = (geometry) => { stored = { ...stored, geometry } }
1427
+ draw.setProperties = jest.fn((id, properties) => { stored = { ...stored, properties: { ...stored.properties, ...properties } } })
1428
+ draw._commitValidator = jest.fn(() => true)
1429
+ commit(draw, square)
1430
+ return context
1431
+ }
1432
+
1433
+ // Finishes the new shape, and lets it reopen in edit mode
1434
+ const finish = (draw) => {
1435
+ drawHandler(draw, 'create')(draw.get())
1436
+ jest.runAllTimers()
1437
+ draw.getMode.mockReturnValue('edit_vertex')
1438
+ }
1439
+
1440
+ const expectReopenedValid = ({ draw, dispatch, eventBus }) => {
1441
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:reopened', expect.objectContaining({ status: 'valid', reason: null }))
1442
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:created', expect.anything())
1443
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:geometryinvalid', expect.anything())
1444
+ expect(canFinish(dispatch)).toBe(true)
1445
+ expect(draw.setCanFinish).toHaveBeenLastCalledWith(true)
1446
+ expect(draw.setInvalid).toHaveBeenLastCalledWith(false)
1447
+ }
1448
+
1449
+ test('reopens the shape in edit mode, valid, and reads out the announcement', () => {
1450
+ const context = drawing()
1451
+ const { draw, announce } = context
1452
+ draw._finishValidator = () => ({ valid: true, reopen: true, announcement: '1 suggestion for your shape' })
1453
+ finish(draw)
1454
+ expectReopenedValid(context)
1455
+ expect(draw.changeMode).toHaveBeenCalledWith('edit_vertex', expect.objectContaining({ featureId: 'new' }))
1456
+ expect(announce).toHaveBeenCalledWith('1 suggestion for your shape', 'action')
1457
+ })
1458
+
1459
+ test('reopens a shape held while onGeometryFinish runs, with Done disabled until it replies', async () => {
1460
+ const context = drawing()
1461
+ const { draw, dispatch } = context
1462
+ const reply = deferred()
1463
+ draw._finishValidator = () => reply.promise
1464
+ drawHandler(draw, 'create')(draw.get())
1465
+ jest.runAllTimers()
1466
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_HOLDING', payload: true })
1467
+ expect(dispatch).not.toHaveBeenCalledWith({ type: 'SET_HOLDING', payload: false })
1468
+
1469
+ reply.resolve({ valid: true, reopen: true })
1470
+ await flush()
1471
+ jest.runAllTimers()
1472
+ expectReopenedValid(context)
1473
+ })
1474
+
1475
+ test('Done on the unchanged shape creates it with the result’s properties, without checking it again', () => {
1476
+ const { draw, eventBus, buttonConfig } = drawing()
1477
+ draw._finishValidator = jest.fn(() => ({ valid: true, reopen: true, properties: suggestions }))
1478
+ finish(draw)
1479
+
1480
+ buttonConfig.drawDone.onClick()
1481
+ expect(draw._finishValidator).toHaveBeenCalledTimes(1)
1482
+ expect(draw.done).toHaveBeenCalledTimes(1)
1483
+ drawHandler(draw, 'editfinish')(draw.get())
1484
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'new', properties: suggestions }))
1485
+ })
1486
+
1487
+ test('Done on a changed shape checks it again, ignoring reopen, and uses that result’s properties', () => {
1488
+ const { draw, eventBus, buttonConfig } = drawing()
1489
+ draw._finishValidator = jest.fn(({ phase }) => phase === 'create'
1490
+ ? { valid: true, reopen: true, properties: suggestions }
1491
+ : { valid: true, reopen: true, properties: { drawRules: { suggestions: 0 } } })
1492
+ finish(draw)
1493
+
1494
+ draw.setGeometry(otherSquare.geometry)
1495
+ commit(draw, otherSquare)
1496
+ buttonConfig.drawDone.onClick()
1497
+ expect(draw._finishValidator).toHaveBeenLastCalledWith(expect.objectContaining({ phase: 'edit-finish' }), expect.anything())
1498
+ expect(draw.done).toHaveBeenCalledTimes(1)
1499
+ drawHandler(draw, 'editfinish')(draw.get())
1500
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ properties: { drawRules: { suggestions: 0 } } }))
1501
+ })
1502
+
1503
+ test('a failing result reopens the shape with Done disabled, as without reopen', () => {
1504
+ const { draw, dispatch, eventBus } = drawing()
1505
+ draw._finishValidator = () => ({ valid: false, reason: 'overlap', reopen: true })
1506
+ finish(draw)
1507
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:reopened', expect.objectContaining({ status: 'invalid', reason: 'overlap' }))
1508
+ expect(canFinish(dispatch)).toBe(false)
1509
+ })
1510
+
1511
+ test('has no effect from onGeometryChange, including when it finishes the shape', () => {
1512
+ const { draw, eventBus } = drawing()
1513
+ draw._commitValidator = () => ({ valid: true, reopen: true })
1514
+ commit(draw, square)
1515
+ finish(draw)
1516
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:created', expect.objectContaining({ id: 'new' }))
1517
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:reopened', expect.anything())
1518
+ })
1519
+
1520
+ test('Cancel discards the reopened shape', () => {
1521
+ const { draw, eventBus, buttonConfig } = drawing({ pluginState: { tempFeature: { id: 'new' } } })
1522
+ draw._finishValidator = () => ({ valid: true, reopen: true })
1523
+ finish(draw)
1524
+ buttonConfig.drawCancel.onClick()
1525
+ expect(draw.delete).toHaveBeenCalledWith('new')
1526
+ expect(eventBus.emit).toHaveBeenCalledWith('draw:cancelled', expect.anything())
1527
+ expect(eventBus.emit).not.toHaveBeenCalledWith('draw:created', expect.anything())
1528
+ })
1529
+ })
1111
1530
  })
1112
1531
 
1113
1532
  describe('detach', () => {
@@ -6,6 +6,7 @@ import { newPoint } from './api/newPoint.js'
6
6
  import { editFeature } from './api/editFeature.js'
7
7
  import { addFeature } from './api/addFeature.js'
8
8
  import { setStyle } from './api/setStyle.js'
9
+ import { setProperties } from './api/setProperties.js'
9
10
  import { deleteFeature } from './api/deleteFeature.js'
10
11
  import { split } from './api/split.js'
11
12
  import { merge } from './api/merge.js'
@@ -81,11 +82,11 @@ export const manifest = {
81
82
  // Also shown while a new point waits on its check, to read "Checking…"
82
83
  hiddenWhen: ({ pluginState }) => !isCheckedMode(pluginState.mode) && !pluginState.holding,
83
84
  enableWhen: ({ pluginState }) => {
84
- const { mode, validationStatus, previewValid, holding } = pluginState
85
- // validationStatus is the latest commit-level check (built-in rules + onGeometryChange);
85
+ const { mode, canFinish, previewValid, holding } = pluginState
86
+ // canFinish follows the latest commit-level check (built-in rules + onGeometryChange);
86
87
  // previewValid is the live check of a shape being edited. Done can be pressed while a
87
- // check is pending: finishing then waits for it (holding) until it settles.
88
- const canFinish = validationStatus === 'valid' || validationStatus === 'pending'
88
+ // check is pending, unless the last result failed: finishing then waits for it
89
+ // (holding) until it settles.
89
90
  return isCheckedMode(mode) && canFinish && previewValid && !holding
90
91
  },
91
92
  ...createButtonSlots(true)
@@ -189,6 +190,7 @@ export const manifest = {
189
190
  editFeature,
190
191
  addFeature,
191
192
  setStyle,
193
+ setProperties,
192
194
  deleteFeature,
193
195
  split,
194
196
  merge,
@@ -8,7 +8,7 @@ describe('manifest structure', () => {
8
8
  expect(manifest.reducer).toHaveProperty('actions')
9
9
  expect(manifest.InitComponent).toBeDefined()
10
10
  expect(Object.keys(manifest.api)).toEqual(expect.arrayContaining([
11
- 'newPolygon', 'newLine', 'newPoint', 'editFeature', 'addFeature', 'setStyle', 'deleteFeature', 'split', 'merge'
11
+ 'newPolygon', 'newLine', 'newPoint', 'editFeature', 'addFeature', 'setStyle', 'setProperties', 'deleteFeature', 'split', 'merge'
12
12
  ]))
13
13
  })
14
14
  })
@@ -69,35 +69,29 @@ describe('drawDone', () => {
69
69
  expect(btn().hiddenWhen({ pluginState: { mode: 'edit_point' } })).toBe(false)
70
70
  })
71
71
 
72
- test('enables in a draw/edit mode only when the geometry is valid', () => {
73
- const valid = { validationStatus: 'valid', previewValid: true }
74
- expect(btn().enableWhen({ pluginState: { mode: 'draw_polygon', ...valid } })).toBe(true)
75
- expect(btn().enableWhen({ pluginState: { mode: 'draw_line', ...valid } })).toBe(true)
76
- expect(btn().enableWhen({ pluginState: { mode: 'edit_vertex', ...valid } })).toBe(true)
77
- expect(btn().enableWhen({ pluginState: { mode: 'edit_point', ...valid } })).toBe(true)
78
- expect(btn().enableWhen({ pluginState: { mode: 'disabled', ...valid } })).toBe(false)
72
+ test('enables in a draw/edit mode only when the shape can be finished', () => {
73
+ const finishable = { canFinish: true, previewValid: true }
74
+ expect(btn().enableWhen({ pluginState: { mode: 'draw_polygon', ...finishable } })).toBe(true)
75
+ expect(btn().enableWhen({ pluginState: { mode: 'draw_line', ...finishable } })).toBe(true)
76
+ expect(btn().enableWhen({ pluginState: { mode: 'edit_vertex', ...finishable } })).toBe(true)
77
+ expect(btn().enableWhen({ pluginState: { mode: 'edit_point', ...finishable } })).toBe(true)
78
+ expect(btn().enableWhen({ pluginState: { mode: 'disabled', ...finishable } })).toBe(false)
79
79
  })
80
80
 
81
- test('stays disabled while the geometry is invalid', () => {
82
- expect(btn().enableWhen({ pluginState: { mode: 'draw_polygon', validationStatus: 'invalid', previewValid: true } })).toBe(false)
83
- expect(btn().enableWhen({ pluginState: { mode: 'draw_line', validationStatus: 'invalid', previewValid: true } })).toBe(false)
84
- expect(btn().enableWhen({ pluginState: { mode: 'edit_vertex', validationStatus: 'invalid', previewValid: true } })).toBe(false)
85
- expect(btn().enableWhen({ pluginState: { mode: 'edit_point', validationStatus: 'invalid', previewValid: true } })).toBe(false)
86
- })
87
-
88
- // A live edit-mode flip back to valid must not enable Done while the commit check
89
- // still fails or is pending, and vice versa.
90
- test('stays enabled while a check is pending, so pressing it can wait for the check', () => {
91
- expect(btn().enableWhen({ pluginState: { mode: 'edit_vertex', validationStatus: 'pending', previewValid: true } })).toBe(true)
81
+ test('stays disabled while the shape can\u2019t be finished', () => {
82
+ expect(btn().enableWhen({ pluginState: { mode: 'draw_polygon', canFinish: false, previewValid: true } })).toBe(false)
83
+ expect(btn().enableWhen({ pluginState: { mode: 'edit_vertex', canFinish: false, previewValid: true } })).toBe(false)
92
84
  })
93
85
 
94
86
  test('is disabled while waiting on a check after being pressed', () => {
95
- expect(btn().enableWhen({ pluginState: { mode: 'edit_vertex', validationStatus: 'pending', previewValid: true, holding: true } })).toBe(false)
87
+ expect(btn().enableWhen({ pluginState: { mode: 'edit_vertex', canFinish: true, previewValid: true, holding: true } })).toBe(false)
96
88
  })
97
89
 
90
+ // A live edit-mode flip back to valid must not enable Done while the commit check
91
+ // still fails, and vice versa.
98
92
  test('needs both the commit check and the live preview to pass', () => {
99
- expect(btn().enableWhen({ pluginState: { mode: 'edit_vertex', validationStatus: 'valid', previewValid: false } })).toBe(false)
100
- expect(btn().enableWhen({ pluginState: { mode: 'edit_vertex', validationStatus: 'invalid', previewValid: true } })).toBe(false)
93
+ expect(btn().enableWhen({ pluginState: { mode: 'edit_vertex', canFinish: true, previewValid: false } })).toBe(false)
94
+ expect(btn().enableWhen({ pluginState: { mode: 'edit_vertex', canFinish: false, previewValid: true } })).toBe(false)
101
95
  })
102
96
 
103
97
  test('is busy once it\u2019s waiting on a slow check', () => {
@@ -117,14 +111,14 @@ describe('drawDone', () => {
117
111
  const drawing = { mode: 'draw_polygon', previewValid: true }
118
112
 
119
113
  test('stays enabled while a check is pending, reading "Done", so the shape can be finished', () => {
120
- const pluginState = { ...drawing, validationStatus: 'pending', checkSlow: true }
114
+ const pluginState = { ...drawing, validationStatus: 'pending', canFinish: true, checkSlow: true }
121
115
  expect(btn().enableWhen({ pluginState })).toBe(true)
122
116
  expect(btn().label({ pluginState })).toBe('Done')
123
117
  expect(btn().busyWhen({ pluginState })).toBe(false)
124
118
  })
125
119
 
126
120
  test('is disabled once the shape is held, reading "Checking…" once the check is slow', () => {
127
- const held = { ...drawing, validationStatus: 'pending', holding: true }
121
+ const held = { ...drawing, validationStatus: 'pending', canFinish: true, holding: true }
128
122
  expect(btn().enableWhen({ pluginState: held })).toBe(false)
129
123
  expect(btn().label({ pluginState: { ...held, checkSlow: false } })).toBe('Done')
130
124
  expect(btn().label({ pluginState: { ...held, checkSlow: true } })).toBe('Checking…')
@@ -140,8 +134,8 @@ describe('drawDone', () => {
140
134
  })
141
135
 
142
136
  test('stays disabled while a check has failed', () => {
143
- expect(btn().enableWhen({ pluginState: { ...drawing, validationStatus: 'invalid' } })).toBe(false)
144
- expect(btn().enableWhen({ pluginState: { ...drawing, validationStatus: 'error' } })).toBe(false)
137
+ expect(btn().enableWhen({ pluginState: { ...drawing, validationStatus: 'invalid', canFinish: false } })).toBe(false)
138
+ expect(btn().enableWhen({ pluginState: { ...drawing, validationStatus: 'error', canFinish: false } })).toBe(false)
145
139
  })
146
140
  })
147
141
  })
@@ -13,6 +13,9 @@ const initialState = {
13
13
  // else should set them.
14
14
  // Status of the latest check: 'idle' | 'pending' | 'valid' | 'invalid' | 'error'
15
15
  validationStatus: 'idle',
16
+ // Whether the shape can be finished: the latest check passed, or it's pending and the last
17
+ // callback result didn't fail (canFinishCheck)
18
+ canFinish: false,
16
19
  // Whether a pending check has run long enough to show as "Checking…" (SLOW_CHECK_MS)
17
20
  checkSlow: false,
18
21
  // A finished shape is waiting on its pending check before it's created
@@ -32,6 +35,7 @@ const setMode = (state, payload) => ({
32
35
  // A new/empty shape has no passing check yet; its first commit (or edit mode's
33
36
  // edit-start check in api/editFeature) settles it.
34
37
  validationStatus: 'idle',
38
+ canFinish: false,
35
39
  checkSlow: false,
36
40
  holding: false,
37
41
  previewValid: true,
@@ -65,6 +69,8 @@ const setUndoStackLength = (state, payload) => ({ ...state, undoStackLength: pay
65
69
 
66
70
  const setValidationStatus = (state, payload) => ({ ...state, validationStatus: payload, checkSlow: false })
67
71
 
72
+ const setCanFinish = (state, payload) => ({ ...state, canFinish: !!payload })
73
+
68
74
  const setCheckSlow = (state) => ({ ...state, checkSlow: true })
69
75
 
70
76
  const setHolding = (state, payload) => ({ ...state, holding: !!payload })
@@ -82,6 +88,7 @@ const actions = {
82
88
  SET_HAS_SNAP_LAYERS: setHasSnapLayers,
83
89
  SET_UNDO_STACK_LENGTH: setUndoStackLength,
84
90
  SET_VALIDATION_STATUS: setValidationStatus,
91
+ SET_CAN_FINISH: setCanFinish,
85
92
  SET_CHECK_SLOW: setCheckSlow,
86
93
  SET_HOLDING: setHolding,
87
94
  SET_PREVIEW_VALID: setPreviewValid,
@@ -93,6 +93,17 @@ describe('SET_VALIDATION_STATUS', () => {
93
93
  })
94
94
  })
95
95
 
96
+ describe('SET_CAN_FINISH', () => {
97
+ test('stores whether the shape can be finished', () => {
98
+ expect(actions.SET_CAN_FINISH(initialState, true).canFinish).toBe(true)
99
+ expect(actions.SET_CAN_FINISH({ ...initialState, canFinish: true }, false).canFinish).toBe(false)
100
+ })
101
+
102
+ test('a new mode can\u2019t be finished until its first check', () => {
103
+ expect(actions.SET_MODE({ ...initialState, canFinish: true }, 'draw_polygon').canFinish).toBe(false)
104
+ })
105
+ })
106
+
96
107
  describe('SET_HOLDING', () => {
97
108
  test('marks a finished shape as waiting on its check', () => {
98
109
  expect(actions.SET_HOLDING(initialState, true).holding).toBe(true)