Skip to content

Commit

Permalink
[Cases] fix serviceNow connector flaky test (#170537)
Browse files Browse the repository at this point in the history
## Summary

Use `use await findByTestId` to fix flaky tests

fixes #167384
fixes #167385

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
js-jankisalvi and kibanamachine committed Nov 6, 2023
1 parent 4053f71 commit 42fc7ea
Showing 1 changed file with 31 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ describe('ServiceNowITSM Fields', () => {
jest.clearAllMocks();
});

it('all params fields are rendered', () => {
it('all params fields are rendered', async () => {
appMockRenderer.render(
<MockFormWrapperComponent fields={fields}>
<Fields connector={connector} />
</MockFormWrapperComponent>
);

expect(screen.getByTestId('severitySelect')).toBeInTheDocument();
expect(screen.getByTestId('urgencySelect')).toBeInTheDocument();
expect(screen.getByTestId('impactSelect')).toBeInTheDocument();
expect(screen.getByTestId('categorySelect')).toBeInTheDocument();
expect(screen.getByTestId('subcategorySelect')).toBeInTheDocument();
expect(await screen.findByTestId('severitySelect')).toBeInTheDocument();
expect(await screen.findByTestId('urgencySelect')).toBeInTheDocument();
expect(await screen.findByTestId('impactSelect')).toBeInTheDocument();
expect(await screen.findByTestId('categorySelect')).toBeInTheDocument();
expect(await screen.findByTestId('subcategorySelect')).toBeInTheDocument();
});

it('transforms the categories to options correctly', async () => {
Expand All @@ -62,11 +62,11 @@ describe('ServiceNowITSM Fields', () => {
</MockFormWrapperComponent>
);

expect(screen.getByRole('option', { name: 'Privilege Escalation' }));
expect(screen.getByRole('option', { name: 'Criminal activity/investigation' }));
expect(screen.getByRole('option', { name: 'Denial of Service' }));
expect(screen.getByRole('option', { name: 'Software' }));
expect(screen.getByRole('option', { name: 'Failed Login' }));
expect(await screen.findByRole('option', { name: 'Privilege Escalation' }));
expect(await screen.findByRole('option', { name: 'Criminal activity/investigation' }));
expect(await screen.findByRole('option', { name: 'Denial of Service' }));
expect(await screen.findByRole('option', { name: 'Software' }));
expect(await screen.findByRole('option', { name: 'Failed Login' }));
});

it('transforms the subcategories to options correctly', async () => {
Expand All @@ -76,7 +76,7 @@ describe('ServiceNowITSM Fields', () => {
</MockFormWrapperComponent>
);

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

it('transforms the options correctly', async () => {
Expand All @@ -86,6 +86,8 @@ describe('ServiceNowITSM Fields', () => {
</MockFormWrapperComponent>
);

expect(await screen.findByTestId('connector-fields-sn-itsm')).toBeInTheDocument();

const testers = ['severity', 'urgency', 'impact'];
testers.forEach((subj) => {
const select = within(screen.getByTestId(`${subj}Select`));
Expand All @@ -106,7 +108,7 @@ describe('ServiceNowITSM Fields', () => {
</MockFormWrapperComponent>
);

expect(screen.getByTestId('deprecated-connector-warning-callout')).toBeInTheDocument();
expect(await screen.findByTestId('deprecated-connector-warning-callout')).toBeInTheDocument();
});

it('does not show the deprecated callout when the connector is not deprecated', async () => {
Expand Down Expand Up @@ -135,7 +137,7 @@ describe('ServiceNowITSM Fields', () => {
</MockFormWrapperComponent>
);

expect(screen.queryByTestId('deprecated-connector-warning-callout')).toBeInTheDocument();
expect(await screen.findByTestId('deprecated-connector-warning-callout')).toBeInTheDocument();
});

it('shows the subcategory if the selected category does not have subcategories', async () => {
Expand All @@ -152,21 +154,21 @@ describe('ServiceNowITSM Fields', () => {
</MockFormWrapperComponent>
);

expect(screen.getByTestId('subcategorySelect')).toBeInTheDocument();
expect(screen.getByTestId('subcategorySelect')).not.toHaveValue();
expect(await screen.findByTestId('subcategorySelect')).toBeInTheDocument();
expect(await screen.findByTestId('subcategorySelect')).not.toHaveValue();
});

describe('changing selectables', () => {
const testers = ['severity', 'urgency', 'impact'];
testers.forEach((subj) =>
it(`${subj.toUpperCase()}`, () => {
it(`${subj.toUpperCase()}`, async () => {
appMockRenderer.render(
<MockFormWrapperComponent fields={fields}>
<Fields connector={connector} />
</MockFormWrapperComponent>
);

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

expect(select).toHaveValue('4');
Expand All @@ -181,9 +183,9 @@ describe('ServiceNowITSM Fields', () => {
</MockFormWrapperComponent>
);

const severitySelect = screen.getByTestId('severitySelect');
const urgencySelect = screen.getByTestId('urgencySelect');
const impactSelect = screen.getByTestId('impactSelect');
const severitySelect = await screen.findByTestId('severitySelect');
const urgencySelect = await screen.findByTestId('urgencySelect');
const impactSelect = await screen.findByTestId('impactSelect');

const selectables: Array<[HTMLElement, 'severity' | 'urgency' | 'impact']> = [
[severitySelect, 'severity'],
Expand All @@ -195,19 +197,15 @@ describe('ServiceNowITSM Fields', () => {
userEvent.selectOptions(element, ['2']);
});

const categorySelect = screen.getByTestId('categorySelect');
const categorySelect = await screen.findByTestId('categorySelect');

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

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

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

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

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

Expand All @@ -225,19 +223,15 @@ describe('ServiceNowITSM Fields', () => {
</MockFormWrapperComponent>
);

const categorySelect = screen.getByTestId('categorySelect');
const categorySelect = await screen.findByTestId('categorySelect');

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

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

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

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

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

Expand Down

0 comments on commit 42fc7ea

Please sign in to comment.