Files
TREK/client/src/components/Journey/JourneyShareSection.test.tsx
T
Maurice 31859651d7 feat(help): rewrite the help center for v5 with a Tours section, new pictures in every language, and fix the tour planner bugs it surfaced
Every help screen checked against the v5 redesign. Tours is documented for
the first time, as a screen of its own with twelve screens under it, and
notifications get help of their own. All pictures are taken again on a
realistic demo trip, with short loops where a still cannot show the motion.
The English is plain and every language is written naturally rather than
word for word.

The switcher shows two levels at most, a screen's overview lists its
sub-screens as cards above its guides, each wiki page is linked once, and
the help sidebar is wider.

Fixes found along the way: Save to Collection keeps a tour's route, a track
dropped on the places column opens the Tours import, tour Share on desktop,
the unsaved tour draft lives on the account, a route click no longer adds
an end point, Trip places leaves the trip's tours out, Escape closes a
stage's day menu, confirms before destructive deletes, DWD warnings sharing
one id kept apart, a booking's date moves with it, and smaller fixes across
notifications, bookings, the day plan, stays, Atlas, Collections, Vacay and
the admin screens.
2026-10-22 22:29:35 +02:00

249 lines
12 KiB
TypeScript

// FE-JRN-SHARE-001 to FE-JRN-SHARE-014
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { http, HttpResponse, delay } from 'msw'
import { render, screen, waitFor, act } from '../../../tests/helpers/render'
import { server } from '../../../tests/helpers/msw/server'
import JourneyShareSection from './JourneyShareSection'
type ToastKind = 'success' | 'error' | 'warning' | 'info'
const toastSpy = vi.fn((_message: string, _type?: ToastKind, _duration?: number) => 0)
const writeText = vi.fn(async (_text: string) => {})
const JOURNEY_ID = 7
function existingLink(overrides: Record<string, unknown> = {}) {
return { token: 'tok-abc', share_timeline: true, share_gallery: false, share_map: true, ...overrides }
}
beforeEach(() => {
toastSpy.mockClear()
writeText.mockClear()
window.__addToast = toastSpy
Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true, writable: true })
Object.defineProperty(window, 'isSecureContext', { value: true, configurable: true, writable: true })
})
afterEach(() => {
delete window.__addToast
})
describe('JourneyShareSection', () => {
it('FE-JRN-SHARE-001: offers to create a link when the journey has none', async () => {
server.use(http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: null })))
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
expect(await screen.findByRole('button', { name: /create share link/i })).toBeInTheDocument()
expect(screen.getByText('Public Share')).toBeInTheDocument()
})
it('FE-JRN-SHARE-001b: renders nothing at all when the link is not this user to manage', async () => {
// Anyone but the owner gets 403 on the read, and would be refused on the
// create too - so the section stays out of the dialog rather than offering
// a button that cannot work.
server.use(http.get('/api/journeys/7/share-link', () => HttpResponse.json({ error: 'Not allowed' }, { status: 403 })))
const { container } = render(<JourneyShareSection journeyId={JOURNEY_ID} />)
await waitFor(() => expect(container).toBeEmptyDOMElement())
expect(screen.queryByText('Public Share')).not.toBeInTheDocument()
})
it('FE-JRN-SHARE-002: renders nothing while the share link is still loading', async () => {
server.use(http.get('/api/journeys/7/share-link', async () => {
await delay(30)
return HttpResponse.json({ link: null })
}))
const { container } = render(<JourneyShareSection journeyId={JOURNEY_ID} />)
expect(container).toBeEmptyDOMElement()
await screen.findByRole('button', { name: /create share link/i })
})
it('FE-JRN-SHARE-003: creates a link and shows the public URL', async () => {
server.use(
http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: null })),
http.post('/api/journeys/7/share-link', () => HttpResponse.json({ token: 'fresh-token' })),
)
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
const createBtn = await screen.findByRole('button', { name: /create share link/i })
act(() => { createBtn.click() })
expect(await screen.findByText(`${window.location.origin}/public/journey/fresh-token`)).toBeInTheDocument()
expect(toastSpy).toHaveBeenCalledWith('Share link created', 'success', undefined)
})
it('FE-JRN-SHARE-004: warns when creating the link fails', async () => {
server.use(
http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: null })),
http.post('/api/journeys/7/share-link', () => new HttpResponse(null, { status: 500 })),
)
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
const createBtn = await screen.findByRole('button', { name: /create share link/i })
act(() => { createBtn.click() })
await waitFor(() => {
expect(toastSpy).toHaveBeenCalledWith('Failed to create link', 'error', undefined)
})
expect(screen.getByRole('button', { name: /create share link/i })).toBeInTheDocument()
})
it('FE-JRN-SHARE-005: renders the permission toggles in the state the link reports', async () => {
server.use(http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: existingLink() })))
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
const timeline = await screen.findByRole('button', { name: 'Timeline' })
expect(timeline.className).toContain('bg-accent')
expect(screen.getByRole('button', { name: 'Map' }).className).toContain('bg-accent')
expect(screen.getByRole('button', { name: 'Gallery' }).className).not.toContain('bg-accent')
// The state is said, not only shown (#2604).
expect(timeline).toHaveAttribute('aria-pressed', 'true')
expect(screen.getByRole('button', { name: 'Gallery' })).toHaveAttribute('aria-pressed', 'false')
expect(screen.getByText(`${window.location.origin}/public/journey/tok-abc`)).toBeInTheDocument()
})
it('FE-JRN-SHARE-006: copies the URL and resets the button label after two seconds', async () => {
server.use(http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: existingLink() })))
vi.useFakeTimers({ shouldAdvanceTime: true })
try {
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
const copyBtn = await screen.findByRole('button', { name: 'Copy' })
act(() => { copyBtn.click() })
expect(writeText).toHaveBeenCalledWith(`${window.location.origin}/public/journey/tok-abc`)
expect(await screen.findByRole('button', { name: 'Copied!' })).toBeInTheDocument()
act(() => { vi.advanceTimersByTime(2100) })
expect(screen.getByRole('button', { name: 'Copy' })).toBeInTheDocument()
} finally {
vi.useRealTimers()
}
})
it('FE-JRN-SHARE-007: toggling a permission posts the updated flag set', async () => {
const bodies: Record<string, unknown>[] = []
server.use(
http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: existingLink() })),
http.post('/api/journeys/7/share-link', async ({ request }) => {
bodies.push(await request.json() as Record<string, unknown>)
return HttpResponse.json({ token: 'tok-abc' })
}),
)
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
const gallery = await screen.findByRole('button', { name: 'Gallery' })
act(() => { gallery.click() })
await waitFor(() => expect(bodies).toHaveLength(1))
expect(bodies[0]).toEqual({ share_timeline: true, share_gallery: true, share_map: true })
expect(screen.getByRole('button', { name: 'Gallery' }).className).toContain('bg-accent')
})
it('FE-JRN-SHARE-008: reverts the toggle when the update request fails', async () => {
server.use(
http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: existingLink() })),
http.post('/api/journeys/7/share-link', () => new HttpResponse(null, { status: 500 })),
)
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
const timeline = await screen.findByRole('button', { name: 'Timeline' })
act(() => { timeline.click() })
await waitFor(() => {
expect(toastSpy).toHaveBeenCalledWith('Failed to update', 'error', undefined)
})
expect(screen.getByRole('button', { name: 'Timeline' }).className).toContain('bg-accent')
})
it('FE-JRN-SHARE-009: deleting the link returns to the create state', async () => {
server.use(
http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: existingLink() })),
http.delete('/api/journeys/7/share-link', () => HttpResponse.json({ ok: true })),
)
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
const deleteBtn = await screen.findByRole('button', { name: 'Delete link' })
act(() => { deleteBtn.click() })
expect(await screen.findByRole('button', { name: /create share link/i })).toBeInTheDocument()
expect(toastSpy).toHaveBeenCalledWith('Share link deleted', 'success', undefined)
})
it('FE-JRN-SHARE-010: warns and keeps the link when deleting fails', async () => {
server.use(
http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: existingLink() })),
http.delete('/api/journeys/7/share-link', () => new HttpResponse(null, { status: 500 })),
)
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
const deleteBtn = await screen.findByRole('button', { name: 'Delete link' })
act(() => { deleteBtn.click() })
await waitFor(() => {
expect(toastSpy).toHaveBeenCalledWith('Failed to delete', 'error', undefined)
})
expect(screen.getByRole('button', { name: 'Delete link' })).toBeInTheDocument()
})
it('FE-JRN-SHARE-011: falls back to the create state when the link cannot be loaded', async () => {
server.use(http.get('/api/journeys/7/share-link', () => new HttpResponse(null, { status: 500 })))
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
expect(await screen.findByRole('button', { name: /create share link/i })).toBeInTheDocument()
})
// A self-hosted install served over plain HTTP has no navigator.clipboard, and the
// unguarded call used to throw before the button ever showed feedback.
it('FE-JRN-SHARE-012: copies through execCommand when the clipboard API is unavailable', async () => {
server.use(http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: existingLink() })))
Object.defineProperty(navigator, 'clipboard', { value: undefined, configurable: true, writable: true })
Object.defineProperty(window, 'isSecureContext', { value: false, configurable: true, writable: true })
const execCommand = vi.fn(() => true)
Object.defineProperty(document, 'execCommand', { value: execCommand, configurable: true, writable: true })
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
const copyBtn = await screen.findByRole('button', { name: 'Copy' })
act(() => { copyBtn.click() })
await waitFor(() => expect(execCommand).toHaveBeenCalledWith('copy'))
expect(await screen.findByRole('button', { name: 'Copied!' })).toBeInTheDocument()
// The temporary textarea is removed again.
expect(document.querySelector('textarea')).toBeNull()
})
it('FE-JRN-SHARE-013: the trip plan and the recorded routes switch on under the map, each on its own (JRN-04)', async () => {
const bodies: Record<string, unknown>[] = []
server.use(
http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: existingLink() })),
http.post('/api/journeys/7/share-link', async ({ request }) => {
bodies.push(await request.json() as Record<string, unknown>)
return HttpResponse.json({ token: 'tok-abc' })
}),
)
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
const plan = await screen.findByRole('button', { name: /Trip plan/ })
expect(plan).toHaveAttribute('aria-pressed', 'false')
act(() => { plan.click() })
await waitFor(() => expect(bodies).toHaveLength(1))
expect(bodies[0]).toMatchObject({ share_plan: true })
expect(bodies[0]).not.toHaveProperty('share_recordings')
expect(screen.getByRole('button', { name: /Trip plan/ })).toHaveAttribute('aria-pressed', 'true')
})
it('FE-JRN-SHARE-014: without the map shared the map extras are dimmed and cannot be switched', async () => {
server.use(http.get('/api/journeys/7/share-link', () => HttpResponse.json({ link: existingLink({ share_map: false, share_plan: true }) })))
render(<JourneyShareSection journeyId={JOURNEY_ID} />)
const plan = await screen.findByRole('button', { name: /Trip plan/ })
expect(plan).toBeDisabled()
expect(plan).toHaveAttribute('aria-pressed', 'false')
expect(screen.getByRole('button', { name: /Recorded routes/ })).toBeDisabled()
})
})