@ -9,9 +9,11 @@ import {
deleteQueryInRichHistory ,
filterAndSortQueries ,
SortOrder ,
MAX_HISTORY_ITEMS ,
} from './richHistory' ;
import store from 'app/core/store' ;
import { dateTime , DataQuery } from '@grafana/data' ;
import { RichHistoryQuery } from '../../types' ;
const mock : any = {
storedHistory : [
@ -41,12 +43,21 @@ const mock: any = {
const key = 'grafana.explore.richHistory' ;
describe ( 'richHistory' , ( ) = > {
beforeEach ( ( ) = > {
jest . useFakeTimers ( 'modern' ) ;
jest . setSystemTime ( new Date ( 1970 , 0 , 1 ) ) ;
} ) ;
afterEach ( ( ) = > {
jest . useRealTimers ( ) ;
} ) ;
describe ( 'addToRichHistory' , ( ) = > {
beforeEach ( ( ) = > {
deleteAllFromRichHistory ( ) ;
expect ( store . exists ( key ) ) . toBeFalsy ( ) ;
} ) ;
const expectedResult = [
{
comment : mock.testComment ,
@ -62,14 +73,16 @@ describe('addToRichHistory', () => {
it ( 'should append query to query history' , ( ) = > {
Date . now = jest . fn ( ( ) = > 2 ) ;
const newHistory = addToRichHistory (
const { richHistory : newHistory } = addToRichHistory (
mock . storedHistory ,
mock . testDatasourceId ,
mock . testDatasourceName ,
mock . testQueries ,
mock . testStarred ,
mock . testComment ,
mock . testSessionName
mock . testSessionName ,
true ,
true
) ;
expect ( newHistory ) . toEqual ( expectedResult ) ;
} ) ;
@ -84,7 +97,9 @@ describe('addToRichHistory', () => {
mock . testQueries ,
mock . testStarred ,
mock . testComment ,
mock . testSessionName
mock . testSessionName ,
true ,
true
) ;
expect ( store . exists ( key ) ) . toBeTruthy ( ) ;
expect ( store . getObject ( key ) ) . toMatchObject ( expectedResult ) ;
@ -92,14 +107,16 @@ describe('addToRichHistory', () => {
it ( 'should not append duplicated query to query history' , ( ) = > {
Date . now = jest . fn ( ( ) = > 2 ) ;
const newHistory = addToRichHistory (
const { richHistory : newHistory } = addToRichHistory (
mock . storedHistory ,
mock . storedHistory [ 0 ] . datasourceId ,
mock . storedHistory [ 0 ] . datasourceName ,
[ { expr : 'query1' , maxLines : null , refId : 'A' } as DataQuery , { expr : 'query2' , refId : 'B' } as DataQuery ] ,
mock . testStarred ,
mock . testComment ,
mock . testSessionName
mock . testSessionName ,
true ,
true
) ;
expect ( newHistory ) . toEqual ( [ mock . storedHistory [ 0 ] ] ) ;
} ) ;
@ -113,10 +130,52 @@ describe('addToRichHistory', () => {
[ { expr : 'query1' , maxLines : null , refId : 'A' } as DataQuery , { expr : 'query2' , refId : 'B' } as DataQuery ] ,
mock . testStarred ,
mock . testComment ,
mock . testSessionName
mock . testSessionName ,
true ,
true
) ;
expect ( store . exists ( key ) ) . toBeFalsy ( ) ;
} ) ;
it ( 'should not save more than MAX_HISTORY_ITEMS' , ( ) = > {
Date . now = jest . fn ( ( ) = > 2 ) ;
const extraItems = 100 ;
// the history has more than MAX
let history = [ ] ;
// history = [ { starred: true, comment: "0" }, { starred: false, comment: "1" }, ... ]
for ( let i = 0 ; i < MAX_HISTORY_ITEMS + extraItems ; i ++ ) {
history . push ( {
starred : i % 2 === 0 ,
comment : i.toString ( ) ,
queries : [ ] ,
ts : new Date ( 2019 , 11 , 31 ) . getTime ( ) ,
} ) ;
}
const starredItemsInHistory = ( MAX_HISTORY_ITEMS + extraItems ) / 2 ;
const notStarredItemsInHistory = ( MAX_HISTORY_ITEMS + extraItems ) / 2 ;
expect ( history . filter ( ( h ) = > h . starred ) ) . toHaveLength ( starredItemsInHistory ) ;
expect ( history . filter ( ( h ) = > ! h . starred ) ) . toHaveLength ( notStarredItemsInHistory ) ;
const { richHistory : newHistory } = addToRichHistory (
( history as any ) as RichHistoryQuery [ ] ,
mock . storedHistory [ 0 ] . datasourceId ,
mock . storedHistory [ 0 ] . datasourceName ,
[ { expr : 'query1' , maxLines : null , refId : 'A' } as DataQuery , { expr : 'query2' , refId : 'B' } as DataQuery ] ,
true ,
mock . testComment ,
mock . testSessionName ,
true ,
true
) ;
// one not starred replaced with a newly added starred item
const removedNotStarredItems = extraItems + 1 ; // + 1 to make space for the new item
expect ( newHistory . filter ( ( h ) = > h . starred ) ) . toHaveLength ( starredItemsInHistory + 1 ) ; // starred item added
expect ( newHistory . filter ( ( h ) = > ! h . starred ) ) . toHaveLength ( starredItemsInHistory - removedNotStarredItems ) ;
} ) ;
} ) ;
describe ( 'updateStarredInRichHistory' , ( ) = > {
@ -215,3 +274,4 @@ describe('createQueryHeading', () => {
expect ( heading ) . toEqual ( mock . storedHistory [ 0 ] . datasourceName ) ;
} ) ;
} ) ;
} ) ;