Explore: Add tests around query history errors (#86810)

* WIP build tests

* Fix one test :D

* Unskip, remove console logging, fix limit test

* Add back whitespace
This commit is contained in:
Kristina 2024-04-26 08:09:31 -05:00 committed by GitHub
parent eaad38e492
commit e6f51536bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 1 deletions

View File

@ -185,7 +185,7 @@ function cleanUp(richHistory: RichHistoryLocalStorageDTO[]): RichHistoryLocalSto
* Ensures the entry can be added. Throws an error if current limit has been hit.
* Returns queries that should be saved back giving space for one extra query.
*/
function checkLimits(queriesToKeep: RichHistoryLocalStorageDTO[]): {
export function checkLimits(queriesToKeep: RichHistoryLocalStorageDTO[]): {
queriesToKeep: RichHistoryLocalStorageDTO[];
limitExceeded: boolean;
} {

View File

@ -4,8 +4,10 @@ import { Props } from 'react-virtualized-auto-sizer';
import { EventBusSrv, serializeStateToUrlParam } from '@grafana/data';
import { config } from '@grafana/runtime';
import { DataQuery } from '@grafana/schema';
import store from 'app/core/store';
import { silenceConsoleOutput } from '../../../../test/core/utils/silenceConsoleOutput';
import * as localStorage from '../../../core/history/RichHistoryLocalStorage';
import {
assertDataSourceFilterVisibility,
@ -142,6 +144,55 @@ describe('Explore: Query History', () => {
await assertQueryHistory(['{"expr":"query #2"}', '{"expr":"query #1"}']);
});
it('does not add query if quota exceeded error is reached', async () => {
const urlParams = {
left: serializeStateToUrlParam({
datasource: 'loki',
queries: [{ refId: 'A', expr: 'query #1' }],
range: { from: 'now-1h', to: 'now' },
}),
};
const { datasources } = setupExplore({ urlParams });
jest.mocked(datasources.loki.query).mockReturnValueOnce(makeLogsQueryResponse());
await waitForExplore();
await openQueryHistory();
const storeSpy = jest.spyOn(store, 'setObject').mockImplementation(() => {
const error = new Error('QuotaExceededError');
error.name = 'QuotaExceededError';
throw error;
});
await inputQuery('query #2');
await runQuery();
await assertQueryHistory(['{"expr":"query #1"}']);
storeSpy.mockRestore();
});
it('does add query if limit exceeded error is reached', async () => {
const urlParams = {
left: serializeStateToUrlParam({
datasource: 'loki',
queries: [{ refId: 'A', expr: 'query #1' }],
range: { from: 'now-1h', to: 'now' },
}),
};
const { datasources } = setupExplore({ urlParams });
jest.mocked(datasources.loki.query).mockReturnValueOnce(makeLogsQueryResponse());
await waitForExplore();
await openQueryHistory();
jest.spyOn(localStorage, 'checkLimits').mockImplementationOnce((queries) => {
return { queriesToKeep: queries, limitExceeded: true };
});
await inputQuery('query #2');
await runQuery();
await assertQueryHistory(['{"expr":"query #2"}', '{"expr":"query #1"}']);
});
it('add comments to query history', async () => {
const urlParams = {
left: serializeStateToUrlParam({