import { act, fireEvent, render, screen, waitFor, } from '@toptal/picasso-test-utils' import React, { startTransition, useState } from 'react' import type { FormApi } from 'final-form' import { Field as FinalFormField, useForm } from 'react-final-form' import { FinalField } from '../FinalField' import Radio from '../Radio' import { Form } from '../Form' import type { Props } from './RadioGroup' import { RadioGroup } from './RadioGroup' const arrangeTest = ({ titleCase }: Partial = {}) => render(
{}}>
) const Toggleable = ({ children }: { children: React.ReactNode }) => { const [mounted, setMounted] = useState(true) return ( <> {mounted && children} ) } const FirstValue = ({ value }: { value: unknown }) => { const [first] = useState(value) return {String(first)} } // Outlasts React's 5 ms render slice, so a concurrent render yields after it const SlowRender = () => { const start = performance.now() while (performance.now() - start < 20) { // Keep the render busy } return null } const createRemountable = () => { const setMountedRef: { current?: (mounted: boolean) => void } = {} const Remountable = ({ children }: { children: React.ReactNode }) => { const [mounted, setMounted] = useState(true) setMountedRef.current = setMounted return <>{mounted && children} } return { Remountable, unmount: () => act(() => setMountedRef.current?.(false)), // Outside `act`, React renders the transition concurrently and yields remountInTransition: async () => { const environment = globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean } const actEnvironment = environment.IS_REACT_ACT_ENVIRONMENT environment.IS_REACT_ACT_ENVIRONMENT = false try { startTransition(() => setMountedRef.current?.(true)) await waitFor(() => expect(screen.getByRole('status')).toBeInTheDocument() ) } finally { environment.IS_REACT_ACT_ENVIRONMENT = actEnvironment } }, } } describe('RadioGroup', () => { it('shows the label in default case', () => { const { getByText } = arrangeTest() expect(getByText('Radio group label')).toBeInTheDocument() }) it('shows the label in title case', () => { const { getByText } = arrangeTest({ titleCase: true }) expect(getByText('Radio Group Label')).toBeInTheDocument() }) it('keeps its `beforeSubmit` when a `FinalField` renders the radio', async () => { const handleSubmit = jest.fn() render(
false}>
) await act(async () => { fireEvent.click(screen.getByRole('button', { name: 'submit' })) }) expect(handleSubmit).not.toHaveBeenCalled() }) it("renders a radio's stored choice on its first render after a remount", () => { const formRef: { current?: FormApi } = {} const CaptureForm = () => { formRef.current = useForm() return null } render(
{}} initialValues={{ choice: 'a' }}> {({ input }) => } ) act(() => { formRef.current?.change('choice', 'b') }) fireEvent.click(screen.getByRole('button', { name: 'toggle' })) fireEvent.click(screen.getByRole('button', { name: 'toggle' })) expect(screen.getByRole('status')).toHaveTextContent('true') }) it("renders a radio's stored choice on its first render when a transition render yields before it", async () => { const formRef: { current?: FormApi } = {} const CaptureForm = () => { formRef.current = useForm() return null } const { Remountable, unmount, remountInTransition } = createRemountable() render(
{}} initialValues={{ choice: 'a' }}> {({ input }) => } ) act(() => { formRef.current?.change('choice', 'b') }) unmount() await remountInTransition() expect(screen.getByRole('status')).toHaveTextContent('true') }) })