Skip to content

Commit

Permalink
Test onexit
Browse files Browse the repository at this point in the history
  • Loading branch information
tristen committed Jan 23, 2024
1 parent 36bd73f commit 1e2f7a8
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/components/select/select.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import Select from './select';
import { render, screen, waitFor } from '@testing-library/react';
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

window.HTMLElement.prototype.hasPointerCapture = jest.fn();
Expand All @@ -15,6 +15,7 @@ const SelectTest = (props) => {
}

describe('Select', () => {
const user = userEvent.setup();
const defaultProps = {
options: [
{
Expand All @@ -36,7 +37,6 @@ describe('Select', () => {

describe('basic', () => {
test('renders', async () => {
const user = userEvent.setup();
const { baseElement } = render(<SelectTest {...defaultProps} />);
await user.click(screen.getByTestId("trigger"));
await waitFor(() => {
Expand Down Expand Up @@ -72,27 +72,41 @@ describe('Select', () => {
describe('all options', () => {
const props = {
...defaultProps,
onExit: jest.fn(),
placement: 'left',
padding: 'none',
hasPointer: false,
avoidCollisions: false,
hideWhenAnchorIsOffscreen: true,
allowPlacementAxisChange: false,
escapeCloses: false,
onExit: () => {},
passthroughProps: {
'data-testid': 'foo'
}
};

test('renders', async () => {
const user = userEvent.setup();
const { baseElement } = render(<SelectTest {...props} />);
await user.click(screen.getByTestId("trigger"));
await waitFor(() => {
expect(screen.getByRole('listbox')).toBeInTheDocument();
});
expect(baseElement).toMatchSnapshot();
});

test('esc deos not call onExit', async () => {
const { baseElement } = render(<SelectTest {...props} />);
await user.click(screen.getByTestId("trigger"));

fireEvent.keyDown(baseElement, {
key: 'Escape',
code: 'Escape',
keyCode: 27,
charCode: 27
});

await waitFor(() => {
expect(props.onExit).toHaveBeenCalled();
});
});
});
});

0 comments on commit 1e2f7a8

Please sign in to comment.