mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[Discover] Fix text based query language state transformation hook when there's an error involved (#140697)
This commit is contained in:
parent
517ef60d46
commit
c0c161d709
2 changed files with 75 additions and 3 deletions
|
@ -246,4 +246,73 @@ describe('useTextBasedQueryLanguage', () => {
|
|||
columns: ['field1'],
|
||||
});
|
||||
});
|
||||
|
||||
test('it should not overwrite state column when successfully fetching after an error fetch', async () => {
|
||||
const replaceUrlAppState = jest.fn();
|
||||
const props = getHookProps(replaceUrlAppState, query);
|
||||
props.stateContainer.appStateContainer.getState = jest.fn(() => {
|
||||
return { columns: [], index: 'the-data-view-id' };
|
||||
});
|
||||
const { documents$ } = props;
|
||||
|
||||
renderHook(() => useTextBasedQueryLanguage(props));
|
||||
documents$.next({
|
||||
recordRawType: RecordRawType.PLAIN,
|
||||
fetchStatus: FetchStatus.LOADING,
|
||||
query: { sql: 'SELECT * from the-data-view-title WHERE field1=2' },
|
||||
});
|
||||
await waitFor(() => expect(replaceUrlAppState).toHaveBeenCalledTimes(0));
|
||||
documents$.next({
|
||||
recordRawType: RecordRawType.PLAIN,
|
||||
fetchStatus: FetchStatus.COMPLETE,
|
||||
result: [
|
||||
{
|
||||
id: '1',
|
||||
raw: { field1: 1, field2: 2 },
|
||||
flattened: { field1: 1 },
|
||||
} as unknown as DataTableRecord,
|
||||
],
|
||||
query: { sql: 'SELECT * from the-data-view-title WHERE field1=2' },
|
||||
});
|
||||
await waitFor(() => expect(replaceUrlAppState).toHaveBeenCalledTimes(1));
|
||||
props.stateContainer.appStateContainer.getState = jest.fn(() => {
|
||||
return { columns: ['field1', 'field2'], index: 'the-data-view-id' };
|
||||
});
|
||||
replaceUrlAppState.mockReset();
|
||||
|
||||
documents$.next({
|
||||
recordRawType: RecordRawType.PLAIN,
|
||||
fetchStatus: FetchStatus.LOADING,
|
||||
query: { sql: 'SELECT field1; from the-data-view-title WHERE field1=2' },
|
||||
});
|
||||
|
||||
documents$.next({
|
||||
recordRawType: RecordRawType.PLAIN,
|
||||
fetchStatus: FetchStatus.ERROR,
|
||||
});
|
||||
|
||||
documents$.next({
|
||||
recordRawType: RecordRawType.PLAIN,
|
||||
fetchStatus: FetchStatus.LOADING,
|
||||
query: { sql: 'SELECT field1 from the-data-view-title' },
|
||||
});
|
||||
|
||||
documents$.next({
|
||||
recordRawType: RecordRawType.PLAIN,
|
||||
fetchStatus: FetchStatus.COMPLETE,
|
||||
result: [
|
||||
{
|
||||
id: '1',
|
||||
raw: { field1: 1 },
|
||||
flattened: { field1: 1 },
|
||||
} as unknown as DataTableRecord,
|
||||
],
|
||||
query: { sql: 'SELECT field1 from the-data-view-title' },
|
||||
});
|
||||
|
||||
await waitFor(() => expect(replaceUrlAppState).toHaveBeenCalledTimes(1));
|
||||
expect(replaceUrlAppState).toHaveBeenCalledWith({
|
||||
columns: ['field1'],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -55,11 +55,14 @@ export function useTextBasedQueryLanguage({
|
|||
|
||||
useEffect(() => {
|
||||
const subscription = documents$.subscribe(async (next) => {
|
||||
const { query } = next;
|
||||
const { query, recordRawType } = next;
|
||||
if (!query || next.fetchStatus === FetchStatus.ERROR) {
|
||||
return;
|
||||
}
|
||||
const { columns: stateColumns, index } = stateContainer.appStateContainer.getState();
|
||||
let nextColumns: string[] = [];
|
||||
const isTextBasedQueryLang =
|
||||
next.recordRawType === 'plain' && query && isOfAggregateQueryType(query) && 'sql' in query;
|
||||
recordRawType === 'plain' && isOfAggregateQueryType(query) && 'sql' in query;
|
||||
const hasResults = next.result?.length && next.fetchStatus === FetchStatus.COMPLETE;
|
||||
const initialFetch = !prev.current.columns.length;
|
||||
|
||||
|
@ -72,8 +75,8 @@ export function useTextBasedQueryLanguage({
|
|||
!isEqual(firstRowColumns, prev.current.columns) &&
|
||||
!isEqual(query, prev.current.query)
|
||||
) {
|
||||
prev.current = { columns: firstRowColumns, query };
|
||||
nextColumns = firstRowColumns;
|
||||
prev.current = { columns: nextColumns, query };
|
||||
}
|
||||
if (firstRowColumns && initialFetch) {
|
||||
prev.current = { columns: firstRowColumns, query };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue