mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 13:36:48 +00:00
80bb06c84f
A list could leave the instance as a TREK file, which only another TREK reads. Export now asks for a format, and GPX is the second one: the places as waypoints, for OsmAnd, Organic Maps, a Garmin or gpx.studio. Import takes a GPX in the same dialog as a list file. Asked for in discussion #2301. The GPX is written from the very list file the JSON export returns, so what may leave the instance is still decided in one place. Name, description and address, notes, website and category go into GPX's own elements, where every app shows them. Labels, status, phone and the rest travel in a TREK extension namespace, so a GPX made by TREK comes back as the same list. A place without coordinates cannot be a waypoint; it is left out and the export says how many were. A GPX is read on the server, sent as text, with the XML library the trip importer already uses. A document with a DOCTYPE is refused before parsing, which closes both external entities and entity expansion. Size is capped like the list file, and the element count is capped before a tree is built. The reader only turns the GPX into a list file: the browser previews it and imports it through the existing route, so one contract, one transaction and one skipped counter cover both formats. Waypoints and named route points become places; tracks and unnamed route points are counted in the preview and not imported. The two list-file routes get a body ceiling of twice the file limit. The global 100 kB cap refused most real favourites files, and it already refused list files well under the megabyte the contract allows.
121 lines
4.4 KiB
TypeScript
121 lines
4.4 KiB
TypeScript
// FE-COMP-COLEXPORT-001 to FE-COMP-COLEXPORT-008
|
|
import React from 'react';
|
|
import { render, screen, waitFor } from '../../../tests/helpers/render';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { useTranslation } from '../../i18n/TranslationContext';
|
|
import CollectionExportMenu from './CollectionExportMenu';
|
|
|
|
type MenuProps = Omit<React.ComponentProps<typeof CollectionExportMenu>, 't'>;
|
|
|
|
function Harness(props: MenuProps): React.ReactElement {
|
|
const { t } = useTranslation();
|
|
return (
|
|
<>
|
|
<CollectionExportMenu {...props} t={t} />
|
|
<button type="button">Somewhere else</button>
|
|
</>
|
|
);
|
|
}
|
|
|
|
function renderMenu(overrides: Partial<MenuProps> = {}) {
|
|
const props: MenuProps = { onExport: vi.fn(), ...overrides };
|
|
render(<Harness {...props} />);
|
|
return props;
|
|
}
|
|
|
|
const trigger = () => screen.getByRole('button', { name: 'Export' });
|
|
const items = () => screen.getAllByRole('menuitem');
|
|
|
|
describe('CollectionExportMenu (#2301)', () => {
|
|
it('FE-COMP-COLEXPORT-001: is a closed menu button until pressed', () => {
|
|
renderMenu();
|
|
expect(trigger()).toHaveAttribute('aria-haspopup', 'menu');
|
|
expect(trigger()).toHaveAttribute('aria-expanded', 'false');
|
|
expect(screen.queryByRole('menu')).toBeNull();
|
|
});
|
|
|
|
it('FE-COMP-COLEXPORT-002: offers both formats, each with its extension and what it is for', async () => {
|
|
renderMenu();
|
|
await userEvent.click(trigger());
|
|
|
|
const menu = screen.getByRole('menu', { name: 'Download this list as a file' });
|
|
expect(trigger()).toHaveAttribute('aria-expanded', 'true');
|
|
expect(trigger()).toHaveAttribute('aria-controls', menu.id);
|
|
expect(items().map(item => item.textContent)).toEqual([
|
|
'TREK list.trekcollection.jsonFor another TREK, with labels and status',
|
|
'GPX.gpxWaypoints for OsmAnd, Organic Maps, Garmin and other map apps',
|
|
]);
|
|
});
|
|
|
|
it('FE-COMP-COLEXPORT-003: exports the picked format, closes and gives focus back', async () => {
|
|
const props = renderMenu();
|
|
await userEvent.click(trigger());
|
|
|
|
await userEvent.click(screen.getByRole('menuitem', { name: /GPX/ }));
|
|
|
|
expect(props.onExport).toHaveBeenCalledWith('gpx');
|
|
expect(screen.queryByRole('menu')).toBeNull();
|
|
expect(trigger()).toHaveFocus();
|
|
});
|
|
|
|
it('FE-COMP-COLEXPORT-004: moves focus into the menu and walks it with the arrow keys, Home and End', async () => {
|
|
renderMenu();
|
|
await userEvent.click(trigger());
|
|
await waitFor(() => expect(items()[0]).toHaveFocus());
|
|
|
|
await userEvent.keyboard('{ArrowDown}');
|
|
expect(items()[1]).toHaveFocus();
|
|
// Past the end wraps round, as it does in every menu.
|
|
await userEvent.keyboard('{ArrowDown}');
|
|
expect(items()[0]).toHaveFocus();
|
|
await userEvent.keyboard('{ArrowUp}');
|
|
expect(items()[1]).toHaveFocus();
|
|
await userEvent.keyboard('{Home}');
|
|
expect(items()[0]).toHaveFocus();
|
|
await userEvent.keyboard('{End}');
|
|
expect(items()[1]).toHaveFocus();
|
|
});
|
|
|
|
it('FE-COMP-COLEXPORT-005: opens from the keyboard and picks with Enter', async () => {
|
|
const props = renderMenu();
|
|
trigger().focus();
|
|
|
|
await userEvent.keyboard('{ArrowDown}');
|
|
await waitFor(() => expect(items()[0]).toHaveFocus());
|
|
await userEvent.keyboard('{Enter}');
|
|
|
|
expect(props.onExport).toHaveBeenCalledWith('trek');
|
|
});
|
|
|
|
it('FE-COMP-COLEXPORT-006: closes on Escape without exporting, and gives focus back', async () => {
|
|
const props = renderMenu();
|
|
await userEvent.click(trigger());
|
|
await waitFor(() => expect(items()[0]).toHaveFocus());
|
|
|
|
await userEvent.keyboard('{Escape}');
|
|
|
|
expect(screen.queryByRole('menu')).toBeNull();
|
|
expect(trigger()).toHaveFocus();
|
|
expect(props.onExport).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-COMP-COLEXPORT-007: closes on a click anywhere else, and on Tab', async () => {
|
|
renderMenu();
|
|
await userEvent.click(trigger());
|
|
await userEvent.click(screen.getByRole('button', { name: 'Somewhere else' }));
|
|
expect(screen.queryByRole('menu')).toBeNull();
|
|
|
|
await userEvent.click(trigger());
|
|
await waitFor(() => expect(items()[0]).toHaveFocus());
|
|
await userEvent.keyboard('{Tab}');
|
|
expect(screen.queryByRole('menu')).toBeNull();
|
|
});
|
|
|
|
it('FE-COMP-COLEXPORT-008: cannot be opened while an export is still running', async () => {
|
|
renderMenu({ exporting: true });
|
|
expect(trigger()).toBeDisabled();
|
|
await userEvent.click(trigger());
|
|
expect(screen.queryByRole('menu')).toBeNull();
|
|
});
|
|
});
|