[Watcher] Update skipped jest tests (#88225)

This commit is contained in:
Alison Goryachev 2021-01-14 09:00:18 -05:00 committed by GitHub
parent ca87bc495d
commit 8678954122
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 53 deletions

View file

@ -4,84 +4,86 @@
* you may not use this file except in compliance with the Elastic License. * 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 { fetchAllFromScroll } from './fetch_all_from_scroll';
import { set } from '@elastic/safer-lodash-set';
describe('fetch_all_from_scroll', () => { describe('fetch_all_from_scroll', () => {
let mockResponse; let mockScopedClusterClient;
let stubCallWithRequest;
beforeEach(() => { beforeEach(() => {
mockResponse = {}; mockScopedClusterClient = elasticsearchServiceMock.createLegacyScopedClusterClient();
stubCallWithRequest = jest.fn(); elasticsearchServiceMock
.createLegacyClusterClient()
// TODO: That mocking needs to be migrated to jest .asScoped.mockReturnValue(mockScopedClusterClient);
// 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);
// })
// );
}); });
describe('#fetchAllFromScroll', () => { describe('#fetchAllFromScroll', () => {
describe('when the passed-in response has no hits', () => { describe('when the passed-in response has no hits', () => {
beforeEach(() => { const mockSearchResults = {
set(mockResponse, 'hits.hits', []); hits: {
}); hits: [],
},
};
it('should return an empty array of hits', () => { it('should return an empty array of hits', () => {
return fetchAllFromScroll(mockResponse).then((hits) => { return fetchAllFromScroll(mockSearchResults).then((hits) => {
expect(hits).toEqual([]); expect(hits).toEqual([]);
}); });
}); });
it('should not call callWithRequest', () => { it('should not call callWithRequest', () => {
return fetchAllFromScroll(mockResponse, stubCallWithRequest).then(() => { return fetchAllFromScroll(mockSearchResults, mockScopedClusterClient).then(() => {
expect(stubCallWithRequest).not.toHaveBeenCalled(); expect(mockScopedClusterClient.callAsCurrentUser).not.toHaveBeenCalled();
}); });
}); });
}); });
// TODO: tests were not running and are not up to date describe('when the passed-in response has some hits', () => {
describe.skip('when the passed-in response has some hits', () => { const mockInitialSearchResults = {
hits: {
hits: ['foo', 'bar'],
},
_scroll_id: 'originalScrollId',
};
beforeEach(() => { beforeEach(() => {
set(mockResponse, 'hits.hits', ['foo', 'bar']); const mockResponse1 = {
set(mockResponse, '_scroll_id', 'originalScrollId'); 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', () => { it('should return the hits from the response', () => {
return fetchAllFromScroll(mockResponse, stubCallWithRequest).then((hits) => { return fetchAllFromScroll(mockInitialSearchResults, mockScopedClusterClient).then(
expect(hits).toEqual(['foo', 'bar', 'newhit']); (hits) => {
}); expect(hits).toEqual(['foo', 'bar', 'newHit']);
}
);
}); });
it('should call callWithRequest', () => { it('should call callWithRequest', () => {
return fetchAllFromScroll(mockResponse, stubCallWithRequest).then(() => { return fetchAllFromScroll(mockInitialSearchResults, mockScopedClusterClient).then(() => {
expect(stubCallWithRequest.calledTwice).toBe(true); expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenCalledTimes(2);
const firstCallWithRequestCallArgs = stubCallWithRequest.args[0]; expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenNthCalledWith(1, 'scroll', {
expect(firstCallWithRequestCallArgs[1].body.scroll_id).toEqual('originalScrollId'); body: { scroll: '30s', scroll_id: 'originalScrollId' },
});
const secondCallWithRequestCallArgs = stubCallWithRequest.args[1]; expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenNthCalledWith(2, 'scroll', {
expect(secondCallWithRequestCallArgs[1].body.scroll_id).toEqual('newScrollId'); body: { scroll: '30s', scroll_id: 'newScrollId' },
});
}); });
}); });
}); });

View file

@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License. * 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 { badRequest } from '@hapi/boom';
import { getMoment } from '../../../common/lib/get_moment'; import { getMoment } from '../../../common/lib/get_moment';
import { ActionStatus } from '../action_status'; import { ActionStatus } from '../action_status';
@ -119,7 +119,7 @@ export class WatchStatus {
} }
get lastFired() { get lastFired() {
const actionStatus = max(this.actionStatuses, 'lastExecution'); const actionStatus = maxBy(this.actionStatuses, 'lastExecution');
if (actionStatus) { if (actionStatus) {
return actionStatus.lastExecution; return actionStatus.lastExecution;
} }

View file

@ -60,8 +60,7 @@ describe('watch_status', () => {
}); });
}); });
// TODO: the test was not running before and is not up to date describe('lastFired getter method', () => {
describe.skip('lastFired getter method', () => {
let upstreamJson; let upstreamJson;
beforeEach(() => { beforeEach(() => {
upstreamJson = { upstreamJson = {