upgradeLegacyQueries.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { VariableSupportType } from '@grafana/data';
  2. import { thunkTester } from '../../../../test/core/thunk/thunkTester';
  3. import { customBuilder, queryBuilder } from '../shared/testing/builders';
  4. import { TransactionStatus, VariableModel } from '../types';
  5. import { toKeyedVariableIdentifier } from '../utils';
  6. import { upgradeLegacyQueries } from './actions';
  7. import { getPreloadedState } from './helpers';
  8. import { toKeyedAction } from './keyedVariablesReducer';
  9. import { changeVariableProp } from './sharedReducer';
  10. interface Args {
  11. query?: any;
  12. variable?: VariableModel;
  13. datasource?: any;
  14. transactionStatus?: TransactionStatus;
  15. }
  16. function getTestContext({
  17. query = '',
  18. variable,
  19. datasource,
  20. transactionStatus = TransactionStatus.Fetching,
  21. }: Args = {}) {
  22. const key = 'key';
  23. variable =
  24. variable ??
  25. queryBuilder()
  26. .withId('query')
  27. .withRootStateKey(key)
  28. .withName('query')
  29. .withQuery(query)
  30. .withDatasource({ uid: 'test-data', type: 'test-data' })
  31. .build();
  32. const templatingState = {
  33. transaction: { status: transactionStatus, uid: key, isDirty: false },
  34. variables: {
  35. [variable.id]: variable,
  36. },
  37. };
  38. const state = getPreloadedState(key, templatingState);
  39. datasource = datasource ?? {
  40. name: 'TestData',
  41. metricFindQuery: () => undefined,
  42. variables: { getType: () => VariableSupportType.Standard, toDataQuery: () => undefined },
  43. };
  44. const get = jest.fn().mockResolvedValue(datasource);
  45. const getDatasourceSrv = jest.fn().mockReturnValue({ get });
  46. const identifier = toKeyedVariableIdentifier(variable);
  47. return { key, state, get, getDatasourceSrv, identifier };
  48. }
  49. describe('upgradeLegacyQueries', () => {
  50. describe('when called with a query variable for a standard variable supported data source that has not been upgraded', () => {
  51. it('then it should dispatch changeVariableProp', async () => {
  52. const { key, state, identifier, get, getDatasourceSrv } = getTestContext({ query: '*' });
  53. const dispatchedActions = await thunkTester(state)
  54. .givenThunk(upgradeLegacyQueries)
  55. .whenThunkIsDispatched(identifier, getDatasourceSrv);
  56. expect(dispatchedActions).toEqual([
  57. toKeyedAction(
  58. key,
  59. changeVariableProp({
  60. type: 'query',
  61. id: 'query',
  62. data: {
  63. propName: 'query',
  64. propValue: {
  65. refId: 'TestData-query-Variable-Query',
  66. query: '*',
  67. },
  68. },
  69. })
  70. ),
  71. ]);
  72. expect(get).toHaveBeenCalledTimes(1);
  73. expect(get).toHaveBeenCalledWith({ uid: 'test-data', type: 'test-data' });
  74. });
  75. describe('but there is no ongoing transaction', () => {
  76. it('then it should not dispatch changeVariableProp', async () => {
  77. const { state, identifier, get, getDatasourceSrv } = getTestContext({
  78. query: '*',
  79. transactionStatus: TransactionStatus.NotStarted,
  80. });
  81. const dispatchedActions = await thunkTester(state)
  82. .givenThunk(upgradeLegacyQueries)
  83. .whenThunkIsDispatched(identifier, getDatasourceSrv);
  84. expect(dispatchedActions).toEqual([]);
  85. expect(get).toHaveBeenCalledTimes(0);
  86. });
  87. });
  88. });
  89. describe('when called with a query variable for a standard variable supported data source that has been upgraded', () => {
  90. it('then it should not dispatch any actions', async () => {
  91. const { state, identifier, get, getDatasourceSrv } = getTestContext({ query: { refId: 'A' } });
  92. const dispatchedActions = await thunkTester(state)
  93. .givenThunk(upgradeLegacyQueries)
  94. .whenThunkIsDispatched(identifier, getDatasourceSrv);
  95. expect(dispatchedActions).toEqual([]);
  96. expect(get).toHaveBeenCalledTimes(1);
  97. expect(get).toHaveBeenCalledWith({ uid: 'test-data', type: 'test-data' });
  98. });
  99. });
  100. describe('when called with a query variable for a legacy variable supported data source', () => {
  101. it('then it should not dispatch any actions', async () => {
  102. const datasource = {
  103. name: 'TestData',
  104. metricFindQuery: () => undefined,
  105. };
  106. const { state, identifier, get, getDatasourceSrv } = getTestContext({ datasource });
  107. const dispatchedActions = await thunkTester(state)
  108. .givenThunk(upgradeLegacyQueries)
  109. .whenThunkIsDispatched(identifier, getDatasourceSrv);
  110. expect(dispatchedActions).toEqual([]);
  111. expect(get).toHaveBeenCalledTimes(1);
  112. expect(get).toHaveBeenCalledWith({ uid: 'test-data', type: 'test-data' });
  113. });
  114. });
  115. describe('when called with a query variable for a custom variable supported data source', () => {
  116. it('then it should not dispatch any actions', async () => {
  117. const datasource = {
  118. name: 'TestData',
  119. metricFindQuery: () => undefined,
  120. variables: { getType: () => VariableSupportType.Custom, query: () => undefined, editor: {} },
  121. };
  122. const { state, identifier, get, getDatasourceSrv } = getTestContext({ datasource });
  123. const dispatchedActions = await thunkTester(state)
  124. .givenThunk(upgradeLegacyQueries)
  125. .whenThunkIsDispatched(identifier, getDatasourceSrv);
  126. expect(dispatchedActions).toEqual([]);
  127. expect(get).toHaveBeenCalledTimes(1);
  128. expect(get).toHaveBeenCalledWith({ uid: 'test-data', type: 'test-data' });
  129. });
  130. });
  131. describe('when called with a query variable for a datasource variable supported data source', () => {
  132. it('then it should not dispatch any actions', async () => {
  133. const datasource = {
  134. name: 'TestData',
  135. metricFindQuery: () => undefined,
  136. variables: { getType: () => VariableSupportType.Datasource },
  137. };
  138. const { state, identifier, get, getDatasourceSrv } = getTestContext({ datasource });
  139. const dispatchedActions = await thunkTester(state)
  140. .givenThunk(upgradeLegacyQueries)
  141. .whenThunkIsDispatched(identifier, getDatasourceSrv);
  142. expect(dispatchedActions).toEqual([]);
  143. expect(get).toHaveBeenCalledTimes(1);
  144. expect(get).toHaveBeenCalledWith({ uid: 'test-data', type: 'test-data' });
  145. });
  146. });
  147. describe('when called with a custom variable', () => {
  148. it('then it should not dispatch any actions', async () => {
  149. const variable = customBuilder().withId('custom').withRootStateKey('key').withName('custom').build();
  150. const { state, identifier, get, getDatasourceSrv } = getTestContext({ variable });
  151. const dispatchedActions = await thunkTester(state)
  152. .givenThunk(upgradeLegacyQueries)
  153. .whenThunkIsDispatched(identifier, getDatasourceSrv);
  154. expect(dispatchedActions).toEqual([]);
  155. expect(get).toHaveBeenCalledTimes(0);
  156. });
  157. });
  158. });