-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathMapIndexLayer.test.tsx
More file actions
94 lines (81 loc) · 3.46 KB
/
Copy pathMapIndexLayer.test.tsx
File metadata and controls
94 lines (81 loc) · 3.46 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
import { render, screen, within } from '@testing-library/react'
import { MemoryRouter } from 'react-router'
import type { PositionGroup } from '../map-types'
import { MapIndexLayer } from './MapIndexLayer'
// MapIndexLayer only needs the path/color constants out of MapContent, so stub
// them instead of pulling the whole leaflet map module into the test.
vi.mock('../MapContent', () => ({
actualRouteStopMarkerPath: 'actual-marker.png',
plannedRouteStopMarkerPath: 'planned-marker.png',
plannedRouteLineColor: 'black',
}))
const group = (overrides: Partial<PositionGroup>): PositionGroup => ({
positions: [],
color: '#ff0000',
...overrides,
})
const renderLayer = (props: Parameters<typeof MapIndexLayer>[0]) =>
render(
<MemoryRouter>
<MapIndexLayer {...props} />
</MemoryRouter>,
)
describe('MapIndexLayer', () => {
it('deep-links the legend vehicle number to the vehicle page when the ride has a vehicle ref', () => {
renderLayer({
showPlannedRoute: false,
positionGroups: [group({ label: '12-345-67', vehicleRef: '1234567' })],
})
const link = screen.getByRole('link', { name: '12-345-67' })
expect(link).toHaveAttribute('href', '/vehicle?vehicle.vehicleNumber=1234567')
// the number stays bracketed in the legend
expect(link.closest('bdi')).toHaveTextContent('(12-345-67)')
})
it('renders the vehicle number as plain bracketed text (no link) when no vehicle ref is known', () => {
renderLayer({
showPlannedRoute: false,
positionGroups: [group({ label: '99', vehicleRef: undefined })],
})
expect(screen.queryByRole('link')).not.toBeInTheDocument()
expect(screen.getByText('(', { exact: false }).closest('bdi')).toHaveTextContent('(99)')
})
it('renders one actual-route legend row per position group', () => {
const { container } = renderLayer({
showPlannedRoute: false,
positionGroups: [
group({ label: '12-345-67', vehicleRef: '1234567' }),
group({ label: '76-543-21', vehicleRef: '7654321', color: '#00ff00' }),
],
})
expect(container.querySelectorAll('.map-index-item')).toHaveLength(2)
expect(screen.getByRole('link', { name: '12-345-67' })).toHaveAttribute(
'href',
'/vehicle?vehicle.vehicleNumber=1234567',
)
expect(screen.getByRole('link', { name: '76-543-21' })).toHaveAttribute(
'href',
'/vehicle?vehicle.vehicleNumber=7654321',
)
})
it('shows the planned-route row before any ride is selected, and adds the actual row only once groups arrive', () => {
const { container, rerender } = renderLayer({ showPlannedRoute: true, positionGroups: [] })
// only the planned route, no actual-route legend yet
expect(container.querySelectorAll('.map-index-item')).toHaveLength(1)
expect(screen.queryByRole('link')).not.toBeInTheDocument()
rerender(
<MemoryRouter>
<MapIndexLayer
showPlannedRoute
positionGroups={[group({ label: '12-345-67', vehicleRef: '1234567' })]}
/>
</MemoryRouter>,
)
expect(container.querySelectorAll('.map-index-item')).toHaveLength(2)
})
it('omits the subtitle entirely when a group has no label', () => {
renderLayer({ showPlannedRoute: false, positionGroups: [group({ label: undefined })] })
const item = document.querySelector('.map-index-item')!
// title present, but no parenthesised subtitle span
expect(within(item as HTMLElement).queryByText('(', { exact: false })).not.toBeInTheDocument()
})
})