migrateVariablesDatasourceNameToRef.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { DataSourceRef } from '@grafana/data/src';
  2. import { adHocBuilder, queryBuilder } from '../shared/testing/builders';
  3. import { toVariablePayload } from '../utils';
  4. import { migrateVariablesDatasourceNameToRef } from './actions';
  5. import { getPreloadedState } from './helpers';
  6. import { toKeyedAction } from './keyedVariablesReducer';
  7. import { changeVariableProp } from './sharedReducer';
  8. function getTestContext(ds: DataSourceRef, dsInstance?: { uid: string; type: string }) {
  9. jest.clearAllMocks();
  10. const key = 'key';
  11. const query = queryBuilder().withId('query').withRootStateKey(key).withName('query').withDatasource(ds).build();
  12. const adhoc = adHocBuilder().withId('adhoc').withRootStateKey(key).withName('adhoc').withDatasource(ds).build();
  13. const templatingState = { variables: { query, adhoc } };
  14. const state = getPreloadedState(key, templatingState);
  15. const dispatch = jest.fn();
  16. const getState = jest.fn().mockReturnValue(state);
  17. const getInstanceSettingsMock = jest.fn().mockReturnValue(dsInstance);
  18. const getDatasourceSrvFunc = jest.fn().mockReturnValue({
  19. get: jest.fn().mockResolvedValue({}),
  20. getList: jest.fn().mockReturnValue([]),
  21. getInstanceSettings: getInstanceSettingsMock,
  22. });
  23. return { key, query, adhoc, dispatch, getState, getDatasourceSrvFunc };
  24. }
  25. describe('migrateVariablesDatasourceNameToRef', () => {
  26. describe('when called and variables have legacy data source props', () => {
  27. describe('and data source exists', () => {
  28. it('then correct actions are dispatched', async () => {
  29. const legacyDs = '${ds}' as unknown as DataSourceRef;
  30. const { query, adhoc, dispatch, getState, getDatasourceSrvFunc, key } = getTestContext(legacyDs, {
  31. uid: 'a random uid',
  32. type: 'prometheus',
  33. });
  34. migrateVariablesDatasourceNameToRef(key, getDatasourceSrvFunc)(dispatch, getState, undefined);
  35. expect(dispatch).toHaveBeenCalledTimes(2);
  36. expect(dispatch.mock.calls[0][0]).toEqual(
  37. toKeyedAction(
  38. key,
  39. changeVariableProp(
  40. toVariablePayload(query, {
  41. propName: 'datasource',
  42. propValue: { uid: 'a random uid', type: 'prometheus' },
  43. })
  44. )
  45. )
  46. );
  47. expect(dispatch.mock.calls[1][0]).toEqual(
  48. toKeyedAction(
  49. key,
  50. changeVariableProp(
  51. toVariablePayload(adhoc, {
  52. propName: 'datasource',
  53. propValue: { uid: 'a random uid', type: 'prometheus' },
  54. })
  55. )
  56. )
  57. );
  58. });
  59. });
  60. describe('and data source does not exist', () => {
  61. it('then correct actions are dispatched', async () => {
  62. const legacyDs = '${ds}' as unknown as DataSourceRef;
  63. const { query, adhoc, dispatch, getState, getDatasourceSrvFunc, key } = getTestContext(legacyDs, undefined);
  64. migrateVariablesDatasourceNameToRef(key, getDatasourceSrvFunc)(dispatch, getState, undefined);
  65. expect(dispatch).toHaveBeenCalledTimes(2);
  66. expect(dispatch.mock.calls[0][0]).toEqual(
  67. toKeyedAction(
  68. key,
  69. changeVariableProp(toVariablePayload(query, { propName: 'datasource', propValue: { uid: '${ds}' } }))
  70. )
  71. );
  72. expect(dispatch.mock.calls[1][0]).toEqual(
  73. toKeyedAction(
  74. key,
  75. changeVariableProp(toVariablePayload(adhoc, { propName: 'datasource', propValue: { uid: '${ds}' } }))
  76. )
  77. );
  78. });
  79. });
  80. });
  81. describe('when called and variables have dataSourceRef', () => {
  82. it('then no actions are dispatched', async () => {
  83. const legacyDs = { uid: '${ds}', type: 'prometheus' };
  84. const { dispatch, getState, getDatasourceSrvFunc, key } = getTestContext(legacyDs, undefined);
  85. migrateVariablesDatasourceNameToRef(key, getDatasourceSrvFunc)(dispatch, getState, undefined);
  86. expect(dispatch).toHaveBeenCalledTimes(0);
  87. });
  88. });
  89. });