我在我的react-native项目中设置了一些Jest测试,所以我正在为我的组件设置快照。其中一个涉及DatePicker
(特别是时间),因此我需要模拟一致的Date()
以使快照匹配。
DatePickerIOS的docs表示我在Date
组件(Schedule
)中提供了日期道具中的new Date()
个对象。
我已经尝试过解决这个问题:
const constantDate = new Date('Tue Jan 09 2018 14:04:12 GMT-0330 (NST)')
Date = class extends Date {
constructor() {
super();
return constantDate
}
}
&安培;
const DATE_TO_USE = new Date('Tue Jan 09 2018 14:04:12 GMT-0330 (NST)');
const _Date = Date;
global.Date = jest.fn(() => DATE_TO_USE);
global.Date.UTC = _Date.UTC;
global.Date.parse = _Date.parse;
global.Date.now = _Date.now;
&安培;
const mockedDate = new Date(2017, 11, 10)
const originalDate = Date
global.Date = jest.fn(() => mockedDate)
global.Date.setDate = originalDate.setDate
正确创造&检查快照相互之间,但我收到以下警告:
Warning: Failed prop type: Invalid prop `date` of type `Date` supplied to `DatePickerIOS`, expected instance of `_class`.
in DatePickerIOS (created by DatePicker)
in DatePicker (created by TimePicker)
in TimePicker (created by Schedule)
in View (created by View)
in View (created by Schedule)
in View (created by View)
in View (created by Section)
in Section (created by Schedule)
in View (created by View)
in View (created by Schedule)
in View (created by View)
in View (created by AnimatedComponent)
没有模拟,快照中的时间(显然)不匹配,但我没有得到道具的警告。我是否需要实例化每个Date()
对象方法?这个警告的原因是什么?有可能解决它吗?