1
1
import React from 'react' ;
2
+ import moment from 'moment' ;
3
+
2
4
import { render , screen } from '@testing-library/react' ;
3
5
import userEvent from '@testing-library/user-event' ;
4
- import moment from 'moment' ;
6
+
5
7
import TwoMonthPicker from '../two-month-picker' ;
6
8
7
9
describe ( 'TwoMonthPicker' , ( ) => {
@@ -18,17 +20,17 @@ describe('TwoMonthPicker', () => {
18
20
const currentYear = moment ( ) . format ( 'YYYY' ) ;
19
21
expect ( screen . getByText ( prevMonth ) ) . toBeInTheDocument ( ) ;
20
22
expect ( screen . getByText ( currentMonth ) ) . toBeInTheDocument ( ) ;
21
- expect ( screen . getAllByText ( currentYear ) ) . toHaveLength ( 2 ) ;
23
+ expect ( screen . getByText ( currentYear ) ) . toBeInTheDocument ( ) ;
22
24
} ) ;
23
- it ( 'should call onChange when a date is selected' , ( ) => {
25
+ it ( 'should call onChange when a date is selected' , async ( ) => {
24
26
render ( < TwoMonthPicker { ...mockProps } /> ) ;
25
27
const prevMonthDate = moment ( ) . date ( 1 ) . subtract ( 1 , 'month' ) ;
26
28
const prevMonthDateElement = screen . getAllByText ( prevMonthDate . date ( ) ) [ 0 ] ;
27
29
const prevMonthFullDate = prevMonthDate . format ( 'YYYY-MM-DD' ) ;
28
- userEvent . click ( prevMonthDateElement ) ;
30
+ await userEvent . click ( prevMonthDateElement ) ;
29
31
expect ( mockProps . onChange ) . toHaveBeenCalledWith ( moment . utc ( prevMonthFullDate , 'YYYY-MM-DD' ) ) ;
30
32
} ) ;
31
- it ( 'should jump to current month from previous months upon clicking today button' , ( ) => {
33
+ it ( 'should jump to current month from previous months upon clicking today button' , async ( ) => {
32
34
render ( < TwoMonthPicker { ...mockProps } /> ) ;
33
35
const currentMonth = moment ( ) . format ( 'MMM' ) ;
34
36
const monthBeforePrevious = moment ( ) . subtract ( 2 , 'month' ) . format ( 'MMM' ) ;
@@ -37,12 +39,12 @@ describe('TwoMonthPicker', () => {
37
39
const todayButton = screen . getByTestId ( 'dt_calendar_icon_IcCalendarForwardToday' ) ;
38
40
39
41
// go to previous months
40
- userEvent . click ( prevMonthButton ) ;
42
+ await userEvent . click ( prevMonthButton ) ;
41
43
expect ( screen . getByText ( monthBeforePrevious ) ) . toBeInTheDocument ( ) ;
42
44
expect ( screen . getByText ( prevMonth ) ) . toBeInTheDocument ( ) ;
43
45
44
46
// jump to current month
45
- userEvent . click ( todayButton ) ;
47
+ await userEvent . click ( todayButton ) ;
46
48
expect ( screen . queryByText ( monthBeforePrevious ) ) . not . toBeInTheDocument ( ) ;
47
49
expect ( screen . getByText ( prevMonth ) ) . toBeInTheDocument ( ) ;
48
50
expect ( screen . getByText ( currentMonth ) ) . toBeInTheDocument ( ) ;
0 commit comments