-
Notifications
You must be signed in to change notification settings - Fork 71
[DatePicker] Date, locale mocking error #337
Comments
I think it will be solved by mocking as like jest
.spyOn(Date, 'now')
.mockImplementation(() => new Date('2020-09-01').valueOf()) |
Because date values are used a lot in test cases, I set a standard date instead of using spyOn. |
@oh3vci It is not solved yet. Snapshots change everyday. |
Related issues:
I finally mocked const standardDate = new Date('2020-09-01');
const _Date = Date;
// @ts-ignore
global.Date = jest.fn(() => standardDate);
global.Date.UTC = _Date.UTC;
global.Date.parse = _Date.parse;
global.Date.now = _Date.now; But I couldn't mocking Already tried
const localeOption = {
year: '2-digit',
month: '2-digit',
day: '2-digit',
};
const dateSpy = jest.spyOn(global.Date.prototype, 'toLocaleString')
.mockImplementation(() => new Date().toLocaleString('UTC', localeOption));
|
I think it's good to exclude snapshots from the Datepicker test. |
@JeffGuKang How about we add |
But I couldn't control For example, Weekdays would be 'Mon', 'Tue'... in |
We can except validation from specific area of snapshots through |
@JeffGuKang I suggest we use Here's the relevant documentation : https://formatjs.io/docs/guides/testing#jest import React from 'react'
import renderer from 'react-test-renderer'
import {IntlProvider} from 'react-intl'
const createComponentWithIntl = (children, props = {locale: 'en'}) => {
return renderer.create(<IntlProvider {...props}>{children}</IntlProvider>)
}
export default createComponentWithIntl import React from 'react'
import createComponentWithIntl from '../../utils/createComponentWithIntl'
import AppMain from '../AppMain'
test('app main should be rendered', () => {
const component = createComponentWithIntl(<AppMain />)
let tree = component.toJSON()
expect(tree).toMatchSnapshot()
tree.props.onClick()
tree = component.toJSON()
expect(tree).toMatchSnapshot()
}) |
As adding locale prop to component, I implemented already this approach without |
@oh3vci It looks good to me. So I guess we just need to test |
I think timezone issue is already solved problem, |
@oh3vci Did you try locale props with 'kr-KR' or other timezones when you test? Did it work as like '월', '화'...? |
@JeffGuKang I tried setting |
Isn't it 'ko-KR', not 'kr-KR'?
I guess it seems to be another bug of android or storybook. it works well in iOS. |
@oh3vci How about testing environment? Testing is not work as expectation with locale control or mocking. |
@JeffGuKang <DatePicker selectedDate={standardDate} locale={'ko-KR'} /> then this, <DatePicker selectedDate={standardDate} locale={'en-US'} /> Here's the snapshot diff: - Snapshot - 7
+ Received + 7
@@ -247,11 +247,11 @@
"color": "#ff424c",
},
]
}
>
- 일
+ S
</Text>
</View>
<View
style={
Array [
@@ -279,11 +279,11 @@
"color": "#565656",
},
]
}
>
- 월
+ M
</Text>
</View>
<View
style={
Array [
@@ -311,11 +311,11 @@
"color": "#565656",
},
]
}
>
- 화
+ T
</Text>
</View>
<View
style={
Array [
@@ -343,11 +343,11 @@
"color": "#565656",
},
]
}
>
- 수
+ W
</Text>
</View>
<View
style={
Array [
@@ -375,11 +375,11 @@
"color": "#565656",
},
]
}
>
- 목
+ T
</Text>
</View>
<View
style={
Array [
@@ -407,11 +407,11 @@
"color": "#565656",
},
]
}
>
- 금
+ F
</Text>
</View>
<View
style={
Array [
@@ -439,11 +439,11 @@
"color": "#565656",
},
]
}
>
- 토
+ S
</Text>
</View>
</View>
<ScrollView
data={
|
@0916dhkim Cool. We can use snapshot test again for DatePicker. |
Maybe, this is related issue in android. |
Describe the bug
Tests always fail due to date issues.
To Reproduce
Steps to reproduce the behavior:
npx jest DatePicker --coverage
Expected behavior
Mock date to specific day for test.
Screenshots
Desktop (please complete the following information):
X
Smartphone (please complete the following information):
X
Additional context
X
The text was updated successfully, but these errors were encountered: