Skip to content

Commit

Permalink
fix timeouts for jest tests in x-pack/plugins/cases
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Sep 5, 2024
1 parent 3fdf053 commit 8669579
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';
import { waitFor, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import userEvent, { type UserEvent } from '@testing-library/user-event';

import { connector, choices } from '../mock';
import { useGetChoices } from './use_get_choices';
Expand All @@ -23,6 +23,16 @@ const useGetChoicesMock = useGetChoices as jest.Mock;
let appMockRenderer: AppMockRenderer;

describe('ServiceNowITSM Fields', () => {
let user: UserEvent;

beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

const fields = {
severity: '1',
urgency: '2',
Expand All @@ -32,6 +42,10 @@ describe('ServiceNowITSM Fields', () => {
};

beforeEach(() => {
// Workaround for timeout via https://github.com/testing-library/user-event/issues/833#issuecomment-1171452841
user = userEvent.setup({
advanceTimers: jest.advanceTimersByTime,
});
appMockRenderer = createAppMockRenderer();
useGetChoicesMock.mockReturnValue({
isLoading: false,
Expand Down Expand Up @@ -169,7 +183,7 @@ describe('ServiceNowITSM Fields', () => {
);

const select = await screen.findByTestId(`${subj}Select`);
await userEvent.selectOptions(select, '4 - Low');
await user.selectOptions(select, '4 - Low');

expect(select).toHaveValue('4');
})
Expand All @@ -194,19 +208,19 @@ describe('ServiceNowITSM Fields', () => {
];

for (const [element] of selectables) {
await userEvent.selectOptions(element, ['2']);
await user.selectOptions(element, ['2']);
}
const categorySelect = await screen.findByTestId('categorySelect');

expect(await within(categorySelect).findByRole('option', { name: 'Software' }));

await userEvent.selectOptions(categorySelect, ['software']);
await user.selectOptions(categorySelect, ['software']);

const subcategorySelect = await screen.findByTestId('subcategorySelect');

expect(await within(subcategorySelect).findByRole('option', { name: 'Operation System' }));

await userEvent.selectOptions(subcategorySelect, ['os']);
await user.selectOptions(subcategorySelect, ['os']);

expect(severitySelect).toHaveValue('2');
expect(urgencySelect).toHaveValue('2');
Expand All @@ -226,15 +240,15 @@ describe('ServiceNowITSM Fields', () => {

expect(await within(categorySelect).findByRole('option', { name: 'Software' }));

await userEvent.selectOptions(categorySelect, ['software']);
await user.selectOptions(categorySelect, ['software']);

const subcategorySelect = await screen.findByTestId('subcategorySelect');

expect(await within(subcategorySelect).findByRole('option', { name: 'Operation System' }));

expect(subcategorySelect).toHaveValue('os');

await userEvent.selectOptions(categorySelect, ['Privilege Escalation']);
await user.selectOptions(categorySelect, ['Privilege Escalation']);

await waitFor(() => {
expect(subcategorySelect).not.toHaveValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React from 'react';
import { waitForEuiPopoverOpen } from '@elastic/eui/lib/test/rtl';
import { waitFor, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import userEvent, { type UserEvent } from '@testing-library/user-event';
import type { AppMockRenderer } from '../../../common/mock';
import {
noCasesPermissions,
Expand All @@ -18,6 +18,7 @@ import {
import { AlertPropertyActions } from './alert_property_actions';

describe('AlertPropertyActions', () => {
let user: UserEvent;
let appMock: AppMockRenderer;

const props = {
Expand All @@ -26,8 +27,20 @@ describe('AlertPropertyActions', () => {
onDelete: jest.fn(),
};

beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

beforeEach(() => {
jest.clearAllMocks();
// Workaround for timeout via https://github.com/testing-library/user-event/issues/833#issuecomment-1171452841
user = userEvent.setup({
advanceTimers: jest.advanceTimersByTime,
});
appMock = createAppMockRenderer();
});

Expand All @@ -40,7 +53,7 @@ describe('AlertPropertyActions', () => {

expect(await screen.findByTestId('property-actions-user-action')).toBeInTheDocument();

await userEvent.click(await screen.findByTestId('property-actions-user-action-ellipses'));
await user.click(await screen.findByTestId('property-actions-user-action-ellipses'));
await waitForEuiPopoverOpen();

expect((await screen.findByTestId('property-actions-user-action-group')).children.length).toBe(
Expand All @@ -57,10 +70,10 @@ describe('AlertPropertyActions', () => {

expect(await screen.findByTestId('property-actions-user-action')).toBeInTheDocument();

await userEvent.click(await screen.findByTestId('property-actions-user-action-ellipses'));
await user.click(await screen.findByTestId('property-actions-user-action-ellipses'));
await waitForEuiPopoverOpen();

await userEvent.click(await screen.findByTestId('property-actions-user-action-minusInCircle'));
await user.click(await screen.findByTestId('property-actions-user-action-minusInCircle'));

expect(await screen.findByTestId('property-actions-confirm-modal')).toBeInTheDocument();

Expand All @@ -73,14 +86,14 @@ describe('AlertPropertyActions', () => {

expect(await screen.findByTestId('property-actions-user-action')).toBeInTheDocument();

await userEvent.click(await screen.findByTestId('property-actions-user-action-ellipses'));
await user.click(await screen.findByTestId('property-actions-user-action-ellipses'));
await waitForEuiPopoverOpen();

await userEvent.click(await screen.findByTestId('property-actions-user-action-minusInCircle'));
await user.click(await screen.findByTestId('property-actions-user-action-minusInCircle'));

expect(await screen.findByTestId('property-actions-confirm-modal')).toBeInTheDocument();

await userEvent.click(await screen.findByText('Remove'));
await user.click(await screen.findByText('Remove'));

await waitFor(() => {
expect(props.onDelete).toHaveBeenCalled();
Expand Down

0 comments on commit 8669579

Please sign in to comment.