Skip to content

Commit

Permalink
Mock lodash debounce to avoid test instability (#44592)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liza Katz authored Sep 2, 2019
1 parent a3c2376 commit a31b099
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 } from '../../vis';
import { VisResponseData } from './types';

import { Inspector } from '../../inspector';
import { EmbeddedVisualizeHandler } from './embedded_visualize_handler';
import { AggConfigs } from 'ui/vis/agg_configs';
Expand All @@ -49,6 +49,15 @@ describe('EmbeddedVisualizeHandler', () => {
beforeEach(() => {
jest.clearAllMocks();

jest.spyOn(_, 'debounce').mockImplementation(
// @ts-ignore
(f: Function) => {
// @ts-ignore
f.cancel = () => {};
return f;
}
);

dataLoaderParams = {
aggs: ([] as any) as AggConfigs,
filters: undefined,
Expand Down Expand Up @@ -153,29 +162,29 @@ 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 });
});

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 });
});

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 });
});
Expand Down

0 comments on commit a31b099

Please sign in to comment.