forked from react-native-datetimepicker/datetimepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userlandTestExamples.test.js
109 lines (90 loc) · 3.01 KB
/
userlandTestExamples.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @format
* @flow strict-local
*/
import React, {useState} from 'react';
import {Text, Button} from 'react-native';
// in your code, import from '@react-native-community/datetimepicker'
import DateTimePicker from '../src/index';
// $FlowExpectedError: complains about import path
import {DateTimePickerAndroid} from '../src/DateTimePickerAndroid.android';
// $FlowExpectedError: module treated as any
import {render, fireEvent, waitFor} from '@testing-library/react-native';
import {createDateTimeSetEvtParams} from '../src/index';
import {mockAndroidDialogDateChange, mockAndroidDialogDismissal} from '../jest';
function TestAppWithComponent() {
const [date, setDate] = React.useState<?Date>();
return (
<>
<DateTimePicker
value={new Date(0)}
onChange={(evt, selectedDate) => {
setDate(selectedDate);
}}
/>
<Text>{String(date?.toLocaleString())}</Text>
<Text>{String((date?.getTime() ?? 0) / 1000)}</Text>
</>
);
}
const AppWithImperativePicker = () => {
const [date, setDate] = useState(new Date(0));
const onChange = (event, selectedDate) => {
selectedDate && setDate(selectedDate);
};
const showMode = (currentMode) => {
DateTimePickerAndroid.open({
value: date,
onChange,
display: 'default',
mode: currentMode,
is24Hour: true,
});
};
const showDatepicker = () => {
showMode('date');
};
const time = date?.getTime();
return (
<>
<Button onPress={showDatepicker} title="Show date picker!" />
<Text>selected: {date.toLocaleString()}</Text>
<Text>{String(time)}</Text>
</>
);
};
const renderPickerComponent = async () => {
const utils = render(<TestAppWithComponent />);
await waitFor(() => utils.UNSAFE_getByType(DateTimePicker));
return utils;
};
describe('userland tests', () => {
it("rendering DateTimePicker and calling fireEvent triggers the picker's onChange callback (platform-agnostic)", async () => {
const date = new Date(156e10);
const {UNSAFE_getByType, getByText} = await renderPickerComponent();
fireEvent(
UNSAFE_getByType(DateTimePicker),
'onChange',
...createDateTimeSetEvtParams(date, 0),
);
getByText('1560000000');
});
describe('when using android imperative api, we can simulate', () => {
it('the date being changed', async () => {
const {getByText} = render(<AppWithImperativePicker />);
const newSelectedDate = new Date(0);
const february = 1;
newSelectedDate.setFullYear(2022, february, 1);
const nowAsString = newSelectedDate.getTime().toString();
mockAndroidDialogDateChange(newSelectedDate);
fireEvent.press(getByText('Show date picker!'));
await waitFor(() => getByText(nowAsString));
});
it('the picker being dismissed', async () => {
const {getByText} = render(<AppWithImperativePicker />);
mockAndroidDialogDismissal();
fireEvent.press(getByText('Show date picker!'));
await waitFor(() => getByText('0'));
});
});
});