Skip to content

Commit

Permalink
[SecuritySolution] Replace act with waitFor (elastic#80648) (elastic#…
Browse files Browse the repository at this point in the history
…80702)

* replace act with waitFor

* update unit test

* update unit test

* update unit test
  • Loading branch information
angorayc authored Oct 15, 2020
1 parent 5bd4eb8 commit 088cc19
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { mount, ReactWrapper, shallow } from 'enzyme';
import React from 'react';
import * as redux from 'react-redux';
import { act } from 'react-dom/test-utils';
import { waitFor } from '@testing-library/react';

import '../../../common/mock/match_media';
import { useIndexPatterns } from '../../../common/hooks/use_index_patterns';
Expand Down Expand Up @@ -107,20 +107,19 @@ describe('EmbeddedMapComponent', () => {

(createEmbeddable as jest.Mock).mockResolvedValue(mockCreateEmbeddable);

let wrapper: ReactWrapper;
await act(async () => {
wrapper = mount(
<TestProviders>
<EmbeddedMapComponent {...testProps} />
</TestProviders>
);
});
const wrapper: ReactWrapper = mount(
<TestProviders>
<EmbeddedMapComponent {...testProps} />
</TestProviders>
);

wrapper!.update();
await waitFor(() => {
wrapper.update();

expect(wrapper!.find('[data-test-subj="EmbeddablePanel"]').exists()).toEqual(true);
expect(wrapper!.find('[data-test-subj="IndexPatternsMissingPrompt"]').exists()).toEqual(false);
expect(wrapper!.find('[data-test-subj="loading-panel"]').exists()).toEqual(false);
expect(wrapper.find('[data-test-subj="EmbeddablePanel"]').exists()).toEqual(true);
expect(wrapper.find('[data-test-subj="IndexPatternsMissingPrompt"]').exists()).toEqual(false);
expect(wrapper.find('[data-test-subj="loading-panel"]').exists()).toEqual(false);
});
});

test('renders IndexPatternsMissingPrompt', async () => {
Expand All @@ -132,20 +131,18 @@ describe('EmbeddedMapComponent', () => {

(createEmbeddable as jest.Mock).mockResolvedValue(mockCreateEmbeddable);

let wrapper: ReactWrapper;
await act(async () => {
wrapper = mount(
<TestProviders>
<EmbeddedMapComponent {...testProps} />
</TestProviders>
);
});

wrapper!.update();
const wrapper: ReactWrapper = mount(
<TestProviders>
<EmbeddedMapComponent {...testProps} />
</TestProviders>
);
await waitFor(() => {
wrapper.update();

expect(wrapper!.find('[data-test-subj="EmbeddablePanel"]').exists()).toEqual(false);
expect(wrapper!.find('[data-test-subj="IndexPatternsMissingPrompt"]').exists()).toEqual(true);
expect(wrapper!.find('[data-test-subj="loading-panel"]').exists()).toEqual(false);
expect(wrapper.find('[data-test-subj="EmbeddablePanel"]').exists()).toEqual(false);
expect(wrapper.find('[data-test-subj="IndexPatternsMissingPrompt"]').exists()).toEqual(true);
expect(wrapper.find('[data-test-subj="loading-panel"]').exists()).toEqual(false);
});
});

test('renders Loader', async () => {
Expand All @@ -154,19 +151,17 @@ describe('EmbeddedMapComponent', () => {

(createEmbeddable as jest.Mock).mockResolvedValue(null);

let wrapper: ReactWrapper;
await act(async () => {
wrapper = mount(
<TestProviders>
<EmbeddedMapComponent {...testProps} />
</TestProviders>
);
});

wrapper!.update();
const wrapper: ReactWrapper = mount(
<TestProviders>
<EmbeddedMapComponent {...testProps} />
</TestProviders>
);
await waitFor(() => {
wrapper.update();

expect(wrapper!.find('[data-test-subj="EmbeddablePanel"]').exists()).toEqual(false);
expect(wrapper!.find('[data-test-subj="IndexPatternsMissingPrompt"]').exists()).toEqual(false);
expect(wrapper!.find('[data-test-subj="loading-panel"]').exists()).toEqual(true);
expect(wrapper.find('[data-test-subj="EmbeddablePanel"]').exists()).toEqual(false);
expect(wrapper.find('[data-test-subj="IndexPatternsMissingPrompt"]').exists()).toEqual(false);
expect(wrapper.find('[data-test-subj="loading-panel"]').exists()).toEqual(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { mockSelectedTimeline } from './mocks';
import * as i18n from '../translations';

import { ReactWrapper, mount } from 'enzyme';
import { act } from 'react-dom/test-utils';
import { waitFor } from '@testing-library/react';
import { useParams } from 'react-router-dom';

jest.mock('../translations', () => {
Expand Down Expand Up @@ -102,15 +102,14 @@ describe('TimelineDownloader', () => {
...defaultTestProps,
};

await act(() => {
wrapper = mount(<TimelineDownloader {...testProps} />);
});

wrapper.update();
wrapper = mount(<TimelineDownloader {...testProps} />);
await waitFor(() => {
wrapper.update();

expect(mockDispatchToaster.mock.calls[0][0].title).toEqual(
i18n.SUCCESSFULLY_EXPORTED_TIMELINES
);
expect(mockDispatchToaster.mock.calls[0][0].title).toEqual(
i18n.SUCCESSFULLY_EXPORTED_TIMELINES
);
});
});

test('With correct toast message on success for exported templates', async () => {
Expand All @@ -119,15 +118,15 @@ describe('TimelineDownloader', () => {
};
(useParams as jest.Mock).mockReturnValue({ tabName: 'template' });

await act(() => {
wrapper = mount(<TimelineDownloader {...testProps} />);
});
wrapper = mount(<TimelineDownloader {...testProps} />);

wrapper.update();
await waitFor(() => {
wrapper.update();

expect(mockDispatchToaster.mock.calls[0][0].title).toEqual(
i18n.SUCCESSFULLY_EXPORTED_TIMELINES
);
expect(mockDispatchToaster.mock.calls[0][0].title).toEqual(
i18n.SUCCESSFULLY_EXPORTED_TIMELINES
);
});
});
});
});

0 comments on commit 088cc19

Please sign in to comment.