import React from 'react' import { render, screen } from '@toptal/picasso-test-utils' import type { FormState } from 'final-form' import { FormCompound as Form } from '../FormCompound' import type { FullFormState } from './index' import { useFormState } from './index' interface Values { firstName: string } const renderInForm = ( children: React.ReactNode, initialValues?: Partial ) => render(
{children}
) describe('useFormState', () => { describe('without a subscription', () => { it("passes the form's `initialValues` through", () => { const Probe = () => { const { initialValues } = useFormState() return {initialValues?.firstName} } renderInForm(, { firstName: 'Bruce' }) expect(screen.getByText('Bruce')).toBeInTheDocument() }) it('leaves an absent `initialValues` undefined, as final-form reports it', () => { const Probe = () => { const { initialValues } = useFormState() return {String(initialValues)} } renderInForm() expect(screen.getByText('undefined')).toBeInTheDocument() }) it('fills the state booleans final-form subscribes to', () => { const Probe = () => { const { submitting, pristine, dirty, valid, touched } = useFormState() return ( {JSON.stringify({ submitting, pristine, dirty, valid, touched })} ) } renderInForm() expect( screen.getByText( '{"submitting":false,"pristine":true,"dirty":false,"valid":true,"touched":{}}' ) ).toBeInTheDocument() }) }) describe('with a subscription', () => { it('leaves the keys it does not subscribe to undefined', () => { const Probe = () => { const { submitting, initialValues, pristine } = useFormState({ subscription: { submitting: true }, }) return ( {JSON.stringify({ submitting, initialValues, pristine })} ) } renderInForm(, { firstName: 'Bruce' }) expect(screen.getByText('{"submitting":false}')).toBeInTheDocument() }) }) describe('types', () => { // Type-checked by `pnpm typecheck:react19`, which lists this file in // tsconfig.react19.json's `files` it('makes the subscribed keys non-optional without a subscription', () => { const Probe = () => { const state = useFormState() const submitting: boolean = state.submitting const touched: Record = state.touched // @ts-expect-error `initialValues` stays optional: a form given none reports `undefined` const initialValues: Partial = state.initialValues // @ts-expect-error `active` stays optional: no focused field is an answer const active: keyof Values = state.active return ( {JSON.stringify({ initialValues, submitting, touched, active })} ) } renderInForm() expect( screen.getByText('{"submitting":false,"touched":{}}') ).toBeInTheDocument() }) it('keeps the optional keys under an explicit subscription', () => { const Probe = () => { const state = useFormState({ subscription: { values: true } }) // @ts-expect-error a narrow subscription keeps `initialValues` optional const initialValues: Partial = state.initialValues // @ts-expect-error a narrow subscription keeps `submitting` optional const submitting: boolean = state.submitting return {`${initialValues} ${submitting}`} } renderInForm() expect(screen.getByText('undefined undefined')).toBeInTheDocument() }) it('falls back to the optional keys when the subscription is only known at runtime', () => { const Probe = ({ params }: { params: { subscription?: undefined } }) => { const state: FormState = useFormState(params) return {String(state.submitting)} } renderInForm() expect(screen.getByText('false')).toBeInTheDocument() }) it("assigns the full state to final-form's own type", () => { const full = {} as FullFormState const upstream: FormState = full expect(upstream).toBeDefined() }) }) })