Skip to content

Commit

Permalink
[Cases] Fix useGetCases flaky test (#181254)
Browse files Browse the repository at this point in the history
## Summary

Following @JiaweiWu advice I replaced `waitForNextUpdate` with
`waitFor`.

Fix: #178163

### Checklist

Delete any items that are not applicable to this PR.

- [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: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
cnasikas and kibanamachine committed Apr 22, 2024
1 parent 0682fd3 commit d5c1335
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions x-pack/plugins/cases/public/containers/use_get_cases.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import { OWNERS } from '../../common/constants';
jest.mock('./api');
jest.mock('../common/lib/kibana/hooks');

// FLAKY: https://github.com/elastic/kibana/issues/178163
describe.skip('useGetCases', () => {
describe('useGetCases', () => {
const abortCtrl = new AbortController();
const addSuccess = jest.fn();
(useToasts as jest.Mock).mockReturnValue({ addSuccess, addError: jest.fn() });
Expand All @@ -32,11 +31,14 @@ describe.skip('useGetCases', () => {

it('calls getCases with correct arguments', async () => {
const spyOnGetCases = jest.spyOn(api, 'getCases');
const { waitForNextUpdate } = renderHook(() => useGetCases(), {
const { waitFor } = renderHook(() => useGetCases(), {
wrapper: appMockRender.AppWrapper,
});

await waitForNextUpdate();
await waitFor(() => {
expect(spyOnGetCases).toBeCalled();
});

expect(spyOnGetCases).toBeCalledWith({
filterOptions: { ...DEFAULT_FILTER_OPTIONS, owner: ['securitySolution'] },
queryParams: DEFAULT_QUERY_PARAMS,
Expand All @@ -53,12 +55,13 @@ describe.skip('useGetCases', () => {
const addError = jest.fn();
(useToasts as jest.Mock).mockReturnValue({ addSuccess, addError });

const { waitForNextUpdate } = renderHook(() => useGetCases(), {
const { waitFor } = renderHook(() => useGetCases(), {
wrapper: appMockRender.AppWrapper,
});

await waitForNextUpdate();
expect(addError).toHaveBeenCalled();
await waitFor(() => {
expect(addError).toHaveBeenCalled();
});
});

it('should set all owners when no owner is provided', async () => {
Expand Down Expand Up @@ -87,11 +90,13 @@ describe.skip('useGetCases', () => {
};

const spyOnGetCases = jest.spyOn(api, 'getCases');
const { waitForNextUpdate } = renderHook(() => useGetCases(), {
const { waitFor } = renderHook(() => useGetCases(), {
wrapper: appMockRender.AppWrapper,
});

await waitForNextUpdate();
await waitFor(() => {
expect(spyOnGetCases).toHaveBeenCalled();
});

expect(spyOnGetCases).toBeCalledWith({
filterOptions: { ...DEFAULT_FILTER_OPTIONS, owner: [...OWNERS] },
Expand All @@ -104,11 +109,13 @@ describe.skip('useGetCases', () => {
appMockRender = createAppMockRenderer({ owner: [] });
const spyOnGetCases = jest.spyOn(api, 'getCases');

const { waitForNextUpdate } = renderHook(() => useGetCases(), {
const { waitFor } = renderHook(() => useGetCases(), {
wrapper: appMockRender.AppWrapper,
});

await waitForNextUpdate();
await waitFor(() => {
expect(spyOnGetCases).toHaveBeenCalled();
});

expect(spyOnGetCases).toBeCalledWith({
filterOptions: { ...DEFAULT_FILTER_OPTIONS, owner: ['cases'] },
Expand All @@ -121,11 +128,13 @@ describe.skip('useGetCases', () => {
appMockRender = createAppMockRenderer({ owner: ['observability'] });
const spyOnGetCases = jest.spyOn(api, 'getCases');

const { waitForNextUpdate } = renderHook(() => useGetCases(), {
const { waitFor } = renderHook(() => useGetCases(), {
wrapper: appMockRender.AppWrapper,
});

await waitForNextUpdate();
await waitFor(() => {
expect(spyOnGetCases).toHaveBeenCalled();
});

expect(spyOnGetCases).toBeCalledWith({
filterOptions: { ...DEFAULT_FILTER_OPTIONS, owner: ['observability'] },
Expand All @@ -138,14 +147,13 @@ describe.skip('useGetCases', () => {
appMockRender = createAppMockRenderer({ owner: ['observability'] });
const spyOnGetCases = jest.spyOn(api, 'getCases');

const { waitForNextUpdate } = renderHook(
() => useGetCases({ filterOptions: { owner: ['my-owner'] } }),
{
wrapper: appMockRender.AppWrapper,
}
);
const { waitFor } = renderHook(() => useGetCases({ filterOptions: { owner: ['my-owner'] } }), {
wrapper: appMockRender.AppWrapper,
});

await waitForNextUpdate();
await waitFor(() => {
expect(spyOnGetCases).toHaveBeenCalled();
});

expect(spyOnGetCases).toBeCalledWith({
filterOptions: { ...DEFAULT_FILTER_OPTIONS, owner: ['my-owner'] },
Expand Down

0 comments on commit d5c1335

Please sign in to comment.