Skip to content

Commit

Permalink
[Lens] Remove warnings in tests in EditorFrame (#59447)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
mbondyra and elasticmachine committed Mar 6, 2020
1 parent 44a35ec commit 4977e57
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ beforeEach(() => {
jest.clearAllMocks();
});

export const waitForPromises = () => new Promise(resolve => setTimeout(resolve, 0));
const waitForPromises = async () =>
act(async () => {
await new Promise(resolve => setTimeout(resolve));
});

/**
* this works but logs ugly error messages until we're using React 16.9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import { FrameLayout } from './frame_layout';

// calling this function will wait for all pending Promises from mock
// datasources to be processed by its callers.
async function waitForPromises(n = 3) {
for (let i = 0; i < n; ++i) {
await Promise.resolve();
}
}
const waitForPromises = async () =>
act(async () => {
await new Promise(resolve => setTimeout(resolve));
});

function generateSuggestion(state = {}): DatasourceSuggestion {
return {
Expand Down Expand Up @@ -102,7 +101,7 @@ describe('editor_frame', () => {
});

describe('initialization', () => {
it('should initialize initial datasource', () => {
it('should initialize initial datasource', async () => {
act(() => {
mount(
<EditorFrame
Expand All @@ -119,6 +118,7 @@ describe('editor_frame', () => {
/>
);
});
await waitForPromises();

expect(mockDatasource.initialize).toHaveBeenCalled();
});
Expand All @@ -145,7 +145,7 @@ describe('editor_frame', () => {
expect(mockDatasource.initialize).not.toHaveBeenCalled();
});

it('should initialize all datasources with state from doc', () => {
it('should initialize all datasources with state from doc', async () => {
const mockDatasource3 = createMockDatasource();
const datasource1State = { datasource1: '' };
const datasource2State = { datasource2: '' };
Expand Down Expand Up @@ -185,13 +185,13 @@ describe('editor_frame', () => {
/>
);
});

await waitForPromises();
expect(mockDatasource.initialize).toHaveBeenCalledWith(datasource1State);
expect(mockDatasource2.initialize).toHaveBeenCalledWith(datasource2State);
expect(mockDatasource3.initialize).not.toHaveBeenCalled();
});

it('should not render something before all datasources are initialized', () => {
it('should not render something before all datasources are initialized', async () => {
act(() => {
mount(
<EditorFrame
Expand All @@ -211,6 +211,7 @@ describe('editor_frame', () => {

expect(mockVisualization.renderLayerConfigPanel).not.toHaveBeenCalled();
expect(mockDatasource.renderDataPanel).not.toHaveBeenCalled();
await waitForPromises();
});

it('should not initialize visualization before datasource is initialized', async () => {
Expand Down Expand Up @@ -294,7 +295,9 @@ describe('editor_frame', () => {

await waitForPromises();

mockVisualization.initialize.mock.calls[0][0].addNewLayer();
act(() => {
mockVisualization.initialize.mock.calls[0][0].addNewLayer();
});

expect(mockDatasource2.insertLayer).toHaveBeenCalledWith(initialState, expect.anything());
});
Expand Down Expand Up @@ -325,7 +328,9 @@ describe('editor_frame', () => {

await waitForPromises();

mockVisualization.initialize.mock.calls[0][0].removeLayers(['abc', 'def']);
act(() => {
mockVisualization.initialize.mock.calls[0][0].removeLayers(['abc', 'def']);
});

expect(mockDatasource2.removeLayer).toHaveBeenCalledWith(initialState, 'abc');
expect(mockDatasource2.removeLayer).toHaveBeenCalledWith({ removed: true }, 'def');
Expand Down Expand Up @@ -989,6 +994,7 @@ describe('editor_frame', () => {
'[data-test-subj="datasource-switch-testDatasource2"]'
) as HTMLButtonElement).click();
});
await waitForPromises();
expect(mockDatasource2.initialize).toHaveBeenCalled();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import React from 'react';
import { act } from 'react-dom/test-utils';
import { ReactExpressionRendererProps } from '../../../../../../../src/plugins/expressions/public';
import { FramePublicAPI, TableSuggestion, Visualization } from '../../types';
import {
Expand All @@ -22,7 +23,10 @@ import { Ast } from '@kbn/interpreter/common';
import { coreMock } from 'src/core/public/mocks';
import { esFilters, IFieldType, IIndexPattern } from '../../../../../../../src/plugins/data/public';

const waitForPromises = () => new Promise(resolve => setTimeout(resolve));
const waitForPromises = async () =>
act(async () => {
await new Promise(resolve => setTimeout(resolve));
});

describe('workspace_panel', () => {
let mockVisualization: jest.Mocked<Visualization>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { IndexPattern } from './types';

jest.mock('ui/new_platform');

const waitForPromises = () => new Promise(resolve => setTimeout(resolve));
const waitForPromises = async () =>
act(async () => {
await new Promise(resolve => setTimeout(resolve));
});

describe('IndexPattern Field Item', () => {
let defaultProps: FieldItemProps;
Expand Down

0 comments on commit 4977e57

Please sign in to comment.