77 */
88
99import React from 'react' ;
10- import { fireEvent } from '@testing-library/react' ;
10+ import { act , fireEvent } from '@testing-library/react' ;
1111import { render } from '../../../../test/rtl' ;
1212
1313import { EuiAbsoluteTab } from './absolute_tab' ;
@@ -29,15 +29,23 @@ describe('EuiAbsoluteTab', () => {
2929 } ;
3030
3131 describe ( 'user input' , ( ) => {
32+ beforeAll ( ( ) => jest . useFakeTimers ( ) ) ;
33+ afterAll ( ( ) => jest . useRealTimers ( ) ) ;
34+
35+ const changeInput = ( input : HTMLElement , value : string ) => {
36+ fireEvent . change ( input , { target : { value } } ) ;
37+ act ( ( ) => {
38+ jest . advanceTimersByTime ( 1000 ) ; // Debounce timer
39+ } ) ;
40+ } ;
41+
3242 it ( 'parses the passed `dateFormat` prop' , ( ) => {
3343 const { getByTestSubject } = render (
3444 < EuiAbsoluteTab { ...props } dateFormat = "MMM Do YY" />
3545 ) ;
3646 const input = getByTestSubject ( 'superDatePickerAbsoluteDateInput' ) ;
3747
38- fireEvent . change ( input , {
39- target : { value : 'Jan 31st 01' } ,
40- } ) ;
48+ changeInput ( input , 'Jan 31st 01' ) ;
4149 expect ( input ) . not . toBeInvalid ( ) ;
4250 expect ( input ) . toHaveValue ( 'Jan 31st 01' ) ;
4351 } ) ;
@@ -52,9 +60,7 @@ describe('EuiAbsoluteTab', () => {
5260 const { getByTestSubject } = render ( < EuiAbsoluteTab { ...props } /> ) ;
5361 const input = getByTestSubject ( 'superDatePickerAbsoluteDateInput' ) ;
5462
55- fireEvent . change ( input , {
56- target : { value : '1970-01-01T12:00:00+00:00' } ,
57- } ) ;
63+ changeInput ( input , '1970-01-01T12:00:00+00:00' ) ;
5864 expect ( input ) . not . toBeInvalid ( ) ;
5965 assertOutput ( input as HTMLInputElement ) ;
6066 } ) ;
@@ -63,9 +69,7 @@ describe('EuiAbsoluteTab', () => {
6369 const { getByTestSubject } = render ( < EuiAbsoluteTab { ...props } /> ) ;
6470 const input = getByTestSubject ( 'superDatePickerAbsoluteDateInput' ) ;
6571
66- fireEvent . change ( input , {
67- target : { value : 'Thu, 1 Jan 1970 12:00:00 +0000' } ,
68- } ) ;
72+ changeInput ( input , 'Thu, 1 Jan 1970 12:00:00 +0000' ) ;
6973 expect ( input ) . not . toBeInvalid ( ) ;
7074 assertOutput ( input as HTMLInputElement ) ;
7175 } ) ;
@@ -74,10 +78,10 @@ describe('EuiAbsoluteTab', () => {
7478 const { getByTestSubject } = render ( < EuiAbsoluteTab { ...props } /> ) ;
7579 const input = getByTestSubject ( 'superDatePickerAbsoluteDateInput' ) ;
7680
77- fireEvent . change ( input , { target : { value : Date . now ( ) . toString ( ) } } ) ;
81+ changeInput ( input , Date . now ( ) . toString ( ) ) ;
7882 expect ( input ) . not . toBeInvalid ( ) ;
7983
80- fireEvent . change ( input , { target : { value : '43200' } } ) ;
84+ changeInput ( input , '43200' ) ;
8185 expect ( input ) . not . toBeInvalid ( ) ;
8286 assertOutput ( input as HTMLInputElement ) ;
8387 } ) ;
@@ -87,15 +91,15 @@ describe('EuiAbsoluteTab', () => {
8791 const { getByTestSubject } = render ( < EuiAbsoluteTab { ...props } /> ) ;
8892 const input = getByTestSubject ( 'superDatePickerAbsoluteDateInput' ) ;
8993
90- fireEvent . change ( input , { target : { value : '01-01-1970' } } ) ;
94+ changeInput ( input , '01-01-1970' ) ;
9195 expect ( input ) . toHaveValue ( '01-01-1970' ) ;
9296 expect ( input ) . toBeInvalid ( ) ;
9397
94- fireEvent . change ( input , { target : { value : 'asdfasdf' } } ) ;
98+ changeInput ( input , 'asdfasdf' ) ;
9599 expect ( input ) . toHaveValue ( 'asdfasdf' ) ;
96100 expect ( input ) . toBeInvalid ( ) ;
97101
98- fireEvent . change ( input , { target : { value : '' } } ) ;
102+ changeInput ( input , '' ) ;
99103 expect ( input ) . toHaveValue ( '' ) ;
100104 expect ( input ) . toBeInvalid ( ) ;
101105 } ) ;
0 commit comments