Files
TREK/client/src/utils/fileShare.test.ts
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

96 lines
4.3 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { canShareFile, canShareText, shareFile, shareText } from './fileShare';
const gpx = () => new File(['<gpx/>'], 'Kramer.gpx', { type: 'application/gpx+xml' });
function stubShare(share: unknown, canShare: unknown) {
Object.defineProperty(navigator, 'share', { value: share, configurable: true });
Object.defineProperty(navigator, 'canShare', { value: canShare, configurable: true });
}
afterEach(() => stubShare(undefined, undefined));
describe('canShareFile (IO-01)', () => {
it('asks the platform about this very kind of file', () => {
const canShare = vi.fn((data: ShareData) => data.files?.[0]?.name.endsWith('.gpx') === true);
stubShare(vi.fn(), canShare);
expect(canShareFile('tour.gpx', 'application/gpx+xml')).toBe(true);
expect(canShare.mock.calls[0][0].files?.[0]).toMatchObject({ name: 'tour.gpx', type: 'application/gpx+xml' });
canShare.mockReturnValue(false);
expect(canShareFile('tour.gpx', 'application/gpx+xml')).toBe(false);
});
it('is no without the share API, without canShare, or when the question itself throws', () => {
stubShare(undefined, () => true);
expect(canShareFile('tour.gpx', 'application/gpx+xml')).toBe(false);
stubShare(vi.fn(), undefined);
expect(canShareFile('tour.gpx', 'application/gpx+xml')).toBe(false);
stubShare(vi.fn(), () => {
throw new TypeError('files not supported');
});
expect(canShareFile('tour.gpx', 'application/gpx+xml')).toBe(false);
});
});
describe('shareFile (IO-01)', () => {
it('opens the sheet with the file and its title', async () => {
const share = vi.fn().mockResolvedValue(undefined);
stubShare(share, () => true);
const file = gpx();
await expect(shareFile(file, 'Kramer')).resolves.toBe('shared');
expect(share).toHaveBeenCalledWith({ files: [file], title: 'Kramer' });
});
it('tells a closed sheet, a refused tap, an open sheet and any other failure apart', async () => {
const share = vi.fn();
stubShare(share, () => true);
share.mockRejectedValueOnce(new DOMException('closed', 'AbortError'));
await expect(shareFile(gpx(), 'Kramer')).resolves.toBe('cancelled');
share.mockRejectedValueOnce(new DOMException('no activation', 'NotAllowedError'));
await expect(shareFile(gpx(), 'Kramer')).resolves.toBe('needs-gesture');
share.mockRejectedValueOnce(new DOMException('earlier share still open', 'InvalidStateError'));
await expect(shareFile(gpx(), 'Kramer')).resolves.toBe('busy');
share.mockRejectedValueOnce(new DOMException('bad data', 'DataError'));
await expect(shareFile(gpx(), 'Kramer')).resolves.toBe('failed');
share.mockRejectedValueOnce(new Error('odd'));
await expect(shareFile(gpx(), 'Kramer')).resolves.toBe('failed');
});
});
describe('canShareText', () => {
it('asks the platform about a text, and takes a share API without the question as a yes', () => {
const canShare = vi.fn(() => true);
stubShare(vi.fn(), canShare);
expect(canShareText()).toBe(true);
expect(canShare).toHaveBeenCalledWith({ title: 'TREK', text: 'TREK' });
canShare.mockReturnValue(false);
expect(canShareText()).toBe(false);
stubShare(vi.fn(), undefined);
expect(canShareText()).toBe(true);
});
it('is no without the share API, or when the question itself throws', () => {
stubShare(undefined, () => true);
expect(canShareText()).toBe(false);
stubShare(vi.fn(), () => {
throw new TypeError('no');
});
expect(canShareText()).toBe(false);
});
});
describe('shareText', () => {
it('opens the sheet with the title and the text, and reads a refusal like a file share', async () => {
const share = vi.fn().mockResolvedValue(undefined);
stubShare(share, () => true);
await expect(shareText('Kramer', 'Kramer\nHike')).resolves.toBe('shared');
expect(share).toHaveBeenCalledWith({ title: 'Kramer', text: 'Kramer\nHike' });
share.mockRejectedValueOnce(new DOMException('closed', 'AbortError'));
await expect(shareText('Kramer', 'x')).resolves.toBe('cancelled');
share.mockRejectedValueOnce(new DOMException('earlier share still open', 'InvalidStateError'));
await expect(shareText('Kramer', 'x')).resolves.toBe('busy');
share.mockRejectedValueOnce(new Error('odd'));
await expect(shareText('Kramer', 'x')).resolves.toBe('failed');
});
});