diff --git a/public/app/core/history/RichHistoryLocalStorage.ts b/public/app/core/history/RichHistoryLocalStorage.ts index 4e5a1039d85..eee2d841584 100644 --- a/public/app/core/history/RichHistoryLocalStorage.ts +++ b/public/app/core/history/RichHistoryLocalStorage.ts @@ -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; } { diff --git a/public/app/features/explore/spec/queryHistory.test.tsx b/public/app/features/explore/spec/queryHistory.test.tsx index 7ee48e8962c..22e6472a694 100644 --- a/public/app/features/explore/spec/queryHistory.test.tsx +++ b/public/app/features/explore/spec/queryHistory.test.tsx @@ -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({