actions.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { locationService } from '@grafana/runtime';
  2. import { reduxTester } from '../../../../test/core/redux/reduxTester';
  3. import { variableAdapters } from '../adapters';
  4. import { textboxBuilder } from '../shared/testing/builders';
  5. import { getRootReducer, RootReducerType } from '../state/helpers';
  6. import { toKeyedAction } from '../state/keyedVariablesReducer';
  7. import { addVariable, changeVariableProp, setCurrentVariableValue } from '../state/sharedReducer';
  8. import { VariableOption } from '../types';
  9. import { toKeyedVariableIdentifier, toVariablePayload } from '../utils';
  10. import { setTextBoxVariableOptionsFromUrl, updateTextBoxVariableOptions } from './actions';
  11. import { createTextBoxVariableAdapter } from './adapter';
  12. import { createTextBoxOptions } from './reducer';
  13. jest.mock('@grafana/runtime', () => {
  14. const original = jest.requireActual('@grafana/runtime');
  15. return {
  16. ...original,
  17. locationService: {
  18. partial: jest.fn(),
  19. getSearchObject: () => ({}),
  20. },
  21. };
  22. });
  23. describe('textbox actions', () => {
  24. variableAdapters.setInit(() => [createTextBoxVariableAdapter()]);
  25. describe('when updateTextBoxVariableOptions is dispatched', () => {
  26. it('then correct actions are dispatched', async () => {
  27. const option: VariableOption = {
  28. value: 'A',
  29. text: 'A',
  30. selected: false,
  31. };
  32. const key = 'key';
  33. const variable = textboxBuilder()
  34. .withId('textbox')
  35. .withRootStateKey(key)
  36. .withName('textbox')
  37. .withCurrent('A')
  38. .withQuery('A')
  39. .build();
  40. const tester = await reduxTester<RootReducerType>()
  41. .givenRootReducer(getRootReducer())
  42. .whenActionIsDispatched(
  43. toKeyedAction(key, addVariable(toVariablePayload(variable, { global: false, index: 0, model: variable })))
  44. )
  45. .whenAsyncActionIsDispatched(updateTextBoxVariableOptions(toKeyedVariableIdentifier(variable)), true);
  46. tester.thenDispatchedActionsShouldEqual(
  47. toKeyedAction(key, createTextBoxOptions(toVariablePayload(variable))),
  48. toKeyedAction(key, setCurrentVariableValue(toVariablePayload(variable, { option })))
  49. );
  50. expect(locationService.partial).toHaveBeenLastCalledWith({ 'var-textbox': 'A' });
  51. });
  52. });
  53. describe('when setTextBoxVariableOptionsFromUrl is dispatched', () => {
  54. it('then correct actions are dispatched', async () => {
  55. const urlValue = 'bB';
  56. const key = 'key';
  57. const variable = textboxBuilder()
  58. .withId('textbox')
  59. .withRootStateKey(key)
  60. .withName('textbox')
  61. .withCurrent('A')
  62. .withQuery('A')
  63. .build();
  64. const tester = await reduxTester<RootReducerType>()
  65. .givenRootReducer(getRootReducer())
  66. .whenActionIsDispatched(
  67. toKeyedAction(key, addVariable(toVariablePayload(variable, { global: false, index: 0, model: variable })))
  68. )
  69. .whenAsyncActionIsDispatched(
  70. setTextBoxVariableOptionsFromUrl(toKeyedVariableIdentifier(variable), urlValue),
  71. true
  72. );
  73. tester.thenDispatchedActionsShouldEqual(
  74. toKeyedAction(key, changeVariableProp(toVariablePayload(variable, { propName: 'query', propValue: 'bB' }))),
  75. toKeyedAction(
  76. key,
  77. setCurrentVariableValue(toVariablePayload(variable, { option: { text: 'bB', value: 'bB', selected: false } }))
  78. )
  79. );
  80. });
  81. });
  82. });