blob: 56ba0d716bb22875f41c41e8b910b6b1f8ab4d93 (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 | import { describe, expect, it } from '@jest/globals';
import { userEvent } from '@testing-library/user-event';
import { render, screen as rtlScreen } from '../../../../../tests/utils';
import { SearchForm } from './search-form';
describe('SearchForm', () => {
  it('renders a search input with a submit button', () => {
    render(<SearchForm />);
    expect(
      rtlScreen.getByRole('searchbox', { name: 'Search for:' })
    ).toBeInTheDocument();
    expect(
      rtlScreen.getByRole('button', { name: 'Search' })
    ).toBeInTheDocument();
  });
  it('can submit the form', async () => {
    const onSubmit = jest.fn((_search: { query?: string }) => undefined);
    const user = userEvent.setup();
    const query = 'autem voluptatum eos';
    render(<SearchForm onSubmit={onSubmit} />);
    // eslint-disable-next-line @typescript-eslint/no-magic-numbers
    expect.assertions(3);
    expect(onSubmit).not.toHaveBeenCalled();
    await user.type(
      rtlScreen.getByRole('searchbox', { name: 'Search for:' }),
      query
    );
    await user.click(rtlScreen.getByRole('button', { name: 'Search' }));
    expect(onSubmit).toHaveBeenCalledTimes(1);
    expect(onSubmit).toHaveBeenCalledWith({ query });
  });
});
 |