explorePane.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { of } from 'rxjs';
  2. import { serializeStateToUrlParam } from '@grafana/data';
  3. import { setDataSourceSrv } from '@grafana/runtime';
  4. import { ExploreId, StoreState, ThunkDispatch } from 'app/types';
  5. import { configureStore } from '../../../store/configureStore';
  6. import { refreshExplore } from './explorePane';
  7. import { createDefaultInitialState } from './helpers';
  8. jest.mock('../../dashboard/services/TimeSrv', () => ({
  9. getTimeSrv: jest.fn().mockReturnValue({
  10. init: jest.fn(),
  11. timeRange: jest.fn().mockReturnValue({}),
  12. }),
  13. }));
  14. const { testRange, defaultInitialState } = createDefaultInitialState();
  15. jest.mock('@grafana/runtime', () => ({
  16. ...(jest.requireActual('@grafana/runtime') as unknown as object),
  17. getTemplateSrv: () => ({
  18. updateTimeRange: jest.fn(),
  19. }),
  20. }));
  21. function setupStore(state?: any) {
  22. return configureStore({
  23. ...defaultInitialState,
  24. explore: {
  25. [ExploreId.left]: {
  26. ...defaultInitialState.explore[ExploreId.left],
  27. ...(state || {}),
  28. },
  29. },
  30. } as any);
  31. }
  32. function setup(state?: any) {
  33. const datasources: Record<string, any> = {
  34. newDs: {
  35. testDatasource: jest.fn(),
  36. init: jest.fn(),
  37. query: jest.fn(),
  38. name: 'newDs',
  39. meta: { id: 'newDs' },
  40. getRef: () => ({ uid: 'newDs' }),
  41. },
  42. someDs: {
  43. testDatasource: jest.fn(),
  44. init: jest.fn(),
  45. query: jest.fn(),
  46. name: 'someDs',
  47. meta: { id: 'someDs' },
  48. getRef: () => ({ uid: 'someDs' }),
  49. },
  50. };
  51. setDataSourceSrv({
  52. getList() {
  53. return Object.values(datasources).map((d) => ({ name: d.name }));
  54. },
  55. getInstanceSettings(name: string) {
  56. return { name, getRef: () => ({ uid: name }) };
  57. },
  58. get(name?: string) {
  59. return Promise.resolve(
  60. name
  61. ? datasources[name]
  62. : {
  63. testDatasource: jest.fn(),
  64. init: jest.fn(),
  65. name: 'default',
  66. }
  67. );
  68. },
  69. } as any);
  70. const { dispatch, getState }: { dispatch: ThunkDispatch; getState: () => StoreState } = setupStore({
  71. datasourceInstance: datasources.someDs,
  72. ...(state || {}),
  73. });
  74. return {
  75. dispatch,
  76. getState,
  77. datasources,
  78. };
  79. }
  80. describe('refreshExplore', () => {
  81. it('should change data source when datasource in url changes', async () => {
  82. const { dispatch, getState } = setup();
  83. await dispatch(
  84. refreshExplore(ExploreId.left, serializeStateToUrlParam({ datasource: 'newDs', queries: [], range: testRange }))
  85. );
  86. expect(getState().explore[ExploreId.left].datasourceInstance?.name).toBe('newDs');
  87. });
  88. it('should change and run new queries from the URL', async () => {
  89. const { dispatch, getState, datasources } = setup();
  90. datasources.someDs.query.mockReturnValueOnce(of({}));
  91. await dispatch(
  92. refreshExplore(
  93. ExploreId.left,
  94. serializeStateToUrlParam({ datasource: 'someDs', queries: [{ expr: 'count()', refId: 'A' }], range: testRange })
  95. )
  96. );
  97. // same
  98. const state = getState().explore[ExploreId.left];
  99. expect(state.datasourceInstance?.name).toBe('someDs');
  100. expect(state.queries.length).toBe(1);
  101. expect(state.queries).toMatchObject([{ expr: 'count()' }]);
  102. expect(datasources.someDs.query).toHaveBeenCalledTimes(1);
  103. });
  104. it('should not do anything if pane is not initialized', async () => {
  105. const { dispatch, getState } = setup({
  106. initialized: false,
  107. });
  108. const state = getState();
  109. await dispatch(
  110. refreshExplore(
  111. ExploreId.left,
  112. serializeStateToUrlParam({ datasource: 'newDs', queries: [{ expr: 'count()', refId: 'A' }], range: testRange })
  113. )
  114. );
  115. expect(state).toEqual(getState());
  116. });
  117. });