forked from OurHike/OurHike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapScreen.test.tsx
More file actions
741 lines (607 loc) · 25.9 KB
/
Copy pathMapScreen.test.tsx
File metadata and controls
741 lines (607 loc) · 25.9 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { render, screen, cleanup, act, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MockMap, ScaleControl, resetMapLibreMock } from '../test/mocks/maplibre-gl'
import { MapScreen } from './MapScreen'
import {
mapCredits,
OPENFREEMAP_CREDIT,
OSM_CREDIT,
USGS_TOPO_CREDIT,
} from '../map/credits'
import { closureFeatureCollection, CLOSURE_SOURCE_ID } from '../map/closureLayers'
import { warningFeatureCollection, WARNING_SOURCE_ID } from '../map/warningLayers'
import { HEALTHY, type SourceReport } from '../map/liveSourceHealth'
// WIREFRAMES.md's map screen, top to bottom: status strip, header, elevation
// ribbon, waypoint lanes, map canvas, tab bar - plus the legend sheet over the
// top of it. This covers the shell holding them together and the one thing
// that must be on screen no matter what: attribution. USGS topo is public
// domain, but OSM is ODbL and its credit is a licence condition, not a nicety,
// so it is asserted here rather than left to whichever component renders last.
vi.mock('maplibre-gl', () => import('../test/mocks/maplibre-gl'))
vi.mock('../map/protocol', () => ({
PMTILES_SCHEME: 'pmtiles',
registerPMTilesProtocol: vi.fn(),
}))
const PROPS = {
topoArchiveUrl: 'pmtiles://ourhike-corridor',
trailsUrl: '/data/trails.geojson',
trailName: 'Appalachian Trail',
state: 'Virginia',
position: 'mi 1,407.2 · NOBO',
time: new Date('2026-07-29T12:00:00'),
online: false,
hasGpsFix: true,
lastSyncedAt: new Date('2026-07-29T09:00:00'),
activeTab: 'trail' as const,
onSelectTab: vi.fn(),
onOpenLegend: vi.fn(),
onOpenSearch: vi.fn(),
legendOpen: false,
onCloseLegend: vi.fn(),
bbox: { west: -78, south: 39, east: -77, north: 40 },
viewportPoints: [
{ id: 'w1', type: 'water', lat: 39.5, lon: -77.5, confidence: 'high' as const },
],
blazeCounts: [{ blaze: 'White', count: 12 }],
hiddenTypes: new Set<string>(),
onToggleType: vi.fn(),
verifiedOnly: false,
onToggleVerifiedOnly: vi.fn(),
searchOpen: false,
onCloseSearch: vi.fn(),
searchablePois: [
{ id: 's1', name: 'Rocky Run Shelter', type: 'shelter', mile: 1043.2 },
],
onSelectSearchResult: vi.fn(),
selectedPoi: null,
onSelectPoi: vi.fn(),
onClosePoi: vi.fn(),
elevation: {
samples: [
{ mile: 1400, elevationFt: 1200 },
{ mile: 1410, elevationFt: 1800 },
],
currentMile: 1405,
},
waypoints: {
points: [{ id: 'w1', type: 'water', mile: 1402 }],
startMile: 1400,
endMile: 1410,
},
}
beforeEach(() => {
resetMapLibreMock()
})
afterEach(() => {
cleanup()
vi.clearAllMocks()
})
describe('MapScreen', () => {
it('stacks the status strip, header, map and tab bar together', () => {
render(<MapScreen {...PROPS} />)
expect(screen.getByText(/12:00/)).toBeInTheDocument()
expect(screen.getByRole('banner')).toBeInTheDocument()
expect(screen.getByRole('region', { name: /trail map/i })).toBeInTheDocument()
expect(screen.getByRole('tablist')).toBeInTheDocument()
})
it('always renders the attribution - OSM credit is an ODbL condition, not a nicety', () => {
render(<MapScreen {...PROPS} background="usgs_topo_offline" />)
expect(screen.getByText(OSM_CREDIT, { exact: false })).toBeInTheDocument()
})
it('credits the extra licences the live background brings with it', () => {
// The live sheet adds two more conditions of use - OpenFreeMap's terms for
// the hosting and AWS Terrain Tiles' attribution requirement for the
// elevation - so the corner has to grow when the background changes. A
// fixed string here would have gone quietly out of date the day the
// default flipped.
render(<MapScreen {...PROPS} background="hiking_topo_live" />)
for (const credit of mapCredits({ background: 'hiking_topo_live' })) {
expect(screen.getByText(credit, { exact: false })).toBeInTheDocument()
}
})
it('credits the live sheet by default, because that is the default background', () => {
render(<MapScreen {...PROPS} />)
expect(screen.getByText(OPENFREEMAP_CREDIT, { exact: false })).toBeInTheDocument()
})
it('does not credit the USGS survey on a phone that has none of it', () => {
// The corner used to name USGS US Topo unconditionally, because the string
// was composed from what the app CAN draw. On a fresh install that is a
// credit for a 314 MB archive nobody has downloaded, printed over a map
// drawn entirely from other people's tiles.
render(<MapScreen {...PROPS} hasRasterArchive={false} />)
expect(screen.queryByText(USGS_TOPO_CREDIT, { exact: false })).not.toBeInTheDocument()
})
it('credits the USGS survey once the corridor raster is on the phone', () => {
render(<MapScreen {...PROPS} hasRasterArchive />)
expect(screen.getByText(USGS_TOPO_CREDIT, { exact: false })).toBeInTheDocument()
})
it('never says one credit twice, however the background and the download line up', () => {
// The bug in its most visible form: two composed strings each correctly
// named OpenStreetMap, so the live corner printed it twice.
render(<MapScreen {...PROPS} background="hiking_topo_live" hasRasterArchive />)
expect(screen.getAllByText(OSM_CREDIT, { exact: false })).toHaveLength(1)
})
it('surfaces the offline state it was given', () => {
render(<MapScreen {...PROPS} />)
expect(screen.getByText(/offline/i)).toBeInTheDocument()
})
it('passes tab selection up to its owner', async () => {
const user = userEvent.setup()
render(<MapScreen {...PROPS} />)
await user.click(screen.getByRole('tab', { name: 'More' }))
expect(PROPS.onSelectTab).toHaveBeenCalledWith('more')
})
it('slots the elevation ribbon and waypoint lanes above the canvas', () => {
render(<MapScreen {...PROPS} />)
expect(screen.getByRole('img', { name: /elevation profile/i })).toBeInTheDocument()
expect(screen.getByTestId('lane-water')).toBeInTheDocument()
})
// #619. One preference, one prop, two consumers. The failure this guards is
// not a crash: it is a canvas whose scale bar and contour interval read in
// metres under a ribbon still labelled in feet, which nothing but a test
// holding both at once would catch - each half is right on its own.
it('sends one unit preference to the canvas and to the ribbon over it', () => {
render(<MapScreen {...PROPS} units="metric" />)
const [map] = MockMap.live
const scale = map.controls
.map((c) => c.control)
.find((c) => c instanceof ScaleControl)
expect(scale?.options?.unit).toBe('metric')
// The ribbon's low mark: 1,200 ft is 366 m.
expect(screen.getByText(/366 m/)).toBeInTheDocument()
})
it('draws both in feet for a hiker who has not changed the default', () => {
render(<MapScreen {...PROPS} />)
const [map] = MockMap.live
const scale = map.controls
.map((c) => c.control)
.find((c) => c instanceof ScaleControl)
expect(scale?.options?.unit).toBe('imperial')
expect(screen.getByText(/1,200 ft/)).toBeInTheDocument()
})
it('omits the ribbon and lanes rather than showing empty ones when there is no data', () => {
// An empty ribbon reads as "nothing ahead of you", which is a different and
// much worse claim than "we don't have the profile for this stretch."
render(<MapScreen {...PROPS} elevation={undefined} waypoints={undefined} />)
expect(
screen.queryByRole('img', { name: /elevation profile/i }),
).not.toBeInTheDocument()
expect(screen.queryByTestId('lane-water')).not.toBeInTheDocument()
})
it('keeps the legend sheet closed until it is asked for', () => {
render(<MapScreen {...PROPS} />)
expect(screen.queryByRole('dialog', { name: /legend/i })).not.toBeInTheDocument()
})
it('shows the legend sheet over the map once open', () => {
render(<MapScreen {...PROPS} legendOpen />)
expect(screen.getByRole('dialog', { name: /legend/i })).toBeInTheDocument()
})
it('keeps search out of the way until the header asks for it', () => {
render(<MapScreen {...PROPS} />)
expect(screen.queryByRole('searchbox')).not.toBeInTheDocument()
})
it('lets search take over once opened', () => {
render(<MapScreen {...PROPS} searchOpen />)
expect(screen.getByRole('searchbox')).toBeInTheDocument()
})
it('wires the header buttons to the legend and search handlers', async () => {
const user = userEvent.setup()
render(<MapScreen {...PROPS} />)
await user.click(screen.getByRole('button', { name: /legend/i }))
await user.click(screen.getByRole('button', { name: /search/i }))
expect(PROPS.onOpenLegend).toHaveBeenCalledTimes(1)
expect(PROPS.onOpenSearch).toHaveBeenCalledTimes(1)
})
it('shows no waypoint card until a pin has been tapped', () => {
render(<MapScreen {...PROPS} />)
expect(screen.queryByRole('dialog', { name: /waypoint/i })).not.toBeInTheDocument()
})
it('puts the tapped pin’s detail over the map', () => {
render(
<MapScreen
{...PROPS}
selectedPoi={{
id: 'atc_shelters:abc',
name: 'Rocky Run Shelter',
type: 'shelter',
lat: 39.4,
lon: -77.6,
confidence: 'high',
mile: 1043.2,
}}
/>,
)
expect(screen.getByRole('dialog', { name: /waypoint/i })).toBeInTheDocument()
expect(screen.getByRole('heading', { name: 'Rocky Run Shelter' })).toBeInTheDocument()
})
it('hands the card the live map, so it anchors to the pin it describes', () => {
// The shell above owns the POI data and MapView owns the canvas; this is
// the screen that has to introduce them. A card that never projected the
// POI's coordinates would render docked at the canvas origin - the exact
// bottom-anchored posture this card replaced.
render(
<MapScreen
{...PROPS}
selectedPoi={{
id: 'atc_shelters:abc',
name: 'Rocky Run Shelter',
type: 'shelter',
lat: 39.4,
lon: -77.6,
confidence: 'high',
mile: 1043.2,
}}
/>,
)
expect(MockMap.live).toHaveLength(1)
expect(MockMap.live[0].projectCalls).toContainEqual([-77.6, 39.4])
expect(screen.getByRole('dialog', { name: /waypoint/i }).style.transform).not.toBe('')
})
it('carries the map’s source reports up to the shell', () => {
// Half of a path that used to end here. This screen observed the error
// and decided what it meant; since #334 it does neither, because the
// downloads window needs the same fact and opens over the More tab where
// this screen is not rendered at all. What is left is the wiring, and the
// wiring is worth a test: an unpassed prop is exactly how the flag
// reached nobody before #314.
const reports: SourceReport[] = []
const { unmount } = render(
<MapScreen
{...PROPS}
online
onLiveSourceHealth={(report) => reports.push(report)}
/>,
)
act(() => {
MockMap.live[0].emit('error', {
sourceId: 'osm',
error: new Error('Failed to fetch'),
})
})
expect(reports).toEqual([
{
unreachable: { basemap: true, elevation: false, archive: false },
drew: HEALTHY,
withdrawn: false,
},
])
// And the withdrawal on the way out, flagged as one: the shell remembers
// failures past this screen, so it has to know that this `HEALTHY` is the
// map leaving rather than the sheet arriving.
unmount()
expect(reports.at(-1)).toEqual({
unreachable: HEALTHY,
drew: HEALTHY,
withdrawn: true,
})
})
it('renders the background problem the shell hands it', () => {
// The other half. This screen no longer works out what a failing source
// means - it is told, and it shows it.
render(<MapScreen {...PROPS} online backgroundProblem="live-unreachable" />)
expect(screen.getByText(/no live map/i)).toBeInTheDocument()
})
it('says on the map screen when Data Saver is holding the live sheet back', () => {
render(<MapScreen {...PROPS} backgroundOverride="data-saver" />)
expect(screen.getByText(/data saver/i)).toBeInTheDocument()
})
it('puts the background choice in the legend, one tap from the map', () => {
// It used to live only in Settings, three taps away behind a select. The
// moment someone wants to change the background is the moment the map is
// not showing what they expected, which is the worst moment to send them
// hunting through a settings screen.
render(
<MapScreen
{...PROPS}
legendOpen
backgroundChoice="usgs_topo_offline"
onChangeBackground={vi.fn()}
/>,
)
const legend = screen.getByRole('dialog', { name: /legend/i })
expect(within(legend).getByRole('radio', { name: /live/i })).toBeInTheDocument()
expect(within(legend).getByRole('radio', { name: /downloaded/i })).toBeChecked()
})
it('reports a background change from the legend up to its owner', async () => {
const user = userEvent.setup()
const onChangeBackground = vi.fn()
render(
<MapScreen
{...PROPS}
legendOpen
backgroundChoice="usgs_topo_offline"
onChangeBackground={onChangeBackground}
/>,
)
await user.click(screen.getByRole('radio', { name: /live/i }))
expect(onChangeBackground).toHaveBeenCalledWith('hiking_topo_live')
})
it('carries the way to the download, which is the only one left on this screen', async () => {
// The Downloads tab is gone (chrome/tabs.ts). If this link does not reach
// the shell, a hiker on the map has no route to the download at all.
const user = userEvent.setup()
const onOpenDownloads = vi.fn()
render(
<MapScreen
{...PROPS}
legendOpen
backgroundChoice="usgs_topo_offline"
onChangeBackground={vi.fn()}
onOpenDownloads={onOpenDownloads}
/>,
)
await user.click(screen.getByRole('button', { name: /choose what to download/i }))
expect(onOpenDownloads).toHaveBeenCalledTimes(1)
})
it('draws no picker when the shell has nowhere to write the choice', () => {
// The legend is rendered in tests and stories without a shell behind it,
// and a control that silently discards what it is told is worse than one
// that is not there.
render(<MapScreen {...PROPS} legendOpen />)
expect(screen.queryByRole('radio', { name: /live/i })).not.toBeInTheDocument()
})
})
// --- The safety alert strip (#232) ---------------------------------------
//
// Above the map, because a hiker who is walking has not opened anything: a
// closure that only appears on tapping a red band is a closure they walk
// into.
describe('MapScreen safety alerts', () => {
it('says nothing when there is nothing to say', () => {
render(<MapScreen {...PROPS} />)
expect(screen.queryByRole('alert')).toBe(null)
})
it('shows a closure ahead', () => {
render(
<MapScreen {...PROPS} closureAhead="Trail closed 5.0 mi ahead · Storm damage" />,
)
expect(screen.getByRole('alert')).toHaveTextContent('Trail closed 5.0 mi ahead')
})
it('shows serious warnings on the route', () => {
render(<MapScreen {...PROPS} warningsAhead="2 serious warnings on your route" />)
expect(screen.getByRole('alert')).toHaveTextContent(
'2 serious warnings on your route',
)
})
it('shows both at once rather than picking one', () => {
// A closure and a bear are different problems, and neither substitutes
// for the other.
render(
<MapScreen
{...PROPS}
closureAhead="Trail closed 5.0 mi ahead · Storm damage"
warningsAhead="1 serious warning on your route"
/>,
)
const alert = screen.getByRole('alert')
expect(alert).toHaveTextContent('Trail closed 5.0 mi ahead')
expect(alert).toHaveTextContent('1 serious warning on your route')
})
it('keeps the status strip above it, because the two are read together', () => {
// The strip's sync age is the only thing separating "the way ahead is
// clear" from "we could not check" - an alert area that appeared above
// it would be a claim with no freshness attached.
const { container } = render(
<MapScreen {...PROPS} closureAhead="Trail closed 5.0 mi ahead · Storm damage" />,
)
const strip = container.querySelector('.status-strip')
const alerts = container.querySelector('.map-screen__alerts')
expect(strip).not.toBeNull()
expect(alerts).not.toBeNull()
expect(strip!.compareDocumentPosition(alerts!)).toBe(Node.DOCUMENT_POSITION_FOLLOWING)
})
// The third row (#485). A region-wide advisory is a standing condition, not a
// next action, so it appears without competing for the line above it.
it('shows a broad advisory under the closure rather than instead of it', () => {
// THE CASE #485 REPORTS. Ranked into one line, the advisory scored "inside"
// and the nine-mile closure three miles ahead never appeared at all.
const { container } = render(
<MapScreen
{...PROPS}
closureAhead="Trail closed 3.0 mi ahead · Storm damage · mi 245.0 – 254.0"
advisoryAhead="Advisory along 398 mi of trail · Storm damage · mi 239.4 – 637.8"
/>,
)
const alert = screen.getByRole('alert')
expect(alert).toHaveTextContent('Trail closed 3.0 mi ahead')
expect(alert).toHaveTextContent('Advisory along 398 mi of trail')
// Order is the point, not merely presence: the actionable line is read
// first on a screen glanced at while walking.
const closure = container.querySelector('.map-screen__alert--closure')
const advisory = container.querySelector('.map-screen__alert--advisory')
expect(closure!.compareDocumentPosition(advisory!)).toBe(
Node.DOCUMENT_POSITION_FOLLOWING,
)
})
it('shows an advisory on its own when nothing specific is ahead', () => {
// Otherwise the fix would trade one silence for another: a hiker inside an
// advisory on an otherwise clear stretch has to still be told.
render(
<MapScreen
{...PROPS}
advisoryAhead="Advisory along 398 mi of trail · Storm damage · mi 239.4 – 637.8"
/>,
)
expect(screen.getByRole('alert')).toHaveTextContent('Advisory along 398 mi of trail')
})
it('marks the advisory as its own kind of row', () => {
// The class is what makes it quieter than the two above it (chrome.css).
// Same colour and size - weight only - because this is still a safety line
// on a screen read in direct sun.
const { container } = render(
<MapScreen {...PROPS} advisoryAhead="Advisory along 398 mi of trail" />,
)
expect(container.querySelector('.map-screen__alert--advisory')).not.toBeNull()
expect(container.querySelector('.map-screen__alert--closure')).toBeNull()
})
})
// --- The same two facts on the canvas -------------------------------------
//
// Four props rather than two, and the reason is in MapScreenProps: a banner
// says what is AHEAD of a hiker walking a known direction, and the canvas
// draws what is THERE. Tying them together would leave the map blank until
// the direction tracker had made up its mind.
describe('MapScreen safety overlays', () => {
function loadStyle(map: MockMap): void {
map.sourceIds = [CLOSURE_SOURCE_ID, WARNING_SOURCE_ID]
map.emit('load')
}
it('hands the closure bands to the canvas', () => {
const closures = [{ id: 'c1', lines: [[[-77.1, 39.3] as [number, number]]] }]
render(<MapScreen {...PROPS} closures={closures} />)
const [map] = MockMap.live
loadStyle(map)
expect(map.sourceData.get(CLOSURE_SOURCE_ID)).toEqual(
closureFeatureCollection(closures),
)
})
it('hands the warning pins to the canvas', () => {
const warnings = [{ id: 'r1', lon: -77.2, lat: 39.4 }]
render(<MapScreen {...PROPS} warnings={warnings} />)
const [map] = MockMap.live
loadStyle(map)
expect(map.sourceData.get(WARNING_SOURCE_ID)).toEqual(
warningFeatureCollection(warnings),
)
})
it('draws a closure the banner is silent about', () => {
// The case the four props exist for. `closureAhead` is null before the
// direction tracker settles - and before then the band is the only thing
// telling a hiker the trail is shut.
const closures = [{ id: 'c1', lines: [[[-77.1, 39.3] as [number, number]]] }]
render(<MapScreen {...PROPS} closures={closures} closureAhead={null} />)
const [map] = MockMap.live
loadStyle(map)
expect(screen.queryByRole('alert')).toBe(null)
expect(
(map.sourceData.get(CLOSURE_SOURCE_ID) as { features: unknown[] }).features,
).toHaveLength(1)
})
})
describe('the way to every ATC notice, from the legend (#687)', () => {
// This used to be a permanent button on this screen. It moved into the
// legend - chrome/Legend.test.tsx covers the row itself, so what matters
// here is only that MapScreen hands the count and the handler on honestly.
it('is not there when the app holds no ATC notices', () => {
render(<MapScreen {...PROPS} legendOpen />)
expect(screen.queryByRole('button', { name: /ATC trail update/ })).toBe(null)
})
it('reaches the legend once open', () => {
render(
<MapScreen {...PROPS} legendOpen atcNoticeCount={6} onOpenAtcNotices={vi.fn()} />,
)
const legend = screen.getByRole('dialog', { name: /legend/i })
expect(
within(legend).getByRole('button', { name: 'Read all 6 ATC trail updates' }),
).toBeInTheDocument()
})
it('counts one notice without pluralising it', () => {
render(
<MapScreen {...PROPS} legendOpen atcNoticeCount={1} onOpenAtcNotices={vi.fn()} />,
)
const legend = screen.getByRole('dialog', { name: /legend/i })
expect(
within(legend).getByRole('button', { name: 'Read the 1 ATC trail update' }),
).toBeInTheDocument()
})
it('reports the tap up to the shell, which owns whether the list is open', async () => {
const onOpenAtcNotices = vi.fn()
render(
<MapScreen
{...PROPS}
legendOpen
atcNoticeCount={6}
onOpenAtcNotices={onOpenAtcNotices}
/>,
)
await userEvent.click(screen.getByRole('button', { name: /ATC trail updates/ }))
expect(onOpenAtcNotices).toHaveBeenCalledTimes(1)
})
it('renders the list the shell hands it, over the canvas', () => {
render(
<MapScreen
{...PROPS}
atcNoticeCount={6}
onOpenAtcNotices={vi.fn()}
atcNoticeList={<div data-testid="atc-notice-list" />}
/>,
)
expect(screen.getByTestId('atc-notice-list')).toBeInTheDocument()
})
it('shows nothing until the shell says the list is open', () => {
render(<MapScreen {...PROPS} atcNoticeCount={6} onOpenAtcNotices={vi.fn()} />)
expect(screen.queryByTestId('atc-notice-list')).toBe(null)
})
})
describe('the bottom banner for new ATC alerts (#687)', () => {
// Independent of atcNoticeCount above - a screen can hold six notices and
// none of them new, which is the ordinary case now that the 72-hour gate
// lives in lib/atcAlertsBanner.ts rather than here. MapScreen only renders
// what it is told; the gate itself is that module's own test.
it('is not there when nothing is new', () => {
render(<MapScreen {...PROPS} atcNoticeCount={6} onOpenAtcNotices={vi.fn()} />)
expect(screen.queryByRole('button', { name: /new alerts? issued/i })).toBe(null)
})
it('appears once something is, outside any legend or notice-count prop', () => {
render(<MapScreen {...PROPS} newAtcAlertCount={2} onOpenAtcNotices={vi.fn()} />)
expect(
screen.getByRole('button', { name: 'ATC · 2 new alerts issued' }),
).toBeInTheDocument()
})
it('counts one alert without pluralising it', () => {
render(<MapScreen {...PROPS} newAtcAlertCount={1} onOpenAtcNotices={vi.fn()} />)
expect(
screen.getByRole('button', { name: 'ATC · New alert issued' }),
).toBeInTheDocument()
})
it('opens the same list a tap on the legend row would', async () => {
const onOpenAtcNotices = vi.fn()
render(
<MapScreen {...PROPS} newAtcAlertCount={2} onOpenAtcNotices={onOpenAtcNotices} />,
)
await userEvent.click(screen.getByRole('button', { name: /new alerts issued/ }))
expect(onOpenAtcNotices).toHaveBeenCalledTimes(1)
})
it('offers a silence control that does not also open the list', async () => {
const onOpenAtcNotices = vi.fn()
const onSilenceNewAtcAlerts = vi.fn()
render(
<MapScreen
{...PROPS}
newAtcAlertCount={2}
onOpenAtcNotices={onOpenAtcNotices}
onSilenceNewAtcAlerts={onSilenceNewAtcAlerts}
/>,
)
await userEvent.click(screen.getByRole('button', { name: 'Silence new ATC alerts' }))
expect(onSilenceNewAtcAlerts).toHaveBeenCalledTimes(1)
expect(onOpenAtcNotices).not.toHaveBeenCalled()
})
it('omits the silence control when the shell offers none', () => {
render(<MapScreen {...PROPS} newAtcAlertCount={2} onOpenAtcNotices={vi.fn()} />)
expect(screen.queryByRole('button', { name: /silence/i })).toBe(null)
})
it('is announced politely rather than as a live safety alert', () => {
// role="alert" is reserved for what changes what a hiker does next - the
// strip above the header (#232), which already keeps that role to
// itself and gets no button inside it. This banner is announced instead
// through aria-live="polite", not role="status" - StatusStrip.tsx (the
// "Offline" flag, sync age) already owns that role on this same screen,
// and a second region claiming it would make "the status region"
// ambiguous to a screen reader and to a role query alike.
const { container } = render(
<MapScreen
{...PROPS}
closureAhead="Trail closed 5.0 mi ahead · Storm damage"
newAtcAlertCount={2}
onOpenAtcNotices={vi.fn()}
/>,
)
expect(within(screen.getByRole('alert')).queryByRole('button')).toBe(null)
expect(container.querySelector('.map-screen__new-alerts')).toHaveAttribute(
'aria-live',
'polite',
)
})
})