Skip to content

Commit

Permalink
[Cases] Fix create case submit button flaky test (#174570)
Browse files Browse the repository at this point in the history
## Summary

Fixes: #174376

Successful runs:
https://buildkite.com/elastic/kibana-pull-request/builds?branch=cnasikas%3Afix_174376

### 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)
  • Loading branch information
cnasikas authored Jan 10, 2024
1 parent d54898d commit 30ce43b
Showing 1 changed file with 27 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,55 @@
*/

import React from 'react';
import { mount } from 'enzyme';
import { waitFor } from '@testing-library/react';
import { waitFor, screen } from '@testing-library/react';

import { useForm, Form } from '@kbn/es-ui-shared-plugin/static/forms/hook_form_lib';
import { SubmitCaseButton } from './submit_button';
import type { FormProps } from './schema';
import { schema } from './schema';
import type { AppMockRenderer } from '../../common/mock';
import { createAppMockRenderer } from '../../common/mock';
import { FormTestComponent } from '../../common/test_utils';
import userEvent from '@testing-library/user-event';

// FLAKY: https://github.com/elastic/kibana/issues/174376
describe.skip('SubmitCaseButton', () => {
describe('SubmitCaseButton', () => {
let appMockRender: AppMockRenderer;
const onSubmit = jest.fn();

const MockHookWrapperComponent: React.FC = ({ children }) => {
const { form } = useForm<FormProps>({
defaultValue: { title: 'My title' },
schema: {
title: schema.title,
},
onSubmit,
});

return <Form form={form}>{children}</Form>;
};

beforeEach(() => {
jest.clearAllMocks();
appMockRender = createAppMockRenderer();
});

it('it renders', async () => {
const wrapper = mount(
<MockHookWrapperComponent>
it('renders', async () => {
appMockRender.render(
<FormTestComponent onSubmit={onSubmit}>
<SubmitCaseButton />
</MockHookWrapperComponent>
</FormTestComponent>
);

expect(wrapper.find(`[data-test-subj="create-case-submit"]`).exists()).toBeTruthy();
expect(await screen.findByTestId('create-case-submit')).toBeInTheDocument();
});

it('it submits', async () => {
const wrapper = mount(
<MockHookWrapperComponent>
it('submits', async () => {
appMockRender.render(
<FormTestComponent onSubmit={onSubmit}>
<SubmitCaseButton />
</MockHookWrapperComponent>
</FormTestComponent>
);
wrapper.find(`button[data-test-subj="create-case-submit"]`).first().simulate('click');
await waitFor(() => expect(onSubmit).toBeCalled());
});

it('it disables when submitting', async () => {
const wrapper = mount(
<MockHookWrapperComponent>
<SubmitCaseButton />
</MockHookWrapperComponent>
);
userEvent.click(await screen.findByTestId('create-case-submit'));

wrapper.find(`button[data-test-subj="create-case-submit"]`).first().simulate('click');
await waitFor(() =>
expect(
wrapper.find(`[data-test-subj="create-case-submit"]`).first().prop('isDisabled')
).toBeTruthy()
);
await waitFor(() => expect(onSubmit).toBeCalled());
});

it('it is loading when submitting', async () => {
const wrapper = mount(
<MockHookWrapperComponent>
it('disables when submitting', async () => {
appMockRender.render(
<FormTestComponent onSubmit={onSubmit}>
<SubmitCaseButton />
</MockHookWrapperComponent>
</FormTestComponent>
);

wrapper.find(`button[data-test-subj="create-case-submit"]`).first().simulate('click');
await waitFor(() =>
expect(
wrapper.find(`[data-test-subj="create-case-submit"]`).first().prop('isLoading')
).toBeTruthy()
);
const button = await screen.findByTestId('create-case-submit');
userEvent.click(button);

await waitFor(() => expect(button).toBeDisabled());
});
});

0 comments on commit 30ce43b

Please sign in to comment.