@@ -72,6 +72,190 @@ test('now - custom value', function (assert) {
7272 }
7373} ) ;
7474
75+ test ( 'custom default date parts' , function ( assert ) {
76+ var oldNow = moment . now ,
77+ oldGetDefaultDateParts = moment . _getDefaultDateParts ,
78+ oldLocale = moment . locale ( ) ,
79+ customTime = Date . UTC ( 2025 , 0 , 2 , 1 ) ,
80+ calls = 0 ,
81+ weekCalls = 0 ,
82+ receivedConfig ,
83+ receivedNow ,
84+ receivedThis ;
85+
86+ moment . now = function ( ) {
87+ return customTime ;
88+ } ;
89+ moment . locale ( 'en' ) ;
90+ moment . _getDefaultDateParts = function ( config , now , forWeek ) {
91+ calls ++ ;
92+ receivedConfig = config ;
93+ receivedNow = now ;
94+ receivedThis = this ;
95+ if ( forWeek ) {
96+ weekCalls ++ ;
97+ }
98+ if ( ! config . _useUTC ) {
99+ return [ 2024 , 11 , 25 ] ;
100+ }
101+ return [ 2030 , 6 , 1 ] ;
102+ } ;
103+
104+ try {
105+ assert . equal (
106+ moment ( '01:23' , 'HH:mm' ) . format ( 'YYYY-MM-DD HH:mm' ) ,
107+ '2024-12-25 01:23' ,
108+ 'custom date parts should supply omitted local fields'
109+ ) ;
110+ assert . equal ( receivedNow , customTime , 'should receive the sampled now' ) ;
111+ assert . strictEqual (
112+ receivedThis ,
113+ moment ,
114+ 'should use Moment as the receiver'
115+ ) ;
116+ assert . equal (
117+ receivedConfig . _useUTC ,
118+ false ,
119+ 'should receive the parsing config'
120+ ) ;
121+ assert . equal (
122+ moment ( '1' , 'e' ) . format ( 'YYYY-MM-DD' ) ,
123+ '2024-12-23' ,
124+ 'locale week defaults should use the custom date parts'
125+ ) ;
126+ assert . equal (
127+ moment ( '1' , 'E' ) . format ( 'YYYY-MM-DD' ) ,
128+ '2024-01-01' ,
129+ 'ISO week-year defaults should use the custom date parts'
130+ ) ;
131+ assert . equal (
132+ moment . utc ( '01:23' , 'HH:mm' ) . format ( 'YYYY-MM-DD HH:mm' ) ,
133+ '2030-07-01 01:23' ,
134+ 'custom date parts should supply omitted UTC fields'
135+ ) ;
136+ assert . equal (
137+ moment . utc ( '1' , 'e' ) . format ( 'YYYY-MM-DD' ) ,
138+ '2030-07-01' ,
139+ 'UTC locale week defaults should use the custom date parts'
140+ ) ;
141+ assert . equal (
142+ moment . utc ( '1' , 'E' ) . format ( 'YYYY-MM-DD' ) ,
143+ '2029-12-31' ,
144+ 'UTC ISO week-year defaults should use the custom date parts'
145+ ) ;
146+ assert . equal (
147+ moment ( '1 -08:00' , 'e Z' ) . utc ( ) . format ( 'YYYY-MM-DD' ) ,
148+ '2030-07-01' ,
149+ 'explicit-offset week defaults should use the custom date parts'
150+ ) ;
151+ assert . equal (
152+ moment ( ) . valueOf ( ) ,
153+ customTime ,
154+ 'no-input construction should retain current-instant semantics'
155+ ) ;
156+ assert . equal (
157+ calls ,
158+ 12 ,
159+ 'no-input construction should not request defaults'
160+ ) ;
161+ assert . equal ( weekCalls , 5 , 'week parsing should identify hook calls' ) ;
162+ } finally {
163+ moment . now = oldNow ;
164+ moment . _getDefaultDateParts = oldGetDefaultDateParts ;
165+ moment . locale ( oldLocale ) ;
166+ }
167+ } ) ;
168+
169+ test ( 'default week date preserves updateOffset' , function ( assert ) {
170+ var oldNow = moment . now ,
171+ oldGetDefaultDateParts = moment . _getDefaultDateParts ,
172+ oldUpdateOffset = moment . updateOffset ,
173+ oldLocale = moment . locale ( ) ,
174+ nowCalls = 0 ,
175+ calls = 0 ;
176+
177+ moment . now = function ( ) {
178+ nowCalls ++ ;
179+ return Date . UTC ( 2025 , 0 , 5 ) ;
180+ } ;
181+ moment . locale ( 'en' ) ;
182+ moment . _getDefaultDateParts = function ( config , now ) {
183+ return oldGetDefaultDateParts . call ( this , config , now ) ;
184+ } ;
185+ moment . updateOffset = function ( mom ) {
186+ calls ++ ;
187+ if ( calls === 1 ) {
188+ mom . _d = new Date ( 2024 , 11 , 25 ) ;
189+ }
190+ } ;
191+
192+ try {
193+ assert . equal (
194+ moment . utc ( '1' , 'e' ) . format ( 'YYYY-MM-DD' ) ,
195+ '2024-12-23' ,
196+ 'two-argument wrappers should retain UTC updateOffset behavior'
197+ ) ;
198+ assert . ok ( calls > 0 , 'should create an offset-adjusted current moment' ) ;
199+
200+ calls = 0 ;
201+ assert . equal (
202+ moment ( '1 -08:00' , 'e Z' ) . utc ( ) . format ( 'YYYY-MM-DD' ) ,
203+ '2024-12-23' ,
204+ 'two-argument wrappers should retain offset updateOffset behavior'
205+ ) ;
206+ assert . ok ( calls > 0 , 'should adjust the offset current moment' ) ;
207+ assert . equal ( nowCalls , 2 , 'should sample now once per parse' ) ;
208+ } finally {
209+ moment . now = oldNow ;
210+ moment . _getDefaultDateParts = oldGetDefaultDateParts ;
211+ moment . updateOffset = oldUpdateOffset ;
212+ moment . locale ( oldLocale ) ;
213+ }
214+ } ) ;
215+
216+ test ( 'format arrays share one current instant' , function ( assert ) {
217+ var oldNow = moment . now ,
218+ calls = 0 ;
219+
220+ moment . now = function ( ) {
221+ calls ++ ;
222+ return Date . UTC ( 2025 , 0 , 5 ) ;
223+ } ;
224+
225+ try {
226+ moment ( '01:23' , [ 'HH:mm' , 'H:mm' ] ) ;
227+ assert . equal ( calls , 1 , 'should sample now once for every candidate' ) ;
228+ } finally {
229+ moment . now = oldNow ;
230+ }
231+ } ) ;
232+
233+ test ( 'complete dates with day of year do not need current date' , function ( assert ) {
234+ var oldNow = moment . now ,
235+ calls = 0 ;
236+
237+ moment . now = function ( ) {
238+ calls ++ ;
239+ return Date . UTC ( 2025 , 0 , 5 ) ;
240+ } ;
241+
242+ try {
243+ assert . equal (
244+ moment ( '2020-01-02 123' , 'YYYY-MM-DD DDD' ) . format ( 'YYYY-MM-DD' ) ,
245+ '2020-05-02' ,
246+ 'day of year should take precedence over the calendar date'
247+ ) ;
248+ assert . equal (
249+ moment ( '2020-01-02 123' , [ 'YYYY-MM-DD DDDD' ] ) . format ( 'YYYY-MM-DD' ) ,
250+ '2020-05-02' ,
251+ 'format arrays should accept a complete date with day of year'
252+ ) ;
253+ assert . equal ( calls , 0 , 'complete dates should not sample now' ) ;
254+ } finally {
255+ moment . now = oldNow ;
256+ }
257+ } ) ;
258+
75259test ( 'empty object, empty array' , function ( assert ) {
76260 function assertIsNow ( gen , msg ) {
77261 var before = + new Date ( ) ,
0 commit comments