import { act, renderHook } from '@testing-library/react'; import { readFileSync } from 'node:fs'; import { resolve } from 'node:path'; import { useRef, useState } from 'react'; import { describe, expect, it, vi } from 'vitest'; import { sizeClassOf } from '../../../src/hooks/sizeClass'; import { useAutoGrow } from '../../../src/hooks/useAutoGrow'; import MSheet from '../../../src/mobile/components/MSheet'; import { edgeFadeClass, useEdgeFade } from '../../../src/mobile/components/useEdgeFade'; import { FormSheetBody, FormSheetFooter, FormSheetHeader, SheetDateField, } from '../../../src/mobile/screens/trip/sheets/PlSheetChrome'; import { MemberAvatar } from '../../../src/mobile/screens/trip/tabs/MCostsPayment'; import { fireEvent, render, screen } from '../../helpers/render'; // FE-MOB-FORMSHEET-001 onwards: the form sheet the touch shell presents every // form in (MSheet `form` + PlSheetChrome), and the stylesheet rules that lay it out. function Form({ onClose = () => {} }: { onClose?: () => void }) { return ( {}} submitLabel="Add" /> ); } describe('form sheet chrome', () => { it('FE-MOB-FORMSHEET-001: header, body and footer carry the parts the short-window layout places', () => { render(
); const panel = screen.getByRole('dialog', { name: 'Add expense' }); expect(panel).toHaveClass('m-form-sheet'); const [head, body, foot] = Array.from(panel.children) as HTMLElement[]; expect(head).toHaveClass('m-fs-head'); expect(body).toHaveClass('m-fs-body', 'overflow-y-auto'); expect(foot).toHaveClass('m-fs-foot'); // The close button is the one the layout hides once Cancel stands beside the title. expect(screen.getByRole('button', { name: 'Close' })).toHaveClass('m-fs-close'); }); it('FE-MOB-FORMSHEET-002: a long title wraps to a second line instead of being cut after one', () => { render(); const title = screen.getAllByText('Add expense').find((el) => el.tagName === 'DIV') as HTMLElement; expect(title).toHaveClass('line-clamp-2'); expect(title).not.toHaveClass('truncate'); }); it('FE-MOB-FORMSHEET-003: the body is the container its paired rows measure against', () => { render(); const body = screen.getByRole('textbox', { name: 'Name' }).parentElement as HTMLElement; expect(body.className).toContain('[container-type:inline-size]'); }); it('FE-MOB-FORMSHEET-004: the body fades at the foot while more waits below, and at the top once scrolled', () => { render(); const body = screen.getByRole('textbox', { name: 'Name' }).parentElement as HTMLElement; Object.defineProperty(body, 'scrollHeight', { configurable: true, value: 900 }); Object.defineProperty(body, 'clientHeight', { configurable: true, value: 400 }); act(() => { body.dispatchEvent(new Event('scroll')); }); expect(body).toHaveClass('m-fade-b'); act(() => { body.scrollTop = 200; body.dispatchEvent(new Event('scroll')); }); expect(body).toHaveClass('m-fade-tb'); act(() => { body.scrollTop = 500; body.dispatchEvent(new Event('scroll')); }); expect(body).toHaveClass('m-fade-t'); }); it('FE-MOB-FORMSHEET-005: the date field keeps its keyboard entry in the picker, so the date has the field to itself', () => { render( {}} />); // No second round button beside the date taking its room. expect(screen.queryByRole('button', { name: /enter date manually|common\.datepicker\.enterManually/i })).toBeNull(); const trigger = screen.getByRole('button', { expanded: false }); fireEvent.click(trigger); expect( screen.getByRole('button', { name: /enter date manually|common\.datepicker\.enterManually/i }) ).toBeInTheDocument(); expect(trigger.closest('.rounded-full')).not.toBeNull(); }); }); describe('useEdgeFade on the vertical axis', () => { function box(scrollHeight: number, clientHeight: number, scrollTop = 0) { const el = document.createElement('div'); Object.defineProperty(el, 'scrollHeight', { configurable: true, value: scrollHeight }); Object.defineProperty(el, 'clientHeight', { configurable: true, value: clientHeight }); el.scrollTop = scrollTop; return el; } it('FE-MOB-FORMSHEET-006: reads the top and the foot, and its classes never mirror', () => { expect(edgeFadeClass(renderHook(() => useEdgeFade(box(400, 400), 'y')).result.current, 'y')).toBe(''); expect(edgeFadeClass(renderHook(() => useEdgeFade(box(900, 400), 'y')).result.current, 'y')).toBe('m-fade-b'); expect(edgeFadeClass({ start: true, end: false }, 'y')).toBe('m-fade-t'); expect(edgeFadeClass({ start: true, end: true }, 'y')).toBe('m-fade-tb'); // The strip's own classes are unchanged. expect(edgeFadeClass({ start: true, end: true })).toBe('m-fade-se'); }); it('FE-MOB-FORMSHEET-007: measures again when fields come and go inside a box that keeps its height', async () => { const el = box(400, 400); const { result } = renderHook(() => useEdgeFade(el, 'y')); expect(result.current.end).toBe(false); Object.defineProperty(el, 'scrollHeight', { configurable: true, value: 800 }); await act(async () => { el.appendChild(document.createElement('div')); // Measured on the next frame, so a burst of changes is measured once. await new Promise((resolve) => setTimeout(resolve, 40)); }); expect(result.current.end).toBe(true); }); }); describe('useAutoGrow', () => { it('FE-MOB-FORMSHEET-008: a textarea grows to its lines instead of slicing the last one', () => { function Story() { const ref = useRef(null); const [text, setText] = useState('one'); useAutoGrow(ref, text); return