11import React from 'react' ;
22import renderer from 'react-test-renderer' ;
3+ import Dayjs from 'dayjs' ;
4+ import calendar from 'dayjs/plugin/calendar' ;
35import { cleanup , render } from '@testing-library/react' ;
46import '@testing-library/jest-dom' ;
57
68import DateSeparator from '../DateSeparator' ;
9+ import { TranslationContext } from '../../../context' ;
10+
11+ Dayjs . extend ( calendar ) ;
712
813afterEach ( cleanup ) ; // eslint-disable-line
914
1015// this changes every time tests are run,
1116// but by mocking the actual renderers tests are still deterministic
1217const now = new Date ( ) ;
1318
19+ const withContext = ( props ) => {
20+ const t = jest . fn ( ( key ) => key ) ;
21+ const tDateTimeParser = jest . fn ( ( input ) => Dayjs ( input ) ) ;
22+ const Component = (
23+ < TranslationContext . Provider value = { { t, tDateTimeParser } } >
24+ < DateSeparator { ...props } />
25+ </ TranslationContext . Provider >
26+ ) ;
27+
28+ return { Component, t, tDateTimeParser } ;
29+ } ;
30+
1431describe ( 'DateSeparator' , ( ) => {
1532 it ( 'should use formatDate if it is provided' , ( ) => {
1633 const { queryByText } = render (
@@ -20,9 +37,42 @@ describe('DateSeparator', () => {
2037 expect ( queryByText ( 'the date' ) ) . toBeInTheDocument ( ) ;
2138 } ) ;
2239
23- it . todo (
24- "should use tDateTimeParser's calendar method to format dates if formatDate prop is not specified" ,
25- ) ;
40+ it ( 'should render New text if unread prop is true' , ( ) => {
41+ const { Component, t } = withContext ( { date : now , unread : true } ) ;
42+ const { queryByText } = render ( Component ) ;
43+
44+ expect ( queryByText ( 'New' ) ) . toBeInTheDocument ( ) ;
45+ expect ( t ) . toHaveBeenCalledWith ( 'New' ) ;
46+ } ) ;
47+
48+ it ( 'should render properly for unread' , ( ) => {
49+ const { Component } = withContext ( { date : now , unread : true } ) ;
50+ const tree = renderer . create ( Component ) . toJSON ( ) ;
51+ expect ( tree ) . toMatchInlineSnapshot ( `
52+ <div
53+ className="str-chat__date-separator"
54+ >
55+ <hr
56+ className="str-chat__date-separator-line"
57+ />
58+ <div
59+ className="str-chat__date-separator-date"
60+ >
61+ New
62+ </div>
63+ </div>
64+ ` ) ;
65+ } ) ;
66+
67+ it ( "should use tDateTimeParser's calendar method by default" , ( ) => {
68+ const { Component, tDateTimeParser } = withContext ( { date : now } ) ;
69+ const { queryByText } = render ( Component ) ;
70+
71+ expect ( tDateTimeParser ) . toHaveBeenCalledWith ( now . toISOString ( ) ) ;
72+ expect (
73+ queryByText ( Dayjs ( now . toISOString ( ) ) . calendar ( ) ) ,
74+ ) . toBeInTheDocument ( ) ;
75+ } ) ;
2676
2777 describe ( 'Position prop' , ( ) => {
2878 const renderWithPosition = ( position ) => (
0 commit comments