@@ -9,118 +9,157 @@ import FriendlyDate from './index';
99
1010describe ( 'Friendly Date' , ( ) => {
1111 it ( 'should render correctly' , ( ) => {
12+ jest . useFakeTimers ( ) ;
13+ jest . setSystemTime ( new Date ( '2020-05-15T00:00:00.000Z' ) ) ;
14+
1215 const justNow = new Date ( ) ;
1316 justNow . setSeconds ( justNow . getSeconds ( ) - 10 ) ;
17+ let value = justNow . getTime ( ) ;
1418 const justNowText = renderWithIntl (
15- < FriendlyDate value = { justNow } /> ,
19+ < FriendlyDate value = { value } /> ,
1620 ) ;
1721 expect ( justNowText . getByText ( 'Now' ) ) . toBeTruthy ( ) ;
1822
1923 const minutesAgo = new Date ( ) ;
2024 minutesAgo . setMinutes ( minutesAgo . getMinutes ( ) - 1 ) ;
25+ value = minutesAgo . getTime ( ) ;
2126 const minutesAgoText = renderWithIntl (
22- < FriendlyDate value = { minutesAgo } /> ,
27+ < FriendlyDate value = { value } /> ,
2328 ) ;
24- expect ( minutesAgoText . getByText ( '1 min ago' ) ) . toBeTruthy ( ) ;
29+ expect ( minutesAgoText . getByText ( '1 min. ago' ) ) . toBeTruthy ( ) ;
2530
2631 const hoursAgo = new Date ( ) ;
2732 hoursAgo . setHours ( hoursAgo . getHours ( ) - 4 ) ;
33+ value = hoursAgo . getTime ( ) ;
2834 const hoursAgoText = renderWithIntl (
29- < FriendlyDate value = { hoursAgo } /> ,
35+ < FriendlyDate value = { value } /> ,
3036 ) ;
3137 expect ( hoursAgoText . getByText ( '4 hours ago' ) ) . toBeTruthy ( ) ;
3238
3339 const yesterday = new Date ( ) ;
3440 yesterday . setDate ( yesterday . getDate ( ) - 1 ) ;
41+ value = yesterday . getTime ( ) ;
3542 const yesterdayText = renderWithIntl (
36- < FriendlyDate value = { yesterday } /> ,
43+ < FriendlyDate value = { value } /> ,
3744 ) ;
38- expect ( yesterdayText . getByText ( 'Yesterday ' ) ) . toBeTruthy ( ) ;
45+ expect ( yesterdayText . getByText ( 'yesterday ' ) ) . toBeTruthy ( ) ;
3946
4047 const daysAgo = new Date ( ) ;
4148 daysAgo . setDate ( daysAgo . getDate ( ) - 10 ) ;
49+ value = daysAgo . getTime ( ) ;
4250 const daysAgoText = renderWithIntl (
43- < FriendlyDate value = { daysAgo } /> ,
51+ < FriendlyDate value = { value } /> ,
4452 ) ;
4553 expect ( daysAgoText . getByText ( '10 days ago' ) ) . toBeTruthy ( ) ;
4654
4755 // Difference is less than 30 days
4856 const daysEdgeCase = new Date ( 2020 , 3 , 28 ) ;
4957 const daysEdgeCaseTodayDate = new Date ( 2020 , 4 , 28 ) ;
58+ jest . setSystemTime ( daysEdgeCaseTodayDate ) ;
59+ value = daysEdgeCase . getTime ( ) ;
5060 const daysEdgeCaseText = renderWithIntl (
51- < FriendlyDate
52- sourceDate = { daysEdgeCaseTodayDate }
53- value = { daysEdgeCase }
54- /> ,
61+ < FriendlyDate value = { value } /> ,
5562 ) ;
56- expect ( daysEdgeCaseText . getByText ( '1 month ago ' ) ) . toBeTruthy ( ) ;
63+ expect ( daysEdgeCaseText . getByText ( 'last month' ) ) . toBeTruthy ( ) ;
5764
5865 const daysAgoMax = new Date ( 2020 , 4 , 6 ) ;
5966 const daysAgoMaxTodayDate = new Date ( 2020 , 5 , 5 ) ;
67+ jest . setSystemTime ( daysAgoMaxTodayDate ) ;
68+ value = daysAgoMax . getTime ( ) ;
6069 const daysAgoMaxText = renderWithIntl (
61- < FriendlyDate
62- sourceDate = { daysAgoMaxTodayDate }
63- value = { daysAgoMax }
64- /> ,
70+ < FriendlyDate value = { value } /> ,
6571 ) ;
6672 expect ( daysAgoMaxText . getByText ( '30 days ago' ) ) . toBeTruthy ( ) ;
6773
6874 const monthsAgo = new Date ( ) ;
6975 monthsAgo . setMonth ( monthsAgo . getMonth ( ) - 2 ) ;
76+ value = monthsAgo . getTime ( ) ;
7077 const monthsAgoText = renderWithIntl (
71- < FriendlyDate value = { monthsAgo } /> ,
78+ < FriendlyDate value = { value } /> ,
7279 ) ;
7380 expect ( monthsAgoText . getByText ( '2 months ago' ) ) . toBeTruthy ( ) ;
7481
7582 const yearsAgo = new Date ( ) ;
7683 yearsAgo . setFullYear ( yearsAgo . getFullYear ( ) - 2 ) ;
84+ value = yearsAgo . getTime ( ) ;
7785 const yearsAgoText = renderWithIntl (
78- < FriendlyDate value = { yearsAgo } /> ,
86+ < FriendlyDate value = { value } /> ,
7987 ) ;
8088 expect ( yearsAgoText . getByText ( '2 years ago' ) ) . toBeTruthy ( ) ;
89+
90+ jest . useRealTimers ( ) ;
8191 } ) ;
8292
8393 it ( 'should render correctly with times in the future' , ( ) => {
94+ jest . useFakeTimers ( ) ;
95+ jest . setSystemTime ( new Date ( '2020-05-15T00:00:00.000Z' ) ) ;
96+
8497 const justNow = new Date ( ) ;
8598 justNow . setSeconds ( justNow . getSeconds ( ) + 10 ) ;
99+ let value = justNow . getTime ( ) ;
86100 const justNowText = renderWithIntl (
87- < FriendlyDate value = { justNow } /> ,
101+ < FriendlyDate value = { value } /> ,
88102 ) ;
89103 expect ( justNowText . getByText ( 'Now' ) ) . toBeTruthy ( ) ;
90104
91105 const inMinutes = new Date ( ) ;
92106 inMinutes . setMinutes ( inMinutes . getMinutes ( ) + 2 ) ;
107+ value = inMinutes . getTime ( ) ;
93108 const inMinutesText = renderWithIntl (
94- < FriendlyDate value = { inMinutes } /> ,
109+ < FriendlyDate value = { value } /> ,
95110 ) ;
96- expect ( inMinutesText . getByText ( 'in 2 mins ' ) ) . toBeTruthy ( ) ;
111+ expect ( inMinutesText . getByText ( 'in 2 min. ' ) ) . toBeTruthy ( ) ;
97112
98113 const inHours = new Date ( ) ;
99114 inHours . setHours ( inHours . getHours ( ) + 2 ) ;
115+ value = inHours . getTime ( ) ;
100116 const inHoursText = renderWithIntl (
101- < FriendlyDate value = { inHours } /> ,
117+ < FriendlyDate value = { value } /> ,
102118 ) ;
103119 expect ( inHoursText . getByText ( 'in 2 hours' ) ) . toBeTruthy ( ) ;
104120
105121 const inDays = new Date ( ) ;
106122 inDays . setDate ( inDays . getDate ( ) + 2 ) ;
123+ value = inDays . getTime ( ) ;
107124 const inDaysText = renderWithIntl (
108- < FriendlyDate value = { inDays } /> ,
125+ < FriendlyDate value = { value } /> ,
109126 ) ;
110127 expect ( inDaysText . getByText ( 'in 2 days' ) ) . toBeTruthy ( ) ;
111128
129+ const inDaysEdgeCase = new Date ( 2020 , 5 , 28 ) ;
130+ const inDaysEdgeCaseTodayDate = new Date ( 2020 , 4 , 28 ) ;
131+ jest . setSystemTime ( inDaysEdgeCaseTodayDate ) ;
132+ value = inDaysEdgeCase . getTime ( ) ;
133+ const inDaysEdgeCaseText = renderWithIntl (
134+ < FriendlyDate value = { value } /> ,
135+ ) ;
136+ expect ( inDaysEdgeCaseText . getByText ( 'next month' ) ) . toBeTruthy ( ) ;
137+
138+ const inDaysMax = new Date ( 2020 , 5 , 4 ) ;
139+ const inDaysMaxTodayDate = new Date ( 2020 , 4 , 5 ) ;
140+ jest . setSystemTime ( inDaysMaxTodayDate ) ;
141+ value = inDaysMax . getTime ( ) ;
142+ const inDaysMaxText = renderWithIntl (
143+ < FriendlyDate value = { value } /> ,
144+ ) ;
145+ expect ( inDaysMaxText . getByText ( 'in 30 days' ) ) . toBeTruthy ( ) ;
146+
112147 const inMonths = new Date ( ) ;
113148 inMonths . setMonth ( inMonths . getMonth ( ) + 2 ) ;
149+ value = inMonths . getTime ( ) ;
114150 const inMonthsText = renderWithIntl (
115- < FriendlyDate value = { inMonths } /> ,
151+ < FriendlyDate value = { value } /> ,
116152 ) ;
117153 expect ( inMonthsText . getByText ( 'in 2 months' ) ) . toBeTruthy ( ) ;
118154
119155 const inYears = new Date ( ) ;
120156 inYears . setFullYear ( inYears . getFullYear ( ) + 2 ) ;
157+ value = inYears . getTime ( ) ;
121158 const inYearsText = renderWithIntl (
122- < FriendlyDate value = { inYears } /> ,
159+ < FriendlyDate value = { value } /> ,
123160 ) ;
124161 expect ( inYearsText . getByText ( 'in 2 years' ) ) . toBeTruthy ( ) ;
162+
163+ jest . useRealTimers ( ) ;
125164 } ) ;
126165} ) ;
0 commit comments