[Discover] Fix keeping error state after invalid KQL query (#109514)

This commit is contained in:
Matthias Wilhelm 2021-08-23 14:55:26 +02:00 committed by GitHub
parent 64dff78dce
commit 8489e91f2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 0 deletions

View file

@ -0,0 +1,79 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import {
sendCompleteMsg,
sendErrorMsg,
sendLoadingMsg,
sendPartialMsg,
} from './use_saved_search_messages';
import { FetchStatus } from '../../../types';
import { BehaviorSubject } from 'rxjs';
import { DataMainMsg } from './use_saved_search';
describe('test useSavedSearch message generators', () => {
test('sendCompleteMsg', async (done) => {
const main$ = new BehaviorSubject<DataMainMsg>({ fetchStatus: FetchStatus.LOADING });
main$.subscribe((value) => {
if (value.fetchStatus !== FetchStatus.LOADING) {
expect(value.fetchStatus).toBe(FetchStatus.COMPLETE);
expect(value.foundDocuments).toBe(true);
expect(value.error).toBe(undefined);
done();
}
});
sendCompleteMsg(main$, true);
});
test('sendPartialMessage', async (done) => {
const main$ = new BehaviorSubject<DataMainMsg>({ fetchStatus: FetchStatus.LOADING });
main$.subscribe((value) => {
if (value.fetchStatus !== FetchStatus.LOADING) {
expect(value.fetchStatus).toBe(FetchStatus.PARTIAL);
done();
}
});
sendPartialMsg(main$);
});
test('sendLoadingMsg', async (done) => {
const main$ = new BehaviorSubject<DataMainMsg>({ fetchStatus: FetchStatus.COMPLETE });
main$.subscribe((value) => {
if (value.fetchStatus !== FetchStatus.COMPLETE) {
expect(value.fetchStatus).toBe(FetchStatus.LOADING);
done();
}
});
sendLoadingMsg(main$);
});
test('sendErrorMsg', async (done) => {
const main$ = new BehaviorSubject<DataMainMsg>({ fetchStatus: FetchStatus.PARTIAL });
main$.subscribe((value) => {
if (value.fetchStatus === FetchStatus.ERROR) {
expect(value.fetchStatus).toBe(FetchStatus.ERROR);
expect(value.error).toBeInstanceOf(Error);
done();
}
});
sendErrorMsg(main$, new Error('Pls help!'));
});
test('sendCompleteMsg cleaning error state message', async (done) => {
const initialState = {
fetchStatus: FetchStatus.ERROR,
error: new Error('Oh noes!'),
};
const main$ = new BehaviorSubject<DataMainMsg>(initialState);
main$.subscribe((value) => {
if (value.fetchStatus === FetchStatus.COMPLETE) {
const newState = { ...initialState, ...value };
expect(newState.fetchStatus).toBe(FetchStatus.COMPLETE);
expect(newState.error).toBeUndefined();
done();
}
});
sendCompleteMsg(main$, false);
});
});

View file

@ -27,6 +27,7 @@ export function sendCompleteMsg(main$: DataMain$, foundDocuments = true) {
main$.next({
fetchStatus: FetchStatus.COMPLETE,
foundDocuments,
error: undefined,
});
}