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,76 @@ 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 ? dayjs . utc ( fixedNow ) : dayjs . utc ( ...args ) ) as typeof dayjs . utc ;
26+ const factory = Object . assign (
27+ ( ...args : Parameters < typeof dayjs > ) =>
28+ args . length === 0 ? dayjs ( fixedNow ) : dayjs ( ...args ) ,
29+ { tz : injectedTimezone , utc : injectedUtc } ,
30+ ) ;
31+
32+ return { factory, fixedNow, systemTimezone } ;
33+ } ;
34+
1435describe ( "GIVEN a AdapterDayjs" , ( ) => {
1536 const adapter = new AdapterDayjs ( { locale : "en" } ) ;
1637
38+ describe ( "GIVEN an injected Day.js factory" , ( ) => {
39+ const { factory, fixedNow, systemTimezone } = createInjectedDayjs ( ) ;
40+ const injectedAdapter = new AdapterDayjs ( { locale : "fr" } , factory ) ;
41+
42+ it . each ( [ "default" , "system" ] as const ) (
43+ "SHOULD use its timezone configuration for %s dates" ,
44+ ( timezone ) => {
45+ const date = injectedAdapter . date ( "2025-01-05T00:00:00" , timezone ) ;
46+
47+ expect ( injectedAdapter . getTimezone ( date ) ) . toBe ( systemTimezone ) ;
48+ expect ( date . format ( "Z" ) ) . toBe ( "+09:00" ) ;
49+ } ,
50+ ) ;
51+
52+ it ( "SHOULD use its UTC factory" , ( ) => {
53+ const date = injectedAdapter . date ( "2025-01-05T00:00:00" , "UTC" ) ;
54+
55+ expect ( date . utcOffset ( ) ) . toBe ( 0 ) ;
56+ } ) ;
57+
58+ it ( "SHOULD use its timezone factory for an explicit IANA timezone" , ( ) => {
59+ const date = injectedAdapter . date (
60+ "2025-01-05T00:00:00" ,
61+ "America/New_York" ,
62+ ) ;
63+
64+ expect ( injectedAdapter . getTimezone ( date ) ) . toBe ( "America/New_York" ) ;
65+ expect ( date . format ( "Z" ) ) . toBe ( "-05:00" ) ;
66+ } ) ;
67+
68+ it ( "SHOULD apply the configured locale" , ( ) => {
69+ const date = injectedAdapter . date ( "2025-01-05T00:00:00" , "UTC" ) ;
70+
71+ expect ( injectedAdapter . format ( date , "MMMM" ) ) . toBe ( "janvier" ) ;
72+ } ) ;
73+
74+ it ( "SHOULD use its clock for today" , ( ) => {
75+ expect ( injectedAdapter . today ( "UTC" ) . format ( "YYYY-MM-DD HH:mm:ss" ) ) . toBe (
76+ "2030-06-15 00:00:00" ,
77+ ) ;
78+ } ) ;
79+
80+ it ( "SHOULD use its clock for now" , ( ) => {
81+ expect ( injectedAdapter . now ( "UTC" ) . toISOString ( ) ) . toBe ( fixedNow ) ;
82+ } ) ;
83+ } ) ;
84+
1785 it ( "SHOULD create a Dayjs date in the system timezone" , ( ) => {
1886 const date = adapter . date ( "2023-11-01" , "system" ) ;
1987 expect ( date . isValid ( ) ) . toBe ( true ) ;
0 commit comments