# TanStack Form Testing Examples
Complete examples for testing TanStack Form validation, submission, and field states.
## Test Setup
### Form Dependencies
```typescript
// src/test/form-utils.tsx
import { ReactElement } from 'react';
import { render, RenderOptions } from '@testing-library/react';
export function renderForm(ui: ReactElement, options?: RenderOptions) {
return render(ui, options);
}
```
## Example 1: Basic Form with Validation
### Form Component
```typescript
// src/components/UserForm.tsx
import { useForm } from '@tanstack/react-form';
import { zodValidator } from '@tanstack/zod-form-adapter';
import { z } from 'zod';
const userSchema = z.object({
name: z.string().min(2, 'Name must be at least 2 characters'),
email: z.string().email('Invalid email address'),
age: z.number().min(18, 'Must be at least 18 years old'),
});
export function UserForm({ onSubmit }: { onSubmit: (data: any) => void }) {
const form = useForm({
defaultValues: {
name: '',
email: '',
age: 0,
},
onSubmit: async ({ value }) => {
onSubmit(value);
},
validatorAdapter: zodValidator,
});
return (
);
}
```
### Test Suite
```typescript
// src/components/UserForm.test.tsx
import { describe, it, expect, vi } from 'vitest';
import { screen, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { UserForm } from './UserForm';
describe('UserForm', () => {
it('renders all form fields', () => {
const onSubmit = vi.fn();
render();
expect(screen.getByLabelText('Name')).toBeInTheDocument();
expect(screen.getByLabelText('Email')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Submit' })).toBeInTheDocument();
});
it('shows validation error for short name', async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render();
await user.type(screen.getByLabelText('Name'), 'A');
await user.tab(); // Trigger blur/validation
expect(await screen.findByRole('alert')).toHaveTextContent('Name must be at least 2 characters');
});
it('shows validation error for invalid email', async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render();
await user.type(screen.getByLabelText('Email'), 'invalid-email');
await user.tab();
expect(await screen.findByRole('alert')).toHaveTextContent('Invalid email address');
});
it('submits form with valid data', async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render();
await user.type(screen.getByLabelText('Name'), 'Alice Johnson');
await user.type(screen.getByLabelText('Email'), 'alice@example.com');
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(onSubmit).toHaveBeenCalledWith({
name: 'Alice Johnson',
email: 'alice@example.com',
age: 0,
});
});
it('does not submit form with invalid data', async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render();
await user.type(screen.getByLabelText('Name'), 'A');
await user.type(screen.getByLabelText('Email'), 'invalid');
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(onSubmit).not.toHaveBeenCalled();
});
});
```
## Example 2: Testing Field States
### Form with Field State Display
```typescript
// src/components/FieldStateForm.tsx
import { useForm } from '@tanstack/react-form';
export function FieldStateForm() {
const form = useForm({
defaultValues: {
username: '',
},
});
return (
(