Skip to content

Commit

Permalink
[Watcher] Update skipped jest tests (elastic#88225) (elastic#88336)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth authored Jan 14, 2021
1 parent 32370a4 commit f7bb7c0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit f7bb7c0

Please sign in to comment.