@@ -79,6 +79,19 @@ describe('<DateInputController/>', () => {
7979 expect ( state ) . toEqual ( null ) ;
8080 } ) ;
8181
82+ it ( 'should default undefined value to date with value 0' , ( ) => {
83+ const instance = getInstance ( { value : date } ) ;
84+
85+ const nextProps = { ...instance . props , value : undefined } ;
86+
87+ const state = DateInputController . getDerivedStateFromProps (
88+ nextProps ,
89+ instance . state ,
90+ ) ;
91+
92+ expect ( state . props . value . getTime ( ) ) . toEqual ( new Date ( 0 ) . getTime ( ) ) ;
93+ } ) ;
94+
8295 it ( 'should return updated state if value prop has changed' , ( ) => {
8396 const instance = getInstance ( { value : date } ) ;
8497
@@ -99,7 +112,7 @@ describe('<DateInputController/>', () => {
99112 // $ExpectError cast nullable state as any
100113 ) : any ) ;
101114
102- const { value, day, second, ...rest } = state ;
115+ const { value, day, second, props : _props , ...rest } = state ;
103116
104117 expect ( day ) . toEqual ( nextDate . getDate ( ) ) ;
105118
@@ -132,11 +145,9 @@ describe('<DateInputController/>', () => {
132145 // $ExpectError cast nullable state as any
133146 ) : any ) ;
134147
135- const { value, min, hour, hourMin, ...rest } = state ;
136-
137- expect ( min ) . toBe ( min ) ;
148+ const { value, hour, hourMin, props, ...rest } = state ;
138149
139- expect ( value . getHours ( ) ) . toBe ( min . getHours ( ) ) ;
150+ expect ( value . getHours ( ) ) . toBe ( props . min . getHours ( ) ) ;
140151
141152 expect ( hour ) . toBe ( nextMin . getHours ( ) ) ;
142153
@@ -166,11 +177,9 @@ describe('<DateInputController/>', () => {
166177 // $ExpectError cast nullable state as any
167178 ) : any ) ;
168179
169- const { value, max , hour, hourMax, ...rest } = state ;
180+ const { value, hour, hourMax, props , ...rest } = state ;
170181
171- expect ( max ) . toBe ( max ) ;
172-
173- expect ( value . getHours ( ) ) . toBe ( max . getHours ( ) ) ;
182+ expect ( value . getHours ( ) ) . toBe ( props . max . getHours ( ) ) ;
174183
175184 expect ( hour ) . toBe ( nextMax . getHours ( ) ) ;
176185
@@ -192,28 +201,42 @@ describe('<DateInputController/>', () => {
192201 ) : any ) ;
193202
194203 const {
195- max,
196204 yearMax,
197205 monthMax,
198206 dayMax,
199207 hourMax,
200208 minuteMax,
201209 secondMax,
210+ props,
202211 ...rest
203212 } = state ;
204213
205- expect ( max ) . toBe ( date ) ;
214+ expect ( props . max ) . toBe ( date ) ;
206215
207- expect ( yearMax ) . toBe ( max . getFullYear ( ) ) ;
208- expect ( monthMax ) . toBe ( max . getMonth ( ) ) ;
209- expect ( dayMax ) . toBe ( max . getDate ( ) ) ;
210- expect ( hourMax ) . toBe ( max . getHours ( ) ) ;
211- expect ( minuteMax ) . toBe ( max . getMinutes ( ) ) ;
212- expect ( secondMax ) . toBe ( max . getSeconds ( ) ) ;
216+ expect ( yearMax ) . toBe ( props . max . getFullYear ( ) ) ;
217+ expect ( monthMax ) . toBe ( props . max . getMonth ( ) ) ;
218+ expect ( dayMax ) . toBe ( props . max . getDate ( ) ) ;
219+ expect ( hourMax ) . toBe ( props . max . getHours ( ) ) ;
220+ expect ( minuteMax ) . toBe ( props . max . getMinutes ( ) ) ;
221+ expect ( secondMax ) . toBe ( props . max . getSeconds ( ) ) ;
213222
214223 // it should leave remaining state value unchanged
215224 expect ( shallowIntersect ( rest , instance . state ) ) . toBe ( true ) ;
216225 } ) ;
226+
227+ it ( 'should not overwrite state change with props' , ( ) => {
228+ const instance = getInstance ( { value : date } ) ;
229+
230+ // Update internal state.
231+ instance . setFields ( { year : date . getFullYear ( ) + 1 } ) ;
232+
233+ const state = DateInputController . getDerivedStateFromProps (
234+ instance . props ,
235+ instance . state ,
236+ ) ;
237+
238+ expect ( state ) . toEqual ( null ) ;
239+ } ) ;
217240 } ) ;
218241 } ) ;
219242
0 commit comments