From 05558ca16ea951f6d475753cdcc49e11302dadb5 Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Thu, 14 Jan 2021 09:00:18 -0500 Subject: [PATCH] [Watcher] Update skipped jest tests (#88225) --- .../fetch_all_from_scroll.test.js | 100 +++++++++--------- .../models/watch_status/watch_status.js | 4 +- .../models/watch_status/watch_status.test.js | 3 +- 3 files changed, 54 insertions(+), 53 deletions(-) diff --git a/x-pack/plugins/watcher/server/lib/fetch_all_from_scroll/fetch_all_from_scroll.test.js b/x-pack/plugins/watcher/server/lib/fetch_all_from_scroll/fetch_all_from_scroll.test.js index 8de025d300d5..805722db5431 100644 --- a/x-pack/plugins/watcher/server/lib/fetch_all_from_scroll/fetch_all_from_scroll.test.js +++ b/x-pack/plugins/watcher/server/lib/fetch_all_from_scroll/fetch_all_from_scroll.test.js @@ -4,84 +4,86 @@ * you may not use this file except in compliance with the Elastic License. */ +import { elasticsearchServiceMock } from '../../../../../../src/core/server/mocks'; import { fetchAllFromScroll } from './fetch_all_from_scroll'; -import { set } from '@elastic/safer-lodash-set'; describe('fetch_all_from_scroll', () => { - let mockResponse; - let stubCallWithRequest; + let mockScopedClusterClient; beforeEach(() => { - mockResponse = {}; + mockScopedClusterClient = elasticsearchServiceMock.createLegacyScopedClusterClient(); - stubCallWithRequest = jest.fn(); - - // TODO: That mocking needs to be migrated to jest - // stubCallWithRequest.onCall(0).returns( - // new Promise((resolve) => { - // const mockInnerResponse = { - // hits: { - // hits: ['newhit'], - // }, - // _scroll_id: 'newScrollId', - // }; - // return resolve(mockInnerResponse); - // }) - // ); - // - // stubCallWithRequest.onCall(1).returns( - // new Promise((resolve) => { - // const mockInnerResponse = { - // hits: { - // hits: [], - // }, - // }; - // return resolve(mockInnerResponse); - // }) - // ); + elasticsearchServiceMock + .createLegacyClusterClient() + .asScoped.mockReturnValue(mockScopedClusterClient); }); describe('#fetchAllFromScroll', () => { describe('when the passed-in response has no hits', () => { - beforeEach(() => { - set(mockResponse, 'hits.hits', []); - }); + const mockSearchResults = { + hits: { + hits: [], + }, + }; it('should return an empty array of hits', () => { - return fetchAllFromScroll(mockResponse).then((hits) => { + return fetchAllFromScroll(mockSearchResults).then((hits) => { expect(hits).toEqual([]); }); }); it('should not call callWithRequest', () => { - return fetchAllFromScroll(mockResponse, stubCallWithRequest).then(() => { - expect(stubCallWithRequest).not.toHaveBeenCalled(); + return fetchAllFromScroll(mockSearchResults, mockScopedClusterClient).then(() => { + expect(mockScopedClusterClient.callAsCurrentUser).not.toHaveBeenCalled(); }); }); }); - // TODO: tests were not running and are not up to date - describe.skip('when the passed-in response has some hits', () => { + describe('when the passed-in response has some hits', () => { + const mockInitialSearchResults = { + hits: { + hits: ['foo', 'bar'], + }, + _scroll_id: 'originalScrollId', + }; + beforeEach(() => { - set(mockResponse, 'hits.hits', ['foo', 'bar']); - set(mockResponse, '_scroll_id', 'originalScrollId'); + const mockResponse1 = { + hits: { + hits: ['newHit'], + }, + _scroll_id: 'newScrollId', + }; + + const mockResponse2 = { + hits: { + hits: [], + }, + }; + + mockScopedClusterClient.callAsCurrentUser + .mockReturnValueOnce(Promise.resolve(mockResponse1)) + .mockReturnValueOnce(Promise.resolve(mockResponse2)); }); it('should return the hits from the response', () => { - return fetchAllFromScroll(mockResponse, stubCallWithRequest).then((hits) => { - expect(hits).toEqual(['foo', 'bar', 'newhit']); - }); + return fetchAllFromScroll(mockInitialSearchResults, mockScopedClusterClient).then( + (hits) => { + expect(hits).toEqual(['foo', 'bar', 'newHit']); + } + ); }); it('should call callWithRequest', () => { - return fetchAllFromScroll(mockResponse, stubCallWithRequest).then(() => { - expect(stubCallWithRequest.calledTwice).toBe(true); - - const firstCallWithRequestCallArgs = stubCallWithRequest.args[0]; - expect(firstCallWithRequestCallArgs[1].body.scroll_id).toEqual('originalScrollId'); + return fetchAllFromScroll(mockInitialSearchResults, mockScopedClusterClient).then(() => { + expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenCalledTimes(2); - const secondCallWithRequestCallArgs = stubCallWithRequest.args[1]; - expect(secondCallWithRequestCallArgs[1].body.scroll_id).toEqual('newScrollId'); + expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenNthCalledWith(1, 'scroll', { + body: { scroll: '30s', scroll_id: 'originalScrollId' }, + }); + expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenNthCalledWith(2, 'scroll', { + body: { scroll: '30s', scroll_id: 'newScrollId' }, + }); }); }); }); diff --git a/x-pack/plugins/watcher/server/models/watch_status/watch_status.js b/x-pack/plugins/watcher/server/models/watch_status/watch_status.js index e5dd7d8ef186..55aecc9d6f22 100644 --- a/x-pack/plugins/watcher/server/models/watch_status/watch_status.js +++ b/x-pack/plugins/watcher/server/models/watch_status/watch_status.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { get, map, forEach, max } from 'lodash'; +import { get, map, forEach, maxBy } from 'lodash'; import { badRequest } from '@hapi/boom'; import { getMoment } from '../../../common/lib/get_moment'; import { ActionStatus } from '../action_status'; @@ -119,7 +119,7 @@ export class WatchStatus { } get lastFired() { - const actionStatus = max(this.actionStatuses, 'lastExecution'); + const actionStatus = maxBy(this.actionStatuses, 'lastExecution'); if (actionStatus) { return actionStatus.lastExecution; } diff --git a/x-pack/plugins/watcher/server/models/watch_status/watch_status.test.js b/x-pack/plugins/watcher/server/models/watch_status/watch_status.test.js index 949d37f56d7e..fca34085048a 100644 --- a/x-pack/plugins/watcher/server/models/watch_status/watch_status.test.js +++ b/x-pack/plugins/watcher/server/models/watch_status/watch_status.test.js @@ -60,8 +60,7 @@ describe('watch_status', () => { }); }); - // TODO: the test was not running before and is not up to date - describe.skip('lastFired getter method', () => { + describe('lastFired getter method', () => { let upstreamJson; beforeEach(() => { upstreamJson = {