11import dayjs from "dayjs" ;
2+ import "dayjs/locale/fr" ;
23import customParseFormat from "dayjs/plugin/customParseFormat" ;
34import timezone from "dayjs/plugin/timezone" ;
45import utc from "dayjs/plugin/utc" ;
@@ -11,9 +12,78 @@ dayjs.extend(utc);
1112dayjs . extend ( timezone ) ;
1213dayjs . extend ( customParseFormat ) ;
1314
15+ const createInjectedDayjs = ( ) => {
16+ const fixedNow = "2030-06-15T12:34:56.000Z" ;
17+ const systemTimezone = "Asia/Tokyo" ;
18+ const injectedTimezone = ( (
19+ value ?: Parameters < typeof dayjs . tz > [ 0 ] ,
20+ timezone ?: string ,
21+ ) => dayjs . tz ( value ?? fixedNow , timezone ) ) as typeof dayjs . tz ;
22+ injectedTimezone . guess = ( ) => systemTimezone ;
23+ injectedTimezone . setDefault = dayjs . tz . setDefault ;
24+ const injectedUtc = ( ( ...args : Parameters < typeof dayjs . utc > ) =>
25+ args . length === 0
26+ ? dayjs . utc ( fixedNow )
27+ : dayjs . utc ( ...args ) ) as typeof dayjs . utc ;
28+ const factory = Object . assign (
29+ ( ...args : Parameters < typeof dayjs > ) =>
30+ args . length === 0 ? dayjs ( fixedNow ) : dayjs ( ...args ) ,
31+ { tz : injectedTimezone , utc : injectedUtc } ,
32+ ) ;
33+
34+ return { factory, fixedNow, systemTimezone } ;
35+ } ;
36+
1437describe ( "GIVEN a AdapterDayjs" , ( ) => {
1538 const adapter = new AdapterDayjs ( { locale : "en" } ) ;
1639
40+ describe ( "GIVEN an injected Day.js factory" , ( ) => {
41+ const { factory, fixedNow, systemTimezone } = createInjectedDayjs ( ) ;
42+ const injectedAdapter = new AdapterDayjs ( { locale : "fr" } , factory ) ;
43+
44+ it . each ( [
45+ "default" ,
46+ "system" ,
47+ ] as const ) ( "SHOULD use its timezone configuration for %s dates" , ( timezone ) => {
48+ const date = injectedAdapter . date ( "2025-01-05T00:00:00" , timezone ) ;
49+
50+ expect ( injectedAdapter . getTimezone ( date ) ) . toBe ( systemTimezone ) ;
51+ expect ( date . format ( "Z" ) ) . toBe ( "+09:00" ) ;
52+ } ) ;
53+
54+ it ( "SHOULD use its UTC factory" , ( ) => {
55+ const date = injectedAdapter . date ( "2025-01-05T00:00:00" , "UTC" ) ;
56+
57+ expect ( date . utcOffset ( ) ) . toBe ( 0 ) ;
58+ } ) ;
59+
60+ it ( "SHOULD use its timezone factory for an explicit IANA timezone" , ( ) => {
61+ const date = injectedAdapter . date (
62+ "2025-01-05T00:00:00" ,
63+ "America/New_York" ,
64+ ) ;
65+
66+ expect ( injectedAdapter . getTimezone ( date ) ) . toBe ( "America/New_York" ) ;
67+ expect ( date . format ( "Z" ) ) . toBe ( "-05:00" ) ;
68+ } ) ;
69+
70+ it ( "SHOULD apply the configured locale" , ( ) => {
71+ const date = injectedAdapter . date ( "2025-01-05T00:00:00" , "UTC" ) ;
72+
73+ expect ( injectedAdapter . format ( date , "MMMM" ) ) . toBe ( "janvier" ) ;
74+ } ) ;
75+
76+ it ( "SHOULD use its clock for today" , ( ) => {
77+ expect ( injectedAdapter . today ( "UTC" ) . format ( "YYYY-MM-DD HH:mm:ss" ) ) . toBe (
78+ "2030-06-15 00:00:00" ,
79+ ) ;
80+ } ) ;
81+
82+ it ( "SHOULD use its clock for now" , ( ) => {
83+ expect ( injectedAdapter . now ( "UTC" ) . toISOString ( ) ) . toBe ( fixedNow ) ;
84+ } ) ;
85+ } ) ;
86+
1787 it ( "SHOULD create a Dayjs date in the system timezone" , ( ) => {
1888 const date = adapter . date ( "2023-11-01" , "system" ) ;
1989 expect ( date . isValid ( ) ) . toBe ( true ) ;
0 commit comments