Better async (#89636)

This commit is contained in:
Jason Stoltzfus 2021-01-29 12:55:06 -05:00 committed by GitHub
parent 5feca52dea
commit 8780a2de6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 247 additions and 349 deletions

View file

@ -9,13 +9,14 @@ import {
mockKibanaValues, mockKibanaValues,
mockHttpValues, mockHttpValues,
mockFlashMessageHelpers, mockFlashMessageHelpers,
expectedAsyncError,
} from '../../../__mocks__'; } from '../../../__mocks__';
jest.mock('../engine', () => ({ jest.mock('../engine', () => ({
EngineLogic: { values: { engineName: 'test-engine' } }, EngineLogic: { values: { engineName: 'test-engine' } },
})); }));
import { nextTick } from '@kbn/test/jest';
import { DEFAULT_START_DATE, DEFAULT_END_DATE } from './constants'; import { DEFAULT_START_DATE, DEFAULT_END_DATE } from './constants';
import { AnalyticsLogic } from './'; import { AnalyticsLogic } from './';
@ -176,13 +177,12 @@ describe('AnalyticsLogic', () => {
}); });
it('should make an API call and set state based on the response', async () => { it('should make an API call and set state based on the response', async () => {
const promise = Promise.resolve(MOCK_ANALYTICS_RESPONSE); http.get.mockReturnValueOnce(Promise.resolve(MOCK_ANALYTICS_RESPONSE));
http.get.mockReturnValueOnce(promise);
mount(); mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsDataLoad'); jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsDataLoad');
AnalyticsLogic.actions.loadAnalyticsData(); AnalyticsLogic.actions.loadAnalyticsData();
await promise; await nextTick();
expect(http.get).toHaveBeenCalledWith( expect(http.get).toHaveBeenCalledWith(
'/api/app_search/engines/test-engine/analytics/queries', '/api/app_search/engines/test-engine/analytics/queries',
@ -220,25 +220,23 @@ describe('AnalyticsLogic', () => {
}); });
it('calls onAnalyticsUnavailable if analyticsUnavailable is in response', async () => { it('calls onAnalyticsUnavailable if analyticsUnavailable is in response', async () => {
const promise = Promise.resolve({ analyticsUnavailable: true }); http.get.mockReturnValueOnce(Promise.resolve({ analyticsUnavailable: true }));
http.get.mockReturnValueOnce(promise);
mount(); mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable'); jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable');
AnalyticsLogic.actions.loadAnalyticsData(); AnalyticsLogic.actions.loadAnalyticsData();
await promise; await nextTick();
expect(AnalyticsLogic.actions.onAnalyticsUnavailable).toHaveBeenCalled(); expect(AnalyticsLogic.actions.onAnalyticsUnavailable).toHaveBeenCalled();
}); });
it('handles errors', async () => { it('handles errors', async () => {
const promise = Promise.reject('error'); http.get.mockReturnValueOnce(Promise.reject('error'));
http.get.mockReturnValueOnce(promise);
mount(); mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable'); jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable');
AnalyticsLogic.actions.loadAnalyticsData(); AnalyticsLogic.actions.loadAnalyticsData();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('error'); expect(flashAPIErrors).toHaveBeenCalledWith('error');
expect(AnalyticsLogic.actions.onAnalyticsUnavailable).toHaveBeenCalled(); expect(AnalyticsLogic.actions.onAnalyticsUnavailable).toHaveBeenCalled();
@ -258,13 +256,12 @@ describe('AnalyticsLogic', () => {
}); });
it('should make an API call and set state based on the response', async () => { it('should make an API call and set state based on the response', async () => {
const promise = Promise.resolve(MOCK_QUERY_RESPONSE); http.get.mockReturnValueOnce(Promise.resolve(MOCK_QUERY_RESPONSE));
http.get.mockReturnValueOnce(promise);
mount(); mount();
jest.spyOn(AnalyticsLogic.actions, 'onQueryDataLoad'); jest.spyOn(AnalyticsLogic.actions, 'onQueryDataLoad');
AnalyticsLogic.actions.loadQueryData('some-query'); AnalyticsLogic.actions.loadQueryData('some-query');
await promise; await nextTick();
expect(http.get).toHaveBeenCalledWith( expect(http.get).toHaveBeenCalledWith(
'/api/app_search/engines/test-engine/analytics/queries/some-query', '/api/app_search/engines/test-engine/analytics/queries/some-query',
@ -298,25 +295,23 @@ describe('AnalyticsLogic', () => {
}); });
it('calls onAnalyticsUnavailable if analyticsUnavailable is in response', async () => { it('calls onAnalyticsUnavailable if analyticsUnavailable is in response', async () => {
const promise = Promise.resolve({ analyticsUnavailable: true }); http.get.mockReturnValueOnce(Promise.resolve({ analyticsUnavailable: true }));
http.get.mockReturnValueOnce(promise);
mount(); mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable'); jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable');
AnalyticsLogic.actions.loadQueryData('some-query'); AnalyticsLogic.actions.loadQueryData('some-query');
await promise; await nextTick();
expect(AnalyticsLogic.actions.onAnalyticsUnavailable).toHaveBeenCalled(); expect(AnalyticsLogic.actions.onAnalyticsUnavailable).toHaveBeenCalled();
}); });
it('handles errors', async () => { it('handles errors', async () => {
const promise = Promise.reject('error'); http.get.mockReturnValueOnce(Promise.reject('error'));
http.get.mockReturnValueOnce(promise);
mount(); mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable'); jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable');
AnalyticsLogic.actions.loadQueryData('some-query'); AnalyticsLogic.actions.loadQueryData('some-query');
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('error'); expect(flashAPIErrors).toHaveBeenCalledWith('error');
expect(AnalyticsLogic.actions.onAnalyticsUnavailable).toHaveBeenCalled(); expect(AnalyticsLogic.actions.onAnalyticsUnavailable).toHaveBeenCalled();

View file

@ -4,12 +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 { import { LogicMounter, mockFlashMessageHelpers, mockHttpValues } from '../../../__mocks__';
LogicMounter,
mockFlashMessageHelpers,
mockHttpValues,
expectedAsyncError,
} from '../../../__mocks__';
jest.mock('../../app_logic', () => ({ jest.mock('../../app_logic', () => ({
AppLogic: { AppLogic: {
@ -17,9 +12,12 @@ jest.mock('../../app_logic', () => ({
values: { myRole: jest.fn(() => ({})) }, values: { myRole: jest.fn(() => ({})) },
}, },
})); }));
import { AppLogic } from '../../app_logic';
import { nextTick } from '@kbn/test/jest';
import { AppLogic } from '../../app_logic';
import { ApiTokenTypes } from './constants'; import { ApiTokenTypes } from './constants';
import { CredentialsLogic } from './credentials_logic'; import { CredentialsLogic } from './credentials_logic';
describe('CredentialsLogic', () => { describe('CredentialsLogic', () => {
@ -1064,8 +1062,7 @@ describe('CredentialsLogic', () => {
it('will call an API endpoint and set the results with the `setCredentialsData` action', async () => { it('will call an API endpoint and set the results with the `setCredentialsData` action', async () => {
mount(); mount();
jest.spyOn(CredentialsLogic.actions, 'setCredentialsData').mockImplementationOnce(() => {}); jest.spyOn(CredentialsLogic.actions, 'setCredentialsData').mockImplementationOnce(() => {});
const promise = Promise.resolve({ meta, results }); http.get.mockReturnValue(Promise.resolve({ meta, results }));
http.get.mockReturnValue(promise);
CredentialsLogic.actions.fetchCredentials(2); CredentialsLogic.actions.fetchCredentials(2);
expect(http.get).toHaveBeenCalledWith('/api/app_search/credentials', { expect(http.get).toHaveBeenCalledWith('/api/app_search/credentials', {
@ -1073,17 +1070,16 @@ describe('CredentialsLogic', () => {
'page[current]': 2, 'page[current]': 2,
}, },
}); });
await promise; await nextTick();
expect(CredentialsLogic.actions.setCredentialsData).toHaveBeenCalledWith(meta, results); expect(CredentialsLogic.actions.setCredentialsData).toHaveBeenCalledWith(meta, results);
}); });
it('handles errors', async () => { it('handles errors', async () => {
mount(); mount();
const promise = Promise.reject('An error occured'); http.get.mockReturnValue(Promise.reject('An error occured'));
http.get.mockReturnValue(promise);
CredentialsLogic.actions.fetchCredentials(); CredentialsLogic.actions.fetchCredentials();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured'); expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}); });
@ -1095,12 +1091,11 @@ describe('CredentialsLogic', () => {
jest jest
.spyOn(CredentialsLogic.actions, 'setCredentialsDetails') .spyOn(CredentialsLogic.actions, 'setCredentialsDetails')
.mockImplementationOnce(() => {}); .mockImplementationOnce(() => {});
const promise = Promise.resolve(credentialsDetails); http.get.mockReturnValue(Promise.resolve(credentialsDetails));
http.get.mockReturnValue(promise);
CredentialsLogic.actions.fetchDetails(); CredentialsLogic.actions.fetchDetails();
expect(http.get).toHaveBeenCalledWith('/api/app_search/credentials/details'); expect(http.get).toHaveBeenCalledWith('/api/app_search/credentials/details');
await promise; await nextTick();
expect(CredentialsLogic.actions.setCredentialsDetails).toHaveBeenCalledWith( expect(CredentialsLogic.actions.setCredentialsDetails).toHaveBeenCalledWith(
credentialsDetails credentialsDetails
); );
@ -1108,11 +1103,10 @@ describe('CredentialsLogic', () => {
it('handles errors', async () => { it('handles errors', async () => {
mount(); mount();
const promise = Promise.reject('An error occured'); http.get.mockReturnValue(Promise.reject('An error occured'));
http.get.mockReturnValue(promise);
CredentialsLogic.actions.fetchDetails(); CredentialsLogic.actions.fetchDetails();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured'); expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}); });
@ -1124,23 +1118,21 @@ describe('CredentialsLogic', () => {
it('will call an API endpoint and set the results with the `onApiKeyDelete` action', async () => { it('will call an API endpoint and set the results with the `onApiKeyDelete` action', async () => {
mount(); mount();
jest.spyOn(CredentialsLogic.actions, 'onApiKeyDelete').mockImplementationOnce(() => {}); jest.spyOn(CredentialsLogic.actions, 'onApiKeyDelete').mockImplementationOnce(() => {});
const promise = Promise.resolve(); http.delete.mockReturnValue(Promise.resolve());
http.delete.mockReturnValue(promise);
CredentialsLogic.actions.deleteApiKey(tokenName); CredentialsLogic.actions.deleteApiKey(tokenName);
expect(http.delete).toHaveBeenCalledWith(`/api/app_search/credentials/${tokenName}`); expect(http.delete).toHaveBeenCalledWith(`/api/app_search/credentials/${tokenName}`);
await promise; await nextTick();
expect(CredentialsLogic.actions.onApiKeyDelete).toHaveBeenCalledWith(tokenName); expect(CredentialsLogic.actions.onApiKeyDelete).toHaveBeenCalledWith(tokenName);
expect(setSuccessMessage).toHaveBeenCalled(); expect(setSuccessMessage).toHaveBeenCalled();
}); });
it('handles errors', async () => { it('handles errors', async () => {
mount(); mount();
const promise = Promise.reject('An error occured'); http.delete.mockReturnValue(Promise.reject('An error occured'));
http.delete.mockReturnValue(promise);
CredentialsLogic.actions.deleteApiKey(tokenName); CredentialsLogic.actions.deleteApiKey(tokenName);
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured'); expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}); });
@ -1156,14 +1148,13 @@ describe('CredentialsLogic', () => {
activeApiToken: createdToken, activeApiToken: createdToken,
}); });
jest.spyOn(CredentialsLogic.actions, 'onApiTokenCreateSuccess'); jest.spyOn(CredentialsLogic.actions, 'onApiTokenCreateSuccess');
const promise = Promise.resolve(createdToken); http.post.mockReturnValue(Promise.resolve(createdToken));
http.post.mockReturnValue(promise);
CredentialsLogic.actions.onApiTokenChange(); CredentialsLogic.actions.onApiTokenChange();
expect(http.post).toHaveBeenCalledWith('/api/app_search/credentials', { expect(http.post).toHaveBeenCalledWith('/api/app_search/credentials', {
body: JSON.stringify(createdToken), body: JSON.stringify(createdToken),
}); });
await promise; await nextTick();
expect(CredentialsLogic.actions.onApiTokenCreateSuccess).toHaveBeenCalledWith(createdToken); expect(CredentialsLogic.actions.onApiTokenCreateSuccess).toHaveBeenCalledWith(createdToken);
expect(setSuccessMessage).toHaveBeenCalled(); expect(setSuccessMessage).toHaveBeenCalled();
}); });
@ -1184,25 +1175,23 @@ describe('CredentialsLogic', () => {
}, },
}); });
jest.spyOn(CredentialsLogic.actions, 'onApiTokenUpdateSuccess'); jest.spyOn(CredentialsLogic.actions, 'onApiTokenUpdateSuccess');
const promise = Promise.resolve(updatedToken); http.put.mockReturnValue(Promise.resolve(updatedToken));
http.put.mockReturnValue(promise);
CredentialsLogic.actions.onApiTokenChange(); CredentialsLogic.actions.onApiTokenChange();
expect(http.put).toHaveBeenCalledWith('/api/app_search/credentials/test-key', { expect(http.put).toHaveBeenCalledWith('/api/app_search/credentials/test-key', {
body: JSON.stringify(updatedToken), body: JSON.stringify(updatedToken),
}); });
await promise; await nextTick();
expect(CredentialsLogic.actions.onApiTokenUpdateSuccess).toHaveBeenCalledWith(updatedToken); expect(CredentialsLogic.actions.onApiTokenUpdateSuccess).toHaveBeenCalledWith(updatedToken);
expect(setSuccessMessage).toHaveBeenCalled(); expect(setSuccessMessage).toHaveBeenCalled();
}); });
it('handles errors', async () => { it('handles errors', async () => {
mount(); mount();
const promise = Promise.reject('An error occured'); http.post.mockReturnValue(Promise.reject('An error occured'));
http.post.mockReturnValue(promise);
CredentialsLogic.actions.onApiTokenChange(); CredentialsLogic.actions.onApiTokenChange();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured'); expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}); });

View file

@ -6,6 +6,7 @@
import { LogicMounter, mockHttpValues } from '../../../__mocks__'; import { LogicMounter, mockHttpValues } from '../../../__mocks__';
import { nextTick } from '@kbn/test/jest';
import dedent from 'dedent'; import dedent from 'dedent';
jest.mock('./utils', () => ({ jest.mock('./utils', () => ({
@ -443,10 +444,10 @@ describe('DocumentCreationLogic', () => {
}); });
it('should set and show summary from the returned response', async () => { it('should set and show summary from the returned response', async () => {
const promise = http.post.mockReturnValueOnce(Promise.resolve(mockValidResponse)); http.post.mockReturnValueOnce(Promise.resolve(mockValidResponse));
await DocumentCreationLogic.actions.uploadDocuments({ documents: mockValidDocuments }); await DocumentCreationLogic.actions.uploadDocuments({ documents: mockValidDocuments });
await promise; await nextTick();
expect(DocumentCreationLogic.actions.setSummary).toHaveBeenCalledWith(mockValidResponse); expect(DocumentCreationLogic.actions.setSummary).toHaveBeenCalledWith(mockValidResponse);
expect(DocumentCreationLogic.actions.setCreationStep).toHaveBeenCalledWith( expect(DocumentCreationLogic.actions.setCreationStep).toHaveBeenCalledWith(
@ -462,7 +463,7 @@ describe('DocumentCreationLogic', () => {
}); });
it('handles API errors', async () => { it('handles API errors', async () => {
const promise = http.post.mockReturnValueOnce( http.post.mockReturnValueOnce(
Promise.reject({ Promise.reject({
body: { body: {
statusCode: 400, statusCode: 400,
@ -473,7 +474,7 @@ describe('DocumentCreationLogic', () => {
); );
await DocumentCreationLogic.actions.uploadDocuments({ documents: [{}] }); await DocumentCreationLogic.actions.uploadDocuments({ documents: [{}] });
await promise; await nextTick();
expect(DocumentCreationLogic.actions.setErrors).toHaveBeenCalledWith( expect(DocumentCreationLogic.actions.setErrors).toHaveBeenCalledWith(
'[400 Bad Request] Invalid request payload JSON format' '[400 Bad Request] Invalid request payload JSON format'
@ -481,10 +482,10 @@ describe('DocumentCreationLogic', () => {
}); });
it('handles client-side errors', async () => { it('handles client-side errors', async () => {
const promise = (http.post as jest.Mock).mockReturnValueOnce(new Error()); (http.post as jest.Mock).mockReturnValueOnce(new Error());
await DocumentCreationLogic.actions.uploadDocuments({ documents: [{}] }); await DocumentCreationLogic.actions.uploadDocuments({ documents: [{}] });
await promise; await nextTick();
expect(DocumentCreationLogic.actions.setErrors).toHaveBeenCalledWith( expect(DocumentCreationLogic.actions.setErrors).toHaveBeenCalledWith(
"Cannot read property 'total' of undefined" "Cannot read property 'total' of undefined"
@ -493,14 +494,14 @@ describe('DocumentCreationLogic', () => {
// NOTE: I can't seem to reproduce this in a production setting. // NOTE: I can't seem to reproduce this in a production setting.
it('handles errors returned from the API', async () => { it('handles errors returned from the API', async () => {
const promise = http.post.mockReturnValueOnce( http.post.mockReturnValueOnce(
Promise.resolve({ Promise.resolve({
errors: ['JSON cannot be empty'], errors: ['JSON cannot be empty'],
}) })
); );
await DocumentCreationLogic.actions.uploadDocuments({ documents: [{}] }); await DocumentCreationLogic.actions.uploadDocuments({ documents: [{}] });
await promise; await nextTick();
expect(DocumentCreationLogic.actions.setErrors).toHaveBeenCalledWith([ expect(DocumentCreationLogic.actions.setErrors).toHaveBeenCalledWith([
'JSON cannot be empty', 'JSON cannot be empty',
@ -536,12 +537,12 @@ describe('DocumentCreationLogic', () => {
}); });
it('should correctly merge multiple API calls into a single summary obj', async () => { it('should correctly merge multiple API calls into a single summary obj', async () => {
const promise = (http.post as jest.Mock) (http.post as jest.Mock)
.mockReturnValueOnce(mockFirstResponse) .mockReturnValueOnce(mockFirstResponse)
.mockReturnValueOnce(mockSecondResponse); .mockReturnValueOnce(mockSecondResponse);
await DocumentCreationLogic.actions.uploadDocuments({ documents: largeDocumentsArray }); await DocumentCreationLogic.actions.uploadDocuments({ documents: largeDocumentsArray });
await promise; await nextTick();
expect(http.post).toHaveBeenCalledTimes(2); expect(http.post).toHaveBeenCalledTimes(2);
expect(DocumentCreationLogic.actions.setSummary).toHaveBeenCalledWith({ expect(DocumentCreationLogic.actions.setSummary).toHaveBeenCalledWith({
@ -562,12 +563,12 @@ describe('DocumentCreationLogic', () => {
}); });
it('should correctly merge response errors', async () => { it('should correctly merge response errors', async () => {
const promise = (http.post as jest.Mock) (http.post as jest.Mock)
.mockReturnValueOnce({ ...mockFirstResponse, errors: ['JSON cannot be empty'] }) .mockReturnValueOnce({ ...mockFirstResponse, errors: ['JSON cannot be empty'] })
.mockReturnValueOnce({ ...mockSecondResponse, errors: ['Too large to render'] }); .mockReturnValueOnce({ ...mockSecondResponse, errors: ['Too large to render'] });
await DocumentCreationLogic.actions.uploadDocuments({ documents: largeDocumentsArray }); await DocumentCreationLogic.actions.uploadDocuments({ documents: largeDocumentsArray });
await promise; await nextTick();
expect(http.post).toHaveBeenCalledTimes(2); expect(http.post).toHaveBeenCalledTimes(2);
expect(DocumentCreationLogic.actions.setErrors).toHaveBeenCalledWith([ expect(DocumentCreationLogic.actions.setErrors).toHaveBeenCalledWith([

View file

@ -9,10 +9,11 @@ import {
mockHttpValues, mockHttpValues,
mockKibanaValues, mockKibanaValues,
mockFlashMessageHelpers, mockFlashMessageHelpers,
expectedAsyncError,
} from '../../../__mocks__'; } from '../../../__mocks__';
import { mockEngineValues } from '../../__mocks__'; import { mockEngineValues } from '../../__mocks__';
import { nextTick } from '@kbn/test/jest';
import { DocumentDetailLogic } from './document_detail_logic'; import { DocumentDetailLogic } from './document_detail_logic';
import { InternalSchemaTypes } from '../../../shared/types'; import { InternalSchemaTypes } from '../../../shared/types';
@ -56,23 +57,21 @@ describe('DocumentDetailLogic', () => {
it('will call an API endpoint and then store the result', async () => { it('will call an API endpoint and then store the result', async () => {
const fields = [{ name: 'name', value: 'python', type: 'string' }]; const fields = [{ name: 'name', value: 'python', type: 'string' }];
jest.spyOn(DocumentDetailLogic.actions, 'setFields'); jest.spyOn(DocumentDetailLogic.actions, 'setFields');
const promise = Promise.resolve({ fields }); http.get.mockReturnValue(Promise.resolve({ fields }));
http.get.mockReturnValue(promise);
DocumentDetailLogic.actions.getDocumentDetails('1'); DocumentDetailLogic.actions.getDocumentDetails('1');
expect(http.get).toHaveBeenCalledWith(`/api/app_search/engines/engine1/documents/1`); expect(http.get).toHaveBeenCalledWith(`/api/app_search/engines/engine1/documents/1`);
await promise; await nextTick();
expect(DocumentDetailLogic.actions.setFields).toHaveBeenCalledWith(fields); expect(DocumentDetailLogic.actions.setFields).toHaveBeenCalledWith(fields);
}); });
it('handles errors', async () => { it('handles errors', async () => {
mount(); mount();
const promise = Promise.reject('An error occurred'); http.get.mockReturnValue(Promise.reject('An error occurred'));
http.get.mockReturnValue(promise);
DocumentDetailLogic.actions.getDocumentDetails('1'); DocumentDetailLogic.actions.getDocumentDetails('1');
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('An error occurred', { isQueued: true }); expect(flashAPIErrors).toHaveBeenCalledWith('An error occurred', { isQueued: true });
expect(navigateToUrl).toHaveBeenCalledWith('/engines/engine1/documents'); expect(navigateToUrl).toHaveBeenCalledWith('/engines/engine1/documents');
@ -81,13 +80,11 @@ describe('DocumentDetailLogic', () => {
describe('deleteDocument', () => { describe('deleteDocument', () => {
let confirmSpy: any; let confirmSpy: any;
let promise: Promise<any>;
beforeEach(() => { beforeEach(() => {
confirmSpy = jest.spyOn(window, 'confirm'); confirmSpy = jest.spyOn(window, 'confirm');
confirmSpy.mockImplementation(jest.fn(() => true)); confirmSpy.mockImplementation(jest.fn(() => true));
promise = Promise.resolve({}); http.delete.mockReturnValue(Promise.resolve({}));
http.delete.mockReturnValue(promise);
}); });
afterEach(() => { afterEach(() => {
@ -99,7 +96,7 @@ describe('DocumentDetailLogic', () => {
DocumentDetailLogic.actions.deleteDocument('1'); DocumentDetailLogic.actions.deleteDocument('1');
expect(http.delete).toHaveBeenCalledWith(`/api/app_search/engines/engine1/documents/1`); expect(http.delete).toHaveBeenCalledWith(`/api/app_search/engines/engine1/documents/1`);
await promise; await nextTick();
expect(setQueuedSuccessMessage).toHaveBeenCalledWith( expect(setQueuedSuccessMessage).toHaveBeenCalledWith(
'Successfully marked document for deletion. It will be deleted momentarily.' 'Successfully marked document for deletion. It will be deleted momentarily.'
); );
@ -113,16 +110,15 @@ describe('DocumentDetailLogic', () => {
DocumentDetailLogic.actions.deleteDocument('1'); DocumentDetailLogic.actions.deleteDocument('1');
expect(http.delete).not.toHaveBeenCalled(); expect(http.delete).not.toHaveBeenCalled();
await promise; await nextTick();
}); });
it('handles errors', async () => { it('handles errors', async () => {
mount(); mount();
promise = Promise.reject('An error occured'); http.delete.mockReturnValue(Promise.reject('An error occured'));
http.delete.mockReturnValue(promise);
DocumentDetailLogic.actions.deleteDocument('1'); DocumentDetailLogic.actions.deleteDocument('1');
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured'); expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}); });

View file

@ -4,7 +4,9 @@
* 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 { LogicMounter, mockHttpValues, expectedAsyncError } from '../../../__mocks__'; import { LogicMounter, mockHttpValues } from '../../../__mocks__';
import { nextTick } from '@kbn/test/jest';
import { EngineLogic } from './'; import { EngineLogic } from './';
@ -172,11 +174,10 @@ describe('EngineLogic', () => {
it('fetches and sets engine data', async () => { it('fetches and sets engine data', async () => {
mount({ engineName: 'some-engine' }); mount({ engineName: 'some-engine' });
jest.spyOn(EngineLogic.actions, 'setEngineData'); jest.spyOn(EngineLogic.actions, 'setEngineData');
const promise = Promise.resolve(mockEngineData); http.get.mockReturnValueOnce(Promise.resolve(mockEngineData));
http.get.mockReturnValueOnce(promise);
EngineLogic.actions.initializeEngine(); EngineLogic.actions.initializeEngine();
await promise; await nextTick();
expect(http.get).toHaveBeenCalledWith('/api/app_search/engines/some-engine'); expect(http.get).toHaveBeenCalledWith('/api/app_search/engines/some-engine');
expect(EngineLogic.actions.setEngineData).toHaveBeenCalledWith(mockEngineData); expect(EngineLogic.actions.setEngineData).toHaveBeenCalledWith(mockEngineData);
@ -185,11 +186,10 @@ describe('EngineLogic', () => {
it('handles errors', async () => { it('handles errors', async () => {
mount(); mount();
jest.spyOn(EngineLogic.actions, 'setEngineNotFound'); jest.spyOn(EngineLogic.actions, 'setEngineNotFound');
const promise = Promise.reject('An error occured'); http.get.mockReturnValue(Promise.reject('An error occured'));
http.get.mockReturnValue(promise);
EngineLogic.actions.initializeEngine(); EngineLogic.actions.initializeEngine();
await expectedAsyncError(promise); await nextTick();
expect(EngineLogic.actions.setEngineNotFound).toHaveBeenCalledWith(true); expect(EngineLogic.actions.setEngineNotFound).toHaveBeenCalledWith(true);
}); });

View file

@ -4,17 +4,14 @@
* 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 { import { LogicMounter, mockHttpValues, mockFlashMessageHelpers } from '../../../__mocks__';
LogicMounter,
mockHttpValues,
mockFlashMessageHelpers,
expectedAsyncError,
} from '../../../__mocks__';
jest.mock('../engine', () => ({ jest.mock('../engine', () => ({
EngineLogic: { values: { engineName: 'some-engine' } }, EngineLogic: { values: { engineName: 'some-engine' } },
})); }));
import { nextTick } from '@kbn/test/jest';
import { EngineOverviewLogic } from './'; import { EngineOverviewLogic } from './';
describe('EngineOverviewLogic', () => { describe('EngineOverviewLogic', () => {
@ -85,11 +82,10 @@ describe('EngineOverviewLogic', () => {
it('fetches data and calls onPollingSuccess', async () => { it('fetches data and calls onPollingSuccess', async () => {
mount(); mount();
jest.spyOn(EngineOverviewLogic.actions, 'onPollingSuccess'); jest.spyOn(EngineOverviewLogic.actions, 'onPollingSuccess');
const promise = Promise.resolve(mockEngineMetrics); http.get.mockReturnValueOnce(Promise.resolve(mockEngineMetrics));
http.get.mockReturnValueOnce(promise);
EngineOverviewLogic.actions.pollForOverviewMetrics(); EngineOverviewLogic.actions.pollForOverviewMetrics();
await promise; await nextTick();
expect(http.get).toHaveBeenCalledWith('/api/app_search/engines/some-engine/overview'); expect(http.get).toHaveBeenCalledWith('/api/app_search/engines/some-engine/overview');
expect(EngineOverviewLogic.actions.onPollingSuccess).toHaveBeenCalledWith( expect(EngineOverviewLogic.actions.onPollingSuccess).toHaveBeenCalledWith(
@ -99,11 +95,10 @@ describe('EngineOverviewLogic', () => {
it('handles errors', async () => { it('handles errors', async () => {
mount(); mount();
const promise = Promise.reject('An error occurred'); http.get.mockReturnValue(Promise.reject('An error occurred'));
http.get.mockReturnValue(promise);
EngineOverviewLogic.actions.pollForOverviewMetrics(); EngineOverviewLogic.actions.pollForOverviewMetrics();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('An error occurred'); expect(flashAPIErrors).toHaveBeenCalledWith('An error occurred');
}); });

View file

@ -6,6 +6,8 @@
import { LogicMounter, mockHttpValues } from '../../../__mocks__'; import { LogicMounter, mockHttpValues } from '../../../__mocks__';
import { nextTick } from '@kbn/test/jest';
import { EngineDetails } from '../engine/types'; import { EngineDetails } from '../engine/types';
import { EnginesLogic } from './'; import { EnginesLogic } from './';
@ -124,13 +126,12 @@ describe('EnginesLogic', () => {
describe('loadEngines', () => { describe('loadEngines', () => {
it('should call the engines API endpoint and set state based on the results', async () => { it('should call the engines API endpoint and set state based on the results', async () => {
const promise = Promise.resolve(MOCK_ENGINES_API_RESPONSE); http.get.mockReturnValueOnce(Promise.resolve(MOCK_ENGINES_API_RESPONSE));
http.get.mockReturnValueOnce(promise);
mount({ enginesPage: 10 }); mount({ enginesPage: 10 });
jest.spyOn(EnginesLogic.actions, 'onEnginesLoad'); jest.spyOn(EnginesLogic.actions, 'onEnginesLoad');
EnginesLogic.actions.loadEngines(); EnginesLogic.actions.loadEngines();
await promise; await nextTick();
expect(http.get).toHaveBeenCalledWith('/api/app_search/engines', { expect(http.get).toHaveBeenCalledWith('/api/app_search/engines', {
query: { type: 'indexed', pageIndex: 10 }, query: { type: 'indexed', pageIndex: 10 },
@ -144,13 +145,12 @@ describe('EnginesLogic', () => {
describe('loadMetaEngines', () => { describe('loadMetaEngines', () => {
it('should call the engines API endpoint and set state based on the results', async () => { it('should call the engines API endpoint and set state based on the results', async () => {
const promise = Promise.resolve(MOCK_ENGINES_API_RESPONSE); http.get.mockReturnValueOnce(Promise.resolve(MOCK_ENGINES_API_RESPONSE));
http.get.mockReturnValueOnce(promise);
mount({ metaEnginesPage: 99 }); mount({ metaEnginesPage: 99 });
jest.spyOn(EnginesLogic.actions, 'onMetaEnginesLoad'); jest.spyOn(EnginesLogic.actions, 'onMetaEnginesLoad');
EnginesLogic.actions.loadMetaEngines(); EnginesLogic.actions.loadMetaEngines();
await promise; await nextTick();
expect(http.get).toHaveBeenCalledWith('/api/app_search/engines', { expect(http.get).toHaveBeenCalledWith('/api/app_search/engines', {
query: { type: 'meta', pageIndex: 99 }, query: { type: 'meta', pageIndex: 99 },

View file

@ -4,12 +4,9 @@
* 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 { import { LogicMounter, mockHttpValues, mockFlashMessageHelpers } from '../../../__mocks__';
LogicMounter,
mockHttpValues, import { nextTick } from '@kbn/test/jest';
mockFlashMessageHelpers,
expectedAsyncError,
} from '../../../__mocks__';
import { LogRetentionOptions } from './types'; import { LogRetentionOptions } from './types';
import { LogRetentionLogic } from './log_retention_logic'; import { LogRetentionLogic } from './log_retention_logic';
@ -202,8 +199,7 @@ describe('LogRetentionLogic', () => {
it('will call an API endpoint and update log retention', async () => { it('will call an API endpoint and update log retention', async () => {
jest.spyOn(LogRetentionLogic.actions, 'updateLogRetention'); jest.spyOn(LogRetentionLogic.actions, 'updateLogRetention');
const promise = Promise.resolve(TYPICAL_SERVER_LOG_RETENTION); http.put.mockReturnValue(Promise.resolve(TYPICAL_SERVER_LOG_RETENTION));
http.put.mockReturnValue(promise);
LogRetentionLogic.actions.saveLogRetention(LogRetentionOptions.Analytics, true); LogRetentionLogic.actions.saveLogRetention(LogRetentionOptions.Analytics, true);
@ -215,7 +211,7 @@ describe('LogRetentionLogic', () => {
}), }),
}); });
await promise; await nextTick();
expect(LogRetentionLogic.actions.updateLogRetention).toHaveBeenCalledWith( expect(LogRetentionLogic.actions.updateLogRetention).toHaveBeenCalledWith(
TYPICAL_CLIENT_LOG_RETENTION TYPICAL_CLIENT_LOG_RETENTION
); );
@ -224,11 +220,10 @@ describe('LogRetentionLogic', () => {
}); });
it('handles errors', async () => { it('handles errors', async () => {
const promise = Promise.reject('An error occured'); http.put.mockReturnValue(Promise.reject('An error occured'));
http.put.mockReturnValue(promise);
LogRetentionLogic.actions.saveLogRetention(LogRetentionOptions.Analytics, true); LogRetentionLogic.actions.saveLogRetention(LogRetentionOptions.Analytics, true);
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured'); expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
expect(LogRetentionLogic.actions.clearLogRetentionUpdating).toHaveBeenCalled(); expect(LogRetentionLogic.actions.clearLogRetentionUpdating).toHaveBeenCalled();
@ -276,14 +271,13 @@ describe('LogRetentionLogic', () => {
.spyOn(LogRetentionLogic.actions, 'updateLogRetention') .spyOn(LogRetentionLogic.actions, 'updateLogRetention')
.mockImplementationOnce(() => {}); .mockImplementationOnce(() => {});
const promise = Promise.resolve(TYPICAL_SERVER_LOG_RETENTION); http.get.mockReturnValue(Promise.resolve(TYPICAL_SERVER_LOG_RETENTION));
http.get.mockReturnValue(promise);
LogRetentionLogic.actions.fetchLogRetention(); LogRetentionLogic.actions.fetchLogRetention();
expect(LogRetentionLogic.values.isLogRetentionUpdating).toBe(true); expect(LogRetentionLogic.values.isLogRetentionUpdating).toBe(true);
expect(http.get).toHaveBeenCalledWith('/api/app_search/log_settings'); expect(http.get).toHaveBeenCalledWith('/api/app_search/log_settings');
await promise; await nextTick();
expect(LogRetentionLogic.actions.updateLogRetention).toHaveBeenCalledWith( expect(LogRetentionLogic.actions.updateLogRetention).toHaveBeenCalledWith(
TYPICAL_CLIENT_LOG_RETENTION TYPICAL_CLIENT_LOG_RETENTION
); );
@ -293,11 +287,10 @@ describe('LogRetentionLogic', () => {
it('handles errors', async () => { it('handles errors', async () => {
mount(); mount();
jest.spyOn(LogRetentionLogic.actions, 'clearLogRetentionUpdating'); jest.spyOn(LogRetentionLogic.actions, 'clearLogRetentionUpdating');
const promise = Promise.reject('An error occured'); http.get.mockReturnValue(Promise.reject('An error occured'));
http.get.mockReturnValue(promise);
LogRetentionLogic.actions.fetchLogRetention(); LogRetentionLogic.actions.fetchLogRetention();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured'); expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
expect(LogRetentionLogic.actions.clearLogRetentionUpdating).toHaveBeenCalled(); expect(LogRetentionLogic.actions.clearLogRetentionUpdating).toHaveBeenCalled();

View file

@ -4,12 +4,9 @@
* 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 { import { LogicMounter, mockFlashMessageHelpers, mockHttpValues } from '../../__mocks__';
LogicMounter,
mockFlashMessageHelpers, import { nextTick } from '@kbn/test/jest';
mockHttpValues,
expectedAsyncError,
} from '../../__mocks__';
import { IndexingStatusLogic } from './indexing_status_logic'; import { IndexingStatusLogic } from './indexing_status_logic';
@ -57,37 +54,34 @@ describe('IndexingStatusLogic', () => {
it('calls API and sets values', async () => { it('calls API and sets values', async () => {
const setIndexingStatusSpy = jest.spyOn(IndexingStatusLogic.actions, 'setIndexingStatus'); const setIndexingStatusSpy = jest.spyOn(IndexingStatusLogic.actions, 'setIndexingStatus');
const promise = Promise.resolve(mockStatusResponse); http.get.mockReturnValue(Promise.resolve(mockStatusResponse));
http.get.mockReturnValue(promise);
IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete }); IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete });
jest.advanceTimersByTime(TIMEOUT); jest.advanceTimersByTime(TIMEOUT);
expect(http.get).toHaveBeenCalledWith(statusPath); expect(http.get).toHaveBeenCalledWith(statusPath);
await promise; await nextTick();
expect(setIndexingStatusSpy).toHaveBeenCalledWith(mockStatusResponse); expect(setIndexingStatusSpy).toHaveBeenCalledWith(mockStatusResponse);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('An error occured'); http.get.mockReturnValue(Promise.reject('An error occured'));
http.get.mockReturnValue(promise);
IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete }); IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete });
jest.advanceTimersByTime(TIMEOUT); jest.advanceTimersByTime(TIMEOUT);
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured'); expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}); });
it('handles indexing complete state', async () => { it('handles indexing complete state', async () => {
const promise = Promise.resolve({ ...mockStatusResponse, percentageComplete: 100 }); http.get.mockReturnValue(Promise.resolve({ ...mockStatusResponse, percentageComplete: 100 }));
http.get.mockReturnValue(promise);
IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete }); IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete });
jest.advanceTimersByTime(TIMEOUT); jest.advanceTimersByTime(TIMEOUT);
await promise; await nextTick();
expect(clearInterval).toHaveBeenCalled(); expect(clearInterval).toHaveBeenCalled();
expect(onComplete).toHaveBeenCalledWith(mockStatusResponse.numDocumentsWithErrors); expect(onComplete).toHaveBeenCalledWith(mockStatusResponse.numDocumentsWithErrors);

View file

@ -4,18 +4,15 @@
* 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 { import { LogicMounter, mockFlashMessageHelpers, mockHttpValues } from '../../../../../__mocks__';
LogicMounter,
mockFlashMessageHelpers,
mockHttpValues,
expectedAsyncError,
} from '../../../../../__mocks__';
import { AppLogic } from '../../../../app_logic'; import { AppLogic } from '../../../../app_logic';
jest.mock('../../../../app_logic', () => ({ jest.mock('../../../../app_logic', () => ({
AppLogic: { values: { isOrganization: true } }, AppLogic: { values: { isOrganization: true } },
})); }));
import { nextTick } from '@kbn/test/jest';
import { CustomSource } from '../../../../types'; import { CustomSource } from '../../../../types';
import { sourceConfigData } from '../../../../__mocks__/content_sources.mock'; import { sourceConfigData } from '../../../../__mocks__/content_sources.mock';
@ -271,23 +268,21 @@ describe('AddSourceLogic', () => {
describe('getSourceConfigData', () => { describe('getSourceConfigData', () => {
it('calls API and sets values', async () => { it('calls API and sets values', async () => {
const setSourceConfigDataSpy = jest.spyOn(AddSourceLogic.actions, 'setSourceConfigData'); const setSourceConfigDataSpy = jest.spyOn(AddSourceLogic.actions, 'setSourceConfigData');
const promise = Promise.resolve(sourceConfigData); http.get.mockReturnValue(Promise.resolve(sourceConfigData));
http.get.mockReturnValue(promise);
AddSourceLogic.actions.getSourceConfigData('github'); AddSourceLogic.actions.getSourceConfigData('github');
expect(http.get).toHaveBeenCalledWith( expect(http.get).toHaveBeenCalledWith(
'/api/workplace_search/org/settings/connectors/github' '/api/workplace_search/org/settings/connectors/github'
); );
await promise; await nextTick();
expect(setSourceConfigDataSpy).toHaveBeenCalledWith(sourceConfigData); expect(setSourceConfigDataSpy).toHaveBeenCalledWith(sourceConfigData);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.get.mockReturnValue(Promise.reject('this is an error'));
http.get.mockReturnValue(promise);
AddSourceLogic.actions.getSourceConfigData('github'); AddSourceLogic.actions.getSourceConfigData('github');
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -302,15 +297,14 @@ describe('AddSourceLogic', () => {
AddSourceLogic.actions, AddSourceLogic.actions,
'setSourceConnectData' 'setSourceConnectData'
); );
const promise = Promise.resolve(sourceConnectData); http.get.mockReturnValue(Promise.resolve(sourceConnectData));
http.get.mockReturnValue(promise);
AddSourceLogic.actions.getSourceConnectData('github', successCallback); AddSourceLogic.actions.getSourceConnectData('github', successCallback);
expect(clearFlashMessages).toHaveBeenCalled(); expect(clearFlashMessages).toHaveBeenCalled();
expect(AddSourceLogic.values.buttonLoading).toEqual(true); expect(AddSourceLogic.values.buttonLoading).toEqual(true);
expect(http.get).toHaveBeenCalledWith('/api/workplace_search/org/sources/github/prepare'); expect(http.get).toHaveBeenCalledWith('/api/workplace_search/org/sources/github/prepare');
await promise; await nextTick();
expect(setSourceConnectDataSpy).toHaveBeenCalledWith(sourceConnectData); expect(setSourceConnectDataSpy).toHaveBeenCalledWith(sourceConnectData);
expect(successCallback).toHaveBeenCalledWith(sourceConnectData.oauthUrl); expect(successCallback).toHaveBeenCalledWith(sourceConnectData.oauthUrl);
expect(setButtonNotLoadingSpy).toHaveBeenCalled(); expect(setButtonNotLoadingSpy).toHaveBeenCalled();
@ -327,11 +321,10 @@ describe('AddSourceLogic', () => {
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.get.mockReturnValue(Promise.reject('this is an error'));
http.get.mockReturnValue(promise);
AddSourceLogic.actions.getSourceConnectData('github', successCallback); AddSourceLogic.actions.getSourceConnectData('github', successCallback);
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -343,24 +336,22 @@ describe('AddSourceLogic', () => {
AddSourceLogic.actions, AddSourceLogic.actions,
'setSourceConnectData' 'setSourceConnectData'
); );
const promise = Promise.resolve(sourceConnectData); http.get.mockReturnValue(Promise.resolve(sourceConnectData));
http.get.mockReturnValue(promise);
AddSourceLogic.actions.getSourceReConnectData('github'); AddSourceLogic.actions.getSourceReConnectData('github');
expect(http.get).toHaveBeenCalledWith( expect(http.get).toHaveBeenCalledWith(
'/api/workplace_search/org/sources/github/reauth_prepare' '/api/workplace_search/org/sources/github/reauth_prepare'
); );
await promise; await nextTick();
expect(setSourceConnectDataSpy).toHaveBeenCalledWith(sourceConnectData); expect(setSourceConnectDataSpy).toHaveBeenCalledWith(sourceConnectData);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.get.mockReturnValue(Promise.reject('this is an error'));
http.get.mockReturnValue(promise);
AddSourceLogic.actions.getSourceReConnectData('github'); AddSourceLogic.actions.getSourceReConnectData('github');
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -372,22 +363,20 @@ describe('AddSourceLogic', () => {
AddSourceLogic.actions, AddSourceLogic.actions,
'setPreContentSourceConfigData' 'setPreContentSourceConfigData'
); );
const promise = Promise.resolve(config); http.get.mockReturnValue(Promise.resolve(config));
http.get.mockReturnValue(promise);
AddSourceLogic.actions.getPreContentSourceConfigData('123'); AddSourceLogic.actions.getPreContentSourceConfigData('123');
expect(http.get).toHaveBeenCalledWith('/api/workplace_search/org/pre_sources/123'); expect(http.get).toHaveBeenCalledWith('/api/workplace_search/org/pre_sources/123');
await promise; await nextTick();
expect(setPreContentSourceConfigDataSpy).toHaveBeenCalledWith(config); expect(setPreContentSourceConfigDataSpy).toHaveBeenCalledWith(config);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.get.mockReturnValue(Promise.reject('this is an error'));
http.get.mockReturnValue(promise);
AddSourceLogic.actions.getPreContentSourceConfigData('123'); AddSourceLogic.actions.getPreContentSourceConfigData('123');
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -414,8 +403,7 @@ describe('AddSourceLogic', () => {
const successCallback = jest.fn(); const successCallback = jest.fn();
const setButtonNotLoadingSpy = jest.spyOn(AddSourceLogic.actions, 'setButtonNotLoading'); const setButtonNotLoadingSpy = jest.spyOn(AddSourceLogic.actions, 'setButtonNotLoading');
const setSourceConfigDataSpy = jest.spyOn(AddSourceLogic.actions, 'setSourceConfigData'); const setSourceConfigDataSpy = jest.spyOn(AddSourceLogic.actions, 'setSourceConfigData');
const promise = Promise.resolve({ sourceConfigData }); http.put.mockReturnValue(Promise.resolve({ sourceConfigData }));
http.put.mockReturnValue(promise);
AddSourceLogic.actions.saveSourceConfig(true, successCallback); AddSourceLogic.actions.saveSourceConfig(true, successCallback);
@ -428,7 +416,7 @@ describe('AddSourceLogic', () => {
{ body: JSON.stringify({ params }) } { body: JSON.stringify({ params }) }
); );
await promise; await nextTick();
expect(successCallback).toHaveBeenCalled(); expect(successCallback).toHaveBeenCalled();
expect(setSourceConfigDataSpy).toHaveBeenCalledWith({ sourceConfigData }); expect(setSourceConfigDataSpy).toHaveBeenCalledWith({ sourceConfigData });
expect(setButtonNotLoadingSpy).toHaveBeenCalled(); expect(setButtonNotLoadingSpy).toHaveBeenCalled();
@ -453,11 +441,10 @@ describe('AddSourceLogic', () => {
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.put.mockReturnValue(Promise.reject('this is an error'));
http.put.mockReturnValue(promise);
AddSourceLogic.actions.saveSourceConfig(true); AddSourceLogic.actions.saveSourceConfig(true);
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -495,8 +482,7 @@ describe('AddSourceLogic', () => {
it('calls API and sets values', async () => { it('calls API and sets values', async () => {
const setButtonNotLoadingSpy = jest.spyOn(AddSourceLogic.actions, 'setButtonNotLoading'); const setButtonNotLoadingSpy = jest.spyOn(AddSourceLogic.actions, 'setButtonNotLoading');
const setCustomSourceDataSpy = jest.spyOn(AddSourceLogic.actions, 'setCustomSourceData'); const setCustomSourceDataSpy = jest.spyOn(AddSourceLogic.actions, 'setCustomSourceData');
const promise = Promise.resolve({ sourceConfigData }); http.post.mockReturnValue(Promise.resolve({ sourceConfigData }));
http.post.mockReturnValue(promise);
AddSourceLogic.actions.createContentSource(serviceType, successCallback, errorCallback); AddSourceLogic.actions.createContentSource(serviceType, successCallback, errorCallback);
@ -505,18 +491,17 @@ describe('AddSourceLogic', () => {
expect(http.post).toHaveBeenCalledWith('/api/workplace_search/org/create_source', { expect(http.post).toHaveBeenCalledWith('/api/workplace_search/org/create_source', {
body: JSON.stringify({ ...params }), body: JSON.stringify({ ...params }),
}); });
await promise; await nextTick();
expect(setCustomSourceDataSpy).toHaveBeenCalledWith({ sourceConfigData }); expect(setCustomSourceDataSpy).toHaveBeenCalledWith({ sourceConfigData });
expect(successCallback).toHaveBeenCalled(); expect(successCallback).toHaveBeenCalled();
expect(setButtonNotLoadingSpy).toHaveBeenCalled(); expect(setButtonNotLoadingSpy).toHaveBeenCalled();
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.post.mockReturnValue(Promise.reject('this is an error'));
http.post.mockReturnValue(promise);
AddSourceLogic.actions.createContentSource(serviceType, successCallback, errorCallback); AddSourceLogic.actions.createContentSource(serviceType, successCallback, errorCallback);
await expectedAsyncError(promise); await nextTick();
expect(errorCallback).toHaveBeenCalled(); expect(errorCallback).toHaveBeenCalled();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');

View file

@ -6,11 +6,7 @@
import { LogicMounter } from '../../../../../__mocks__/kea.mock'; import { LogicMounter } from '../../../../../__mocks__/kea.mock';
import { import { mockFlashMessageHelpers, mockHttpValues } from '../../../../../__mocks__';
mockFlashMessageHelpers,
mockHttpValues,
expectedAsyncError,
} from '../../../../../__mocks__';
const contentSource = { id: 'source123' }; const contentSource = { id: 'source123' };
jest.mock('../../source_logic', () => ({ jest.mock('../../source_logic', () => ({
@ -22,6 +18,8 @@ jest.mock('../../../../app_logic', () => ({
AppLogic: { values: { isOrganization: true } }, AppLogic: { values: { isOrganization: true } },
})); }));
import { nextTick } from '@kbn/test/jest';
import { exampleResult } from '../../../../__mocks__/content_sources.mock'; import { exampleResult } from '../../../../__mocks__/content_sources.mock';
import { LEAVE_UNASSIGNED_FIELD } from './constants'; import { LEAVE_UNASSIGNED_FIELD } from './constants';
@ -286,14 +284,13 @@ describe('DisplaySettingsLogic', () => {
DisplaySettingsLogic.actions, DisplaySettingsLogic.actions,
'onInitializeDisplaySettings' 'onInitializeDisplaySettings'
); );
const promise = Promise.resolve(serverProps); http.get.mockReturnValue(Promise.resolve(serverProps));
http.get.mockReturnValue(promise);
DisplaySettingsLogic.actions.initializeDisplaySettings(); DisplaySettingsLogic.actions.initializeDisplaySettings();
expect(http.get).toHaveBeenCalledWith( expect(http.get).toHaveBeenCalledWith(
'/api/workplace_search/org/sources/source123/display_settings/config' '/api/workplace_search/org/sources/source123/display_settings/config'
); );
await promise; await nextTick();
expect(onInitializeDisplaySettingsSpy).toHaveBeenCalledWith({ expect(onInitializeDisplaySettingsSpy).toHaveBeenCalledWith({
...serverProps, ...serverProps,
isOrganization: true, isOrganization: true,
@ -307,14 +304,13 @@ describe('DisplaySettingsLogic', () => {
DisplaySettingsLogic.actions, DisplaySettingsLogic.actions,
'onInitializeDisplaySettings' 'onInitializeDisplaySettings'
); );
const promise = Promise.resolve(serverProps); http.get.mockReturnValue(Promise.resolve(serverProps));
http.get.mockReturnValue(promise);
DisplaySettingsLogic.actions.initializeDisplaySettings(); DisplaySettingsLogic.actions.initializeDisplaySettings();
expect(http.get).toHaveBeenCalledWith( expect(http.get).toHaveBeenCalledWith(
'/api/workplace_search/account/sources/source123/display_settings/config' '/api/workplace_search/account/sources/source123/display_settings/config'
); );
await promise; await nextTick();
expect(onInitializeDisplaySettingsSpy).toHaveBeenCalledWith({ expect(onInitializeDisplaySettingsSpy).toHaveBeenCalledWith({
...serverProps, ...serverProps,
isOrganization: false, isOrganization: false,
@ -322,10 +318,9 @@ describe('DisplaySettingsLogic', () => {
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.get.mockReturnValue(Promise.reject('this is an error'));
http.get.mockReturnValue(promise);
DisplaySettingsLogic.actions.initializeDisplaySettings(); DisplaySettingsLogic.actions.initializeDisplaySettings();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -337,25 +332,23 @@ describe('DisplaySettingsLogic', () => {
DisplaySettingsLogic.actions, DisplaySettingsLogic.actions,
'setServerResponseData' 'setServerResponseData'
); );
const promise = Promise.resolve(serverProps); http.post.mockReturnValue(Promise.resolve(serverProps));
http.post.mockReturnValue(promise);
DisplaySettingsLogic.actions.onInitializeDisplaySettings(serverProps); DisplaySettingsLogic.actions.onInitializeDisplaySettings(serverProps);
DisplaySettingsLogic.actions.setServerData(); DisplaySettingsLogic.actions.setServerData();
expect(http.post).toHaveBeenCalledWith(serverProps.serverRoute, { expect(http.post).toHaveBeenCalledWith(serverProps.serverRoute, {
body: JSON.stringify({ ...searchResultConfig }), body: JSON.stringify({ ...searchResultConfig }),
}); });
await promise; await nextTick();
expect(setServerResponseDataSpy).toHaveBeenCalledWith({ expect(setServerResponseDataSpy).toHaveBeenCalledWith({
...serverProps, ...serverProps,
}); });
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.post.mockReturnValue(Promise.reject('this is an error'));
http.post.mockReturnValue(promise);
DisplaySettingsLogic.actions.setServerData(); DisplaySettingsLogic.actions.setServerData();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });

View file

@ -4,12 +4,9 @@
* 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 { import { LogicMounter, mockFlashMessageHelpers, mockHttpValues } from '../../../../../__mocks__';
LogicMounter,
mockFlashMessageHelpers, import { nextTick } from '@kbn/test/jest';
mockHttpValues,
expectedAsyncError,
} from '../../../../../__mocks__';
const contentSource = { id: 'source123' }; const contentSource = { id: 'source123' };
jest.mock('../../source_logic', () => ({ jest.mock('../../source_logic', () => ({
@ -198,14 +195,13 @@ describe('SchemaLogic', () => {
describe('initializeSchema', () => { describe('initializeSchema', () => {
it('calls API and sets values (org)', async () => { it('calls API and sets values (org)', async () => {
const onInitializeSchemaSpy = jest.spyOn(SchemaLogic.actions, 'onInitializeSchema'); const onInitializeSchemaSpy = jest.spyOn(SchemaLogic.actions, 'onInitializeSchema');
const promise = Promise.resolve(serverResponse); http.get.mockReturnValue(Promise.resolve(serverResponse));
http.get.mockReturnValue(promise);
SchemaLogic.actions.initializeSchema(); SchemaLogic.actions.initializeSchema();
expect(http.get).toHaveBeenCalledWith( expect(http.get).toHaveBeenCalledWith(
'/api/workplace_search/org/sources/source123/schemas' '/api/workplace_search/org/sources/source123/schemas'
); );
await promise; await nextTick();
expect(onInitializeSchemaSpy).toHaveBeenCalledWith(serverResponse); expect(onInitializeSchemaSpy).toHaveBeenCalledWith(serverResponse);
}); });
@ -213,22 +209,20 @@ describe('SchemaLogic', () => {
AppLogic.values.isOrganization = false; AppLogic.values.isOrganization = false;
const onInitializeSchemaSpy = jest.spyOn(SchemaLogic.actions, 'onInitializeSchema'); const onInitializeSchemaSpy = jest.spyOn(SchemaLogic.actions, 'onInitializeSchema');
const promise = Promise.resolve(serverResponse); http.get.mockReturnValue(Promise.resolve(serverResponse));
http.get.mockReturnValue(promise);
SchemaLogic.actions.initializeSchema(); SchemaLogic.actions.initializeSchema();
expect(http.get).toHaveBeenCalledWith( expect(http.get).toHaveBeenCalledWith(
'/api/workplace_search/account/sources/source123/schemas' '/api/workplace_search/account/sources/source123/schemas'
); );
await promise; await nextTick();
expect(onInitializeSchemaSpy).toHaveBeenCalledWith(serverResponse); expect(onInitializeSchemaSpy).toHaveBeenCalledWith(serverResponse);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.get.mockReturnValue(Promise.reject('this is an error'));
http.get.mockReturnValue(promise);
SchemaLogic.actions.initializeSchema(); SchemaLogic.actions.initializeSchema();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -297,13 +291,12 @@ describe('SchemaLogic', () => {
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject({ error: 'this is an error' }); http.get.mockReturnValue(Promise.reject({ error: 'this is an error' }));
http.get.mockReturnValue(promise);
SchemaLogic.actions.initializeSchemaFieldErrors( SchemaLogic.actions.initializeSchemaFieldErrors(
mostRecentIndexJob.activeReindexJobId, mostRecentIndexJob.activeReindexJobId,
contentSource.id contentSource.id
); );
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith({ expect(flashAPIErrors).toHaveBeenCalledWith({
error: 'this is an error', error: 'this is an error',
@ -352,8 +345,7 @@ describe('SchemaLogic', () => {
it('calls API and sets values (org)', async () => { it('calls API and sets values (org)', async () => {
AppLogic.values.isOrganization = true; AppLogic.values.isOrganization = true;
const onSchemaSetSuccessSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetSuccess'); const onSchemaSetSuccessSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetSuccess');
const promise = Promise.resolve(serverResponse); http.post.mockReturnValue(Promise.resolve(serverResponse));
http.post.mockReturnValue(promise);
SchemaLogic.actions.setServerField(schema, ADD); SchemaLogic.actions.setServerField(schema, ADD);
expect(http.post).toHaveBeenCalledWith( expect(http.post).toHaveBeenCalledWith(
@ -362,7 +354,7 @@ describe('SchemaLogic', () => {
body: JSON.stringify({ ...schema }), body: JSON.stringify({ ...schema }),
} }
); );
await promise; await nextTick();
expect(setSuccessMessage).toHaveBeenCalledWith(SCHEMA_FIELD_ADDED_MESSAGE); expect(setSuccessMessage).toHaveBeenCalledWith(SCHEMA_FIELD_ADDED_MESSAGE);
expect(onSchemaSetSuccessSpy).toHaveBeenCalledWith(serverResponse); expect(onSchemaSetSuccessSpy).toHaveBeenCalledWith(serverResponse);
}); });
@ -371,8 +363,7 @@ describe('SchemaLogic', () => {
AppLogic.values.isOrganization = false; AppLogic.values.isOrganization = false;
const onSchemaSetSuccessSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetSuccess'); const onSchemaSetSuccessSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetSuccess');
const promise = Promise.resolve(serverResponse); http.post.mockReturnValue(Promise.resolve(serverResponse));
http.post.mockReturnValue(promise);
SchemaLogic.actions.setServerField(schema, ADD); SchemaLogic.actions.setServerField(schema, ADD);
expect(http.post).toHaveBeenCalledWith( expect(http.post).toHaveBeenCalledWith(
@ -381,16 +372,15 @@ describe('SchemaLogic', () => {
body: JSON.stringify({ ...schema }), body: JSON.stringify({ ...schema }),
} }
); );
await promise; await nextTick();
expect(onSchemaSetSuccessSpy).toHaveBeenCalledWith(serverResponse); expect(onSchemaSetSuccessSpy).toHaveBeenCalledWith(serverResponse);
}); });
it('handles error', async () => { it('handles error', async () => {
const onSchemaSetFormErrorsSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetFormErrors'); const onSchemaSetFormErrorsSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetFormErrors');
const promise = Promise.reject({ message: 'this is an error' }); http.post.mockReturnValue(Promise.reject({ message: 'this is an error' }));
http.post.mockReturnValue(promise);
SchemaLogic.actions.setServerField(schema, ADD); SchemaLogic.actions.setServerField(schema, ADD);
await expectedAsyncError(promise); await nextTick();
expect(onSchemaSetFormErrorsSpy).toHaveBeenCalledWith('this is an error'); expect(onSchemaSetFormErrorsSpy).toHaveBeenCalledWith('this is an error');
}); });
@ -400,8 +390,7 @@ describe('SchemaLogic', () => {
it('calls API and sets values (org)', async () => { it('calls API and sets values (org)', async () => {
AppLogic.values.isOrganization = true; AppLogic.values.isOrganization = true;
const onSchemaSetSuccessSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetSuccess'); const onSchemaSetSuccessSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetSuccess');
const promise = Promise.resolve(serverResponse); http.post.mockReturnValue(Promise.resolve(serverResponse));
http.post.mockReturnValue(promise);
SchemaLogic.actions.setServerField(schema, UPDATE); SchemaLogic.actions.setServerField(schema, UPDATE);
expect(http.post).toHaveBeenCalledWith( expect(http.post).toHaveBeenCalledWith(
@ -410,7 +399,7 @@ describe('SchemaLogic', () => {
body: JSON.stringify({ ...schema }), body: JSON.stringify({ ...schema }),
} }
); );
await promise; await nextTick();
expect(setSuccessMessage).toHaveBeenCalledWith(SCHEMA_UPDATED_MESSAGE); expect(setSuccessMessage).toHaveBeenCalledWith(SCHEMA_UPDATED_MESSAGE);
expect(onSchemaSetSuccessSpy).toHaveBeenCalledWith(serverResponse); expect(onSchemaSetSuccessSpy).toHaveBeenCalledWith(serverResponse);
}); });
@ -419,8 +408,7 @@ describe('SchemaLogic', () => {
AppLogic.values.isOrganization = false; AppLogic.values.isOrganization = false;
const onSchemaSetSuccessSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetSuccess'); const onSchemaSetSuccessSpy = jest.spyOn(SchemaLogic.actions, 'onSchemaSetSuccess');
const promise = Promise.resolve(serverResponse); http.post.mockReturnValue(Promise.resolve(serverResponse));
http.post.mockReturnValue(promise);
SchemaLogic.actions.setServerField(schema, UPDATE); SchemaLogic.actions.setServerField(schema, UPDATE);
expect(http.post).toHaveBeenCalledWith( expect(http.post).toHaveBeenCalledWith(
@ -429,15 +417,14 @@ describe('SchemaLogic', () => {
body: JSON.stringify({ ...schema }), body: JSON.stringify({ ...schema }),
} }
); );
await promise; await nextTick();
expect(onSchemaSetSuccessSpy).toHaveBeenCalledWith(serverResponse); expect(onSchemaSetSuccessSpy).toHaveBeenCalledWith(serverResponse);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.post.mockReturnValue(Promise.reject('this is an error'));
http.post.mockReturnValue(promise);
SchemaLogic.actions.setServerField(schema, UPDATE); SchemaLogic.actions.setServerField(schema, UPDATE);
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });

View file

@ -9,9 +9,10 @@ import {
mockKibanaValues, mockKibanaValues,
mockFlashMessageHelpers, mockFlashMessageHelpers,
mockHttpValues, mockHttpValues,
expectedAsyncError,
} from '../../../__mocks__'; } from '../../../__mocks__';
import { nextTick } from '@kbn/test/jest';
import { groups } from '../../__mocks__/groups.mock'; import { groups } from '../../__mocks__/groups.mock';
import { mockGroupValues } from './__mocks__/group_logic.mock'; import { mockGroupValues } from './__mocks__/group_logic.mock';
import { GroupLogic } from './group_logic'; import { GroupLogic } from './group_logic';
@ -229,32 +230,29 @@ describe('GroupLogic', () => {
describe('initializeGroup', () => { describe('initializeGroup', () => {
it('calls API and sets values', async () => { it('calls API and sets values', async () => {
const onInitializeGroupSpy = jest.spyOn(GroupLogic.actions, 'onInitializeGroup'); const onInitializeGroupSpy = jest.spyOn(GroupLogic.actions, 'onInitializeGroup');
const promise = Promise.resolve(group); http.get.mockReturnValue(Promise.resolve(group));
http.get.mockReturnValue(promise);
GroupLogic.actions.initializeGroup(sourceIds[0]); GroupLogic.actions.initializeGroup(sourceIds[0]);
expect(http.get).toHaveBeenCalledWith('/api/workplace_search/groups/123'); expect(http.get).toHaveBeenCalledWith('/api/workplace_search/groups/123');
await promise; await nextTick();
expect(onInitializeGroupSpy).toHaveBeenCalledWith(group); expect(onInitializeGroupSpy).toHaveBeenCalledWith(group);
}); });
it('handles 404 error', async () => { it('handles 404 error', async () => {
const promise = Promise.reject({ response: { status: 404 } }); http.get.mockReturnValue(Promise.reject({ response: { status: 404 } }));
http.get.mockReturnValue(promise);
GroupLogic.actions.initializeGroup(sourceIds[0]); GroupLogic.actions.initializeGroup(sourceIds[0]);
await expectedAsyncError(promise); await nextTick();
expect(navigateToUrl).toHaveBeenCalledWith(GROUPS_PATH); expect(navigateToUrl).toHaveBeenCalledWith(GROUPS_PATH);
expect(setQueuedErrorMessage).toHaveBeenCalledWith('Unable to find group with ID: "123".'); expect(setQueuedErrorMessage).toHaveBeenCalledWith('Unable to find group with ID: "123".');
}); });
it('handles non-404 error', async () => { it('handles non-404 error', async () => {
const promise = Promise.reject('this is an error'); http.get.mockReturnValue(Promise.reject('this is an error'));
http.get.mockReturnValue(promise);
GroupLogic.actions.initializeGroup(sourceIds[0]); GroupLogic.actions.initializeGroup(sourceIds[0]);
await expectedAsyncError(promise); await nextTick();
expect(navigateToUrl).toHaveBeenCalledWith(GROUPS_PATH); expect(navigateToUrl).toHaveBeenCalledWith(GROUPS_PATH);
expect(setQueuedErrorMessage).toHaveBeenCalledWith('this is an error'); expect(setQueuedErrorMessage).toHaveBeenCalledWith('this is an error');
@ -266,13 +264,12 @@ describe('GroupLogic', () => {
GroupLogic.actions.onInitializeGroup(group); GroupLogic.actions.onInitializeGroup(group);
}); });
it('deletes a group', async () => { it('deletes a group', async () => {
const promise = Promise.resolve(true); http.delete.mockReturnValue(Promise.resolve(true));
http.delete.mockReturnValue(promise);
GroupLogic.actions.deleteGroup(); GroupLogic.actions.deleteGroup();
expect(http.delete).toHaveBeenCalledWith('/api/workplace_search/groups/123'); expect(http.delete).toHaveBeenCalledWith('/api/workplace_search/groups/123');
await promise; await nextTick();
expect(navigateToUrl).toHaveBeenCalledWith(GROUPS_PATH); expect(navigateToUrl).toHaveBeenCalledWith(GROUPS_PATH);
expect(setQueuedSuccessMessage).toHaveBeenCalledWith( expect(setQueuedSuccessMessage).toHaveBeenCalledWith(
'Group "group" was successfully deleted.' 'Group "group" was successfully deleted.'
@ -280,11 +277,10 @@ describe('GroupLogic', () => {
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.delete.mockReturnValue(Promise.reject('this is an error'));
http.delete.mockReturnValue(promise);
GroupLogic.actions.deleteGroup(); GroupLogic.actions.deleteGroup();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -297,15 +293,14 @@ describe('GroupLogic', () => {
}); });
it('updates name', async () => { it('updates name', async () => {
const onGroupNameChangedSpy = jest.spyOn(GroupLogic.actions, 'onGroupNameChanged'); const onGroupNameChangedSpy = jest.spyOn(GroupLogic.actions, 'onGroupNameChanged');
const promise = Promise.resolve(group); http.put.mockReturnValue(Promise.resolve(group));
http.put.mockReturnValue(promise);
GroupLogic.actions.updateGroupName(); GroupLogic.actions.updateGroupName();
expect(http.put).toHaveBeenCalledWith('/api/workplace_search/groups/123', { expect(http.put).toHaveBeenCalledWith('/api/workplace_search/groups/123', {
body: JSON.stringify({ group: { name: 'new name' } }), body: JSON.stringify({ group: { name: 'new name' } }),
}); });
await promise; await nextTick();
expect(onGroupNameChangedSpy).toHaveBeenCalledWith(group); expect(onGroupNameChangedSpy).toHaveBeenCalledWith(group);
expect(setSuccessMessage).toHaveBeenCalledWith( expect(setSuccessMessage).toHaveBeenCalledWith(
'Successfully renamed this group to "group".' 'Successfully renamed this group to "group".'
@ -313,11 +308,10 @@ describe('GroupLogic', () => {
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.put.mockReturnValue(Promise.reject('this is an error'));
http.put.mockReturnValue(promise);
GroupLogic.actions.updateGroupName(); GroupLogic.actions.updateGroupName();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -330,15 +324,14 @@ describe('GroupLogic', () => {
}); });
it('updates name', async () => { it('updates name', async () => {
const onGroupSourcesSavedSpy = jest.spyOn(GroupLogic.actions, 'onGroupSourcesSaved'); const onGroupSourcesSavedSpy = jest.spyOn(GroupLogic.actions, 'onGroupSourcesSaved');
const promise = Promise.resolve(group); http.post.mockReturnValue(Promise.resolve(group));
http.post.mockReturnValue(promise);
GroupLogic.actions.saveGroupSources(); GroupLogic.actions.saveGroupSources();
expect(http.post).toHaveBeenCalledWith('/api/workplace_search/groups/123/share', { expect(http.post).toHaveBeenCalledWith('/api/workplace_search/groups/123/share', {
body: JSON.stringify({ content_source_ids: sourceIds }), body: JSON.stringify({ content_source_ids: sourceIds }),
}); });
await promise; await nextTick();
expect(onGroupSourcesSavedSpy).toHaveBeenCalledWith(group); expect(onGroupSourcesSavedSpy).toHaveBeenCalledWith(group);
expect(setSuccessMessage).toHaveBeenCalledWith( expect(setSuccessMessage).toHaveBeenCalledWith(
'Successfully updated shared content sources.' 'Successfully updated shared content sources.'
@ -346,11 +339,10 @@ describe('GroupLogic', () => {
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.post.mockReturnValue(Promise.reject('this is an error'));
http.post.mockReturnValue(promise);
GroupLogic.actions.saveGroupSources(); GroupLogic.actions.saveGroupSources();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -362,15 +354,14 @@ describe('GroupLogic', () => {
}); });
it('updates name', async () => { it('updates name', async () => {
const onGroupUsersSavedSpy = jest.spyOn(GroupLogic.actions, 'onGroupUsersSaved'); const onGroupUsersSavedSpy = jest.spyOn(GroupLogic.actions, 'onGroupUsersSaved');
const promise = Promise.resolve(group); http.post.mockReturnValue(Promise.resolve(group));
http.post.mockReturnValue(promise);
GroupLogic.actions.saveGroupUsers(); GroupLogic.actions.saveGroupUsers();
expect(http.post).toHaveBeenCalledWith('/api/workplace_search/groups/123/assign', { expect(http.post).toHaveBeenCalledWith('/api/workplace_search/groups/123/assign', {
body: JSON.stringify({ user_ids: userIds }), body: JSON.stringify({ user_ids: userIds }),
}); });
await promise; await nextTick();
expect(onGroupUsersSavedSpy).toHaveBeenCalledWith(group); expect(onGroupUsersSavedSpy).toHaveBeenCalledWith(group);
expect(setSuccessMessage).toHaveBeenCalledWith( expect(setSuccessMessage).toHaveBeenCalledWith(
'Successfully updated the users of this group.' 'Successfully updated the users of this group.'
@ -378,11 +369,10 @@ describe('GroupLogic', () => {
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.post.mockReturnValue(Promise.reject('this is an error'));
http.post.mockReturnValue(promise);
GroupLogic.actions.saveGroupUsers(); GroupLogic.actions.saveGroupUsers();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -397,8 +387,7 @@ describe('GroupLogic', () => {
GroupLogic.actions, GroupLogic.actions,
'onGroupPrioritiesChanged' 'onGroupPrioritiesChanged'
); );
const promise = Promise.resolve(group); http.put.mockReturnValue(Promise.resolve(group));
http.put.mockReturnValue(promise);
GroupLogic.actions.saveGroupSourcePrioritization(); GroupLogic.actions.saveGroupSourcePrioritization();
expect(http.put).toHaveBeenCalledWith('/api/workplace_search/groups/123/boosts', { expect(http.put).toHaveBeenCalledWith('/api/workplace_search/groups/123/boosts', {
@ -410,7 +399,7 @@ describe('GroupLogic', () => {
}), }),
}); });
await promise; await nextTick();
expect(setSuccessMessage).toHaveBeenCalledWith( expect(setSuccessMessage).toHaveBeenCalledWith(
'Successfully updated shared source prioritization.' 'Successfully updated shared source prioritization.'
); );
@ -418,11 +407,10 @@ describe('GroupLogic', () => {
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.put.mockReturnValue(Promise.reject('this is an error'));
http.put.mockReturnValue(promise);
GroupLogic.actions.saveGroupSourcePrioritization(); GroupLogic.actions.saveGroupSourcePrioritization();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });

View file

@ -4,12 +4,9 @@
* 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 { import { LogicMounter, mockFlashMessageHelpers, mockHttpValues } from '../../../__mocks__';
LogicMounter,
mockFlashMessageHelpers, import { nextTick } from '@kbn/test/jest';
mockHttpValues,
expectedAsyncError,
} from '../../../__mocks__';
import { DEFAULT_META } from '../../../shared/constants'; import { DEFAULT_META } from '../../../shared/constants';
import { JSON_HEADER as headers } from '../../../../../common/constants'; import { JSON_HEADER as headers } from '../../../../../common/constants';
@ -22,7 +19,6 @@ import { GroupsLogic } from './groups_logic';
// We need to mock out the debounced functionality // We need to mock out the debounced functionality
const TIMEOUT = 400; const TIMEOUT = 400;
const delay = () => new Promise((resolve) => setTimeout(resolve, TIMEOUT));
describe('GroupsLogic', () => { describe('GroupsLogic', () => {
const { mount } = new LogicMounter(GroupsLogic); const { mount } = new LogicMounter(GroupsLogic);
@ -218,21 +214,19 @@ describe('GroupsLogic', () => {
describe('initializeGroups', () => { describe('initializeGroups', () => {
it('calls API and sets values', async () => { it('calls API and sets values', async () => {
const onInitializeGroupsSpy = jest.spyOn(GroupsLogic.actions, 'onInitializeGroups'); const onInitializeGroupsSpy = jest.spyOn(GroupsLogic.actions, 'onInitializeGroups');
const promise = Promise.resolve(groupsResponse); http.get.mockReturnValue(Promise.resolve(groupsResponse));
http.get.mockReturnValue(promise);
GroupsLogic.actions.initializeGroups(); GroupsLogic.actions.initializeGroups();
expect(http.get).toHaveBeenCalledWith('/api/workplace_search/groups'); expect(http.get).toHaveBeenCalledWith('/api/workplace_search/groups');
await promise; await nextTick();
expect(onInitializeGroupsSpy).toHaveBeenCalledWith(groupsResponse); expect(onInitializeGroupsSpy).toHaveBeenCalledWith(groupsResponse);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.get.mockReturnValue(Promise.reject('this is an error'));
http.get.mockReturnValue(promise);
GroupsLogic.actions.initializeGroups(); GroupsLogic.actions.initializeGroups();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -256,15 +250,22 @@ describe('GroupsLogic', () => {
headers, headers,
}; };
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
it('calls API and sets values', async () => { it('calls API and sets values', async () => {
const setSearchResultsSpy = jest.spyOn(GroupsLogic.actions, 'setSearchResults'); const setSearchResultsSpy = jest.spyOn(GroupsLogic.actions, 'setSearchResults');
const promise = Promise.resolve(groups); http.post.mockReturnValue(Promise.resolve(groups));
http.post.mockReturnValue(promise);
GroupsLogic.actions.getSearchResults(); GroupsLogic.actions.getSearchResults();
await delay(); jest.advanceTimersByTime(TIMEOUT);
await nextTick();
expect(http.post).toHaveBeenCalledWith('/api/workplace_search/groups/search', payload); expect(http.post).toHaveBeenCalledWith('/api/workplace_search/groups/search', payload);
await promise;
expect(setSearchResultsSpy).toHaveBeenCalledWith(groups); expect(setSearchResultsSpy).toHaveBeenCalledWith(groups);
}); });
@ -272,24 +273,22 @@ describe('GroupsLogic', () => {
// Set active page to 2 to confirm resetting sends the `payload` value of 1 for the current page. // Set active page to 2 to confirm resetting sends the `payload` value of 1 for the current page.
GroupsLogic.actions.setActivePage(2); GroupsLogic.actions.setActivePage(2);
const setSearchResultsSpy = jest.spyOn(GroupsLogic.actions, 'setSearchResults'); const setSearchResultsSpy = jest.spyOn(GroupsLogic.actions, 'setSearchResults');
const promise = Promise.resolve(groups); http.post.mockReturnValue(Promise.resolve(groups));
http.post.mockReturnValue(promise);
GroupsLogic.actions.getSearchResults(true); GroupsLogic.actions.getSearchResults(true);
// Account for `breakpoint` that debounces filter value. // Account for `breakpoint` that debounces filter value.
await delay(); jest.advanceTimersByTime(TIMEOUT);
await nextTick();
expect(http.post).toHaveBeenCalledWith('/api/workplace_search/groups/search', payload); expect(http.post).toHaveBeenCalledWith('/api/workplace_search/groups/search', payload);
await promise;
expect(setSearchResultsSpy).toHaveBeenCalledWith(groups); expect(setSearchResultsSpy).toHaveBeenCalledWith(groups);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.post.mockReturnValue(Promise.reject('this is an error'));
http.post.mockReturnValue(promise);
GroupsLogic.actions.getSearchResults(); GroupsLogic.actions.getSearchResults();
await expectedAsyncError(promise); jest.advanceTimersByTime(TIMEOUT);
await delay(); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -298,21 +297,19 @@ describe('GroupsLogic', () => {
describe('fetchGroupUsers', () => { describe('fetchGroupUsers', () => {
it('calls API and sets values', async () => { it('calls API and sets values', async () => {
const setGroupUsersSpy = jest.spyOn(GroupsLogic.actions, 'setGroupUsers'); const setGroupUsersSpy = jest.spyOn(GroupsLogic.actions, 'setGroupUsers');
const promise = Promise.resolve(users); http.get.mockReturnValue(Promise.resolve(users));
http.get.mockReturnValue(promise);
GroupsLogic.actions.fetchGroupUsers('123'); GroupsLogic.actions.fetchGroupUsers('123');
expect(http.get).toHaveBeenCalledWith('/api/workplace_search/groups/123/group_users'); expect(http.get).toHaveBeenCalledWith('/api/workplace_search/groups/123/group_users');
await promise; await nextTick();
expect(setGroupUsersSpy).toHaveBeenCalledWith(users); expect(setGroupUsersSpy).toHaveBeenCalledWith(users);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.get.mockReturnValue(Promise.reject('this is an error'));
http.get.mockReturnValue(promise);
GroupsLogic.actions.fetchGroupUsers('123'); GroupsLogic.actions.fetchGroupUsers('123');
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -323,24 +320,22 @@ describe('GroupsLogic', () => {
const GROUP_NAME = 'new group'; const GROUP_NAME = 'new group';
GroupsLogic.actions.setNewGroupName(GROUP_NAME); GroupsLogic.actions.setNewGroupName(GROUP_NAME);
const setNewGroupSpy = jest.spyOn(GroupsLogic.actions, 'setNewGroup'); const setNewGroupSpy = jest.spyOn(GroupsLogic.actions, 'setNewGroup');
const promise = Promise.resolve(groups[0]); http.post.mockReturnValue(Promise.resolve(groups[0]));
http.post.mockReturnValue(promise);
GroupsLogic.actions.saveNewGroup(); GroupsLogic.actions.saveNewGroup();
expect(http.post).toHaveBeenCalledWith('/api/workplace_search/groups', { expect(http.post).toHaveBeenCalledWith('/api/workplace_search/groups', {
body: JSON.stringify({ group_name: GROUP_NAME }), body: JSON.stringify({ group_name: GROUP_NAME }),
headers, headers,
}); });
await promise; await nextTick();
expect(setNewGroupSpy).toHaveBeenCalledWith(groups[0]); expect(setNewGroupSpy).toHaveBeenCalledWith(groups[0]);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.post.mockReturnValue(Promise.reject('this is an error'));
http.post.mockReturnValue(promise);
GroupsLogic.actions.saveNewGroup(); GroupsLogic.actions.saveNewGroup();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });

View file

@ -6,12 +6,9 @@
import { LogicMounter } from '../../../__mocks__/kea.mock'; import { LogicMounter } from '../../../__mocks__/kea.mock';
import { import { mockFlashMessageHelpers, mockHttpValues, mockKibanaValues } from '../../../__mocks__';
mockFlashMessageHelpers,
mockHttpValues, import { nextTick } from '@kbn/test/jest';
expectedAsyncError,
mockKibanaValues,
} from '../../../__mocks__';
import { configuredSources, oauthApplication } from '../../__mocks__/content_sources.mock'; import { configuredSources, oauthApplication } from '../../__mocks__/content_sources.mock';
@ -89,20 +86,18 @@ describe('SettingsLogic', () => {
describe('initializeSettings', () => { describe('initializeSettings', () => {
it('calls API and sets values', async () => { it('calls API and sets values', async () => {
const setServerPropsSpy = jest.spyOn(SettingsLogic.actions, 'setServerProps'); const setServerPropsSpy = jest.spyOn(SettingsLogic.actions, 'setServerProps');
const promise = Promise.resolve(configuredSources); http.get.mockReturnValue(Promise.resolve(configuredSources));
http.get.mockReturnValue(promise);
SettingsLogic.actions.initializeSettings(); SettingsLogic.actions.initializeSettings();
expect(http.get).toHaveBeenCalledWith('/api/workplace_search/org/settings'); expect(http.get).toHaveBeenCalledWith('/api/workplace_search/org/settings');
await promise; await nextTick();
expect(setServerPropsSpy).toHaveBeenCalledWith(configuredSources); expect(setServerPropsSpy).toHaveBeenCalledWith(configuredSources);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.get.mockReturnValue(Promise.reject('this is an error'));
http.get.mockReturnValue(promise);
SettingsLogic.actions.initializeSettings(); SettingsLogic.actions.initializeSettings();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -114,20 +109,18 @@ describe('SettingsLogic', () => {
SettingsLogic.actions, SettingsLogic.actions,
'onInitializeConnectors' 'onInitializeConnectors'
); );
const promise = Promise.resolve(serverProps); http.get.mockReturnValue(Promise.resolve(serverProps));
http.get.mockReturnValue(promise);
SettingsLogic.actions.initializeConnectors(); SettingsLogic.actions.initializeConnectors();
expect(http.get).toHaveBeenCalledWith('/api/workplace_search/org/settings/connectors'); expect(http.get).toHaveBeenCalledWith('/api/workplace_search/org/settings/connectors');
await promise; await nextTick();
expect(onInitializeConnectorsSpy).toHaveBeenCalledWith(serverProps); expect(onInitializeConnectorsSpy).toHaveBeenCalledWith(serverProps);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.get.mockReturnValue(Promise.reject('this is an error'));
http.get.mockReturnValue(promise);
SettingsLogic.actions.initializeConnectors(); SettingsLogic.actions.initializeConnectors();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -138,25 +131,23 @@ describe('SettingsLogic', () => {
const NAME = 'updated name'; const NAME = 'updated name';
SettingsLogic.actions.onOrgNameInputChange(NAME); SettingsLogic.actions.onOrgNameInputChange(NAME);
const setUpdatedNameSpy = jest.spyOn(SettingsLogic.actions, 'setUpdatedName'); const setUpdatedNameSpy = jest.spyOn(SettingsLogic.actions, 'setUpdatedName');
const promise = Promise.resolve({ organizationName: NAME }); http.put.mockReturnValue(Promise.resolve({ organizationName: NAME }));
http.put.mockReturnValue(promise);
SettingsLogic.actions.updateOrgName(); SettingsLogic.actions.updateOrgName();
expect(http.put).toHaveBeenCalledWith('/api/workplace_search/org/settings/customize', { expect(http.put).toHaveBeenCalledWith('/api/workplace_search/org/settings/customize', {
body: JSON.stringify({ name: NAME }), body: JSON.stringify({ name: NAME }),
}); });
await promise; await nextTick();
expect(setSuccessMessage).toHaveBeenCalledWith(ORG_UPDATED_MESSAGE); expect(setSuccessMessage).toHaveBeenCalledWith(ORG_UPDATED_MESSAGE);
expect(setUpdatedNameSpy).toHaveBeenCalledWith({ organizationName: NAME }); expect(setUpdatedNameSpy).toHaveBeenCalledWith({ organizationName: NAME });
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.put.mockReturnValue(Promise.reject('this is an error'));
http.put.mockReturnValue(promise);
SettingsLogic.actions.updateOrgName(); SettingsLogic.actions.updateOrgName();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
}); });
@ -168,8 +159,7 @@ describe('SettingsLogic', () => {
SettingsLogic.actions, SettingsLogic.actions,
'setUpdatedOauthApplication' 'setUpdatedOauthApplication'
); );
const promise = Promise.resolve({ oauthApplication }); http.put.mockReturnValue(Promise.resolve({ oauthApplication }));
http.put.mockReturnValue(promise);
SettingsLogic.actions.setOauthApplication(oauthApplication); SettingsLogic.actions.setOauthApplication(oauthApplication);
SettingsLogic.actions.updateOauthApplication(); SettingsLogic.actions.updateOauthApplication();
@ -183,16 +173,15 @@ describe('SettingsLogic', () => {
}), }),
} }
); );
await promise; await nextTick();
expect(setUpdatedOauthApplicationSpy).toHaveBeenCalledWith({ oauthApplication }); expect(setUpdatedOauthApplicationSpy).toHaveBeenCalledWith({ oauthApplication });
expect(setSuccessMessage).toHaveBeenCalledWith(OAUTH_APP_UPDATED_MESSAGE); expect(setSuccessMessage).toHaveBeenCalledWith(OAUTH_APP_UPDATED_MESSAGE);
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.put.mockReturnValue(Promise.reject('this is an error'));
http.put.mockReturnValue(promise);
SettingsLogic.actions.updateOauthApplication(); SettingsLogic.actions.updateOauthApplication();
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });
@ -203,20 +192,18 @@ describe('SettingsLogic', () => {
const NAME = 'baz'; const NAME = 'baz';
it('calls API and sets values', async () => { it('calls API and sets values', async () => {
const promise = Promise.resolve({}); http.delete.mockReturnValue(Promise.resolve({}));
http.delete.mockReturnValue(promise);
SettingsLogic.actions.deleteSourceConfig(SERVICE_TYPE, NAME); SettingsLogic.actions.deleteSourceConfig(SERVICE_TYPE, NAME);
await promise; await nextTick();
expect(navigateToUrl).toHaveBeenCalledWith('/settings/connectors'); expect(navigateToUrl).toHaveBeenCalledWith('/settings/connectors');
expect(setQueuedSuccessMessage).toHaveBeenCalled(); expect(setQueuedSuccessMessage).toHaveBeenCalled();
}); });
it('handles error', async () => { it('handles error', async () => {
const promise = Promise.reject('this is an error'); http.delete.mockReturnValue(Promise.reject('this is an error'));
http.delete.mockReturnValue(promise);
SettingsLogic.actions.deleteSourceConfig(SERVICE_TYPE, NAME); SettingsLogic.actions.deleteSourceConfig(SERVICE_TYPE, NAME);
await expectedAsyncError(promise); await nextTick();
expect(flashAPIErrors).toHaveBeenCalledWith('this is an error'); expect(flashAPIErrors).toHaveBeenCalledWith('this is an error');
}); });