Explore: rich history: add more tests (#40769)

* explore: rich history: add unit test

* explore: rich-history: added info comment

* improved comment

* better test names
This commit is contained in:
Gábor Farkas
2021-10-25 15:23:47 +02:00
committed by GitHub
parent cadfdab155
commit cc271b0a42
2 changed files with 53 additions and 22 deletions

View File

@@ -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]);
});
});
});
});

View File

@@ -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] };
}