emilkowalski / vaul

A drawer component for React.
https://vaul.emilkowal.ski
MIT License
6.02k stars 203 forks source link

Unit test with Jest and testing libray #240

Closed tmoutinho closed 2 weeks ago

tmoutinho commented 8 months ago

Hi,

Is there an example on how you can test opening a Drawer and testing the content of the drawer? Tried several times but the Drawer cannot seem to open, even we an fireEvent.

Thanks

fireEvent.submit(screen.getByRole('button', { name: /open drawer/i, }));

await waitFor(() => {
   expect(screen.getByText(/content/i)).toBeInTheDocument();
});
k1tikurisu commented 4 months ago

@tmoutinho

it works for me

describe('Drawer', () => {
  beforeEach(() => {
    Object.defineProperty(window, 'matchMedia', {
      writable: true,
      value: jest.fn().mockImplementation((query) => ({
        matches: false,
        media: query,
        onchange: null,
        addListener: jest.fn(),
        removeListener: jest.fn(),
        addEventListener: jest.fn(),
        removeEventListener: jest.fn(),
        dispatchEvent: jest.fn(),
      })),
    });
  });

  test('drawer open', async () => {
    render(<CustomDrawer />);

    const button = screen.getByRole('button');

    // click button
    await act(() => userEvent.click(button));
  });
});