@@ -12,7 +12,6 @@ import React from 'react';
1212import { __IntlProvider as IntlProvider } from '@kbn/i18n-react' ;
1313import { render , screen } from '@testing-library/react' ;
1414import { TimeTypeSection } from './time_type_section' ;
15- import * as timeUtils from '../../../lib/time_utils' ;
1615
1716const renderComponent = ( props : ComponentProps < typeof TimeTypeSection > ) => {
1817 render (
@@ -25,123 +24,132 @@ const renderComponent = (props: ComponentProps<typeof TimeTypeSection>) => {
2524describe ( 'TimeTypeSection' , ( ) => {
2625 beforeEach ( ( ) => {
2726 jest . clearAllMocks ( ) ;
28- jest
29- . spyOn ( timeUtils , 'convertRelativeTimeStringToAbsoluteTimeDate' )
30- . mockReturnValue ( new Date ( ) ) ;
31- jest
32- . spyOn ( timeUtils , 'getRelativeTimeValueAndUnitFromTimeString' )
33- . mockImplementation ( ( time ) => {
34- if ( time === 'now' ) return { value : 0 , unit : 'second' , roundingUnit : undefined } ;
35- if ( time === 'now-1m' ) return { value : - 1 , unit : 'minute' , roundingUnit : undefined } ;
36- if ( time === 'now-30m' ) return { value : - 30 , unit : 'minute' , roundingUnit : undefined } ;
37- return { value : 0 , unit : 'second' , roundingUnit : undefined } ;
38- } ) ;
39- jest . spyOn ( timeUtils , 'isTimeRangeAbsoluteTime' ) . mockReturnValue ( false ) ;
4027 } ) ;
4128
42- it ( 'renders null when timeRange is not provided' , ( ) => {
43- const changeTimeType = jest . fn ( ) ;
29+ it ( 'should render null when timeRange is not provided' , ( ) => {
30+ const onTimeTypeChange = jest . fn ( ) ;
4431
4532 renderComponent ( {
46- isAbsoluteTime : false ,
47- changeTimeType ,
33+ isAbsoluteTimeByDefault : false ,
34+ onTimeTypeChange ,
4835 } ) ;
4936
5037 const timeRangeSwitch = screen . queryByRole ( 'switch' ) ;
5138
5239 expect ( timeRangeSwitch ) . not . toBeInTheDocument ( ) ;
5340 } ) ;
5441
55- it ( 'renders with absolute time range' , ( ) => {
42+ it ( 'should render absolute time range' , ( ) => {
5643 const timeRange = { from : '2022-01-01T00:00:00.000Z' , to : '2022-01-02T00:00:00.000Z' } ;
57- const changeTimeType = jest . fn ( ) ;
44+ const onTimeTypeChange = jest . fn ( ) ;
5845
5946 renderComponent ( {
6047 timeRange,
61- isAbsoluteTime : true ,
62- changeTimeType ,
48+ isAbsoluteTimeByDefault : true ,
49+ onTimeTypeChange ,
6350 } ) ;
6451
65- const timeRangeSwitch = screen . getByRole ( 'switch' ) ;
66-
67- expect ( timeRangeSwitch ) . toBeChecked ( ) ;
68-
6952 const absoluteTimeInfoText = screen . getByTestId ( 'absoluteTimeInfoText' ) ;
7053
7154 expect ( absoluteTimeInfoText ) . toBeInTheDocument ( ) ;
55+ expect ( screen . getByText ( / J a n u a r y 0 1 , 2 0 2 2 / ) ) . toBeInTheDocument ( ) ;
56+ expect ( screen . getByText ( / J a n u a r y 0 2 , 2 0 2 2 / ) ) . toBeInTheDocument ( ) ;
7257 } ) ;
7358
74- it ( 'renders with relative time range (from now to specific time) ' , ( ) => {
59+ it ( 'should render relative time range' , ( ) => {
7560 const timeRange = { from : 'now' , to : 'now+15m' } ;
76- const changeTimeType = jest . fn ( ) ;
61+ const onTimeTypeChange = jest . fn ( ) ;
7762
7863 renderComponent ( {
7964 timeRange,
80- isAbsoluteTime : false ,
81- changeTimeType ,
65+ isAbsoluteTimeByDefault : false ,
66+ onTimeTypeChange ,
8267 } ) ;
8368
8469 const timeRangeSwitch = screen . getByRole ( 'switch' ) ;
8570
8671 expect ( timeRangeSwitch ) . not . toBeChecked ( ) ;
8772
88- const relativeTimeFromNowInfoText = screen . getByTestId ( 'relativeTimeInfoTextFromNow' ) ;
73+ expect ( screen . getByText ( 'now' ) ) . toBeInTheDocument ( ) ;
74+ expect ( screen . getByText ( 'in 15 minutes' ) ) . toBeInTheDocument ( ) ;
75+ } ) ;
76+
77+ it ( 'should hide switch when timeRange is already absolute' , ( ) => {
78+ const timeRange = { from : '2022-01-01T00:00:00.000Z' , to : '2022-01-02T00:00:00.000Z' } ;
79+ const onTimeTypeChange = jest . fn ( ) ;
80+
81+ renderComponent ( {
82+ timeRange,
83+ isAbsoluteTimeByDefault : true ,
84+ onTimeTypeChange,
85+ } ) ;
8986
90- expect ( relativeTimeFromNowInfoText ) . toBeInTheDocument ( ) ;
87+ const timeRangeSwitch = screen . queryByRole ( 'switch' ) ;
88+
89+ expect ( timeRangeSwitch ) . not . toBeInTheDocument ( ) ;
9190 } ) ;
9291
93- it ( 'renders with relative time range (from specific time to now )' , ( ) => {
94- const timeRange = { from : 'now-30m ' , to : 'now' } ;
95- const changeTimeType = jest . fn ( ) ;
92+ it ( 'should render with mixed time range (absolute from, relative to )' , ( ) => {
93+ const timeRange = { from : '2022-01-01T00:00:00.000Z ' , to : 'now' } ;
94+ const onTimeTypeChange = jest . fn ( ) ;
9695
9796 renderComponent ( {
9897 timeRange,
99- isAbsoluteTime : false ,
100- changeTimeType ,
98+ isAbsoluteTimeByDefault : false ,
99+ onTimeTypeChange ,
101100 } ) ;
102101
103102 const timeRangeSwitch = screen . getByRole ( 'switch' ) ;
104103
105104 expect ( timeRangeSwitch ) . not . toBeChecked ( ) ;
106105
107- const relativeTimeToNowInfoText = screen . getByTestId ( 'relativeTimeInfoTextToNow' ) ;
108-
109- expect ( relativeTimeToNowInfoText ) . toBeInTheDocument ( ) ;
106+ expect ( screen . getByText ( / J a n u a r y 0 1 , 2 0 2 2 / ) ) . toBeInTheDocument ( ) ;
107+ expect ( screen . getByText ( 'now' ) ) . toBeInTheDocument ( ) ;
110108 } ) ;
111109
112- it ( 'renders with relative time range (between two relative times )' , ( ) => {
113- const timeRange = { from : 'now-30m' , to : 'now-1m ' } ;
114- const changeTimeType = jest . fn ( ) ;
110+ it ( 'should render with mixed time range (relative from, absolute to )' , ( ) => {
111+ const timeRange = { from : 'now-30m' , to : '2022-01-01T00:00:00.000Z ' } ;
112+ const onTimeTypeChange = jest . fn ( ) ;
115113
116114 renderComponent ( {
117115 timeRange,
118- isAbsoluteTime : false ,
119- changeTimeType ,
116+ isAbsoluteTimeByDefault : false ,
117+ onTimeTypeChange ,
120118 } ) ;
121119
122120 const timeRangeSwitch = screen . getByRole ( 'switch' ) ;
123121
124122 expect ( timeRangeSwitch ) . not . toBeChecked ( ) ;
125123
126- const relativeTimeInfoText = screen . getByTestId ( 'relativeTimeInfoTextDefault' ) ;
127-
128- expect ( relativeTimeInfoText ) . toBeInTheDocument ( ) ;
124+ expect ( screen . getByText ( '30 minutes ago' ) ) . toBeInTheDocument ( ) ;
125+ expect ( screen . getByText ( / J a n u a r y 0 1 , 2 0 2 2 / ) ) . toBeInTheDocument ( ) ;
129126 } ) ;
130127
131- it ( 'disables switch when timeRange is already absolute' , ( ) => {
132- const timeRange = { from : '2022-01-01T00:00:00.000Z' , to : '2022-01-02T00:00:00.000Z' } ;
133- const changeTimeType = jest . fn ( ) ;
134-
135- jest . spyOn ( timeUtils , 'isTimeRangeAbsoluteTime' ) . mockReturnValue ( true ) ;
128+ it ( 'should render "now"' , ( ) => {
129+ const timeRange = { from : 'now-30m' , to : 'now' } ;
130+ const onTimeTypeChange = jest . fn ( ) ;
136131
137132 renderComponent ( {
138133 timeRange,
139- isAbsoluteTime : false ,
140- changeTimeType ,
134+ isAbsoluteTimeByDefault : false ,
135+ onTimeTypeChange ,
141136 } ) ;
142137
143- const timeRangeSwitch = screen . queryByRole ( 'switch' ) ;
138+ expect ( screen . getByText ( '30 minutes ago' ) ) . toBeInTheDocument ( ) ;
139+ expect ( screen . getByText ( 'now' ) ) . toBeInTheDocument ( ) ;
140+ } ) ;
144141
145- expect ( timeRangeSwitch ) . not . toBeInTheDocument ( ) ;
142+ it ( 'should handle plain "now" value correctly in mixed ranges' , ( ) => {
143+ const timeRange = { from : '2025-11-10T14:17:51.794Z' , to : 'now' } ;
144+ const onTimeTypeChange = jest . fn ( ) ;
145+
146+ renderComponent ( {
147+ timeRange,
148+ isAbsoluteTimeByDefault : false ,
149+ onTimeTypeChange,
150+ } ) ;
151+
152+ expect ( screen . getByText ( / N o v e m b e r 1 0 , 2 0 2 5 / ) ) . toBeInTheDocument ( ) ;
153+ expect ( screen . getByText ( 'now' ) ) . toBeInTheDocument ( ) ;
146154 } ) ;
147155} ) ;
0 commit comments