// @vitest-environment jsdom /** * What the Form page claims, pinned: Enter and a submit button send it, the * page's own submit is prevented, `busy` switches off everything inside and * holds focus, and focus comes back in the page's order. * * jsdom does not move focus off a field that becomes disabled; Chrome drops it * to ``. So "focus is held" is checked as "focus is on the form, and not * on a disabled element" — without the form taking focus, that fails here too * (UIG-29 measured it). The browser check is in the PR. */ import { useState } from 'react' import { afterEach, describe, expect, it, vi } from 'vitest' import { act, cleanup, fireEvent, render, screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { Button } from './Button' import { Checkbox } from './Checkbox' import { IconButton } from './IconButton' import { Field } from './Field' import { Form } from './Form' import { Popover } from './Popover' import { TextInput } from './TextInput' import { Textarea } from './Textarea' afterEach(cleanup) function Harness({ onSubmit = vi.fn(), error, busy = false }: { onSubmit?: () => void; error?: string; busy?: boolean }) { return (
) } describe('Form', () => { it('sends on Enter in a field', async () => { const user = userEvent.setup() const onSubmit = vi.fn() render() await user.type(screen.getByRole('textbox', { name: 'First' }), 'x{Enter}') expect(onSubmit).toHaveBeenCalledTimes(1) }) it('a Form in a Popover inside a Form sends only itself (C1)', async () => { // A link field in a pop-up over a composer: Enter there adds the link, and // must not also send the message around it. React carries the submit // through the Popover's portal to the outer form. const user = userEvent.setup() const outer = vi.fn() const inner = vi.fn() render(
Link} open ariaLabel="Link"> , ) await user.type(screen.getByRole('textbox', { name: 'Address' }), 'example.com{Enter}') expect(inner).toHaveBeenCalledTimes(1) expect(outer).not.toHaveBeenCalled() await user.type(screen.getByRole('textbox', { name: 'Message' }), 'hi{Enter}') expect(outer).toHaveBeenCalledTimes(1) expect(inner).toHaveBeenCalledTimes(1) }) it('sends from its submit button', async () => { const user = userEvent.setup() const onSubmit = vi.fn() render() await user.click(screen.getByRole('button', { name: 'Send' })) expect(onSubmit).toHaveBeenCalledTimes(1) }) describe('the keys, the same in every form', () => { function Keys({ onSubmit, enterSends }: { onSubmit: () => void; enterSends?: boolean }) { return (