Files
TREK/client/src/components/Studio/StudioElementsPanel.test.tsx
T
Maurice 00018959c6 fix(studio): a full page takes no new element, the opacity slider commits a move only, a theme draft survives saves
Every add in the Studio (a sticker, a shape, an icon, a text, a frame, a line,
a travel element, a photo from the content panel or a drop), a text style and
a grid of frames appended without looking at the page's cap of 90 elements,
which paste and duplicate already kept. The save then failed with a 400 and
every later save of the book failed the same way. Each of them now leaves a
full page as it is and selects nothing, as a paste does.

The opacity slider committed on every key let go on it, so the Tab that moved
the focus there wrote the value it was last moved to, an empty undo step at
best and another colour's opacity at worst. It commits once after it moved,
on the key or the pointer let go or when the focus leaves.

The Theme panel threw away colours, faces and motifs being tried whenever the
book came back from a save by another editor or the assistant, because the
same theme read anew is a new object. The draft now follows the book's theme
only when the theme itself changed.
2026-10-24 04:19:25 +00:00

148 lines
7.1 KiB
TypeScript

import type { BookDocument } from '@trek/shared';
import { BOOK_GRIDS, MAX_SPREAD_ELEMENTS, bookPageSetupSchema, normalizeBookDocument } from '@trek/shared';
import { beforeEach, describe, expect, it } from 'vitest';
import { fireEvent, render, screen } from '../../../tests/helpers/render';
import { useStudioStore } from '../../store/studioStore';
import { StudioElementsPanel } from './StudioElementsPanel';
import { searchIcons } from './iconLibrary';
import { DIVIDER_PRESETS, LINE_PRESETS } from './linePresets';
import { matchStickers } from './stickerLibrary';
/**
* FE-STUDIO-ELEMENTS-001 to FE-STUDIO-ELEMENTS-007: the Elements panel's six
* tabs, each opening its own shelves, and each placing what it shows on the
* open page, as long as the page has room for it.
*/
const t = (k: string) => k;
const page = bookPageSetupSchema.parse({ preset: 'square-210', pageWidth: 210, pageHeight: 210, bleed: 3, safe: 5 });
const source = { entries: [], photos: [] };
const book = (): BookDocument =>
normalizeBookDocument({
version: 1,
title: 'Porto',
page: { preset: 'square-210', pageWidth: 210, pageHeight: 210, bleed: 3, safe: 5 },
spreads: [{ id: 'sp1', role: 'inner', entryId: null, background: null, elements: [], parked: [] }],
});
const elements = () => useStudioStore.getState().doc?.spreads[0]?.elements ?? [];
const last = () => elements()[elements().length - 1];
const open = (name: string) => fireEvent.click(screen.getByRole('tab', { name }));
beforeEach(() => {
window.localStorage.clear();
useStudioStore.getState().load(book());
});
describe('the Elements panel', () => {
it('FE-STUDIO-ELEMENTS-001: opens on Text, with the quotation mark among the plain styles', () => {
render(<StudioElementsPanel page={page} t={t} source={source} />);
const panel = screen.getByRole('tabpanel');
expect(panel.getAttribute('aria-labelledby')).toBe(screen.getByRole('tab', { name: 'journey.studio.text' }).id);
fireEvent.click(screen.getByRole('button', { name: 'journey.studio.quoteMark' }));
expect(last()).toMatchObject({ kind: 'text', text: '“', size: 46 });
});
it('FE-STUDIO-ELEMENTS-002: puts the outline-only rectangle first among the basic shapes', () => {
render(<StudioElementsPanel page={page} t={t} source={source} />);
open('journey.studio.shapes');
const outline = screen.getByRole('button', { name: 'journey.studio.shapeKind.outline' });
expect(outline.closest('.st-shape-grid')?.firstElementChild).toBe(outline);
fireEvent.click(outline);
expect(last()).toMatchObject({ kind: 'shape', shape: 'rect', fill: null, stroke: '#141414' });
});
it('FE-STUDIO-ELEMENTS-003: keeps the plain rule with the drawn lines and the dividers', () => {
const { container } = render(<StudioElementsPanel page={page} t={t} source={source} />);
open('journey.studio.lines');
expect(container.querySelectorAll('.st-shape-btn')).toHaveLength(LINE_PRESETS.length + DIVIDER_PRESETS.length + 1);
fireEvent.click(screen.getByRole('button', { name: 'journey.studio.shapeKind.line' }));
expect(last()).toMatchObject({ kind: 'shape', shape: 'rect', frame: { h: 0.5 } });
fireEvent.click(screen.getByRole('button', { name: 'journey.studio.linePreset.route' }));
expect(last()).toMatchObject({ kind: 'line' });
});
it('FE-STUDIO-ELEMENTS-004: offers frames in their styles and shapes, and the grids, in one tab', () => {
render(<StudioElementsPanel page={page} t={t} source={source} />);
open('journey.studio.frames');
expect(screen.getByText('journey.studio.frameStyles')).toBeTruthy();
expect(screen.getByText('journey.studio.frameShapes')).toBeTruthy();
expect(screen.getByText('journey.studio.grids')).toBeTruthy();
fireEvent.click(screen.getByRole('button', { name: 'journey.studio.polaroidFrame' }));
expect(last()).toMatchObject({ kind: 'photo', photoId: null, frameStyle: 'polaroid' });
const grid = BOOK_GRIDS[0];
if (!grid) throw new Error('no grids');
fireEvent.click(screen.getByRole('button', { name: grid.id }));
expect(useStudioStore.getState().selection).toHaveLength(grid.cells.length);
});
it('FE-STUDIO-ELEMENTS-007: places nothing on a page that holds all it can, a frame or a whole grid', () => {
const grid = BOOK_GRIDS.find((g) => g.cells.length > 1);
if (!grid) throw new Error('no grid of several frames');
const crowded = (n: number) =>
normalizeBookDocument({
...book(),
spreads: [
{
id: 'sp1',
role: 'inner',
entryId: null,
background: null,
parked: [],
elements: Array.from({ length: n }, (_, i) => ({
id: `s${i}`,
kind: 'shape',
frame: { x: 0, y: 0, w: 5, h: 5 },
})),
},
],
});
useStudioStore.getState().load(crowded(MAX_SPREAD_ELEMENTS));
render(<StudioElementsPanel page={page} t={t} source={source} />);
open('journey.studio.frames');
fireEvent.click(screen.getByRole('button', { name: 'journey.studio.polaroidFrame' }));
expect(elements()).toHaveLength(MAX_SPREAD_ELEMENTS);
expect(useStudioStore.getState().selection).toEqual([]);
useStudioStore.getState().load(crowded(MAX_SPREAD_ELEMENTS - 1));
fireEvent.click(screen.getByRole('button', { name: grid.id }));
expect(elements()).toHaveLength(MAX_SPREAD_ELEMENTS - 1);
expect(useStudioStore.getState().canUndo()).toBe(false);
});
it('FE-STUDIO-ELEMENTS-005: searches the icons and the stickers above their grids', () => {
render(<StudioElementsPanel page={page} t={t} source={source} />);
open('journey.studio.icons');
fireEvent.change(screen.getByRole('textbox', { name: 'journey.studio.searchIcons' }), {
target: { value: 'compass' },
});
fireEvent.click(screen.getByRole('button', { name: 'Compass' }));
expect(last()).toMatchObject({ kind: 'icon', name: 'Compass' });
open('journey.studio.stickers');
fireEvent.click(screen.getByRole('button', { name: 'journey.studio.sticker.sun' }));
expect(last()).toMatchObject({ kind: 'sticker', stickerId: 'sun' });
});
it('FE-STUDIO-ELEMENTS-006: says in the search field how many stickers and icons the words match', () => {
render(<StudioElementsPanel page={page} t={t} source={source} />);
open('journey.studio.stickers');
expect(screen.queryByRole('status')).toBeNull();
fireEvent.change(screen.getByRole('textbox', { name: 'journey.studio.searchStickers' }), {
target: { value: 'sun' },
});
expect(screen.getByRole('status').textContent).toBe(String(matchStickers('sun', null, t).length));
open('journey.studio.icons');
fireEvent.change(screen.getByRole('textbox', { name: 'journey.studio.searchIcons' }), {
target: { value: 'compass' },
});
expect(screen.getByRole('status').textContent).toBe(String(searchIcons('compass').length));
// Spaces are not a search, so they leave the count out.
fireEvent.change(screen.getByRole('textbox', { name: 'journey.studio.searchIcons' }), {
target: { value: ' ' },
});
expect(screen.queryByRole('status')).toBeNull();
});
});