Skip to content

Commit

Permalink
[@mantine/form] Add form.watch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rtivital committed Apr 8, 2024
1 parent 3440af1 commit f8bd5c4
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/@mantine/form/src/tests/watch.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import type { FormFieldSubscriber } from '../types';
import { useForm } from '../use-form';

function TestComponent({ watch }: { watch: FormFieldSubscriber<any, any> }) {
const form = useForm({ initialValues: { name: 'test', area: 'area' } });
form.watch('name', watch);

return (
<>
<input {...form.getInputProps('name')} aria-label="name" />
<input {...form.getInputProps('area')} aria-label="area" />
</>
);
}

describe('@mantine/form/watch', () => {
it('allows observing field changes', async () => {
const spy = jest.fn();
render(<TestComponent watch={spy} />);
expect(spy).not.toHaveBeenCalled();

await userEvent.type(screen.getByLabelText('name'), '1');

expect(spy).toHaveBeenCalledWith({
previousValue: 'test',
value: 'test1',
touched: true,
dirty: true,
});

await userEvent.type(screen.getByLabelText('name'), '{backspace}');

expect(spy).toHaveBeenCalledWith({
previousValue: 'test1',
value: 'test',
touched: true,
dirty: false,
});
});

it('does now call subscriber function when other field changes', async () => {
const spy = jest.fn();
render(<TestComponent watch={spy} />);
expect(spy).not.toHaveBeenCalled();

await userEvent.type(screen.getByLabelText('area'), '1');

expect(spy).not.toHaveBeenCalled();
});
});

0 comments on commit f8bd5c4

Please sign in to comment.