mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 13:23:04 +00:00
a5de1bc52d
The trip PDF gets a new layout in the app's current look, with nothing it printed before left out and more of each place on the page. - Cover: the trip photo full bleed, title and dates over it, the trip's figures on frosted tiles. - Overview: the route map, every day at a glance (colour, date, plan, the night's stay, stops, cost) and all bookings and stays with their codes. - Days: the header in the day's map colour, a photo strip of the day's stops, where the night is spent, then the plan as a timeline with the times on the left. - Places: photo or category tile, category, duration, price, rating, tags, address, coordinates, description, notes, opening hours of that day, phone, website, e-mail and the members going. The rating, e-mail, hours and reservation note come from the trip's place, since the assignment only carries a projection of it. - Bookings: route first, then airline, number, seat or platform as badges, each leg with its own code, status and the booking's code. - A second row of switches in the preview: overview, photos, coordinates, and contact and hours, next to page per day, large text and transport notes. Each one is a class on the document and remembered. The document is split into modules for its helpers, icons, stylesheet, cover and overview, and the day, so the export itself only gathers the data.
167 lines
6.4 KiB
TypeScript
167 lines
6.4 KiB
TypeScript
/**
|
|
* FE-PDFPREVIEW-001 to FE-PDFPREVIEW-009: the trip PDF's preview and the control
|
|
* that saves it. An iPhone or iPad ignores print() in the Home Screen app, so
|
|
* there the preview hands the export to Safari instead of offering a button that
|
|
* does nothing.
|
|
*/
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import {
|
|
COORDINATES_KEY,
|
|
DETAILS_KEY,
|
|
LARGE_TEXT_KEY,
|
|
OVERVIEW_KEY,
|
|
PAGE_BREAK_KEY,
|
|
PHOTOS_KEY,
|
|
openPdfPreview,
|
|
type PdfPreviewOptions,
|
|
} from './pdfPreview';
|
|
|
|
const tr = (key: string) => key;
|
|
|
|
function open(overrides: Partial<PdfPreviewOptions> = {}) {
|
|
openPdfPreview({
|
|
html: '<html><body>plan</body></html>',
|
|
title: 'Lisbon',
|
|
tripId: 9,
|
|
tr,
|
|
breaksPerDay: true,
|
|
showTransportNotes: true,
|
|
hasTransportNotes: false,
|
|
largeText: false,
|
|
photos: true,
|
|
overview: true,
|
|
coordinates: true,
|
|
details: true,
|
|
...overrides,
|
|
});
|
|
}
|
|
|
|
function standalone(value: boolean) {
|
|
Object.defineProperty(navigator, 'standalone', { configurable: true, get: () => value });
|
|
}
|
|
|
|
afterEach(() => {
|
|
document.getElementById('pdf-preview-overlay')?.remove();
|
|
delete (navigator as unknown as Record<string, unknown>).standalone;
|
|
localStorage.clear();
|
|
});
|
|
|
|
describe('openPdfPreview', () => {
|
|
it('FE-PDFPREVIEW-001: a browser gets the print button, and it prints the frame', () => {
|
|
open();
|
|
expect(document.getElementById('pdf-safari-link')).toBeNull();
|
|
expect(document.getElementById('pdf-safari-hint')).toBeNull();
|
|
|
|
const iframe = document.querySelector<HTMLIFrameElement>('#pdf-preview-overlay iframe');
|
|
const print = vi.fn();
|
|
Object.defineProperty(iframe, 'contentWindow', { value: { print }, configurable: true });
|
|
const button = document.getElementById('pdf-print-btn');
|
|
expect(button?.textContent).toBe('pdf.saveAsPdf');
|
|
button?.click();
|
|
expect(print).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('FE-PDFPREVIEW-002: the iOS Home Screen app gets a Safari link to the trip export and says why', () => {
|
|
standalone(true);
|
|
open();
|
|
expect(document.getElementById('pdf-print-btn')).toBeNull();
|
|
|
|
const link = document.getElementById('pdf-safari-link') as HTMLAnchorElement;
|
|
expect(link.textContent).toBe('pdf.openInSafari');
|
|
expect(link.target).toBe('_blank');
|
|
expect(link.rel).toBe('noopener');
|
|
const url = new URL(link.href);
|
|
expect(url.pathname).toBe('/trips/9');
|
|
expect(url.searchParams.get('export')).toBe('pdf');
|
|
expect(document.getElementById('pdf-safari-hint')?.textContent).toBe('pdf.safariHint');
|
|
});
|
|
|
|
it('FE-PDFPREVIEW-003: without a trip to hand over, the preview keeps the print button', () => {
|
|
standalone(true);
|
|
open({ tripId: undefined });
|
|
expect(document.getElementById('pdf-print-btn')).not.toBeNull();
|
|
expect(document.getElementById('pdf-safari-link')).toBeNull();
|
|
});
|
|
|
|
it('FE-PDFPREVIEW-004: the title is escaped and the close button removes the preview', () => {
|
|
open({ title: '<b>Lisbon</b>' });
|
|
const overlay = document.getElementById('pdf-preview-overlay');
|
|
expect(overlay?.querySelector('b')).toBeNull();
|
|
expect(overlay?.textContent).toContain('<b>Lisbon</b>');
|
|
document.getElementById('pdf-close-btn')?.click();
|
|
expect(document.getElementById('pdf-preview-overlay')).toBeNull();
|
|
});
|
|
|
|
it('FE-PDFPREVIEW-005: the page-break switch flips and remembers the choice', () => {
|
|
open();
|
|
const toggle = document.getElementById('pdf-daybreak-toggle');
|
|
expect(toggle?.getAttribute('aria-checked')).toBe('true');
|
|
toggle?.click();
|
|
expect(toggle?.getAttribute('aria-checked')).toBe('false');
|
|
expect(localStorage.getItem(PAGE_BREAK_KEY)).not.toBeNull();
|
|
});
|
|
|
|
it('FE-PDFPREVIEW-006: the large-text switch starts as given, flips, and remembers the choice', () => {
|
|
open();
|
|
const toggle = document.getElementById('pdf-large-text-toggle');
|
|
expect(toggle?.getAttribute('aria-label')).toBe('pdf.largeText');
|
|
expect(toggle?.getAttribute('aria-checked')).toBe('false');
|
|
toggle?.click();
|
|
expect(toggle?.getAttribute('aria-checked')).toBe('true');
|
|
expect(localStorage.getItem(LARGE_TEXT_KEY)).toBe('1');
|
|
toggle?.click();
|
|
expect(localStorage.getItem(LARGE_TEXT_KEY)).toBe('0');
|
|
});
|
|
|
|
it('FE-PDFPREVIEW-007: each label stays with its switch, so a narrow header wraps between them', () => {
|
|
open({ largeText: true, hasTransportNotes: true });
|
|
for (const id of ['pdf-transport-notes-toggle', 'pdf-daybreak-toggle', 'pdf-large-text-toggle']) {
|
|
const toggle = document.getElementById(id);
|
|
const unit = toggle?.parentElement;
|
|
expect(unit?.querySelector(`label[for="${id}"]`)).not.toBeNull();
|
|
expect(unit?.style.whiteSpace).toBe('nowrap');
|
|
}
|
|
expect(document.getElementById('pdf-large-text-toggle')?.getAttribute('aria-checked')).toBe('true');
|
|
});
|
|
|
|
it('FE-PDFPREVIEW-008: the switches sit in a row of their own under the title and the actions', () => {
|
|
open({ hasTransportNotes: true });
|
|
const row = document.getElementById('pdf-settings-row');
|
|
const ids = [...(row?.querySelectorAll('[role="switch"]') ?? [])].map((toggle) => toggle.id);
|
|
expect(ids).toEqual([
|
|
'pdf-daybreak-toggle',
|
|
'pdf-large-text-toggle',
|
|
'pdf-overview-toggle',
|
|
'pdf-photos-toggle',
|
|
'pdf-coordinates-toggle',
|
|
'pdf-details-toggle',
|
|
'pdf-transport-notes-toggle',
|
|
]);
|
|
// The header keeps only the title and its two actions.
|
|
expect(row?.previousElementSibling?.querySelector('[role="switch"]')).toBeNull();
|
|
expect(row?.previousElementSibling?.querySelector('#pdf-print-btn')).not.toBeNull();
|
|
});
|
|
|
|
it.each([
|
|
['pdf-overview-toggle', OVERVIEW_KEY, 'pdf-no-overview'],
|
|
['pdf-photos-toggle', PHOTOS_KEY, 'pdf-no-photos'],
|
|
['pdf-coordinates-toggle', COORDINATES_KEY, 'pdf-no-coords'],
|
|
['pdf-details-toggle', DETAILS_KEY, 'pdf-no-details'],
|
|
])('FE-PDFPREVIEW-009: %s hides its part of the open document at once and is remembered', (id, key, hidden) => {
|
|
open();
|
|
const toggle = document.getElementById(id);
|
|
const body = document.querySelector<HTMLIFrameElement>('#pdf-preview-overlay iframe')?.contentDocument?.body;
|
|
expect(toggle?.getAttribute('aria-checked')).toBe('true');
|
|
|
|
toggle?.click();
|
|
expect(toggle?.getAttribute('aria-checked')).toBe('false');
|
|
expect(localStorage.getItem(key)).toBe('0');
|
|
if (body) expect(body.classList.contains(hidden)).toBe(true);
|
|
|
|
toggle?.click();
|
|
expect(localStorage.getItem(key)).toBe('1');
|
|
if (body) expect(body.classList.contains(hidden)).toBe(false);
|
|
});
|
|
});
|