From d43b1a875b5124d2a9be5577a56454dfa5a6a092 Mon Sep 17 00:00:00 2001 From: Liza K Date: Mon, 2 Sep 2019 12:20:49 +0300 Subject: [PATCH] Mock lodash debounce to avoid test instability https://github.com/facebook/jest/issues/3465 --- .../loader/embedded_visualize_handler.test.ts | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.test.ts b/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.test.ts index ba809ae86d7d6a..2cd06fa2dd98f3 100644 --- a/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.test.ts +++ b/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.test.ts @@ -18,11 +18,11 @@ */ import { mockDataLoaderFetch, timefilter } from './embedded_visualize_handler.test.mocks'; +import _ from 'lodash'; // @ts-ignore import MockState from '../../../../../fixtures/mock_state'; import { RequestHandlerParams, Vis, AggConfig } from '../../vis'; import { VisResponseData } from './types'; - import { Inspector } from '../../inspector'; import { EmbeddedVisualizeHandler } from './embedded_visualize_handler'; @@ -48,6 +48,15 @@ describe('EmbeddedVisualizeHandler', () => { beforeEach(() => { jest.clearAllMocks(); + jest.spyOn(_, 'debounce').mockImplementation( + // @ts-ignore + (f: Function) => { + // @ts-ignore + f.cancel = () => {}; + return f; + } + ); + dataLoaderParams = { aggs: [] as AggConfig[], filters: undefined, @@ -152,9 +161,9 @@ describe('EmbeddedVisualizeHandler', () => { it('should call dataLoader.render with updated timeRange', () => { const params = { timeRange: { foo: 'bar' } }; handler.update(params); - jest.runAllTimers(); expect(mockDataLoaderFetch).toHaveBeenCalled(); - const { abortSignal, ...otherParams } = mockDataLoaderFetch.mock.calls[0][0]; + const callIndex = mockDataLoaderFetch.mock.calls.length - 1; + const { abortSignal, ...otherParams } = mockDataLoaderFetch.mock.calls[callIndex][0]; expect(abortSignal).toBeInstanceOf(AbortSignal); expect(otherParams).toEqual({ ...dataLoaderParams, ...params }); }); @@ -162,9 +171,9 @@ describe('EmbeddedVisualizeHandler', () => { it('should call dataLoader.render with updated filters', () => { const params = { filters: [{ meta: { disabled: false } }] }; handler.update(params); - jest.runAllTimers(); expect(mockDataLoaderFetch).toHaveBeenCalled(); - const { abortSignal, ...otherParams } = mockDataLoaderFetch.mock.calls[0][0]; + const callIndex = mockDataLoaderFetch.mock.calls.length - 1; + const { abortSignal, ...otherParams } = mockDataLoaderFetch.mock.calls[callIndex][0]; expect(abortSignal).toBeInstanceOf(AbortSignal); expect(otherParams).toEqual({ ...dataLoaderParams, ...params }); }); @@ -172,9 +181,9 @@ describe('EmbeddedVisualizeHandler', () => { it('should call dataLoader.render with updated query', () => { const params = { query: { foo: 'bar' } }; handler.update(params); - jest.runAllTimers(); expect(mockDataLoaderFetch).toHaveBeenCalled(); - const { abortSignal, ...otherParams } = mockDataLoaderFetch.mock.calls[0][0]; + const callIndex = mockDataLoaderFetch.mock.calls.length - 1; + const { abortSignal, ...otherParams } = mockDataLoaderFetch.mock.calls[callIndex][0]; expect(abortSignal).toBeInstanceOf(AbortSignal); expect(otherParams).toEqual({ ...dataLoaderParams, ...params }); });