reducer.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { cloneDeep } from 'lodash';
  2. import { reducerTester } from '../../../../test/core/redux/reducerTester';
  3. import { getVariableTestContext } from '../state/helpers';
  4. import { VariablesState } from '../state/types';
  5. import { ConstantVariableModel } from '../types';
  6. import { toVariablePayload } from '../utils';
  7. import { createConstantVariableAdapter } from './adapter';
  8. import { constantVariableReducer, createConstantOptionsFromQuery } from './reducer';
  9. describe('constantVariableReducer', () => {
  10. const adapter = createConstantVariableAdapter();
  11. describe('when createConstantOptionsFromQuery is dispatched', () => {
  12. it('then state should be correct', () => {
  13. const query = 'ABC';
  14. const id = '0';
  15. const { initialState } = getVariableTestContext(adapter, { id, query });
  16. const payload = toVariablePayload({ id: '0', type: 'constant' });
  17. reducerTester<VariablesState>()
  18. .givenReducer(constantVariableReducer, cloneDeep(initialState))
  19. .whenActionIsDispatched(createConstantOptionsFromQuery(payload))
  20. .thenStateShouldEqual({
  21. [id]: {
  22. ...initialState[id],
  23. options: [
  24. {
  25. text: query,
  26. value: query,
  27. selected: false,
  28. },
  29. ],
  30. } as ConstantVariableModel,
  31. });
  32. });
  33. });
  34. describe('when createConstantOptionsFromQuery is dispatched and query contains spaces', () => {
  35. it('then state should be correct', () => {
  36. const query = ' ABC ';
  37. const id = '0';
  38. const { initialState } = getVariableTestContext(adapter, { id, query });
  39. const payload = toVariablePayload({ id: '0', type: 'constant' });
  40. reducerTester<VariablesState>()
  41. .givenReducer(constantVariableReducer, cloneDeep(initialState))
  42. .whenActionIsDispatched(createConstantOptionsFromQuery(payload))
  43. .thenStateShouldEqual({
  44. [id]: {
  45. ...initialState[id],
  46. options: [
  47. {
  48. text: query.trim(),
  49. value: query.trim(),
  50. selected: false,
  51. },
  52. ],
  53. } as ConstantVariableModel,
  54. });
  55. });
  56. });
  57. });