From cc271b0a422f9b813544472156c0eb569687f703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Farkas?= Date: Mon, 25 Oct 2021 15:23:47 +0200 Subject: [PATCH] Explore: rich history: add more tests (#40769) * explore: rich history: add unit test * explore: rich-history: added info comment * improved comment * better test names --- public/app/core/utils/richHistory.test.ts | 72 ++++++++++++++++------- public/app/core/utils/richHistory.ts | 3 + 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/public/app/core/utils/richHistory.test.ts b/public/app/core/utils/richHistory.test.ts index 08be5c3ec1b..c1267e526ec 100644 --- a/public/app/core/utils/richHistory.test.ts +++ b/public/app/core/utils/richHistory.test.ts @@ -281,29 +281,57 @@ describe('richHistory', () => { deleteAllFromRichHistory(); expect(store.exists(key)).toBeFalsy(); }); - it('should load from localStorage data in old format', () => { - const oldHistoryItem = { ...mock.storedHistory[0], queries: ['test query 1', 'test query 2', 'test query 3'] }; - store.setObject(key, [oldHistoryItem]); - const expectedHistoryItem = { - ...mock.storedHistory[0], - queries: [ - { - expr: 'test query 1', - refId: 'A', - }, - { - expr: 'test query 2', - refId: 'B', - }, - { - expr: 'test query 3', - refId: 'C', - }, - ], - }; + describe('should load from localStorage data in old formats', () => { + it('should load when queries are strings', () => { + const oldHistoryItem = { ...mock.storedHistory[0], queries: ['test query 1', 'test query 2', 'test query 3'] }; + store.setObject(key, [oldHistoryItem]); + const expectedHistoryItem = { + ...mock.storedHistory[0], + queries: [ + { + expr: 'test query 1', + refId: 'A', + }, + { + expr: 'test query 2', + refId: 'B', + }, + { + expr: 'test query 3', + refId: 'C', + }, + ], + }; - const result = getRichHistory(); - expect(result).toStrictEqual([expectedHistoryItem]); + const result = getRichHistory(); + expect(result).toStrictEqual([expectedHistoryItem]); + }); + + it('should load when queries are json-encoded strings', () => { + const oldHistoryItem = { + ...mock.storedHistory[0], + queries: ['{"refId":"A","key":"key1","metrics":[]}', '{"refId":"B","key":"key2","metrics":[]}'], + }; + store.setObject(key, [oldHistoryItem]); + const expectedHistoryItem = { + ...mock.storedHistory[0], + queries: [ + { + refId: 'A', + key: 'key1', + metrics: [], + }, + { + refId: 'B', + key: 'key2', + metrics: [], + }, + ], + }; + + const result = getRichHistory(); + expect(result).toStrictEqual([expectedHistoryItem]); + }); }); }); }); diff --git a/public/app/core/utils/richHistory.ts b/public/app/core/utils/richHistory.ts index b0d995f94bb..ff7b032d25a 100644 --- a/public/app/core/utils/richHistory.ts +++ b/public/app/core/utils/richHistory.ts @@ -406,10 +406,13 @@ function migrateRichHistory(richHistory: RichHistoryQuery[]) { function createDataQuery(query: RichHistoryQuery, individualQuery: DataQuery | string, index: number) { const letters = 'ABCDEFGHIJKLMNOPQRSTUVXYZ'; if (typeof individualQuery === 'object') { + // the current format return individualQuery; } else if (isParsable(individualQuery)) { + // ElasticSearch (maybe other datasoures too) before grafana7 return JSON.parse(individualQuery); } + // prometehus (maybe other datasources too) before grafana7 return { expr: individualQuery, refId: letters[index] }; }