Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

5.1 KB
Raw Blame
import '@testing-library/jest-dom'

import { cleanup, render, screen } from '@testing-library/react'
import { MakeLogicType, kea, path } from 'kea'
import { Form, forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'

import { LemonInput } from 'lib/lemon-ui/LemonInput/LemonInput'

import { initKeaTests } from '~/test/init'

import { LemonField } from './LemonField'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface lemonFieldTestLogicValues {
    isMyFormSubmitting: boolean
    isMyFormValid: boolean
    myForm: {
        email: string
        referral_source: string
    }
    myFormAllErrors: Record<string, any>
    myFormChanged: boolean
    myFormErrors: DeepPartialMap<
        {
            email: string
            referral_source: string
        },
        ValidationErrorType
    >
    myFormHasErrors: boolean
    myFormManualErrors: Record<string, any>
    myFormTouched: boolean
    myFormTouches: Record<string, boolean>
    myFormValidationErrors: DeepPartialMap<
        {
            email: string
            referral_source: string
        },
        ValidationErrorType
    >
    showMyFormErrors: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface lemonFieldTestLogicActions {
    resetMyForm: (values?: { email: string; referral_source: string }) => {
        values?: {
            email: string
            referral_source: string
        }
    }
    setMyFormManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setMyFormValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setMyFormValues: (
        values: DeepPartial<{
            email: string
            referral_source: string
        }>
    ) => {
        values: DeepPartial<{
            email: string
            referral_source: string
        }>
    }
    submitMyForm: () => {
        value: boolean
    }
    submitMyFormFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitMyFormRequest: (myForm: { email: string; referral_source: string }) => {
        myForm: {
            email: string
            referral_source: string
        }
    }
    submitMyFormSuccess: (myForm: { email: string; referral_source: string }) => {
        myForm: {
            email: string
            referral_source: string
        }
    }
    touchMyFormField: (key: string) => {
        key: string
    }
}

export type lemonFieldTestLogicType = MakeLogicType<lemonFieldTestLogicValues, lemonFieldTestLogicActions>

const lemonFieldTestLogic = kea<lemonFieldTestLogicType>([
    path(['lib', 'lemon-ui', 'LemonField', 'test']),
    forms(() => ({
        myForm: {
            defaults: { referral_source: '', email: '' } as { referral_source: string; email: string },
            errors: () => ({}),
            submit: () => {},
        },
    })),
])

describe('LemonField', () => {
    beforeEach(() => {
        initKeaTests()
        lemonFieldTestLogic.mount()
    })

    afterEach(() => {
        cleanup()
    })

    it('Pure field wires the explicit htmlFor onto the label', () => {
        render(
            <LemonField.Pure label="Email" htmlFor="email-input">
                <input id="email-input" />
            </LemonField.Pure>
        )

        // getByLabelText only resolves when the label is correctly associated with the input,
        // so it's both the assertion and the proof of correct wiring.
        expect(screen.getByLabelText('Email')).toBe(document.getElementById('email-input'))
    })

    it.each([
        {
            desc: 'function-as-child render prop (the signup dead-click pattern)',
            label: 'Where did you hear about us?',
            field: (
                <LemonField name="referral_source" label="Where did you hear about us?">
                    {({ value, onChange }) => (
                        <LemonInput value={value ?? ''} onChange={(val: string) => onChange(val)} />
                    )}
                </LemonField>
            ),
        },
        {
            desc: 'React-element child (no render prop)',
            label: 'Email address',
            field: (
                <LemonField name="email" label="Email address">
                    <LemonInput type="email" />
                </LemonField>
            ),
        },
    ])('clicking the label focuses the wrapped input — $desc', ({ label, field }) => {
        render(
            <Form logic={lemonFieldTestLogic} formKey="myForm">
                {field}
            </Form>
        )

        // getByLabelText only finds the input when label[for] matches input[id].
        // If LemonField regressed to emitting htmlFor={undefined}, this throws.
        const input = screen.getByLabelText(label) as HTMLInputElement
        expect(input).toBeInstanceOf(HTMLInputElement)
        expect(input.id).toBeTruthy()
        expect(screen.getByText(label).closest('label')?.getAttribute('for')).toBe(input.id)
    })
})