import { createMockGraphStore , MockedGraphEnvironment } from './mocks' ; import { loadSavedWorkspace , loadingSaga , saveWorkspace , savingSaga , LoadSavedWorkspacePayload , } from './persistence' ; import { UrlTemplate , AdvancedSettings , WorkspaceField , GraphWorkspaceSavedObject } from '../types' ; import { IndexpatternDatasource , datasourceSelector } from './datasource' ; import { fieldsSelector } from './fields' ; import { metaDataSelector , updateMetaData } from './meta_data' ; import { templatesSelector } from './url_templates' ; import { migrateLegacyIndexPatternRef , appStateToSavedWorkspace } from '../services/persistence' ; import { settingsSelector } from './advanced_settings' ; import { openSaveModal } from '../services/save_modal' ; const waitForPromise = ( ) => new Promise ( ( r ) => setTimeout ( r ) ) ; jest . mock ( '@elastic/eui' , ( ) => { const original = jest . requireActual ( '@elastic/eui' ) ; return { ... original , htmlIdGenerator : ( fn ) => { let counter = 0 ; return ( ) => counter ++ ; } , } ; } ) ; jest . mock ( '../services/persistence' , ( ) => ( { lookupIndexPatternId : jest . fn ( ( ) => ( { id : '123' , attributes : { title : 'test-pattern' } } ) ) , migrateLegacyIndexPatternRef : jest . fn ( ( ) => ( { success : true } ) ) , savedWorkspaceToAppState : jest . fn ( ( ) => ( { urlTemplates : [ { description : 'template' , url : 'http://example.org/q={{gquery}}' , } , ] as UrlTemplate [ ] , advancedSettings : { minDocCount : 12 } as AdvancedSettings , allFields : [ { name : 'testfield' , } , ] as WorkspaceField [ ] , } ) ) , appStateToSavedWorkspace : jest . fn ( ) , } ) ) ; jest . mock ( '../services/save_modal' , ( ) => ( { openSaveModal : jest . fn ( ) , } ) ) ; jest . mock ( '../helpers/saved_workspace_utils' , ( ) => ( { saveSavedWorkspace : jest . fn ( ) . mockResolvedValueOnce ( '123' ) , } ) ) ; describe ( 'persistence sagas' , ( ) => { let env ; describe ( 'loading saga' , ( ) => { beforeEach ( ( ) => { env = createMockGraphStore ( { sagas : [ loadingSaga ] } ) ; } ) ; it ( 'should deserialize saved object and populate state' , async ( ) => { env . store . dispatch ( loadSavedWorkspace ( { savedWorkspace : { title : 'my workspace' } , } as LoadSavedWorkspacePayload ) ) ; await waitForPromise ( ) ; const resultingState = env . store . getState ( ) ; expect ( settingsSelector ( resultingState ) . minDocCount ) . toEqual ( 12 ) ; expect ( ( datasourceSelector ( resultingState ) . current as IndexpatternDatasource ) . title ) . toEqual ( 'test-pattern' ) ; expect ( fieldsSelector ( resultingState ) [ 0 ] . name ) . toEqual ( 'testfield' ) ; expect ( metaDataSelector ( resultingState ) . title ) . toEqual ( 'my workspace' ) ; expect ( templatesSelector ( resultingState ) [ 0 ] . url ) . toEqual ( 'http://example.org/q={{gquery}}' ) ; } ) ; it ( 'should warn with a toast and abort if index pattern is not found' , async ( ) => { ( migrateLegacyIndexPatternRef as jest . Mock ) . mockReturnValueOnce ( { success : false } ) ; env . store . dispatch ( loadSavedWorkspace ( { savedWorkspace : { } } as LoadSavedWorkspacePayload ) ) ; await waitForPromise ( ) ; expect ( env . mockedDeps . notifications . toasts . addDanger ) . toHaveBeenCalled ( ) ; const resultingState = env . store . getState ( ) ; expect ( datasourceSelector ( resultingState ) . current . type ) . toEqual ( 'none' ) ; } ) ; } ) ; describe ( 'saving saga' , ( ) => { beforeEach ( ( ) => { env = createMockGraphStore ( { sagas : [ savingSaga ] , initialStateOverwrites : { datasource : { current : { type : 'indexpattern' , id : '78698689' , title : 'test-pattern' , } , loading : false , } , } , mockedDepsOverwrites : { savePolicy : 'configAndDataWithConsent' , } , } ) ; } ) ; it ( 'should serialize saved object and save after confirmation' , async ( ) => { env . store . dispatch ( saveWorkspace ( { id : '123' } as GraphWorkspaceSavedObject ) ) ; ( openSaveModal as jest . Mock ) . mock . calls [ 0 ] [ 0 ] . saveWorkspace ( { } , true ) ; expect ( appStateToSavedWorkspace ) . toHaveBeenCalled ( ) ; await waitForPromise ( ) ; expect ( env . mockedDeps . notifications . toasts . addSuccess ) . toHaveBeenCalled ( ) ; expect ( metaDataSelector ( env . store . getState ( ) ) . savedObjectId ) . toEqual ( '123' ) ; expect ( env . mockedDeps . changeUrl ) . toHaveBeenCalledWith ( expect . stringContaining ( '123' ) ) ; } ) ; it ( 'should not save data if user does not give consent in the modal' , async ( ) => { env . store . dispatch ( saveWorkspace ( { } as GraphWorkspaceSavedObject ) ) ; ( openSaveModal as jest . Mock ) . mock . calls [ 0 ] [ 0 ] . saveWorkspace ( { } , false ) ; expect ( appStateToSavedWorkspace ) . toHaveBeenCalledWith ( expect . anything ( ) , expect . anything ( ) , false ) ; } ) ; it ( 'should not change url if it was just updating existing workspace' , async ( ) => { env . store . dispatch ( updateMetaData ( { savedObjectId : '123' } ) ) ; env . store . dispatch ( saveWorkspace ( { } as GraphWorkspaceSavedObject ) ) ; await waitForPromise ( ) ; expect ( env . mockedDeps . changeUrl ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; } ) ;