From 4977e57a3ecbaec4eed658701f9561ad74aef5c4 Mon Sep 17 00:00:00 2001 From: Marta Bondyra Date: Fri, 6 Mar 2020 13:06:41 +0100 Subject: [PATCH] [Lens] Remove warnings in tests in EditorFrame (#59447) Co-authored-by: Elastic Machine --- .../np_ready/components/doc/doc.test.tsx | 5 +++- .../editor_frame/editor_frame.test.tsx | 28 +++++++++++-------- .../editor_frame/workspace_panel.test.tsx | 6 +++- .../field_item.test.tsx | 5 +++- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/components/doc/doc.test.tsx b/src/legacy/core_plugins/kibana/public/discover/np_ready/components/doc/doc.test.tsx index e09f26311e4e32..2278b243ecc143 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/components/doc/doc.test.tsx +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/components/doc/doc.test.tsx @@ -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 diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.test.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.test.tsx index 0e256d0ab181b5..4736dd75831e46 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.test.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.test.tsx @@ -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 { @@ -102,7 +101,7 @@ describe('editor_frame', () => { }); describe('initialization', () => { - it('should initialize initial datasource', () => { + it('should initialize initial datasource', async () => { act(() => { mount( { /> ); }); + await waitForPromises(); expect(mockDatasource.initialize).toHaveBeenCalled(); }); @@ -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: '' }; @@ -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( { expect(mockVisualization.renderLayerConfigPanel).not.toHaveBeenCalled(); expect(mockDatasource.renderDataPanel).not.toHaveBeenCalled(); + await waitForPromises(); }); it('should not initialize visualization before datasource is initialized', async () => { @@ -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()); }); @@ -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'); @@ -989,6 +994,7 @@ describe('editor_frame', () => { '[data-test-subj="datasource-switch-testDatasource2"]' ) as HTMLButtonElement).click(); }); + await waitForPromises(); expect(mockDatasource2.initialize).toHaveBeenCalled(); }); diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel.test.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel.test.tsx index 929b4667aeb667..92a14963ff0b61 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel.test.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel.test.tsx @@ -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 { @@ -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; diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_datasource/field_item.test.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_datasource/field_item.test.tsx index 46a8304cc395e9..1a38ffa44f6f2f 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_datasource/field_item.test.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_datasource/field_item.test.tsx @@ -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;