timeEpic.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { dateTime, DefaultTimeZone } from '@grafana/data';
  2. import { epicTester } from 'test/core/redux/epicTester';
  3. import { mockExploreState } from 'test/mocks/mockExploreState';
  4. import { timeEpic } from './timeEpic';
  5. import { updateTimeRangeAction, changeRangeAction } from '../actionTypes';
  6. import { EpicDependencies } from 'app/store/configureStore';
  7. const from = dateTime('2019-01-01 10:00:00.000Z');
  8. const to = dateTime('2019-01-01 16:00:00.000Z');
  9. const rawFrom = 'now-6h';
  10. const rawTo = 'now';
  11. const rangeMock = {
  12. from,
  13. to,
  14. raw: {
  15. from: rawFrom,
  16. to: rawTo,
  17. },
  18. };
  19. describe('timeEpic', () => {
  20. describe('when updateTimeRangeAction is dispatched', () => {
  21. describe('and no rawRange is supplied', () => {
  22. describe('and no absoluteRange is supplied', () => {
  23. it('then the correct actions are dispatched', () => {
  24. const { exploreId, state, range } = mockExploreState({ range: rangeMock });
  25. const absoluteRange = { from: range.from.valueOf(), to: range.to.valueOf() };
  26. const stateToTest = { ...state, user: { timeZone: 'browser', orgId: -1 } };
  27. const getTimeRange = jest.fn().mockReturnValue(rangeMock);
  28. const dependencies: Partial<EpicDependencies> = {
  29. getTimeRange,
  30. };
  31. epicTester(timeEpic, stateToTest, dependencies)
  32. .whenActionIsDispatched(updateTimeRangeAction({ exploreId }))
  33. .thenDependencyWasCalledTimes(1, 'getTimeSrv', 'init')
  34. .thenDependencyWasCalledTimes(1, 'getTimeRange')
  35. .thenDependencyWasCalledWith([DefaultTimeZone, rangeMock.raw], 'getTimeRange')
  36. .thenResultingActionsEqual(
  37. changeRangeAction({
  38. exploreId,
  39. range,
  40. absoluteRange,
  41. })
  42. );
  43. });
  44. });
  45. describe('and absoluteRange is supplied', () => {
  46. it('then the correct actions are dispatched', () => {
  47. const { exploreId, state, range } = mockExploreState({ range: rangeMock });
  48. const absoluteRange = { from: range.from.valueOf(), to: range.to.valueOf() };
  49. const stateToTest = { ...state, user: { timeZone: 'browser', orgId: -1 } };
  50. const getTimeRange = jest.fn().mockReturnValue(rangeMock);
  51. const dependencies: Partial<EpicDependencies> = {
  52. getTimeRange,
  53. };
  54. epicTester(timeEpic, stateToTest, dependencies)
  55. .whenActionIsDispatched(updateTimeRangeAction({ exploreId, absoluteRange }))
  56. .thenDependencyWasCalledTimes(1, 'getTimeSrv', 'init')
  57. .thenDependencyWasCalledTimes(1, 'getTimeRange')
  58. .thenDependencyWasCalledWith([DefaultTimeZone, { from: null, to: null }], 'getTimeRange')
  59. .thenDependencyWasCalledTimes(2, 'dateTime')
  60. .thenResultingActionsEqual(
  61. changeRangeAction({
  62. exploreId,
  63. range,
  64. absoluteRange,
  65. })
  66. );
  67. });
  68. });
  69. });
  70. describe('and rawRange is supplied', () => {
  71. describe('and no absoluteRange is supplied', () => {
  72. it('then the correct actions are dispatched', () => {
  73. const { exploreId, state, range } = mockExploreState({ range: rangeMock });
  74. const rawRange = { from: 'now-5m', to: 'now' };
  75. const absoluteRange = { from: range.from.valueOf(), to: range.to.valueOf() };
  76. const stateToTest = { ...state, user: { timeZone: 'browser', orgId: -1 } };
  77. const getTimeRange = jest.fn().mockReturnValue(rangeMock);
  78. const dependencies: Partial<EpicDependencies> = {
  79. getTimeRange,
  80. };
  81. epicTester(timeEpic, stateToTest, dependencies)
  82. .whenActionIsDispatched(updateTimeRangeAction({ exploreId, rawRange }))
  83. .thenDependencyWasCalledTimes(1, 'getTimeSrv', 'init')
  84. .thenDependencyWasCalledTimes(1, 'getTimeRange')
  85. .thenDependencyWasCalledWith([DefaultTimeZone, rawRange], 'getTimeRange')
  86. .thenResultingActionsEqual(
  87. changeRangeAction({
  88. exploreId,
  89. range,
  90. absoluteRange,
  91. })
  92. );
  93. });
  94. });
  95. });
  96. });
  97. });