@defra/interactive-map 0.0.52-alpha → 0.0.54-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 (194) hide show
  1. package/dist/css/index.css +1 -1
  2. package/dist/esm/im-core.js +1 -1
  3. package/dist/esm/im-shell.js +1 -1
  4. package/dist/umd/im-core.js +1 -1
  5. package/dist/umd/index.js +1 -1
  6. package/docs/api/context.md +30 -12
  7. package/docs/api/marker-config.md +16 -8
  8. package/docs/api/symbol-config.md +24 -9
  9. package/docs/api/symbol-registry.md +49 -14
  10. package/docs/api.md +11 -3
  11. package/docs/plugins/datasets.md +6 -10
  12. package/docs/plugins/draw.md +2 -1
  13. package/docs/plugins/map-key.md +47 -4
  14. package/package.json +1 -1
  15. package/plugins/beta/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
  16. package/plugins/beta/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
  17. package/plugins/beta/map-styles/dist/umd/index.js +1 -1
  18. package/plugins/datasets/dist/esm/im-datasets-ml-adapter.js +1 -1
  19. package/plugins/datasets/dist/esm/im-datasets-ol-adapter.js +1 -1
  20. package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
  21. package/plugins/datasets/dist/esm/mapboxStyleLayerAdapter.js +1 -1
  22. package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
  23. package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
  24. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  25. package/plugins/datasets/src/adapters/esri/esriLayerAdapter.js +6 -6
  26. package/plugins/datasets/src/adapters/esri/registry/esriDataset.test.js +1 -1
  27. package/plugins/datasets/src/adapters/maplibre/layerBuilders.js +10 -10
  28. package/plugins/datasets/src/adapters/maplibre/layerBuilders.test.js +22 -6
  29. package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.js +9 -3
  30. package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.test.js +18 -3
  31. package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.js +3 -3
  32. package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.test.js +20 -27
  33. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.js +16 -70
  34. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.test.js +30 -115
  35. package/plugins/datasets/src/adapters/openlayers/layerBuilders.js +11 -13
  36. package/plugins/datasets/src/adapters/openlayers/layerBuilders.test.js +11 -3
  37. package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.js +48 -19
  38. package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.test.js +101 -13
  39. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.js +27 -28
  40. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.test.js +51 -32
  41. package/plugins/datasets/src/fetch/createDynamicSource.js +2 -2
  42. package/plugins/datasets/src/initialise/DatasetsInit.jsx +2 -2
  43. package/plugins/datasets/src/initialise/defaults.js +16 -27
  44. package/plugins/datasets/src/initialise/defaults.test.js +25 -57
  45. package/plugins/datasets/src/initialise/initialiseDatasets.js +1 -4
  46. package/plugins/datasets/src/reducers/__data__/demoDatasets.js +1 -3
  47. package/plugins/datasets/src/reducers/mappedDatasetsReducer.test.js +1 -1
  48. package/plugins/datasets/src/registry/dataset.js +23 -17
  49. package/plugins/datasets/src/registry/dataset.test.js +114 -11
  50. package/plugins/datasets/src/registry/mapboxStyleDataset.js +1 -1
  51. package/plugins/datasets/src/registry/mapboxStyleDataset.test.js +1 -1
  52. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  53. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  54. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  55. package/plugins/draw/dist/esm/index.js +1 -1
  56. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  57. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  58. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  59. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  60. package/plugins/draw/dist/umd/index.js +1 -1
  61. package/plugins/draw/src/DrawInit.jsx +7 -5
  62. package/plugins/draw/src/DrawInit.test.jsx +18 -2
  63. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +11 -5
  64. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -11
  65. package/plugins/draw/src/adapters/maplibre/mapboxDraw.js +12 -9
  66. package/plugins/draw/src/adapters/maplibre/mapboxDraw.test.js +18 -0
  67. package/plugins/draw/src/adapters/maplibre/pointSymbolImages.js +81 -56
  68. package/plugins/draw/src/adapters/maplibre/pointSymbolImages.test.js +149 -80
  69. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +7 -6
  70. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +22 -25
  71. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.js +26 -8
  72. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.test.js +58 -16
  73. package/plugins/draw/src/adapters/openlayers/core/styles.js +33 -20
  74. package/plugins/draw/src/adapters/openlayers/core/styles.test.js +47 -21
  75. package/plugins/draw/src/adapters/openlayers/olDraw.js +16 -12
  76. package/plugins/draw/src/adapters/openlayers/olDraw.test.js +27 -32
  77. package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.js +44 -49
  78. package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.test.js +100 -119
  79. package/plugins/draw/src/defaults.js +2 -6
  80. package/plugins/draw/src/utils/stripInternalProperties.test.js +2 -1
  81. package/plugins/draw/src/utils/symbolKeys.js +3 -4
  82. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  83. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  84. package/plugins/interact/dist/umd/index.js +1 -1
  85. package/plugins/map-key/dist/css/index.css +1 -1
  86. package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
  87. package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -2
  88. package/plugins/map-key/dist/umd/index.js +1 -1
  89. package/plugins/map-key/src/components/Key/Key.jsx +8 -4
  90. package/plugins/map-key/src/components/Key/Key.module.scss +144 -53
  91. package/plugins/map-key/src/components/Key/KeyGroupItem.jsx +36 -3
  92. package/plugins/map-key/src/components/Key/KeyGroupItem.test.jsx +44 -2
  93. package/plugins/map-key/src/components/Key/KeyItem.jsx +2 -2
  94. package/plugins/map-key/src/components/Key/KeySvg.jsx +19 -18
  95. package/plugins/map-key/src/components/Key/KeySvg.test.jsx +25 -18
  96. package/plugins/map-key/src/components/Key/KeySvgPattern.jsx +1 -2
  97. package/plugins/map-key/src/components/Key/KeySvgPattern.test.jsx +3 -1
  98. package/plugins/map-key/src/components/Key/KeySvgRamp.jsx +35 -6
  99. package/plugins/map-key/src/components/Key/KeySvgRamp.test.jsx +56 -16
  100. package/plugins/map-key/src/components/Key/KeySvgSymbol.jsx +26 -12
  101. package/plugins/map-key/src/components/Key/KeySvgSymbol.test.jsx +24 -74
  102. package/plugins/map-key/src/components/Key/MapKey.jsx +3 -1
  103. package/plugins/map-key/src/components/Key/svgProperties.js +8 -2
  104. package/plugins/map-key/src/components/Key/useRampOrientation.js +61 -0
  105. package/plugins/map-key/src/components/Key/useRampOrientation.test.jsx +155 -0
  106. package/plugins/map-key/src/index.js +34 -0
  107. package/plugins/map-key/src/reducers/mergeKeyGroupItems.test.js +15 -0
  108. package/plugins/map-key/src/registry/index.js +0 -2
  109. package/plugins/map-key/src/registry/index.test.js +1 -9
  110. package/plugins/map-key/src/utils/groupStyles.js +5 -2
  111. package/plugins/menu/src/registry/index.js +0 -2
  112. package/plugins/menu/src/registry/index.test.js +1 -9
  113. package/plugins/search/dist/esm/im-search-plugin.js +1 -1
  114. package/plugins/search/dist/umd/im-search-plugin.js +1 -1
  115. package/plugins/search/dist/umd/index.js +1 -1
  116. package/plugins/search/src/Search.jsx +0 -1
  117. package/plugins/search/src/components/Form/Form.jsx +3 -3
  118. package/plugins/search/src/components/Suggestions/Suggestions.jsx +2 -0
  119. package/plugins/search/src/components/Suggestions/Suggestions.test.jsx +6 -0
  120. package/plugins/search/src/defaults.js +3 -0
  121. package/plugins/search/src/events/formHandlers.js +3 -9
  122. package/plugins/search/src/events/formHandlers.test.js +6 -9
  123. package/plugins/search/src/events/inputHandlers.js +8 -2
  124. package/plugins/search/src/events/inputHandlers.test.js +20 -0
  125. package/plugins/search/src/events/suggestionHandlers.js +3 -5
  126. package/plugins/search/src/events/suggestionHandlers.test.js +5 -8
  127. package/plugins/search/src/utils/showResult.js +9 -0
  128. package/plugins/search/src/utils/showResult.test.js +16 -0
  129. package/providers/beta/esri/dist/esm/im-esri-provider.js +1 -1
  130. package/providers/beta/openlayers/dist/esm/im-openlayers-provider.js +1 -1
  131. package/providers/beta/openlayers/dist/umd/im-openlayers-provider.js +1 -1
  132. package/providers/beta/openlayers/dist/umd/index.js +1 -1
  133. package/providers/beta/openlayers/src/openlayersProvider.js +102 -2
  134. package/providers/beta/openlayers/src/openlayersProvider.test.js +95 -0
  135. package/providers/beta/openlayers/src/utils/highlightFeatures.js +20 -25
  136. package/providers/beta/openlayers/src/utils/highlightFeatures.test.js +73 -38
  137. package/providers/beta/openlayers/src/utils/patternImages.js +61 -0
  138. package/providers/beta/openlayers/src/utils/patternImages.test.js +74 -0
  139. package/providers/beta/openlayers/src/utils/symbolImages.js +103 -95
  140. package/providers/beta/openlayers/src/utils/symbolImages.test.js +136 -85
  141. package/providers/mapProvider.js +53 -1
  142. package/providers/mapProvider.test.js +6 -0
  143. package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
  144. package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
  145. package/providers/maplibre/dist/umd/index.js +1 -1
  146. package/providers/maplibre/src/maplibreProvider.js +50 -17
  147. package/providers/maplibre/src/maplibreProvider.test.js +74 -20
  148. package/providers/maplibre/src/utils/highlightFeatures.js +19 -14
  149. package/providers/maplibre/src/utils/highlightFeatures.test.js +20 -14
  150. package/providers/maplibre/src/utils/patternImages.js +46 -29
  151. package/providers/maplibre/src/utils/patternImages.test.js +52 -4
  152. package/providers/maplibre/src/utils/symbolImages.js +80 -39
  153. package/providers/maplibre/src/utils/symbolImages.test.js +157 -54
  154. package/src/App/components/Hints/Hints.module.scss +1 -1
  155. package/src/App/components/Markers/Markers.jsx +16 -25
  156. package/src/App/components/Markers/Markers.test.jsx +34 -48
  157. package/src/App/components/Markers/SymbolLabelMarker.jsx +1 -1
  158. package/src/App/components/Markers/SymbolMarker.jsx +1 -1
  159. package/src/App/components/Viewport/Viewport.jsx +6 -4
  160. package/src/App/components/Viewport/Viewport.test.jsx +18 -1
  161. package/src/App/hooks/useMapAnnouncements.js +7 -2
  162. package/src/App/hooks/useMapAnnouncements.test.js +13 -2
  163. package/src/App/hooks/useModalPanelBehaviour.js +3 -1
  164. package/src/App/initialiseApp.js +1 -1
  165. package/src/App/store/ServiceProvider.jsx +12 -3
  166. package/src/App/store/ServiceProvider.test.jsx +39 -0
  167. package/src/InteractiveMap/InteractiveMap.js +3 -3
  168. package/src/config/appConfig.js +1 -5
  169. package/src/config/mapSizeScales.js +8 -0
  170. package/src/config/symbolConfig.js +101 -25
  171. package/src/services/mapFocus.js +64 -0
  172. package/src/services/mapFocus.test.js +76 -0
  173. package/src/services/patternRegistry.js +97 -76
  174. package/src/services/patternRegistry.test.js +19 -1
  175. package/src/services/symbolComposer.js +184 -0
  176. package/src/services/symbolComposer.test.js +132 -0
  177. package/src/services/symbolRegistry.js +326 -155
  178. package/src/services/symbolRegistry.test.js +334 -32
  179. package/src/types.js +11 -5
  180. package/src/utils/detectInterfaceType.js +16 -5
  181. package/src/utils/detectInterfaceType.test.js +32 -0
  182. package/src/utils/hashString.js +23 -0
  183. package/src/utils/hashString.test.js +29 -0
  184. package/src/utils/imageDataCache.js +43 -0
  185. package/src/utils/imageDataCache.test.js +38 -0
  186. package/src/utils/pathBounds.js +330 -0
  187. package/src/utils/pathBounds.test.js +145 -0
  188. package/src/utils/patternUtils.js +0 -8
  189. package/src/utils/patternUtils.test.js +0 -25
  190. package/src/utils/recentImageSets.js +35 -0
  191. package/src/utils/recentImageSets.test.js +46 -0
  192. package/src/utils/symbolUtils.js +26 -29
  193. package/src/utils/symbolUtils.test.js +27 -41
  194. package/plugins/map-key/dist/umd/im-map-key-plugin.js.LICENSE.txt +0 -1
@@ -1,16 +1,49 @@
1
+ import { useRef } from 'react'
2
+ import { getValueForStyle } from '../../../../../src/utils/getValueForStyle.js'
3
+ import { hasSymbol } from '../../../../../src/utils/symbolUtils.js'
1
4
  import { KeyItem } from './KeyItem.jsx'
5
+ import { RAMP } from '../../utils/groupStyles.js'
6
+ import { useRampOrientation } from './useRampOrientation.js'
2
7
 
3
- export const KeyGroupItem = ({ headingId, label, groupStyle, keyDefinitions, mapStyle }) => {
4
- const className = 'im-c-map-key-list' + (groupStyle ? ` im-c-map-key-list-${groupStyle}` : '')
8
+ const getListClassName = (groupStyle, orientation, hasStroke) => {
9
+ if (!groupStyle) {
10
+ return 'im-c-map-key-list'
11
+ }
12
+ const className = `im-c-map-key-list im-c-map-key-list--${groupStyle}`
13
+ if (groupStyle !== RAMP) {
14
+ return className
15
+ }
16
+ // Stroked ramp bands are separated by a gap; unstroked bands butt up to each other
17
+ const rampClassName = `${className} im-c-map-key-list--ramp-${orientation}`
18
+ return hasStroke ? `${rampClassName} im-c-map-key-list--has-stroke` : rampClassName
19
+ }
20
+
21
+ // A ramp's bands stretch to fill their row or column, which point symbols can't, so a ramp
22
+ // group with any symbol entries is shown as a standard group
23
+ const getGroupStyle = (groupStyle, keyDefinitions) =>
24
+ groupStyle === RAMP && keyDefinitions.some(keyDefinition => hasSymbol(keyDefinition.style))
25
+ ? undefined
26
+ : groupStyle
27
+
28
+ export const KeyGroupItem = ({ headingId, label, groupStyle: configuredGroupStyle, keyDefinitions, mapStyle, symbolRegistry, patternRegistry }) => {
29
+ const listRef = useRef(null)
30
+ const groupStyle = getGroupStyle(configuredGroupStyle, keyDefinitions)
31
+ const isRamp = groupStyle === RAMP
32
+ const hasStroke = isRamp &&
33
+ keyDefinitions.some(keyDefinition => getValueForStyle(keyDefinition.style?.stroke, mapStyle.id))
34
+ const orientation = useRampOrientation(listRef, keyDefinitions.map(keyDefinition => keyDefinition.label ?? ''), isRamp, hasStroke)
35
+ const className = getListClassName(groupStyle, orientation, hasStroke)
5
36
  return (
6
37
  <section className='im-c-map-key__group' aria-labelledby={headingId}>
7
38
  <h3 id={headingId} className='im-c-map-key__group-heading'>{label}</h3>
8
- <dl className={className}>
39
+ <dl ref={listRef} className={className}>
9
40
  {keyDefinitions.map(keyDefinition =>
10
41
  <KeyItem
11
42
  key={`${keyDefinition.id}`}
12
43
  keyDefinition={keyDefinition}
13
44
  mapStyle={mapStyle}
45
+ symbolRegistry={symbolRegistry}
46
+ patternRegistry={patternRegistry}
14
47
  groupStyle={groupStyle}
15
48
  />
16
49
  )}
@@ -1,5 +1,10 @@
1
1
  import { render, screen } from '@testing-library/react'
2
2
  import { KeyGroupItem } from './KeyGroupItem'
3
+ import { useRampOrientation } from './useRampOrientation.js'
4
+
5
+ jest.mock('./useRampOrientation.js', () => ({
6
+ useRampOrientation: jest.fn(() => 'horizontal')
7
+ }))
3
8
 
4
9
  jest.mock('./KeyItem.jsx', () => ({
5
10
  KeyItem: ({ keyDefinition }) => <div data-testid='key-item'>{keyDefinition.id}</div>
@@ -52,8 +57,45 @@ describe('KeyGroupItem', () => {
52
57
  })
53
58
 
54
59
  it('appends the groupStyle modifier class to the dl when groupStyle is provided', () => {
55
- const { container } = render(<KeyGroupItem {...baseProps} groupStyle='horizontal-ramp' />)
56
- expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list-horizontal-ramp')
60
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='other' />)
61
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list--other')
62
+ })
63
+
64
+ it('adds the measured orientation to a ramp', () => {
65
+ const keyDefinitions = [{ id: 'a', label: 'Low', style: {} }, { id: 'b', label: 'High', style: {} }]
66
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='ramp' keyDefinitions={keyDefinitions} />)
67
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list--ramp im-c-map-key-list--ramp-horizontal')
68
+ expect(useRampOrientation).toHaveBeenLastCalledWith(expect.anything(), ['Low', 'High'], true, false)
69
+ })
70
+
71
+ it('renders a vertical ramp when the labels do not fit', () => {
72
+ useRampOrientation.mockReturnValueOnce('vertical')
73
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='ramp' />)
74
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list--ramp im-c-map-key-list--ramp-vertical')
75
+ })
76
+
77
+ it('shows a ramp with any symbol entries as a standard group', () => {
78
+ const keyDefinitions = [{ id: 'a', label: 'Low', style: { fill: '#000' } }, { id: 'b', label: 'High', style: { symbol: 'pin' } }]
79
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='ramp' keyDefinitions={keyDefinitions} />)
80
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list')
81
+ expect(useRampOrientation).toHaveBeenLastCalledWith(expect.anything(), ['Low', 'High'], false, false)
82
+ })
83
+
84
+ it('does not measure groups that are not ramps', () => {
85
+ render(<KeyGroupItem {...baseProps} />)
86
+ expect(useRampOrientation).toHaveBeenLastCalledWith(expect.anything(), [], false, false)
87
+ })
88
+
89
+ it('adds the has-stroke modifier to a ramp when any item has a stroke', () => {
90
+ const keyDefinitions = [{ id: 'a', style: { fill: '#000' } }, { id: 'b', style: { stroke: { default: '#123' } } }]
91
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='ramp' keyDefinitions={keyDefinitions} />)
92
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list--ramp im-c-map-key-list--ramp-horizontal im-c-map-key-list--has-stroke')
93
+ })
94
+
95
+ it('omits the has-stroke modifier when no ramp item has a stroke', () => {
96
+ const keyDefinitions = [{ id: 'a', style: { fill: '#000' } }, { id: 'b', style: { fillPattern: 'dot' } }]
97
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='ramp' keyDefinitions={keyDefinitions} />)
98
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list--ramp im-c-map-key-list--ramp-horizontal')
57
99
  })
58
100
 
59
101
  it('uses the base dl class when groupStyle is not provided', () => {
@@ -1,7 +1,7 @@
1
1
  import { getValueForStyle } from '../../../../../src/utils/getValueForStyle.js'
2
2
  import { KeySvg } from './KeySvg.jsx'
3
3
 
4
- export const KeyItem = ({ keyDefinition, groupStyle, mapStyle }) => {
4
+ export const KeyItem = ({ keyDefinition, groupStyle, mapStyle, symbolRegistry, patternRegistry }) => {
5
5
  // Pure derivations of props — computed directly on every render rather than staged through
6
6
  // useState/useEffect, so there's no post-mount frame where the label/description are blank
7
7
  // (every panel reopen mounts this fresh, so that frame was visible as a flicker).
@@ -11,7 +11,7 @@ export const KeyItem = ({ keyDefinition, groupStyle, mapStyle }) => {
11
11
  return (
12
12
  <div className='im-c-map-key-list__item'>
13
13
  <dt className='im-c-map-key-list__item-symbol'>
14
- <KeySvg keyDefinition={keyDefinition} groupStyle={groupStyle} mapStyle={mapStyle} />
14
+ <KeySvg keyDefinition={keyDefinition} groupStyle={groupStyle} mapStyle={mapStyle} symbolRegistry={symbolRegistry} patternRegistry={patternRegistry} />
15
15
  </dt>
16
16
  <dd className='im-c-map-key-list__item-label'>
17
17
  {label}
@@ -3,46 +3,47 @@ import { KeySvgSymbol } from './KeySvgSymbol.jsx'
3
3
  import { KeySvgLine } from './KeySvgLine.jsx'
4
4
  import { KeySvgRect } from './KeySvgRect.jsx'
5
5
  import { KeySvgRamp } from './KeySvgRamp.jsx'
6
- import { symbolRegistry } from '../../registry/index.js'
7
- import { HORIZONTAL_RAMP } from '../../utils/groupStyles.js'
6
+ import { RAMP } from '../../utils/groupStyles.js'
8
7
 
9
8
  // Pure derivation of keyDefinition — computed directly during render (see KeyItem.jsx for why:
10
9
  // staging this through useState/useEffect meant every fresh mount painted a blank symbol first).
11
10
 
12
- const getSymbolShape = (keyDefinition, groupStyle) => {
11
+ const getSymbolShape = (keyDefinition, groupStyle, mapStyle, symbolRegistry) => {
13
12
  if (!keyDefinition) {
14
- return { symbolShape: null, symbolDef: null }
13
+ return { symbolShape: null, keySymbol: null }
15
14
  }
16
15
  const { hasSymbol, hasPattern, style } = keyDefinition
17
- if (groupStyle === HORIZONTAL_RAMP) {
18
- return { symbolShape: HORIZONTAL_RAMP, symbolDef: null }
16
+ if (groupStyle === RAMP) {
17
+ return { symbolShape: RAMP, keySymbol: null }
19
18
  }
20
19
  if (hasSymbol) {
21
- const symbolDef = symbolRegistry.getSymbolDef(style)
22
- return { symbolShape: symbolDef ? 'symbol' : 'rect', symbolDef }
20
+ const keySymbol = symbolRegistry.getKeySymbol(style, mapStyle)
21
+ return { symbolShape: keySymbol ? 'symbol' : 'rect', keySymbol }
23
22
  }
24
23
  if (hasPattern) {
25
- return { symbolShape: 'pattern', symbolDef: null }
24
+ return { symbolShape: 'pattern', keySymbol: null }
26
25
  }
27
- if (style.keySymbolShape === 'line') {
28
- return { symbolShape: 'line', symbolDef: null }
26
+ // Inferred from the style: a stroke with no fill at all is a line; any fill — including
27
+ // 'transparent' or 'none', for an outline-only shape — is a shape.
28
+ if (style.fill == null && style.stroke) {
29
+ return { symbolShape: 'line', keySymbol: null }
29
30
  }
30
- return { symbolShape: 'rect', symbolDef: null }
31
+ return { symbolShape: 'rect', keySymbol: null }
31
32
  }
32
33
 
33
- export const KeySvg = ({ keyDefinition, groupStyle, mapStyle }) => {
34
- const { symbolShape, symbolDef } = getSymbolShape(keyDefinition, groupStyle)
34
+ export const KeySvg = ({ keyDefinition, groupStyle, mapStyle, symbolRegistry, patternRegistry }) => {
35
+ const { symbolShape, keySymbol } = getSymbolShape(keyDefinition, groupStyle, mapStyle, symbolRegistry)
35
36
 
36
37
  if (!symbolShape) {
37
38
  return null
38
39
  } else if (symbolShape === 'symbol') {
39
- return <KeySvgSymbol mapStyle={mapStyle} keyDefinition={keyDefinition} symbolDef={symbolDef} />
40
+ return <KeySvgSymbol keySymbol={keySymbol} />
40
41
  } else if (symbolShape === 'pattern') {
41
- return <KeySvgPattern mapStyle={mapStyle} keyDefinition={keyDefinition} />
42
+ return <KeySvgPattern mapStyle={mapStyle} keyDefinition={keyDefinition} patternRegistry={patternRegistry} />
42
43
  } else if (symbolShape === 'line') {
43
44
  return <KeySvgLine mapStyle={mapStyle} keyDefinition={keyDefinition} />
44
- } else if (symbolShape === HORIZONTAL_RAMP) {
45
- return <KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />
45
+ } else if (symbolShape === RAMP) {
46
+ return <KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} patternRegistry={patternRegistry} />
46
47
  } else {
47
48
  return <KeySvgRect mapStyle={mapStyle} keyDefinition={keyDefinition} />
48
49
  }
@@ -1,9 +1,10 @@
1
1
  import { render } from '@testing-library/react'
2
2
  import { KeySvg } from './KeySvg'
3
+ import { createSymbolRegistry } from '../../../../../src/services/symbolRegistry.js'
3
4
 
4
- import { symbolRegistry } from '../../../../../src/services/symbolRegistry.js'
5
+ const symbolRegistry = createSymbolRegistry()
5
6
 
6
- const getSymbolDef = jest.spyOn(symbolRegistry, 'getSymbolDef')
7
+ const getKeySymbol = jest.spyOn(symbolRegistry, 'getKeySymbol')
7
8
 
8
9
  jest.mock('./KeySvgPattern.jsx', () => ({
9
10
  KeySvgPattern: () => <svg data-testid='key-svg-pattern' />
@@ -22,7 +23,7 @@ jest.mock('./KeySvgRect.jsx', () => ({
22
23
  }))
23
24
 
24
25
  jest.mock('./KeySvgRamp.jsx', () => ({
25
- KeySvgRamp: () => <svg data-testid='key-svg-horizontal-ramp' />
26
+ KeySvgRamp: () => <svg data-testid='key-svg-ramp' />
26
27
  }))
27
28
 
28
29
  const baseKeyDefinition = {
@@ -33,16 +34,17 @@ const baseKeyDefinition = {
33
34
 
34
35
  const baseProps = {
35
36
  mapStyle: { id: 'default' },
36
- keyDefinition: baseKeyDefinition
37
+ keyDefinition: baseKeyDefinition,
38
+ symbolRegistry
37
39
  }
38
40
 
39
41
  beforeEach(() => {
40
- getSymbolDef.mockReturnValue(null)
42
+ getKeySymbol.mockReturnValue(null)
41
43
  })
42
44
 
43
45
  describe('KeySvg', () => {
44
- it('renders KeySvgSymbol when a symbolDef is resolved', () => {
45
- getSymbolDef.mockReturnValue({ id: 'marker' })
46
+ it('renders KeySvgSymbol when the style resolves to a key symbol', () => {
47
+ getKeySymbol.mockReturnValue({ svg: '<path/>', viewBox: '0 0 44 44' })
46
48
  const { getByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition, hasSymbol: true }} />)
47
49
  expect(getByTestId('key-svg-symbol')).toBeTruthy()
48
50
  })
@@ -52,36 +54,41 @@ describe('KeySvg', () => {
52
54
  expect(getByTestId('key-svg-pattern')).toBeTruthy()
53
55
  })
54
56
 
55
- it('renders KeySvgLine when keySymbolShape is line and no symbol or pattern', () => {
56
- const { getByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition, style: { keySymbolShape: 'line' } }} />)
57
+ it('renders KeySvgLine for a stroke with no fill, and no symbol or pattern', () => {
58
+ const { getByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition, style: { stroke: '#b58840' } }} />)
57
59
  expect(getByTestId('key-svg-line')).toBeTruthy()
58
60
  })
59
61
 
62
+ it.each(['transparent', 'none', '#ff0000'])('renders KeySvgRect for a stroke with fill %s', (fill) => {
63
+ const { getByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition, style: { stroke: '#b58840', fill } }} />)
64
+ expect(getByTestId('key-svg-rect')).toBeTruthy()
65
+ })
66
+
60
67
  it('renders KeySvgRect as the default fallback', () => {
61
68
  const { getByTestId } = render(<KeySvg {...baseProps} />)
62
69
  expect(getByTestId('key-svg-rect')).toBeTruthy()
63
70
  })
64
71
 
65
72
  it('prefers symbol over pattern when both are present', () => {
66
- getSymbolDef.mockReturnValue({ id: 'marker' })
73
+ getKeySymbol.mockReturnValue({ svg: '<path/>', viewBox: '0 0 44 44' })
67
74
  const { getByTestId, queryByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition, hasSymbol: true, hasPattern: true }} />)
68
75
  expect(getByTestId('key-svg-symbol')).toBeTruthy()
69
76
  expect(queryByTestId('key-svg-pattern')).toBeNull()
70
77
  })
71
78
 
72
79
  it('prefers pattern over line when both conditions are met', () => {
73
- const { getByTestId, queryByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition, hasPattern: true, style: { keySymbolShape: 'line' } }} />)
80
+ const { getByTestId, queryByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition, hasPattern: true, style: { stroke: '#b58840' } }} />)
74
81
  expect(getByTestId('key-svg-pattern')).toBeTruthy()
75
82
  expect(queryByTestId('key-svg-line')).toBeNull()
76
83
  })
77
84
 
78
- it('renders KeySvgRect when keySymbolShape is not line and no symbol or pattern', () => {
79
- const { getByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition, style: { keySymbolShape: 'polygon' } }} />)
85
+ it('renders KeySvgRect for a fill with no stroke', () => {
86
+ const { getByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition, style: { fill: '#ff0000' } }} />)
80
87
  expect(getByTestId('key-svg-rect')).toBeTruthy()
81
88
  })
82
89
 
83
- it('does not render KeySvgSymbol when hasSymbol is true but getSymbolDef returns null', () => {
84
- getSymbolDef.mockReturnValue(null)
90
+ it('does not render KeySvgSymbol when hasSymbol is true but there is no key symbol', () => {
91
+ getKeySymbol.mockReturnValue(null)
85
92
  const { getByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition, hasSymbol: true }} />)
86
93
  expect(getByTestId('key-svg-rect')).toBeTruthy()
87
94
  })
@@ -91,8 +98,8 @@ describe('KeySvg', () => {
91
98
  expect(container.firstChild).toBeNull()
92
99
  })
93
100
 
94
- it('renders KeySvgRamp when groupStyle is horizontal-ramp', () => {
95
- const { getByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition }} groupStyle='horizontal-ramp' />)
96
- expect(getByTestId('key-svg-horizontal-ramp')).toBeTruthy()
101
+ it('renders KeySvgRamp when groupStyle is ramp', () => {
102
+ const { getByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition }} groupStyle='ramp' />)
103
+ expect(getByTestId('key-svg-ramp')).toBeTruthy()
97
104
  })
98
105
  })
@@ -1,8 +1,7 @@
1
1
  import { svgProps } from './svgProperties.js'
2
- import { patternRegistry } from '../../registry/index.js'
3
2
  const PATTERN_INSET = 2
4
3
 
5
- export const KeySvgPattern = ({ keyDefinition, mapStyle }) => {
4
+ export const KeySvgPattern = ({ keyDefinition, mapStyle, patternRegistry }) => {
6
5
  const { style } = keyDefinition
7
6
  const paths = patternRegistry.getKeyPatternPaths(style, mapStyle.id)
8
7
 
@@ -1,6 +1,8 @@
1
1
  import { render } from '@testing-library/react'
2
2
  import { KeySvgPattern } from './KeySvgPattern'
3
- import { patternRegistry } from '../../../../../src/services/patternRegistry.js'
3
+ import { createPatternRegistry } from '../../../../../src/services/patternRegistry.js'
4
+
5
+ const patternRegistry = createPatternRegistry()
4
6
 
5
7
  const getKeyPatternPaths = jest.spyOn(patternRegistry, 'getKeyPatternPaths')
6
8
  const defaultProps = {
@@ -1,13 +1,42 @@
1
+ import { useId } from 'react'
1
2
  import { getValueForStyle } from '../../../../../src/utils/getValueForStyle.js'
2
3
 
3
- export const KeySvgRamp = ({ mapStyle, keyDefinition }) => {
4
- const { style } = keyDefinition
5
- const fill = getValueForStyle(style.fill, mapStyle.id)
6
- const stroke = getValueForStyle(style.fill, mapStyle.id)
4
+ // Matches the 2px border and 16×16 tile inset of the standard pattern key symbol.
5
+ const BORDER_WIDTH = 2
6
+ const PATTERN_TILE_SIZE = 16
7
+
8
+ // No viewBox: the band stretches to its column's width, and a viewBox would scale the
9
+ // pattern tile and stroke with it. Shapes and userSpaceOnUse tiles resolve in real pixels.
10
+ export const KeySvgRamp = ({ mapStyle, keyDefinition, patternRegistry }) => {
11
+ const { style, hasPattern } = keyDefinition
12
+ const patternId = 'im-c-map-key-ramp-' + useId().replace(/[^\w-]/g, '')
13
+ const paths = hasPattern ? patternRegistry.getKeyPatternPaths(style, mapStyle.id) : null
14
+ const stroke = getValueForStyle(style.stroke, mapStyle.id)
15
+ const fill = paths ? `url(#${patternId})` : getValueForStyle(style.fill, mapStyle.id)
7
16
 
8
17
  return (
9
- <svg viewBox='0 0 5 5' preserveAspectRatio='none' fill={fill} stroke={stroke} strokeWidth='1'>
10
- <path d='M0 0h5v5H0z' />
18
+ <svg xmlns='http://www.w3.org/2000/svg' className='im-c-map-key-symbol' aria-hidden='true' focusable='false'>
19
+ {paths && (
20
+ <defs>
21
+ <pattern
22
+ id={patternId}
23
+ patternUnits='userSpaceOnUse'
24
+ x={BORDER_WIDTH}
25
+ y={BORDER_WIDTH}
26
+ width={PATTERN_TILE_SIZE}
27
+ height={PATTERN_TILE_SIZE}
28
+ dangerouslySetInnerHTML={{ __html: paths.content }}
29
+ />
30
+ </defs>
31
+ )}
32
+ {/* The stroke is drawn at double width on a flush rect; the svg clips the outer half */}
33
+ <rect
34
+ width='100%'
35
+ height='100%'
36
+ fill={fill || 'none'}
37
+ stroke={stroke}
38
+ strokeWidth={stroke ? BORDER_WIDTH * 2 : undefined}
39
+ />
11
40
  </svg>
12
41
  )
13
42
  }
@@ -7,37 +7,77 @@ jest.mock('../../../../../src/utils/getValueForStyle.js', () => ({
7
7
  }))
8
8
 
9
9
  const mapStyle = { id: 'outdoor' }
10
- const keyDefinition = { style: { fill: { outdoor: '#3d9', dark: '#1a5' } } }
10
+ const fillKeyDefinition = { style: { fill: { outdoor: '#3d9', dark: '#1a5' }, stroke: '#123' } }
11
+ const patternKeyDefinition = {
12
+ hasPattern: true,
13
+ style: { fillPattern: 'dot', fillPatternForegroundColor: '#2E7D32' }
14
+ }
15
+ const patternRegistry = {
16
+ getKeyPatternPaths: jest.fn(() => ({ border: '<path/>', content: '<path d="M0 0h1v1H0z" fill="#2E7D32"/>' }))
17
+ }
11
18
 
12
19
  beforeEach(() => {
20
+ jest.clearAllMocks()
13
21
  getValueForStyle.mockImplementation((value) =>
14
22
  typeof value === 'object' ? value.outdoor : value
15
23
  )
16
24
  })
17
25
 
26
+ const renderRamp = (keyDefinition) =>
27
+ render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} patternRegistry={patternRegistry} />)
28
+
18
29
  describe('KeySvgRamp', () => {
19
- it('renders an svg element', () => {
20
- const { container } = render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />)
21
- expect(container.querySelector('svg')).toBeTruthy()
30
+ it('renders an svg without a viewBox, so the band stretches without distortion', () => {
31
+ const { container } = renderRamp(fillKeyDefinition)
32
+ const svg = container.querySelector('svg')
33
+ expect(svg.getAttribute('viewBox')).toBeNull()
34
+ expect(svg.getAttribute('class')).toBe('im-c-map-key-symbol')
35
+ })
36
+
37
+ it('fills the rect with the resolved fill and strokes it with the resolved stroke', () => {
38
+ const { container } = renderRamp(fillKeyDefinition)
39
+ const rect = container.querySelector('rect')
40
+ expect(rect.getAttribute('fill')).toBe('#3d9')
41
+ expect(rect.getAttribute('stroke')).toBe('#123')
42
+ expect(rect.getAttribute('stroke-width')).toBe('4')
43
+ })
44
+
45
+ it('omits the stroke when none is resolved', () => {
46
+ const { container } = renderRamp({ style: { fill: '#3d9' } })
47
+ const rect = container.querySelector('rect')
48
+ expect(rect.getAttribute('stroke')).toBeNull()
49
+ expect(rect.getAttribute('stroke-width')).toBeNull()
22
50
  })
23
51
 
24
- it('passes the resolved fill to the svg', () => {
25
- const { container } = render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />)
26
- expect(container.querySelector('svg').getAttribute('fill')).toBe('#3d9')
52
+ it('fills the rect with a userSpaceOnUse pattern tile when the item has a pattern', () => {
53
+ const { container } = renderRamp(patternKeyDefinition)
54
+ const pattern = container.querySelector('pattern')
55
+ const rect = container.querySelector('rect')
56
+ expect(patternRegistry.getKeyPatternPaths).toHaveBeenCalledWith(patternKeyDefinition.style, 'outdoor')
57
+ expect(pattern.getAttribute('patternUnits')).toBe('userSpaceOnUse')
58
+ expect(pattern.getAttribute('width')).toBe('16')
59
+ expect(pattern.querySelector('path')).toBeTruthy()
60
+ expect(rect.getAttribute('fill')).toBe(`url(#${pattern.id})`)
27
61
  })
28
62
 
29
- it('passes the resolved stroke to the svg', () => {
30
- const { container } = render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />)
31
- expect(container.querySelector('svg').getAttribute('stroke')).toBe('#3d9')
63
+ it('draws no stroke on a pattern without a stroke', () => {
64
+ const { container } = renderRamp(patternKeyDefinition)
65
+ expect(container.querySelector('rect').getAttribute('stroke')).toBeNull()
32
66
  })
33
67
 
34
- it('uses preserveAspectRatio none', () => {
35
- const { container } = render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />)
36
- expect(container.querySelector('svg').getAttribute('preserveAspectRatio')).toBe('none')
68
+ it('gives each band its own pattern id', () => {
69
+ const { container } = render(
70
+ <>
71
+ <KeySvgRamp mapStyle={mapStyle} keyDefinition={patternKeyDefinition} patternRegistry={patternRegistry} />
72
+ <KeySvgRamp mapStyle={mapStyle} keyDefinition={patternKeyDefinition} patternRegistry={patternRegistry} />
73
+ </>
74
+ )
75
+ const [a, b] = container.querySelectorAll('pattern')
76
+ expect(a.id).not.toBe(b.id)
37
77
  })
38
78
 
39
- it('renders a path child', () => {
40
- const { container } = render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />)
41
- expect(container.querySelector('path')).toBeTruthy()
79
+ it('uses no fill when neither a fill nor a pattern is set', () => {
80
+ const { container } = renderRamp({ style: { stroke: '#123' } })
81
+ expect(container.querySelector('rect').getAttribute('fill')).toBe('none')
42
82
  })
43
83
  })
@@ -1,21 +1,35 @@
1
- import { getSymbolStyleColors, getSymbolViewBox } from '../../../../../src/utils/symbolUtils.js'
2
- import { svgSymbolProps } from './svgProperties.js'
3
- import { symbolRegistry } from '../../registry/index.js'
1
+ import { svgSymbolProps, SVG_SYMBOL_SIZE, KEY_SYMBOL_MAX_SIZE } from './svgProperties.js'
4
2
 
5
- export const KeySvgSymbol = ({ keyDefinition, mapStyle, symbolDef }) => {
6
- const { style } = keyDefinition
7
- const mapColorScheme = mapStyle?.appColorScheme ?? 'light'
8
- const keyMapStyle = { ...mapStyle, mapColorScheme }
9
- const resolvedSvg = symbolRegistry.resolve(symbolDef, getSymbolStyleColors(style), keyMapStyle)
10
- const viewBox = getSymbolViewBox(style, symbolDef)
3
+ const HALF = 0.5
11
4
 
12
- if (!(resolvedSvg && viewBox)) {
5
+ // keySymbol is symbolRegistry.getKeySymbol's { svg, viewBox }
6
+ export const KeySvgSymbol = ({ keySymbol }) => {
7
+ const { svg, viewBox } = keySymbol ?? {}
8
+ if (!(svg && viewBox)) {
13
9
  return null
14
10
  }
15
11
 
12
+ // Drawn 1:1 (one viewBox unit per pixel, as on the map) and centred in the key's fixed square.
13
+ // Symbols vary in size (pin's tail, triangle's width), so the square lets them overflow it
14
+ // rather than shrinking the larger ones; only a symbol bigger than KEY_SYMBOL_MAX_SIZE is
15
+ // scaled down. The nested svg clips to the symbol's own viewBox, the same as on the map.
16
+ const [,, width, height] = viewBox.split(' ').map(Number)
17
+ const scale = Math.min(1, KEY_SYMBOL_MAX_SIZE / Math.max(width, height))
18
+ const drawnWidth = width * scale
19
+ const drawnHeight = height * scale
20
+
16
21
  return (
17
- <svg {...svgSymbolProps} viewBox={viewBox}>
18
- <g dangerouslySetInnerHTML={{ __html: resolvedSvg }} />
22
+ <svg {...svgSymbolProps} overflow='visible'>
23
+ <svg
24
+ x={(SVG_SYMBOL_SIZE - drawnWidth) * HALF}
25
+ y={(SVG_SYMBOL_SIZE - drawnHeight) * HALF}
26
+ width={drawnWidth}
27
+ height={drawnHeight}
28
+ viewBox={viewBox}
29
+ overflow='hidden'
30
+ >
31
+ <g dangerouslySetInnerHTML={{ __html: svg }} />
32
+ </svg>
19
33
  </svg>
20
34
  )
21
35
  }
@@ -1,91 +1,41 @@
1
1
  import { render } from '@testing-library/react'
2
2
  import { KeySvgSymbol } from './KeySvgSymbol'
3
- import { getSymbolStyleColors, getSymbolViewBox } from '../../../../../src/utils/symbolUtils.js'
4
- import { symbolRegistry } from '../../registry/index.js'
5
3
 
6
- jest.mock('../../../../../src/utils/symbolUtils.js', () => ({
7
- getSymbolStyleColors: jest.fn(() => ({ foreground: '#000', background: '#fff' })),
8
- getSymbolViewBox: jest.fn(() => '0 0 38 38')
9
- }))
10
-
11
- const mockResolve = jest.spyOn(symbolRegistry, 'resolve')
12
-
13
- const defaultProps = {
14
- symbolDef: { id: 'marker' },
15
- keyDefinition: {
16
- style: {
17
- stroke: '#000000'
18
- }
19
- },
20
- mapStyle: { id: 'default' }
21
- }
22
-
23
- beforeEach(() => {
24
- mockResolve.mockClear()
25
- mockResolve.mockReturnValue('<path d="M0 0"/>')
26
- getSymbolStyleColors.mockReturnValue({ foreground: '#000', background: '#fff' })
27
- getSymbolViewBox.mockReturnValue('0 0 38 38')
28
- })
4
+ const keySymbol = (viewBox, svg = '<path d="M0 0"/>') => ({ svg, viewBox })
29
5
 
30
6
  describe('KeySvgSymbol', () => {
31
- it('renders an svg element', () => {
32
- const { container } = render(<KeySvgSymbol {...defaultProps} />)
33
- expect(container.querySelector('svg')).toBeTruthy()
34
- })
35
-
36
- it('calls symbolRegistry.resolve with the symbolDef, style colors, and mapStyle', () => {
37
- render(<KeySvgSymbol {...defaultProps} />)
38
- expect(mockResolve).toHaveBeenCalledWith(
39
- defaultProps.symbolDef,
40
- { foreground: '#000', background: '#fff' },
41
- expect.objectContaining({ id: 'default' })
42
- )
43
- })
44
-
45
- it('calls getSymbolStyleColors with the dataset props', () => {
46
- render(<KeySvgSymbol {...defaultProps} />)
47
- expect(getSymbolStyleColors).toHaveBeenCalledWith(defaultProps.keyDefinition.style)
7
+ it('draws the symbol 1:1, centred in a fixed 44×44 box it may overflow', () => {
8
+ const { container } = render(<KeySvgSymbol keySymbol={keySymbol('0 0 42 48')} />)
9
+ const [outer, inner] = container.querySelectorAll('svg')
10
+ expect(outer.getAttribute('width')).toBe('44')
11
+ expect(outer.getAttribute('height')).toBe('44')
12
+ expect(outer.getAttribute('viewBox')).toBe('0 0 44 44')
13
+ expect(outer.getAttribute('overflow')).toBe('visible')
14
+ expect([inner.getAttribute('x'), inner.getAttribute('y')]).toEqual(['1', '-2'])
15
+ expect([inner.getAttribute('width'), inner.getAttribute('height')]).toEqual(['42', '48'])
48
16
  })
49
17
 
50
- it('calls getSymbolViewBox with the dataset props and symbolDef', () => {
51
- render(<KeySvgSymbol {...defaultProps} />)
52
- expect(getSymbolViewBox).toHaveBeenCalledWith(defaultProps.keyDefinition.style, defaultProps.symbolDef)
18
+ it('clips the symbol to its own viewBox, as on the map', () => {
19
+ const { container } = render(<KeySvgSymbol keySymbol={keySymbol('10 10 30 30')} />)
20
+ const inner = container.querySelectorAll('svg')[1]
21
+ expect(inner.getAttribute('viewBox')).toBe('10 10 30 30')
22
+ expect(inner.getAttribute('overflow')).toBe('hidden')
53
23
  })
54
24
 
55
- it('sets the viewBox from getSymbolViewBox', () => {
56
- getSymbolViewBox.mockReturnValue('0 0 64 64')
57
- const { container } = render(<KeySvgSymbol {...defaultProps} />)
58
- expect(container.querySelector('svg').getAttribute('viewBox')).toBe('0 0 64 64')
25
+ it('scales a symbol bigger than the key maximum down to fit it', () => {
26
+ const { container } = render(<KeySvgSymbol keySymbol={keySymbol('0 0 112 56')} />)
27
+ const inner = container.querySelectorAll('svg')[1]
28
+ expect([inner.getAttribute('width'), inner.getAttribute('height')]).toEqual(['56', '28'])
29
+ expect([inner.getAttribute('x'), inner.getAttribute('y')]).toEqual(['-6', '8'])
59
30
  })
60
31
 
61
- it('renders the resolved svg html inside a g element', () => {
62
- mockResolve.mockReturnValue('<circle id="sym-el"/>')
63
- const { container } = render(<KeySvgSymbol {...defaultProps} />)
32
+ it('renders the symbol\'s svg inside a g element', () => {
33
+ const { container } = render(<KeySvgSymbol keySymbol={keySymbol('0 0 38 38', '<circle id="sym-el"/>')} />)
64
34
  expect(container.querySelector('#sym-el')).toBeTruthy()
65
35
  })
66
36
 
67
- it('defaults mapColorScheme to light when appColorScheme is not set', () => {
68
- render(<KeySvgSymbol {...defaultProps} />)
69
- const callArg = mockResolve.mock.calls[0][2]
70
- expect(callArg.mapColorScheme).toBe('light')
71
- })
72
-
73
- it('uses the mapStyle appColorScheme when provided', () => {
74
- const props = { ...defaultProps, mapStyle: { id: 'dark', appColorScheme: 'dark' } }
75
- render(<KeySvgSymbol {...props} />)
76
- const callArg = mockResolve.mock.calls[0][2]
77
- expect(callArg.mapColorScheme).toBe('dark')
78
- })
79
-
80
- it('renders nothing when resolve returns falsy', () => {
81
- mockResolve.mockReturnValue(null)
82
- const { container } = render(<KeySvgSymbol {...defaultProps} />)
83
- expect(container.firstChild).toBeNull()
84
- })
85
-
86
- it('renders nothing when getSymbolViewBox returns falsy', () => {
87
- getSymbolViewBox.mockReturnValue(null)
88
- const { container } = render(<KeySvgSymbol {...defaultProps} />)
37
+ it('renders nothing without a key symbol', () => {
38
+ const { container } = render(<KeySvgSymbol keySymbol={null} />)
89
39
  expect(container.firstChild).toBeNull()
90
40
  })
91
41
  })