mirror of https://github.com/grafana/grafana
Refactor/Explore: Inline datasource actions into initialisation (#28953)
* Inline datasource actions into initialisation * Fix datasource change * Fix rich history * Fix test * Move rich history init to Wrapper * Remove console log * TS fixes Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>pull/28935/head^2
parent
2c898ab1bc
commit
66cdc18705
@ -1,115 +1,43 @@ |
||||
import { |
||||
loadDatasource, |
||||
loadDatasourcePendingAction, |
||||
loadDatasourceReadyAction, |
||||
updateDatasourceInstanceAction, |
||||
datasourceReducer, |
||||
} from './datasource'; |
||||
import { updateDatasourceInstanceAction, datasourceReducer } from './datasource'; |
||||
import { ExploreId, ExploreItemState } from 'app/types'; |
||||
import { thunkTester } from 'test/core/thunk/thunkTester'; |
||||
import { DataQuery, DataSourceApi } from '@grafana/data'; |
||||
import { createEmptyQueryResponse } from './utils'; |
||||
import { reducerTester } from '../../../../test/core/redux/reducerTester'; |
||||
|
||||
describe('loading datasource', () => { |
||||
describe('when loadDatasource thunk is dispatched', () => { |
||||
describe('and all goes fine', () => { |
||||
it('then it should dispatch correct actions', async () => { |
||||
const exploreId = ExploreId.left; |
||||
const name = 'some-datasource'; |
||||
const initialState = { explore: { [exploreId]: { requestedDatasourceName: name } } }; |
||||
const mockDatasourceInstance = { |
||||
testDatasource: () => { |
||||
return Promise.resolve({ status: 'success' }); |
||||
}, |
||||
name, |
||||
init: jest.fn(), |
||||
meta: { id: 'some id' }, |
||||
}; |
||||
|
||||
const dispatchedActions = await thunkTester(initialState) |
||||
.givenThunk(loadDatasource) |
||||
.whenThunkIsDispatched(exploreId, mockDatasourceInstance); |
||||
|
||||
expect(dispatchedActions).toEqual([ |
||||
loadDatasourcePendingAction({ |
||||
exploreId, |
||||
requestedDatasourceName: mockDatasourceInstance.name, |
||||
}), |
||||
loadDatasourceReadyAction({ exploreId, history: [] }), |
||||
]); |
||||
}); |
||||
}); |
||||
|
||||
describe('and user changes datasource during load', () => { |
||||
it('then it should dispatch correct actions', async () => { |
||||
const exploreId = ExploreId.left; |
||||
const name = 'some-datasource'; |
||||
const initialState = { explore: { [exploreId]: { requestedDatasourceName: 'some-other-datasource' } } }; |
||||
const mockDatasourceInstance = { |
||||
testDatasource: () => { |
||||
return Promise.resolve({ status: 'success' }); |
||||
}, |
||||
name, |
||||
init: jest.fn(), |
||||
meta: { id: 'some id' }, |
||||
}; |
||||
|
||||
const dispatchedActions = await thunkTester(initialState) |
||||
.givenThunk(loadDatasource) |
||||
.whenThunkIsDispatched(exploreId, mockDatasourceInstance); |
||||
|
||||
expect(dispatchedActions).toEqual([ |
||||
loadDatasourcePendingAction({ |
||||
exploreId, |
||||
requestedDatasourceName: mockDatasourceInstance.name, |
||||
}), |
||||
]); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('Explore item reducer', () => { |
||||
describe('changing datasource', () => { |
||||
describe('when updateDatasourceInstanceAction is dispatched', () => { |
||||
describe('and datasourceInstance supports graph, logs, table and has a startpage', () => { |
||||
it('then it should set correct state', () => { |
||||
const StartPage = {}; |
||||
const datasourceInstance = { |
||||
meta: { |
||||
metrics: true, |
||||
logs: true, |
||||
}, |
||||
components: { |
||||
ExploreStartPage: StartPage, |
||||
}, |
||||
} as DataSourceApi; |
||||
const queries: DataQuery[] = []; |
||||
const queryKeys: string[] = []; |
||||
const initialState: ExploreItemState = ({ |
||||
datasourceInstance: null, |
||||
queries, |
||||
queryKeys, |
||||
} as unknown) as ExploreItemState; |
||||
const expectedState: any = { |
||||
datasourceInstance, |
||||
queries, |
||||
queryKeys, |
||||
graphResult: null, |
||||
logsResult: null, |
||||
tableResult: null, |
||||
latency: 0, |
||||
loading: false, |
||||
queryResponse: createEmptyQueryResponse(), |
||||
}; |
||||
|
||||
reducerTester<ExploreItemState>() |
||||
.givenReducer(datasourceReducer, initialState) |
||||
.whenActionIsDispatched(updateDatasourceInstanceAction({ exploreId: ExploreId.left, datasourceInstance })) |
||||
.thenStateShouldEqual(expectedState); |
||||
}); |
||||
}); |
||||
}); |
||||
describe('Datasource reducer', () => { |
||||
it('should handle set updateDatasourceInstanceAction correctly', () => { |
||||
const StartPage = {}; |
||||
const datasourceInstance = { |
||||
meta: { |
||||
metrics: true, |
||||
logs: true, |
||||
}, |
||||
components: { |
||||
ExploreStartPage: StartPage, |
||||
}, |
||||
} as DataSourceApi; |
||||
const queries: DataQuery[] = []; |
||||
const queryKeys: string[] = []; |
||||
const initialState: ExploreItemState = ({ |
||||
datasourceInstance: null, |
||||
queries, |
||||
queryKeys, |
||||
} as unknown) as ExploreItemState; |
||||
const expectedState: any = { |
||||
datasourceInstance, |
||||
queries, |
||||
queryKeys, |
||||
graphResult: null, |
||||
logsResult: null, |
||||
tableResult: null, |
||||
latency: 0, |
||||
loading: false, |
||||
queryResponse: createEmptyQueryResponse(), |
||||
}; |
||||
|
||||
const result = datasourceReducer( |
||||
initialState, |
||||
updateDatasourceInstanceAction({ exploreId: ExploreId.left, datasourceInstance, history: [] }) |
||||
); |
||||
expect(result).toMatchObject(expectedState); |
||||
}); |
||||
}); |
||||
|
Loading…
Reference in new issue