time.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { reducerTester } from 'test/core/redux/reducerTester';
  2. import { dateTime, LoadingState } from '@grafana/data';
  3. import { configureStore } from 'app/store/configureStore';
  4. import { ExploreId, ExploreItemState } from 'app/types/explore';
  5. import { silenceConsoleOutput } from '../../../../test/core/utils/silenceConsoleOutput';
  6. import { createDefaultInitialState } from './helpers';
  7. import { changeRangeAction, changeRefreshIntervalAction, timeReducer, updateTime } from './time';
  8. import { makeExplorePaneState } from './utils';
  9. const MOCK_TIME_RANGE = {};
  10. const mockTimeSrv = {
  11. init: jest.fn(),
  12. timeRange: jest.fn().mockReturnValue(MOCK_TIME_RANGE),
  13. };
  14. jest.mock('app/features/dashboard/services/TimeSrv', () => ({
  15. ...jest.requireActual('app/features/dashboard/services/TimeSrv'),
  16. getTimeSrv: () => mockTimeSrv,
  17. }));
  18. const mockTemplateSrv = {
  19. updateTimeRange: jest.fn(),
  20. };
  21. jest.mock('@grafana/runtime', () => ({
  22. ...(jest.requireActual('@grafana/runtime') as unknown as object),
  23. getTemplateSrv: () => mockTemplateSrv,
  24. }));
  25. describe('Explore item reducer', () => {
  26. silenceConsoleOutput();
  27. describe('When time is updated', () => {
  28. it('Time service is re-initialized and template service is updated with the new time range', async () => {
  29. const { dispatch } = configureStore({
  30. ...(createDefaultInitialState() as any),
  31. });
  32. await dispatch(updateTime({ exploreId: ExploreId.left }));
  33. expect(mockTimeSrv.init).toBeCalled();
  34. expect(mockTemplateSrv.updateTimeRange).toBeCalledWith(MOCK_TIME_RANGE);
  35. });
  36. });
  37. describe('changing refresh intervals', () => {
  38. it("should result in 'streaming' state, when live-tailing is active", () => {
  39. const initialState = makeExplorePaneState();
  40. const expectedState = {
  41. ...initialState,
  42. refreshInterval: 'LIVE',
  43. isLive: true,
  44. loading: true,
  45. logsResult: {
  46. hasUniqueLabels: false,
  47. rows: [] as any[],
  48. },
  49. queryResponse: {
  50. ...initialState.queryResponse,
  51. state: LoadingState.Streaming,
  52. },
  53. };
  54. reducerTester<ExploreItemState>()
  55. .givenReducer(timeReducer, initialState)
  56. .whenActionIsDispatched(changeRefreshIntervalAction({ exploreId: ExploreId.left, refreshInterval: 'LIVE' }))
  57. .thenStateShouldEqual(expectedState);
  58. });
  59. it("should result in 'done' state, when live-tailing is stopped", () => {
  60. const initialState = makeExplorePaneState();
  61. const expectedState = {
  62. ...initialState,
  63. refreshInterval: '',
  64. logsResult: {
  65. hasUniqueLabels: false,
  66. rows: [] as any[],
  67. },
  68. queryResponse: {
  69. ...initialState.queryResponse,
  70. state: LoadingState.Done,
  71. },
  72. };
  73. reducerTester<ExploreItemState>()
  74. .givenReducer(timeReducer, initialState)
  75. .whenActionIsDispatched(changeRefreshIntervalAction({ exploreId: ExploreId.left, refreshInterval: '' }))
  76. .thenStateShouldEqual(expectedState);
  77. });
  78. });
  79. describe('changing range', () => {
  80. describe('when changeRangeAction is dispatched', () => {
  81. it('then it should set correct state', () => {
  82. reducerTester<ExploreItemState>()
  83. .givenReducer(timeReducer, {
  84. range: null,
  85. absoluteRange: null,
  86. } as unknown as ExploreItemState)
  87. .whenActionIsDispatched(
  88. changeRangeAction({
  89. exploreId: ExploreId.left,
  90. absoluteRange: { from: 1546297200000, to: 1546383600000 },
  91. range: { from: dateTime('2019-01-01'), to: dateTime('2019-01-02'), raw: { from: 'now-1d', to: 'now' } },
  92. })
  93. )
  94. .thenStateShouldEqual({
  95. absoluteRange: { from: 1546297200000, to: 1546383600000 },
  96. range: { from: dateTime('2019-01-01'), to: dateTime('2019-01-02'), raw: { from: 'now-1d', to: 'now' } },
  97. } as unknown as ExploreItemState);
  98. });
  99. });
  100. });
  101. });