forked from OurHike/OurHike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapIcon.test.tsx
More file actions
220 lines (184 loc) · 8.71 KB
/
Copy pathMapIcon.test.tsx
File metadata and controls
220 lines (184 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { describe, it, expect, afterEach } from 'vitest'
import { render, cleanup } from '@testing-library/react'
import { MapIcon } from './MapIcon'
import {
glyphPath,
pinGeometry,
poiGlyphPath,
POI_COLORS,
POI_FALLBACK_COLOR,
PIN_EDGE_COLOR,
PIN_HALO_COLOR,
RIM_DASHES,
UNKNOWN_POI_TYPE,
} from './poiIcons'
import { WARNING_GLYPH } from './warningPin'
import { WARNING_PIN } from '../lib/seriousWarnings'
import {
CLOSURE_BAR_RHYTHM,
CLOSURE_CASING_COLOR,
CLOSURE_COLOR,
CLOSURE_LINE_WIDTH,
} from '../lib/closureStyle'
// The map's pins, drawn in the DOM for the legend (#572).
//
// The whole value of this component is that it is not a second drawing of the
// pin, so what is tested is fidelity rather than appearance: every number in
// the SVG is checked against pinGeometry(), the colours against POI_COLORS,
// the silhouettes against GLYPHS, and the broken rim against RIM_DASHES. A
// test that only asserted "renders a circle" would pass just as happily over
// a legend teaching a symbol the map does not use.
const PIN = pinGeometry(1)
afterEach(cleanup)
function draw(node: React.ReactElement): SVGElement {
const { container } = render(node)
const svg = container.querySelector('svg')
if (svg === null) throw new Error('no icon rendered')
return svg
}
function part(svg: SVGElement, className: string): Element {
const found = svg.querySelector(`.${className}`)
if (found === null) throw new Error(`no .${className} in the icon`)
return found
}
function num(element: Element, attribute: string): number {
return Number(element.getAttribute(attribute))
}
describe('MapIcon: a waypoint pin', () => {
it('takes its disc straight from the pin geometry, in a unit box', () => {
const svg = draw(<MapIcon type="water" />)
expect(svg.getAttribute('viewBox')).toBe('0 0 1 1')
expect(num(part(svg, 'map-icon__disc'), 'r')).toBeCloseTo(PIN.rDisc)
expect(num(part(svg, 'map-icon__disc'), 'cx')).toBeCloseTo(PIN.center)
})
it('puts the dark hairline exactly where the rasteriser puts it', () => {
// buildPinImage inks the outermost `edgeWidth` of the rim dark, so a
// stroke of that width centred half of it inside rOuter is the same band.
const svg = draw(<MapIcon type="water" />)
const edge = part(svg, 'map-icon__edge')
expect(num(edge, 'r')).toBeCloseTo(PIN.rOuter - PIN.edgeWidth / 2)
expect(num(edge, 'stroke-width')).toBeCloseTo(PIN.edgeWidth)
expect(edge.getAttribute('stroke')).toBe(PIN_EDGE_COLOR)
})
it('overlaps the halo under both its neighbours, so no seam can show', () => {
// The rasteriser picks one colour per pixel and its bands simply abut.
// SVG antialiases each shape against what is behind it, so two shapes that
// merely touch leave a hairline of page between them. Asserted as bounds
// rather than as exact numbers - how much bleed is a tuning question, that
// there is some is not.
const svg = draw(<MapIcon type="water" />)
const halo = part(svg, 'map-icon__halo')
const inner = num(halo, 'r') - num(halo, 'stroke-width') / 2
const outer = num(halo, 'r') + num(halo, 'stroke-width') / 2
expect(inner).toBeLessThan(PIN.rDisc)
expect(outer).toBeGreaterThanOrEqual(PIN.rOuter - PIN.edgeWidth)
expect(halo.getAttribute('stroke')).toBe(PIN_HALO_COLOR)
})
it('draws the glyph in the box the rasteriser samples it in', () => {
// Which is what keeps a glyph's corners off the halo: glyphBox is derived
// from rDisc so its half-diagonal stays inside the disc.
const svg = draw(<MapIcon type="shelter" />)
const glyph = part(svg, 'map-icon__glyph')
const numbers = (glyph.closest('g')?.getAttribute('transform') ?? '')
.match(/-?\d+\.?\d*/g)
?.map(Number)
expect(numbers).toHaveLength(3)
const [tx, ty, scale] = numbers ?? []
expect(scale).toBeCloseTo(PIN.glyphBox)
expect(tx).toBeCloseTo(PIN.center - PIN.glyphBox / 2)
expect(ty).toBeCloseTo(PIN.center - PIN.glyphBox / 2)
})
it('fills the glyph even-odd, which is what keeps the doorway open', () => {
const svg = draw(<MapIcon type="shelter" />)
expect(part(svg, 'map-icon__glyph').getAttribute('fill-rule')).toBe('evenodd')
})
it.each(Object.keys(POI_COLORS))('draws %s in its own map colour and shape', (type) => {
const svg = draw(<MapIcon type={type} />)
expect(part(svg, 'map-icon__disc').getAttribute('fill')).toBe(
POI_COLORS[type as keyof typeof POI_COLORS],
)
expect(part(svg, 'map-icon__glyph').getAttribute('d')).toBe(poiGlyphPath(type))
})
it('falls back to the neutral diamond for a type this build never heard of', () => {
// The map draws an unrecognised category as a neutral pin rather than as
// nothing, so a release that adds one upstream looks unfamiliar here
// instead of silently missing a row's icon.
const svg = draw(<MapIcon type="hot_springs" />)
expect(part(svg, 'map-icon__disc').getAttribute('fill')).toBe(POI_FALLBACK_COLOR)
expect(part(svg, 'map-icon__glyph').getAttribute('d')).toBe(
poiGlyphPath(UNKNOWN_POI_TYPE),
)
})
it('is decorative, because whatever carries it names the category in text', () => {
const svg = draw(<MapIcon type="water" />)
expect(svg.getAttribute('aria-hidden')).toBe('true')
expect(svg.getAttribute('focusable')).toBe('false')
})
})
describe('MapIcon: the rim says whether anyone has verified the waypoint', () => {
it('leaves a verified rim solid, with no dash pattern at all', () => {
// Absent rather than a solid-looking pattern, so "unbroken" is visible in
// the DOM instead of being a number someone has to evaluate.
const svg = draw(<MapIcon type="water" confidence="high" />)
expect(part(svg, 'map-icon__edge')).not.toHaveAttribute('stroke-dasharray')
expect(part(svg, 'map-icon__halo')).not.toHaveAttribute('stroke-dasharray')
})
it.each(['map-icon__edge', 'map-icon__halo'])(
'breaks %s into the rasteriser’s own rhythm when nobody has verified it',
(className) => {
// buildPinImage inks the rim where floor(turns * RIM_DASHES * 2) is
// even: sixteen equal arcs, alternating, around the full turn. Checked
// against this ring's own circumference rather than against a literal,
// which is what makes it the same rhythm and not a similar one.
const svg = draw(<MapIcon type="water" confidence="low" />)
const ring = part(svg, className)
const [dash, gap] = (ring.getAttribute('stroke-dasharray') ?? '')
.split(' ')
.map(Number)
expect(dash).toBeCloseTo(gap)
expect(dash * RIM_DASHES * 2).toBeCloseTo(2 * Math.PI * num(ring, 'r'))
},
)
})
describe('MapIcon: a serious warning', () => {
it('is the hollow hazard triangle, in the warning pin’s own red', () => {
const svg = draw(<MapIcon type="serious-warning" />)
expect(part(svg, 'map-icon__disc').getAttribute('fill')).toBe(WARNING_PIN.color)
expect(part(svg, 'map-icon__glyph').getAttribute('d')).toBe(glyphPath(WARNING_GLYPH))
})
it('keeps a solid rim even when asked for an unverified one', () => {
// `serious` is set by a moderator and never self-declared, so a warning
// that reaches this pin has been looked at by a person. A broken rim means
// "nobody has verified this", which is the opposite of the one thing that
// is certainly true about it.
const svg = draw(<MapIcon type="serious-warning" confidence="low" />)
expect(part(svg, 'map-icon__edge')).not.toHaveAttribute('stroke-dasharray')
})
})
describe('MapIcon: a closure', () => {
it('is the barred band the map draws, not a pin', () => {
const svg = draw(<MapIcon type="closure" />)
expect(part(svg, 'map-icon__closure-band').getAttribute('stroke')).toBe(CLOSURE_COLOR)
expect(part(svg, 'map-icon__closure-casing').getAttribute('fill')).toBe(
CLOSURE_CASING_COLOR,
)
expect(svg.querySelector('.map-icon__disc')).toBeNull()
})
it('bars it to the map’s own rhythm, converted out of line-width units', () => {
// MapLibre's dasharray is in line-width units and SVG's is in user units.
// The viewBox is drawn in line-width units so the conversion happens once.
const svg = draw(<MapIcon type="closure" />)
const dashes = (
part(svg, 'map-icon__closure-band').getAttribute('stroke-dasharray') ?? ''
)
.split(' ')
.map(Number)
expect(dashes).toEqual(CLOSURE_BAR_RHYTHM.map((unit) => unit * CLOSURE_LINE_WIDTH))
})
it('keeps the casing continuous, so the bars’ gaps show it through', () => {
// The band is dashed and the casing is not - which is what makes this read
// as barrier tape rather than as a dashed red trail line.
const svg = draw(<MapIcon type="closure" />)
expect(part(svg, 'map-icon__closure-casing')).not.toHaveAttribute('stroke-dasharray')
})
})