datasource.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { DataQuery, DataSourceApi } from '@grafana/data';
  2. import { ExploreId, ExploreItemState } from 'app/types';
  3. import { updateDatasourceInstanceAction, datasourceReducer } from './datasource';
  4. import { createEmptyQueryResponse } from './utils';
  5. describe('Datasource reducer', () => {
  6. it('should handle set updateDatasourceInstanceAction correctly', () => {
  7. const StartPage = {};
  8. const datasourceInstance = {
  9. meta: {
  10. metrics: true,
  11. logs: true,
  12. },
  13. components: {
  14. QueryEditorHelp: StartPage,
  15. },
  16. } as DataSourceApi;
  17. const queries: DataQuery[] = [];
  18. const queryKeys: string[] = [];
  19. const initialState: ExploreItemState = {
  20. datasourceInstance: null,
  21. queries,
  22. queryKeys,
  23. } as unknown as ExploreItemState;
  24. const result = datasourceReducer(
  25. initialState,
  26. updateDatasourceInstanceAction({ exploreId: ExploreId.left, datasourceInstance, history: [] })
  27. );
  28. const expectedState: Partial<ExploreItemState> = {
  29. datasourceInstance,
  30. queries,
  31. queryKeys,
  32. graphResult: null,
  33. logsResult: null,
  34. tableResult: null,
  35. loading: false,
  36. queryResponse: {
  37. // When creating an empty query response we also create a timeRange object with the current time.
  38. // Copying the range from the reducer here prevents intermittent failures when creating them at different times.
  39. ...createEmptyQueryResponse(),
  40. timeRange: result.queryResponse.timeRange,
  41. },
  42. };
  43. expect(result).toMatchObject(expectedState);
  44. });
  45. });