@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.
- package/dist/esm/im-core.js +1 -1
- package/dist/umd/im-core.js +1 -1
- package/dist/umd/index.js +1 -1
- package/docs/api/button-definition.md +11 -0
- package/docs/api/context.md +2 -0
- package/docs/api.md +3 -1
- package/docs/assets/css/docusaurus.scss +7 -1
- package/docs/plugins/draw.md +326 -121
- package/package.json +1 -1
- package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
- package/plugins/datasets/dist/umd/index.js +1 -1
- package/plugins/datasets/src/initialise/DatasetsInit.jsx +6 -17
- package/plugins/datasets/src/initialise/DatasetsInit.test.jsx +3 -43
- package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/esm/index.js +1 -1
- package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/umd/index.js +1 -1
- package/plugins/draw/src/DrawInit.jsx +9 -6
- package/plugins/draw/src/DrawInit.test.jsx +15 -0
- package/plugins/draw/src/adapterEvents.js +82 -44
- package/plugins/draw/src/adapters/adapterContract.test.js +3 -3
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +18 -10
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +31 -22
- package/plugins/draw/src/adapters/maplibre/drawEvents.js +2 -1
- package/plugins/draw/src/adapters/maplibre/modes/drawMode/clickHandlers.js +13 -12
- package/plugins/draw/src/adapters/maplibre/modes/drawMode/clickHandlers.test.js +5 -5
- package/plugins/draw/src/adapters/maplibre/modes/drawMode/undoHandlers.js +1 -1
- package/plugins/draw/src/adapters/maplibre/modes/drawMode/undoHandlers.test.js +2 -2
- package/plugins/draw/src/adapters/maplibre/modes/drawPointMode.js +6 -9
- package/plugins/draw/src/adapters/maplibre/modes/drawPointMode.test.js +4 -4
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.js +6 -9
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.test.js +1 -1
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode/undoHandlers.js +8 -14
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode/undoHandlers.test.js +37 -8
- package/plugins/draw/src/adapters/maplibre/modes/editPointMode.js +11 -1
- package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/undoHandlers.js +14 -19
- package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/undoHandlers.test.js +41 -4
- package/plugins/draw/src/adapters/maplibre/modes/editVertexMode.js +12 -1
- package/plugins/draw/src/adapters/maplibre/utils/editModeEvents.js +3 -1
- package/plugins/draw/src/adapters/maplibre/utils/geometryValidation.js +9 -10
- package/plugins/draw/src/adapters/maplibre/utils/geometryValidation.test.js +5 -24
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +10 -7
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +12 -7
- package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.js +5 -0
- package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.test.js +7 -3
- package/plugins/draw/src/adapters/openlayers/draw/DrawMode.js +11 -11
- package/plugins/draw/src/adapters/openlayers/draw/DrawMode.test.js +15 -15
- package/plugins/draw/src/adapters/openlayers/edit/EditMode.js +21 -28
- package/plugins/draw/src/adapters/openlayers/edit/EditMode.test.js +26 -5
- package/plugins/draw/src/adapters/openlayers/edit/selectionState.js +5 -6
- package/plugins/draw/src/adapters/openlayers/edit/selectionState.test.js +2 -12
- package/plugins/draw/src/adapters/openlayers/edit/undoOps.js +9 -0
- package/plugins/draw/src/adapters/openlayers/edit/undoOps.test.js +7 -0
- package/plugins/draw/src/adapters/openlayers/point/drawPointMode.js +6 -9
- package/plugins/draw/src/adapters/openlayers/point/drawPointMode.test.js +3 -3
- package/plugins/draw/src/adapters/openlayers/point/editPointMode.js +18 -9
- package/plugins/draw/src/adapters/openlayers/point/editPointMode.test.js +29 -4
- package/plugins/draw/src/adapters/openlayers/point/pointOps.js +3 -2
- package/plugins/draw/src/adapters/openlayers/point/pointOps.test.js +6 -0
- package/plugins/draw/src/adapters/openlayers/point/pointSelectionState.js +3 -4
- package/plugins/draw/src/adapters/openlayers/point/pointSelectionState.test.js +2 -12
- package/plugins/draw/src/api/createNewShape.js +4 -4
- package/plugins/draw/src/api/createNewShape.test.js +1 -1
- package/plugins/draw/src/api/editFeature.js +8 -11
- package/plugins/draw/src/api/editFeature.test.js +33 -22
- package/plugins/draw/src/api/newLine.test.js +3 -3
- package/plugins/draw/src/api/newPoint.js +4 -5
- package/plugins/draw/src/api/newPoint.test.js +11 -3
- package/plugins/draw/src/api/newPolygon.test.js +11 -3
- package/plugins/draw/src/api/recheckGeometry.js +32 -0
- package/plugins/draw/src/api/recheckGeometry.test.js +55 -0
- package/plugins/draw/src/api/setGeometry.js +33 -0
- package/plugins/draw/src/api/setGeometry.test.js +48 -0
- package/plugins/draw/src/api/split.js +26 -22
- package/plugins/draw/src/api/split.test.js +38 -18
- package/plugins/draw/src/events.js +269 -77
- package/plugins/draw/src/events.test.js +779 -34
- package/plugins/draw/src/hooks/useSpatialList.js +10 -11
- package/plugins/draw/src/hooks/useSpatialList.test.js +3 -3
- package/plugins/draw/src/manifest.js +30 -15
- package/plugins/draw/src/manifest.test.js +91 -9
- package/plugins/draw/src/reducer.js +31 -9
- package/plugins/draw/src/reducer.test.js +35 -3
- package/plugins/draw/src/utils/drawModes.js +27 -0
- package/plugins/draw/src/utils/drawModes.test.js +27 -0
- package/plugins/draw/src/utils/needsDrawTarget.js +1 -5
- package/plugins/draw/src/utils/needsDrawTarget.test.js +1 -11
- package/plugins/draw/src/utils/touchTarget.js +34 -26
- package/plugins/draw/src/utils/touchTarget.test.js +32 -20
- package/plugins/draw/src/validation/commitChecks.js +187 -0
- package/plugins/draw/src/validation/commitChecks.test.js +209 -0
- package/plugins/draw/src/validation/liveDrawChecks.js +11 -11
- package/plugins/draw/src/validation/liveDrawChecks.test.js +37 -37
- package/plugins/draw/src/validation/liveStroke.js +6 -6
- package/plugins/draw/src/validation/liveStroke.test.js +27 -27
- package/plugins/draw/src/validation/rules.js +16 -14
- package/plugins/draw/src/validation/rules.test.js +5 -5
- package/plugins/draw/src/validation/validateGeometry.js +58 -31
- package/plugins/draw/src/validation/validateGeometry.test.js +68 -44
- package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/index.js +1 -1
- package/plugins/interact/src/hooks/useInteractionHandlers.js +3 -5
- package/plugins/interact/src/hooks/useInteractionHandlers.test.js +4 -3
- package/plugins/interact/src/hooks/useSpatialList.js +16 -17
- package/plugins/interact/src/hooks/useSpatialList.test.js +9 -9
- package/plugins/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
- package/plugins/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
- package/plugins/map-styles/src/MapStyles.jsx +2 -2
- package/providers/maplibre/dist/esm/index.js +1 -1
- package/providers/maplibre/dist/umd/index.js +1 -1
- package/providers/maplibre/src/index.js +11 -2
- package/providers/maplibre/src/index.test.js +23 -1
- package/providers/maplibre/src/utils/detectModuleWorker.js +8 -19
- package/providers/maplibre/src/utils/detectModuleWorker.test.js +19 -23
- package/providers/maplibre/src/utils/workerUrl.js +12 -0
- package/providers/maplibre/src/utils/workerUrl.test.js +14 -1
- package/src/App/components/MapButton/MapButton.jsx +6 -0
- package/src/App/components/MapButton/MapButton.test.jsx +10 -0
- package/src/App/components/Markers/Markers.jsx +3 -5
- package/src/App/components/Markers/Markers.test.jsx +9 -11
- package/src/App/components/Panel/Panel.jsx +2 -9
- package/src/App/hooks/useInterfaceAPI.js +16 -4
- package/src/App/hooks/useInterfaceAPI.test.js +66 -1
- package/src/App/renderer/mapButtons.js +2 -1
- package/src/App/renderer/mapButtons.test.js +9 -0
- package/src/App/store/MapProvider.jsx +3 -0
- package/src/App/store/MapProvider.test.jsx +24 -0
- package/src/config/mapSizeScales.js +3 -0
- package/src/types.js +4 -0
- package/src/utils/focusAfterPanelClose.js +9 -0
- package/src/utils/focusAfterPanelClose.test.js +40 -0
|
@@ -2,9 +2,11 @@ import { renderHook, act } from '@testing-library/react'
|
|
|
2
2
|
import { useInterfaceAPI } from './useInterfaceAPI.js'
|
|
3
3
|
import { useApp } from '../store/appContext.js'
|
|
4
4
|
import { useService } from '../store/serviceContext.js'
|
|
5
|
+
import { useConfig } from '../store/configContext.js'
|
|
5
6
|
|
|
6
7
|
jest.mock('../store/appContext.js')
|
|
7
8
|
jest.mock('../store/serviceContext.js')
|
|
9
|
+
jest.mock('../store/configContext.js')
|
|
8
10
|
|
|
9
11
|
const makeEventBus = () => {
|
|
10
12
|
const handlers = {}
|
|
@@ -26,11 +28,15 @@ describe('useInterfaceAPI', () => {
|
|
|
26
28
|
hiddenButtons: new Set(),
|
|
27
29
|
disabledButtons: new Set(),
|
|
28
30
|
pressedButtons: new Set(),
|
|
29
|
-
expandedButtons: new Set()
|
|
31
|
+
expandedButtons: new Set(),
|
|
32
|
+
openPanels: {},
|
|
33
|
+
layoutRefs: { viewportRef: { current: { focus: jest.fn() } } },
|
|
34
|
+
interfaceType: 'keyboard'
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
useApp.mockReturnValue({ dispatch: mockDispatch, ...mockState })
|
|
33
38
|
useService.mockReturnValue({ eventBus: mockEventBus })
|
|
39
|
+
useConfig.mockReturnValue({ id: 'map' })
|
|
34
40
|
})
|
|
35
41
|
|
|
36
42
|
it('sets a mode on app:setapplicationmode', () => {
|
|
@@ -122,6 +128,65 @@ describe('useInterfaceAPI', () => {
|
|
|
122
128
|
expect(mockDispatch).toHaveBeenCalledWith({ type: 'CLOSE_PANEL', payload: 'panel1' })
|
|
123
129
|
})
|
|
124
130
|
|
|
131
|
+
describe('focus on app:hidepanel', () => {
|
|
132
|
+
let panelEl, panelButton, triggeringElement, outsideButton
|
|
133
|
+
|
|
134
|
+
beforeEach(() => {
|
|
135
|
+
jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => cb())
|
|
136
|
+
panelEl = document.createElement('div')
|
|
137
|
+
panelEl.id = 'map-panel-panel1'
|
|
138
|
+
panelButton = panelEl.appendChild(document.createElement('button'))
|
|
139
|
+
triggeringElement = document.createElement('button')
|
|
140
|
+
outsideButton = document.createElement('button')
|
|
141
|
+
document.body.append(panelEl, triggeringElement, outsideButton)
|
|
142
|
+
jest.spyOn(triggeringElement, 'focus')
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
afterEach(() => {
|
|
146
|
+
window.requestAnimationFrame.mockRestore()
|
|
147
|
+
document.body.innerHTML = ''
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
const renderWithOpenPanel = (props) => {
|
|
151
|
+
useApp.mockReturnValue({ dispatch: mockDispatch, ...mockState, openPanels: { panel1: { props } } })
|
|
152
|
+
renderHook(() => useInterfaceAPI())
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
it('focuses the triggering element when focus is inside the panel', () => {
|
|
156
|
+
renderWithOpenPanel({ triggeringElement })
|
|
157
|
+
panelButton.focus()
|
|
158
|
+
act(() => mockEventBus.emit('app:hidepanel', 'panel1'))
|
|
159
|
+
expect(triggeringElement.focus).toHaveBeenCalledWith({ preventScroll: false })
|
|
160
|
+
expect(mockDispatch).toHaveBeenCalledWith({ type: 'CLOSE_PANEL', payload: 'panel1' })
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('focuses the map when focus is inside the panel and the triggering element has gone', () => {
|
|
164
|
+
triggeringElement.remove()
|
|
165
|
+
renderWithOpenPanel({ triggeringElement })
|
|
166
|
+
panelButton.focus()
|
|
167
|
+
act(() => mockEventBus.emit('app:hidepanel', 'panel1'))
|
|
168
|
+
expect(triggeringElement.focus).not.toHaveBeenCalled()
|
|
169
|
+
expect(mockState.layoutRefs.viewportRef.current.focus).toHaveBeenCalled()
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('focuses the map when focus is inside a panel opened without a triggering element', () => {
|
|
173
|
+
renderWithOpenPanel({})
|
|
174
|
+
panelButton.focus()
|
|
175
|
+
act(() => mockEventBus.emit('app:hidepanel', 'panel1'))
|
|
176
|
+
expect(mockState.layoutRefs.viewportRef.current.focus).toHaveBeenCalled()
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('leaves focus where it is when focus is outside the panel', () => {
|
|
180
|
+
renderWithOpenPanel({ triggeringElement })
|
|
181
|
+
outsideButton.focus()
|
|
182
|
+
act(() => mockEventBus.emit('app:hidepanel', 'panel1'))
|
|
183
|
+
expect(triggeringElement.focus).not.toHaveBeenCalled()
|
|
184
|
+
expect(mockState.layoutRefs.viewportRef.current.focus).not.toHaveBeenCalled()
|
|
185
|
+
expect(document.activeElement).toBe(outsideButton)
|
|
186
|
+
expect(mockDispatch).toHaveBeenCalledWith({ type: 'CLOSE_PANEL', payload: 'panel1' })
|
|
187
|
+
})
|
|
188
|
+
})
|
|
189
|
+
|
|
125
190
|
it('dispatches ADD_CONTROL on app:addcontrol', () => {
|
|
126
191
|
renderHook(() => useInterfaceAPI())
|
|
127
192
|
act(() => mockEventBus.emit('app:addcontrol', { id: 'ctrl1', config: { position: 'top-left' } }))
|
|
@@ -116,7 +116,7 @@ const slotButtonProps = ({ buttonId, config, isHidden, appState, appConfig, eval
|
|
|
116
116
|
* Renders a single button's MapButton element for its slot, computing its click handler
|
|
117
117
|
* and derived state (panel-open, showLabel fallback, etc.) from its config and appState.
|
|
118
118
|
*/
|
|
119
|
-
function SlotButton ({ buttonId, config, isHidden = false, appState, appConfig, evaluateProp }) {
|
|
119
|
+
function SlotButton ({ buttonId, config, isHidden = false, appState, appConfig, evaluateProp }) { // NOSONAR, the inline props read more clearly than a helper would
|
|
120
120
|
const bpConfig = config[appState.breakpoint] ?? {}
|
|
121
121
|
const handleClick = createButtonClickHandler(config, appState, evaluateProp)
|
|
122
122
|
const isPanelOpen = !!(config.panelId && appState.openPanels[config.panelId])
|
|
@@ -136,6 +136,7 @@ function SlotButton ({ buttonId, config, isHidden = false, appState, appConfig,
|
|
|
136
136
|
isHidden={isHidden}
|
|
137
137
|
isPressed={(config.isPressed !== undefined || config.pressedWhen) ? appState.pressedButtons.has(buttonId) : undefined}
|
|
138
138
|
isExpanded={(config.isExpanded !== undefined || config.expandedWhen) ? appState.expandedButtons.has(buttonId) : undefined}
|
|
139
|
+
isBusy={config.busyWhen ? !!evaluateProp(config.busyWhen, config.pluginId) : undefined}
|
|
139
140
|
isPanelOpen={isPanelOpen}
|
|
140
141
|
panelRole={panelRole}
|
|
141
142
|
onClick={handleClick}
|
|
@@ -165,6 +165,15 @@ describe('mapButtons module', () => {
|
|
|
165
165
|
expect(result.props).toMatchObject({ isDisabled: true, isHidden: true, isPressed: true, isExpanded: true })
|
|
166
166
|
})
|
|
167
167
|
|
|
168
|
+
it('evaluates busyWhen on render, and leaves isBusy undefined without it', () => {
|
|
169
|
+
const busyEvaluate = jest.fn((prop) => (typeof prop === 'function' ? prop() : prop))
|
|
170
|
+
const busy = SlotButton({ buttonId: 'id', config: { ...baseBtn, busyWhen: () => true }, appState, appConfig, evaluateProp: busyEvaluate })
|
|
171
|
+
expect(busy.props.isBusy).toBe(true)
|
|
172
|
+
const idle = SlotButton({ buttonId: 'id', config: { ...baseBtn, busyWhen: () => false }, appState, appConfig, evaluateProp: busyEvaluate })
|
|
173
|
+
expect(idle.props.isBusy).toBe(false)
|
|
174
|
+
expect(render(baseBtn).props.isBusy).toBeUndefined()
|
|
175
|
+
})
|
|
176
|
+
|
|
168
177
|
it.each([
|
|
169
178
|
[{ slot: 'right-top', open: false }, 'dialog'],
|
|
170
179
|
[{ slot: 'right-top', open: false, dismissible: false }, 'region'],
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import React, { createContext, useEffect, useReducer, useMemo, useRef } from 'react'
|
|
3
3
|
import { initialState, reducer } from './mapReducer.js'
|
|
4
4
|
import { EVENTS as events } from '../../config/events.js'
|
|
5
|
+
import { getMapSizeScale } from '../../config/mapSizeScales.js'
|
|
5
6
|
|
|
6
7
|
export const MapContext = createContext(null)
|
|
7
8
|
|
|
@@ -68,8 +69,10 @@ export const MapProvider = ({ options, children }) => {
|
|
|
68
69
|
localStorage.setItem(`${options.id}:mapSize`, state.mapSize)
|
|
69
70
|
}, [state.mapStyle, state.mapSize])
|
|
70
71
|
|
|
72
|
+
// mapSizeScale is worked out from mapSize here rather than stored, so the two can't drift apart
|
|
71
73
|
const mapStore = useMemo(() => ({
|
|
72
74
|
...state,
|
|
75
|
+
mapSizeScale: getMapSizeScale(state.mapSize),
|
|
73
76
|
dispatch
|
|
74
77
|
}), [state])
|
|
75
78
|
|
|
@@ -125,6 +125,30 @@ describe('MapProvider', () => {
|
|
|
125
125
|
expect(contextValue.isMapReady).toBe(true)
|
|
126
126
|
})
|
|
127
127
|
|
|
128
|
+
test.each([
|
|
129
|
+
['small', 1],
|
|
130
|
+
['medium', 1.5],
|
|
131
|
+
['large', 2],
|
|
132
|
+
['unknown', 1]
|
|
133
|
+
])('provides mapSizeScale for map size %s', (mapSize, expected) => {
|
|
134
|
+
let contextValue
|
|
135
|
+
const Child = () => {
|
|
136
|
+
contextValue = React.useContext(MapContext)
|
|
137
|
+
return null
|
|
138
|
+
}
|
|
139
|
+
render(
|
|
140
|
+
<MapProvider options={{ id: 'map1', mapSize: 'small', eventBus: mockEventBus }}>
|
|
141
|
+
<Child />
|
|
142
|
+
</MapProvider>
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
act(() => {
|
|
146
|
+
capturedHandlers['map:setsize'](mapSize)
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
expect(contextValue.mapSizeScale).toBe(expected)
|
|
150
|
+
})
|
|
151
|
+
|
|
128
152
|
test('emits map:sizechange when mapSize changes after initial value', () => {
|
|
129
153
|
render(
|
|
130
154
|
<MapProvider options={{ id: 'map1', mapSize: '100x100', eventBus: mockEventBus }}>
|
package/src/types.js
CHANGED
|
@@ -257,6 +257,10 @@
|
|
|
257
257
|
* Callback to determine if the button should appear pressed. Sets aria-pressed accordingly.
|
|
258
258
|
* Only evaluated for plugin-defined buttons; use toggleButtonState() for host buttons.
|
|
259
259
|
*
|
|
260
|
+
* @property {(context: PluginContext) => boolean} [busyWhen]
|
|
261
|
+
* Callback to determine if the action behind the button is in progress. Sets aria-busy
|
|
262
|
+
* accordingly. Evaluated on every render, like a function label.
|
|
263
|
+
*
|
|
260
264
|
* @property {ButtonBreakpointConfig} tablet
|
|
261
265
|
* Tablet breakpoint configuration.
|
|
262
266
|
*/
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Moves focus to the element that opened a closed panel, or to the map viewport if that element
|
|
2
|
+
// has since left the page (e.g. a Features-list item that dropped out of the list). Runs in the
|
|
3
|
+
// next frame so a modal panel has released its focus trap and inert state first.
|
|
4
|
+
export const focusAfterPanelClose = ({ triggeringElement, viewportRef, interfaceType }) => {
|
|
5
|
+
requestAnimationFrame(() => {
|
|
6
|
+
const focusTarget = triggeringElement && document.contains(triggeringElement) ? triggeringElement : viewportRef.current
|
|
7
|
+
focusTarget?.focus?.({ preventScroll: interfaceType !== 'keyboard' })
|
|
8
|
+
})
|
|
9
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { focusAfterPanelClose } from './focusAfterPanelClose.js'
|
|
2
|
+
|
|
3
|
+
describe('focusAfterPanelClose', () => {
|
|
4
|
+
let viewportRef
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => cb())
|
|
8
|
+
viewportRef = { current: { focus: jest.fn() } }
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
window.requestAnimationFrame.mockRestore()
|
|
13
|
+
document.body.innerHTML = ''
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('focuses the triggering element when it is still on the page', () => {
|
|
17
|
+
const triggeringElement = document.body.appendChild(document.createElement('button'))
|
|
18
|
+
const focusSpy = jest.spyOn(triggeringElement, 'focus')
|
|
19
|
+
focusAfterPanelClose({ triggeringElement, viewportRef, interfaceType: 'keyboard' })
|
|
20
|
+
expect(focusSpy).toHaveBeenCalledWith({ preventScroll: false })
|
|
21
|
+
expect(viewportRef.current.focus).not.toHaveBeenCalled()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('focuses the viewport when the triggering element has left the page', () => {
|
|
25
|
+
const triggeringElement = document.createElement('button')
|
|
26
|
+
focusAfterPanelClose({ triggeringElement, viewportRef, interfaceType: 'pointer' })
|
|
27
|
+
expect(viewportRef.current.focus).toHaveBeenCalledWith({ preventScroll: true })
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('focuses the viewport when there is no triggering element', () => {
|
|
31
|
+
focusAfterPanelClose({ viewportRef, interfaceType: 'keyboard' })
|
|
32
|
+
expect(viewportRef.current.focus).toHaveBeenCalledWith({ preventScroll: false })
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('waits for the next frame before moving focus', () => {
|
|
36
|
+
window.requestAnimationFrame.mockImplementation(() => 1)
|
|
37
|
+
focusAfterPanelClose({ viewportRef, interfaceType: 'keyboard' })
|
|
38
|
+
expect(viewportRef.current.focus).not.toHaveBeenCalled()
|
|
39
|
+
})
|
|
40
|
+
})
|