Skip to content

Commit

Permalink
fix(Dropdown): flaky tests test (#1851)
Browse files Browse the repository at this point in the history
* empty

* fix: remove sleep
  • Loading branch information
saurabhdaware authored Nov 29, 2023
1 parent 1cbbe5b commit 2c06730
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Counter } from '../../Counter';
import renderWithTheme from '~utils/testing/renderWithTheme.native';
import { Button } from '~components/Button';
import { Badge } from '~components/Badge';
jest.useFakeTimers();

describe('<BottomSheet />', () => {
test('should render Header/Footer/Body properly', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Text } from '~components/Typography';
import { Badge } from '~components/Badge';
import { Counter } from '~components/Counter';
import { Button } from '~components/Button';
jest.useFakeTimers();

describe('<BottomSheet />', () => {
it('should render a BottomSheet ssr', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ jest.mock('~utils/useId', () => ({
useId: () => 'dropdown-456',
}));

export const sleep = (delay = 10): Promise<number> =>
new Promise((resolve) => setTimeout(resolve, delay));

const SingleSelectContent = (): React.ReactElement => {
return (
<ActionList>
Expand Down Expand Up @@ -159,11 +156,9 @@ describe('<BottomSheet />', () => {

expect(queryByText('BottomSheet body')).not.toBeInTheDocument();
await user.click(getByRole('button', { name: /open/i }));
await sleep(250);
expect(queryByText('BottomSheet body')).toBeInTheDocument();
await waitFor(() => expect(queryByText('BottomSheet body')).toBeVisible());
await user.click(queryByTestId('bottomsheet-backdrop')!);
await sleep(250);
await waitFor(() => expect(queryByText('BottomSheet body')).not.toBeInTheDocument());
await waitFor(() => expect(queryByText('BottomSheet body')).not.toBeVisible());
mockConsoleError.mockRestore();
});

Expand Down Expand Up @@ -263,28 +258,27 @@ describe('<BottomSheet />', () => {
expect(queryByTestId('bottomsheet-body')).not.toBeVisible();
expect(getByRole('combobox', { name: 'Select Action' })).toBeInTheDocument();
await user.click(getByRole('combobox', { name: 'Select Action' }));
await sleep(250);
expect(queryByTestId('bottomsheet-body')).toBeVisible();

await waitFor(() => expect(queryByTestId('bottomsheet-body')).toBeVisible());
await user.click(queryByTestId('bottomsheet-backdrop')!);
await sleep(250);
expect(queryByTestId('bottomsheet-body')).not.toBeVisible();
await waitFor(() => expect(queryByTestId('bottomsheet-body')).not.toBeVisible());

// close by selecting an element & assert the select's value
await user.click(getByRole('combobox', { name: 'Select Action' }));
await sleep(250);
expect(queryByTestId('bottomsheet-body')).toBeVisible();
await waitFor(() => expect(queryByTestId('bottomsheet-body')).toBeVisible());
await user.click(getByRole('option', { name: 'Settings' }));
await sleep(250);
expect(getByRole('combobox', { name: 'Select Action' })).toHaveTextContent('Settings');
await waitFor(() =>
expect(getByRole('combobox', { name: 'Select Action' })).toHaveTextContent('Settings'),
);
expect(queryByTestId('bottomsheet-body')).not.toBeVisible();

// check that cancelling should not update select's value
await user.click(getByRole('combobox', { name: 'Select Action' }));
await sleep(250);
expect(queryByTestId('bottomsheet-body')).toBeVisible();
await waitFor(() => expect(queryByTestId('bottomsheet-body')).toBeVisible());
await user.click(getByRole('button', { name: /Close/i })!);
await sleep(250);
expect(getByRole('combobox', { name: 'Select Action' })).toHaveTextContent('Settings');
await waitFor(() =>
expect(getByRole('combobox', { name: 'Select Action' })).toHaveTextContent('Settings'),
);
expect(queryByTestId('bottomsheet-body')).not.toBeVisible();
mockConsoleError.mockRestore();
});
Expand Down Expand Up @@ -316,8 +310,7 @@ describe('<BottomSheet />', () => {
expect(queryByTestId('bottomsheet-body')).not.toBeVisible();
expect(selectInput).toBeInTheDocument();
await user.click(selectInput);
await sleep(250);
expect(queryByTestId('bottomsheet-body')).toBeVisible();
await waitFor(() => expect(queryByTestId('bottomsheet-body')).toBeVisible());

// assert no items selected
expect(selectInput).toHaveTextContent('Select Option');
Expand All @@ -339,8 +332,7 @@ describe('<BottomSheet />', () => {

// open again and ensure the previously selected elements are there
await user.click(selectInput);
await sleep(250);
expect(queryByTestId('bottomsheet-body')).toBeVisible();
await waitFor(() => expect(queryByTestId('bottomsheet-body')).toBeVisible());
expect(queryAllByLabelText('Close Apple tag')[0]).toBeInTheDocument();
expect(queryAllByLabelText('Close Orange tag')[0]).toBeInTheDocument();

Expand Down Expand Up @@ -415,27 +407,22 @@ describe('<BottomSheet />', () => {
expect(queryByTestId('bottomsheet-body')).not.toBeVisible();
expect(getByRole('button', { name: 'Status: approve' })).toBeInTheDocument();
await user.click(getByRole('button', { name: 'Status: approve' }));
await sleep(250);
expect(queryByTestId('bottomsheet-body')).toBeVisible();
await waitFor(() => expect(queryByTestId('bottomsheet-body')).toBeVisible());
await user.click(queryByTestId('bottomsheet-backdrop')!);
await sleep(250);
expect(queryByTestId('bottomsheet-body')).not.toBeVisible();
await waitFor(() => expect(queryByTestId('bottomsheet-body')).not.toBeVisible());

// close by selecting an element & assert the select's value
await user.click(getByRole('button', { name: 'Status: approve' }));
await sleep(250);
expect(queryByTestId('bottomsheet-body')).toBeVisible();
await waitFor(() => expect(queryByTestId('bottomsheet-body')).toBeVisible());
await user.click(getByRole('menuitem', { name: 'In Progress' }));
await sleep(250);
expect(queryByTestId('bottomsheet-body')).not.toBeVisible();
await waitFor(() => expect(queryByTestId('bottomsheet-body')).not.toBeVisible());

// check that cancelling should not update select's value
await user.click(getByRole('button', { name: 'Status: in-progress' }));
await sleep(250);
expect(queryByTestId('bottomsheet-body')).toBeVisible();
await waitFor(() => expect(queryByTestId('bottomsheet-body')).toBeVisible());
await user.click(getByRole('button', { name: /Close/i })!);
await sleep(250);
expect(queryByTestId('bottomsheet-body')).not.toBeVisible();

await waitFor(() => expect(queryByTestId('bottomsheet-body')).not.toBeVisible());
mockConsoleError.mockRestore();
});

Expand Down

0 comments on commit 2c06730

Please sign in to comment.