Skip to content

Commit

Permalink
[ResponseOps][Cases] clear query cache for cases flaky tests (#191212)
Browse files Browse the repository at this point in the history
## Summary

Clear query cache to fix flakiness of jest unit tests in cases plugin.


### 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

---------

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
js-jankisalvi and elasticmachine authored Sep 2, 2024
1 parent c5fe618 commit a118728
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 48 deletions.
10 changes: 9 additions & 1 deletion x-pack/plugins/cases/public/common/mock/test_providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import React, { useMemo } from 'react';
import { MemoryRouter } from 'react-router-dom';
import { render as reactRender } from '@testing-library/react';
import { render as reactRender, waitFor } from '@testing-library/react';
import type { RenderOptions, RenderResult } from '@testing-library/react';
import type { ILicense } from '@kbn/licensing-plugin/public';
import type { ScopedFilesClient } from '@kbn/files-plugin/public';
Expand Down Expand Up @@ -124,6 +124,7 @@ export interface AppMockRenderer {
queryClient: QueryClient;
AppWrapper: React.FC<{ children: React.ReactNode }>;
getFilesClient: () => ScopedFilesClient;
clearQueryCache: () => Promise<void>;
}

export const testQueryClient = new QueryClient({
Expand Down Expand Up @@ -202,6 +203,12 @@ export const createAppMockRenderer = ({
});
};

const clearQueryCache = async () => {
queryClient.getQueryCache().clear();

await waitFor(() => expect(queryClient.isFetching()).toBe(0));
};

return {
coreStart: services,
queryClient,
Expand All @@ -210,5 +217,6 @@ export const createAppMockRenderer = ({
externalReferenceAttachmentTypeRegistry,
persistableStateAttachmentTypeRegistry,
getFilesClient,
clearQueryCache,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ import type { AppMockRenderer } from '../../common/mock';
import { createAppMockRenderer } from '../../common/mock';
import { ColumnsPopover } from './columns_popover';

// FLAKY: https://github.com/elastic/kibana/issues/174682
describe.skip('ColumnsPopover', () => {
describe('ColumnsPopover', () => {
let appMockRenderer: AppMockRenderer;

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

afterEach(async () => {
await appMockRenderer.clearQueryCache();
});

const selectedColumns = [
{ field: 'title', name: 'Title', isChecked: true },
{ field: 'category', name: 'Category', isChecked: false },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ import {
jest.mock('../../containers/api');
jest.mock('../../containers/user_profiles/api');

// FLAKY: https://github.com/elastic/kibana/issues/188361
describe.skip('useActions', () => {
describe('useActions', () => {
let appMockRender: AppMockRenderer;

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

afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('renders column actions', async () => {
const { result } = renderHook(() => useActions({ disableActions: false }), {
wrapper: appMockRender.AppWrapper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

import React from 'react';
import { screen, waitFor, render } from '@testing-library/react';
import { screen, waitFor } from '@testing-library/react';
import type { AppMockRenderer } from '../../common/mock';
import { createAppMockRenderer } from '../../common/mock';
import { Severity } from './severity';
import userEvent from '@testing-library/user-event';
import { waitForEuiPopoverOpen } from '@elastic/eui/lib/test/rtl';
Expand All @@ -15,8 +17,18 @@ import { FormTestComponent } from '../../common/test_utils';
const onSubmit = jest.fn();

describe('Severity form field', () => {
let appMockRender: AppMockRenderer;

beforeEach(() => {
appMockRender = createAppMockRenderer();
});

afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('renders', async () => {
render(
appMockRender.render(
<FormTestComponent onSubmit={onSubmit}>
<Severity isLoading={false} />
</FormTestComponent>
Expand All @@ -28,7 +40,7 @@ describe('Severity form field', () => {

// default to LOW in this test configuration
it('defaults to the correct value', async () => {
render(
appMockRender.render(
<FormTestComponent onSubmit={onSubmit}>
<Severity isLoading={false} />
</FormTestComponent>
Expand All @@ -39,7 +51,7 @@ describe('Severity form field', () => {
});

it('selects the correct value when changed', async () => {
render(
appMockRender.render(
<FormTestComponent onSubmit={onSubmit}>
<Severity isLoading={false} />
</FormTestComponent>
Expand All @@ -61,7 +73,7 @@ describe('Severity form field', () => {
});

it('disables when loading data', async () => {
render(
appMockRender.render(
<FormTestComponent onSubmit={onSubmit}>
<Severity isLoading={true} />
</FormTestComponent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { FormTestComponent } from '../../common/test_utils';
import type { AppMockRenderer } from '../../common/mock';
import { createAppMockRenderer } from '../../common/mock';

// Failing: See https://github.com/elastic/kibana/issues/190270
describe.skip('SyncAlertsToggle', () => {
describe('SyncAlertsToggle', () => {
let appMockRender: AppMockRenderer;
const onSubmit = jest.fn();
const defaultFormProps = {
Expand All @@ -31,6 +30,10 @@ describe.skip('SyncAlertsToggle', () => {
appMockRender = createAppMockRenderer();
});

afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('it renders', async () => {
appMockRender.render(
<FormTestComponent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ const caseData: CaseUI = {
comments: [...basicCase.comments, alertCommentWithIndices],
};

// FLAKY: https://github.com/elastic/kibana/issues/185046
describe.skip('Case View Page files tab', () => {
describe('Case View Page files tab', () => {
let appMockRender: AppMockRenderer;

useGetCaseFilesMock.mockReturnValue({
Expand All @@ -37,10 +36,11 @@ describe.skip('Case View Page files tab', () => {

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

afterEach(() => {
jest.clearAllMocks();
afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('should render the utility bar for the files table', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ import { customFieldsMock, customFieldsConfigurationMock } from '../../../contai
import userEvent from '@testing-library/user-event';
import { CustomFieldTypes } from '../../../../common/types/domain';

describe.skip('Case View Page files tab', () => {
describe('Case View Page files tab', () => {
const onSubmit = jest.fn();
let appMockRender: AppMockRenderer;

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

afterEach(() => {
jest.clearAllMocks();
afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('should render the custom fields correctly', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ jest.mock('../../common/lib/kibana');

const useKibanaMock = useKibana as jest.Mocked<typeof useKibana>;

// FLAKY: https://github.com/elastic/kibana/issues/189530
describe.skip('ConnectorSelector', () => {
describe('ConnectorSelector', () => {
const handleChange = jest.fn();
const defaultProps = {
connectors: [],
Expand All @@ -45,6 +44,10 @@ describe.skip('ConnectorSelector', () => {
});
});

afterEach(async () => {
await appMock.clearQueryCache();
});

it('should render', async () => {
appMock.render(
<FormTestComponent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { createAppMockRenderer } from '../../common/mock';
import { templatesConfigurationMock } from '../../containers/mock';
import { TemplateSelector } from './templates';

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

Expand All @@ -23,6 +22,10 @@ describe.skip('CustomFields', () => {
appMockRender = createAppMockRenderer();
});

afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('renders correctly', async () => {
appMockRender.render(
<TemplateSelector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { FormTestComponent } from '../../common/test_utils';
import { CustomFieldTypes } from '../../../common/types/domain';
import { FormFields } from './form_fields';

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

Expand All @@ -25,6 +24,10 @@ describe.skip('FormFields ', () => {
appMockRender = createAppMockRenderer();
});

afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('renders correctly', async () => {
appMockRender.render(
<FormTestComponent onSubmit={onSubmit}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ const defaultProps = {
isLoadingDescription: false,
};

// Failing: See https://github.com/elastic/kibana/issues/185879
describe.skip('Description', () => {
describe('Description', () => {
const onUpdateField = jest.fn();
let appMockRender: AppMockRenderer;

Expand All @@ -37,6 +36,10 @@ describe.skip('Description', () => {
appMockRender = createAppMockRenderer();
});

afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('renders description correctly', async () => {
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ import { createAppMockRenderer } from '../../common/mock';
import { basicFileMock } from '../../containers/mock';
import { FileAttachmentEvent } from './file_attachment_event';

// FLAKY: https://github.com/elastic/kibana/issues/174661
describe.skip('FileAttachmentEvent', () => {
describe('FileAttachmentEvent', () => {
let appMockRender: AppMockRenderer;

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

afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('renders clickable name', async () => {
appMockRender.render(
<FileAttachmentEvent file={basicFileMock as unknown as DownloadableFile} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ import { createAppMockRenderer, mockedTestProvidersOwner } from '../../common/mo
import { basicFileMock } from '../../containers/mock';
import { FilePreview } from './file_preview';

// FLAKY: https://github.com/elastic/kibana/issues/182364
describe.skip('FilePreview', () => {
describe('FilePreview', () => {
let appMockRender: AppMockRenderer;

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

afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('FilePreview rendered correctly', async () => {
appMockRender.render(<FilePreview closePreview={jest.fn()} selectedFile={basicFileMock} />);

Expand Down
12 changes: 9 additions & 3 deletions x-pack/plugins/cases/public/components/files/file_type.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import { basicCase, basicFileMock } from '../../containers/mock';
import { getFileType } from './file_type';
import { FILE_ATTACHMENT_TYPE } from '../../../common/constants';

// FLAKY: https://github.com/elastic/kibana/issues/175841
describe.skip('getFileType', () => {
describe('getFileType', () => {
const fileType = getFileType();

it('invalid props return blank FileAttachmentViewObject', () => {
Expand All @@ -45,6 +44,10 @@ describe.skip('getFileType', () => {
jest.clearAllMocks();
});

afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('event renders a clickable name if the file is an image', async () => {
appMockRender = createAppMockRenderer();

Expand Down Expand Up @@ -142,7 +145,10 @@ describe.skip('getFileType', () => {

it('empty externalReferenceMetadata returns blank FileAttachmentViewObject', () => {
expect(
fileType.getAttachmentViewObject({ ...attachmentViewProps, externalReferenceMetadata: {} })
fileType.getAttachmentViewObject({
...attachmentViewProps,
externalReferenceMetadata: {},
})
).toEqual({
event: 'added an unknown file',
hideDefaultActions: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { createAppMockRenderer } from '../../common/mock';

import { FilterPopover } from '.';

// FLAKY: https://github.com/elastic/kibana/issues/176679
describe.skip('FilterPopover ', () => {
describe('FilterPopover ', () => {
let appMockRender: AppMockRenderer;
const onSelectedOptionsChanged = jest.fn();
const tags: string[] = ['coke', 'pepsi'];
Expand All @@ -26,6 +25,10 @@ describe.skip('FilterPopover ', () => {
jest.clearAllMocks();
});

afterEach(async () => {
await appMockRender.clearQueryCache();
});

it('renders button label correctly', async () => {
appMockRender.render(
<FilterPopover
Expand Down
Loading

0 comments on commit a118728

Please sign in to comment.