From be1414bc8dacfc19b41a7f503eb0cc3cebd5ee92 Mon Sep 17 00:00:00 2001 From: Stratoula Kalafateli Date: Thu, 19 Dec 2024 08:56:26 +0100 Subject: [PATCH] Fix and update unit tests --- .../src/history_local_storage.test.ts | 35 ++++++++++--------- .../src/history_local_storage.ts | 18 ++++++---- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/platform/packages/private/kbn-esql-editor/src/history_local_storage.test.ts b/src/platform/packages/private/kbn-esql-editor/src/history_local_storage.test.ts index 4632bd124f80d..9b8372b146695 100644 --- a/src/platform/packages/private/kbn-esql-editor/src/history_local_storage.test.ts +++ b/src/platform/packages/private/kbn-esql-editor/src/history_local_storage.test.ts @@ -8,15 +8,28 @@ */ import { addQueriesToCache, getCachedQueries } from './history_local_storage'; +class LocalStorageMock { + public store: Record; + constructor(defaultStore: Record) { + this.store = defaultStore; + } + clear() { + this.store = {}; + } + getItem(key: string) { + return this.store[key] || null; + } + setItem(key: string, value: unknown) { + this.store[key] = String(value); + } +} + describe('history local storage', function () { - const mockGetItem = jest.fn(); - const mockSetItem = jest.fn(); + const storage = new LocalStorageMock({}) as unknown as Storage; Object.defineProperty(window, 'localStorage', { - value: { - getItem: (...args: string[]) => mockGetItem(...args), - setItem: (...args: string[]) => mockSetItem(...args), - }, + value: storage, }); + it('should add queries to cache correctly ', function () { addQueriesToCache({ queryString: 'from kibana_sample_data_flights | limit 10', @@ -37,11 +50,6 @@ describe('history local storage', function () { expect(historyItems.length).toBe(2); expect(historyItems[1].timeRan).toBeDefined(); expect(historyItems[1].status).toBe('success'); - - expect(mockSetItem).toHaveBeenCalledWith( - 'QUERY_HISTORY_ITEM_KEY', - JSON.stringify(historyItems) - ); }); it('should update queries to cache correctly if they are the same with different format', function () { @@ -54,11 +62,6 @@ describe('history local storage', function () { expect(historyItems.length).toBe(2); expect(historyItems[1].timeRan).toBeDefined(); expect(historyItems[1].status).toBe('success'); - - expect(mockSetItem).toHaveBeenCalledWith( - 'QUERY_HISTORY_ITEM_KEY', - JSON.stringify(historyItems) - ); }); it('should allow maximum x queries ', function () { diff --git a/src/platform/packages/private/kbn-esql-editor/src/history_local_storage.ts b/src/platform/packages/private/kbn-esql-editor/src/history_local_storage.ts index dea49dad80761..0b739c6d4057b 100644 --- a/src/platform/packages/private/kbn-esql-editor/src/history_local_storage.ts +++ b/src/platform/packages/private/kbn-esql-editor/src/history_local_storage.ts @@ -66,10 +66,15 @@ export const addQueriesToCache = ( // the cachedQueries Map might not contain all // the localStorage queries const queries = getHistoryItems('desc'); - queries.forEach((queryItem) => { - const trimmedQueryString = getTrimmedQuery(queryItem.queryString); - cachedQueries.set(trimmedQueryString, queryItem); - }); + if (queries.length > 0) { + queries.forEach((queryItem) => { + const trimmedQueryString = getTrimmedQuery(queryItem.queryString); + cachedQueries.set(trimmedQueryString, queryItem); + }); + } else { + // if there are no queries in the localStorage, clear the cache + cachedQueries.clear(); + } const trimmedQueryString = getTrimmedQuery(item.queryString); if (item.queryString) { @@ -87,11 +92,12 @@ export const addQueriesToCache = ( // queries to store in the localstorage allQueries = sortedByDate.slice(0, maxQueriesAllowed); + + // clear and reset the queries in the cache + cachedQueries.clear(); allQueries.forEach((queryItem) => { cachedQueries.set(queryItem.queryString, queryItem); }); } - // clear and reset the queries in the cache - cachedQueries.clear(); localStorage.setItem(QUERY_HISTORY_ITEM_KEY, JSON.stringify(allQueries)); };