reducer.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { IntervalVariableModel } from '../types';
  6. import { toVariablePayload } from '../utils';
  7. import { createIntervalVariableAdapter } from './adapter';
  8. import { createIntervalOptions, intervalVariableReducer } from './reducer';
  9. describe('intervalVariableReducer', () => {
  10. const adapter = createIntervalVariableAdapter();
  11. describe('when createIntervalOptions is dispatched', () => {
  12. describe('and auto is false', () => {
  13. it('then state should be correct', () => {
  14. const id = '0';
  15. const query = '1s,1m,1h,1d';
  16. const auto = false;
  17. const { initialState } = getVariableTestContext<IntervalVariableModel>(adapter, { id, query, auto });
  18. const payload = toVariablePayload({ id: '0', type: 'interval' });
  19. reducerTester<VariablesState>()
  20. .givenReducer(intervalVariableReducer, cloneDeep(initialState))
  21. .whenActionIsDispatched(createIntervalOptions(payload))
  22. .thenStateShouldEqual({
  23. '0': {
  24. ...initialState['0'],
  25. id: '0',
  26. query: '1s,1m,1h,1d',
  27. auto: false,
  28. options: [
  29. { text: '1s', value: '1s', selected: false },
  30. { text: '1m', value: '1m', selected: false },
  31. { text: '1h', value: '1h', selected: false },
  32. { text: '1d', value: '1d', selected: false },
  33. ],
  34. } as IntervalVariableModel,
  35. });
  36. });
  37. });
  38. describe('and auto is true', () => {
  39. it('then state should be correct', () => {
  40. const id = '0';
  41. const query = '1s,1m,1h,1d';
  42. const auto = true;
  43. const { initialState } = getVariableTestContext<IntervalVariableModel>(adapter, { id, query, auto });
  44. const payload = toVariablePayload({ id: '0', type: 'interval' });
  45. reducerTester<VariablesState>()
  46. .givenReducer(intervalVariableReducer, cloneDeep(initialState))
  47. .whenActionIsDispatched(createIntervalOptions(payload))
  48. .thenStateShouldEqual({
  49. '0': {
  50. ...initialState['0'],
  51. id: '0',
  52. query: '1s,1m,1h,1d',
  53. auto: true,
  54. options: [
  55. { text: 'auto', value: '$__auto_interval_0', selected: false },
  56. { text: '1s', value: '1s', selected: false },
  57. { text: '1m', value: '1m', selected: false },
  58. { text: '1h', value: '1h', selected: false },
  59. { text: '1d', value: '1d', selected: false },
  60. ],
  61. } as IntervalVariableModel,
  62. });
  63. });
  64. });
  65. describe('and query contains "', () => {
  66. it('then state should be correct', () => {
  67. const id = '0';
  68. const query = '"kalle, anka","donald, duck"';
  69. const auto = false;
  70. const { initialState } = getVariableTestContext<IntervalVariableModel>(adapter, { id, query, auto });
  71. const payload = toVariablePayload({ id: '0', type: 'interval' });
  72. reducerTester<VariablesState>()
  73. .givenReducer(intervalVariableReducer, cloneDeep(initialState))
  74. .whenActionIsDispatched(createIntervalOptions(payload))
  75. .thenStateShouldEqual({
  76. '0': {
  77. ...initialState['0'],
  78. id: '0',
  79. query: '"kalle, anka","donald, duck"',
  80. auto: false,
  81. options: [
  82. { text: 'kalle, anka', value: 'kalle, anka', selected: false },
  83. { text: 'donald, duck', value: 'donald, duck', selected: false },
  84. ],
  85. } as IntervalVariableModel,
  86. });
  87. });
  88. });
  89. describe("and query contains '", () => {
  90. it('then state should be correct', () => {
  91. const id = '0';
  92. const query = "'kalle, anka','donald, duck'";
  93. const auto = false;
  94. const { initialState } = getVariableTestContext<IntervalVariableModel>(adapter, { id, query, auto });
  95. const payload = toVariablePayload({ id: '0', type: 'interval' });
  96. reducerTester<VariablesState>()
  97. .givenReducer(intervalVariableReducer, cloneDeep(initialState))
  98. .whenActionIsDispatched(createIntervalOptions(payload))
  99. .thenStateShouldEqual({
  100. '0': {
  101. ...initialState['0'],
  102. id: '0',
  103. query: "'kalle, anka','donald, duck'",
  104. auto: false,
  105. options: [
  106. { text: 'kalle, anka', value: 'kalle, anka', selected: false },
  107. { text: 'donald, duck', value: 'donald, duck', selected: false },
  108. ],
  109. } as IntervalVariableModel,
  110. });
  111. });
  112. });
  113. });
  114. });