import React, { StrictMode, createRef, useState } from 'react' import { renderToString } from 'react-dom/server' import { act, fireEvent, render, screen } from '@toptal/picasso-test-utils' import type { FormApi } from 'final-form' import { createForm } from 'final-form' import { Form as FinalForm, useForm } from 'react-final-form' import { FormCompound as Form } from '../FormCompound' import { FinalField, useField } from './index' const renderForm = (children: React.ReactNode) => render(
{children}
) const TAGGED = { tag: 'tagged' } const FirstValue = ({ value }: { value: unknown }) => { const [first] = useState(value) return {String(first)} } const Toggleable = ({ children }: { children: React.ReactNode }) => { const [mounted, setMounted] = useState(true) return ( <> {mounted && children} ) } const editAndRemount = () => { fireEvent.change(screen.getByRole('textbox'), { target: { value: 'y' } }) fireEvent.click(screen.getByRole('button', { name: 'toggle' })) fireEvent.click(screen.getByRole('button', { name: 'toggle' })) } const HookInput = () => { const { input } = useField('a') return ( <> ) } describe('FinalField', () => { it('keeps the value the user edited when it remounts', () => { renderForm( ) editAndRemount() expect(screen.getByRole('textbox')).toHaveValue('y') }) it('renders the stored value on its first render after a remount', () => { renderForm( {({ input }) => ( <> )} ) editAndRemount() expect(screen.getByRole('status')).toHaveTextContent('y') }) it('still renders a field-level `initialValue` on its first mount', () => { renderForm( {({ input }) => } ) expect(screen.getByRole('status')).toHaveTextContent('initial') }) it('keeps its `beforeSubmit` after a remount', async () => { const handleSubmit = jest.fn() render(
false} />
) editAndRemount() await act(async () => { fireEvent.click(screen.getByRole('button', { name: 'submit' })) }) expect(handleSubmit).not.toHaveBeenCalled() }) it('forwards `ref` to a string component', () => { const ref = createRef() renderForm() expect(ref.current).toBe(screen.getByRole('textbox')) }) describe('useField', () => { it('keeps the value the user edited when the field remounts', () => { renderForm( ) editAndRemount() expect(screen.getByRole('textbox')).toHaveValue('y') }) it('renders the stored value on the first render after a remount', () => { renderForm( ) editAndRemount() expect(screen.getByRole('status')).toHaveTextContent('y') }) it('renders its `data` on the first render', () => { const Tag = () => { const { meta } = useField('a', { data: TAGGED }) return } renderForm() expect(screen.getByRole('status')).toHaveTextContent('tagged') }) it('compares with its `isEqual` on the first render', () => { const form = createForm<{ item: { id: number } }>({ onSubmit: jest.fn(), initialValues: { item: { id: 1 } }, }) const Pristine = () => { const { meta } = useField<{ id: number }>('item', { isEqual: (left, right) => left?.id === right?.id, }) return } form.change('item', { id: 1 }) render( } /> ) expect(screen.getByRole('status')).toHaveTextContent('true') }) it('leaves nothing registered once the field unmounts, in StrictMode', () => { const formRef: { current?: FormApi } = {} const CaptureForm = () => { formRef.current = useForm() return null } render(
) editAndRemount() expect(screen.getByRole('status')).toHaveTextContent('y') fireEvent.click(screen.getByRole('button', { name: 'toggle' })) expect(formRef.current?.getRegisteredFields()).not.toContain('a') }) it('leaves no timer behind once the remounted field commits', () => { jest.useFakeTimers() try { renderForm( ) editAndRemount() expect(jest.getTimerCount()).toBe(0) } finally { jest.useRealTimers() } }) // The server path, where a render can't yield, is in server.test.tsx it('leaves nothing registered once a render that never commits times out', async () => { jest.useFakeTimers() // `renderToString` renders without committing. jsdom's `window` makes // the claim pick a layout effect, which React warns about there const consoleError = jest .spyOn(console, 'error') .mockImplementation(() => {}) try { const form = createForm<{ a: string }>({ onSubmit: jest.fn(), initialValues: { a: 'x' }, }) form.change('a', 'y') const html = renderToString( } /> ) await Promise.resolve() expect(html).toContain('y') // A yielding render could still be rendering, so the hold outlasts it expect(form.getRegisteredFields()).toContain('a') jest.runOnlyPendingTimers() expect(form.getRegisteredFields()).not.toContain('a') } finally { consoleError.mockRestore() jest.useRealTimers() } }) }) })