Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Watcher] Update skipped jests tests #88225

Merged
merged 2 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be an actual bug that the test caught. I don't see how this was working before based on the lodash documentation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice it got caught 👍

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