reducer.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 { TextBoxVariableModel } from '../types';
  6. import { toVariablePayload } from '../utils';
  7. import { createTextBoxVariableAdapter } from './adapter';
  8. import { createTextBoxOptions, textBoxVariableReducer } from './reducer';
  9. describe('textBoxVariableReducer', () => {
  10. const adapter = createTextBoxVariableAdapter();
  11. describe('when createTextBoxOptions 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: 'textbox' });
  17. reducerTester<VariablesState>()
  18. .givenReducer(textBoxVariableReducer, cloneDeep(initialState))
  19. .whenActionIsDispatched(createTextBoxOptions(payload))
  20. .thenStateShouldEqual({
  21. [id]: {
  22. ...initialState[id],
  23. options: [
  24. {
  25. text: query,
  26. value: query,
  27. selected: false,
  28. },
  29. ],
  30. current: {
  31. text: query,
  32. value: query,
  33. selected: false,
  34. },
  35. } as TextBoxVariableModel,
  36. });
  37. });
  38. });
  39. describe('when createTextBoxOptions is dispatched and query contains spaces', () => {
  40. it('then state should be correct', () => {
  41. const query = ' ABC ';
  42. const id = '0';
  43. const { initialState } = getVariableTestContext(adapter, { id, query });
  44. const payload = toVariablePayload({ id: '0', type: 'textbox' });
  45. reducerTester<VariablesState>()
  46. .givenReducer(textBoxVariableReducer, cloneDeep(initialState))
  47. .whenActionIsDispatched(createTextBoxOptions(payload))
  48. .thenStateShouldEqual({
  49. [id]: {
  50. ...initialState[id],
  51. options: [
  52. {
  53. text: query.trim(),
  54. value: query.trim(),
  55. selected: false,
  56. },
  57. ],
  58. current: {
  59. text: query.trim(),
  60. value: query.trim(),
  61. selected: false,
  62. },
  63. } as TextBoxVariableModel,
  64. });
  65. });
  66. });
  67. });