Skip to content

Commit 00c7c5a

Browse files
rickhanloniigrabbou
authored andcommitted
Add e2e tests, bug fixes for testIDs (#22537)
Summary: This PR adds e2e tests for the Picker and DatePicker components. While writing these tests, I also found and fixed two bugs where we wern't passing the `testID` down to the native components, so detox couldn't look them up. This confirms what was mentioned by rotemmiz [here](wix/Detox#798 (comment)) Pull Request resolved: #22537 Reviewed By: cpojer Differential Revision: D13371307 Pulled By: rickhanlonii fbshipit-source-id: a4dfcdb5913645bceca0c7353328eeb9ad0f6558
1 parent 1fdfed5 commit 00c7c5a

File tree

6 files changed

+107
-2
lines changed

6 files changed

+107
-2
lines changed

Libraries/Components/DatePicker/DatePickerIOS.ios.js

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class DatePickerIOS extends React.Component<Props> {
148148
return (
149149
<View style={props.style}>
150150
<RCTDatePickerIOS
151+
testID={props.testID}
151152
ref={picker => {
152153
this._picker = picker;
153154
}}

Libraries/Components/Picker/PickerIOS.ios.js

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type RCTPickerIOSType = Class<
4747
onStartShouldSetResponder: () => boolean,
4848
selectedIndex: number,
4949
style?: ?TextStyleProp,
50+
testID?: ?string,
5051
|}>,
5152
>,
5253
>;
@@ -114,6 +115,7 @@ class PickerIOS extends React.Component<Props, State> {
114115
ref={picker => {
115116
this._picker = picker;
116117
}}
118+
testID={this.props.testID}
117119
style={[styles.pickerIOS, this.props.itemStyle]}
118120
items={this.state.items}
119121
selectedIndex={this.state.selectedIndex}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails oncall+react_native
8+
* @format
9+
*/
10+
11+
/* global element, by, expect */
12+
13+
describe('DatePickerIOS', () => {
14+
beforeAll(async () => {
15+
await element(by.id('explorer_search')).replaceText('<DatePickerIOS>');
16+
await element(
17+
by.label(
18+
'<DatePickerIOS> Select dates and times using the native UIDatePicker.',
19+
),
20+
).tap();
21+
});
22+
23+
afterAll(async () => {
24+
await element(by.label('Back')).tap();
25+
});
26+
27+
it('Should change indicator with datetime picker', async () => {
28+
const testID = 'date-and-time';
29+
const indicatorID = 'date-and-time-indicator';
30+
31+
const testElement = await element(
32+
by.type('UIPickerView').withAncestor(by.id(testID)),
33+
);
34+
const indicator = await element(by.id(indicatorID));
35+
36+
await expect(testElement).toBeVisible();
37+
await expect(indicator).toBeVisible();
38+
39+
await testElement.setColumnToValue(0, 'Dec 4');
40+
await testElement.setColumnToValue(1, '4');
41+
await testElement.setColumnToValue(2, '10');
42+
await testElement.setColumnToValue(3, 'AM');
43+
44+
await expect(indicator).toHaveText('12/4/2005 4:10 AM');
45+
});
46+
47+
it('Should change indicator with date-only picker', async () => {
48+
const testID = 'date-only';
49+
const indicatorID = 'date-and-time-indicator';
50+
51+
const testElement = await element(
52+
by.type('UIPickerView').withAncestor(by.id(testID)),
53+
);
54+
const indicator = await element(by.id(indicatorID));
55+
56+
await expect(testElement).toBeVisible();
57+
await expect(indicator).toBeVisible();
58+
59+
await testElement.setColumnToValue(0, 'November');
60+
await testElement.setColumnToValue(1, '3');
61+
await testElement.setColumnToValue(2, '2006');
62+
63+
await expect(indicator).toHaveText('11/3/2006 4:10 AM');
64+
});
65+
});

RNTester/e2e/__tests__/Picker-test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails oncall+react_native
8+
* @format
9+
*/
10+
11+
/* global element, by, expect */
12+
13+
describe('Picker', () => {
14+
beforeAll(async () => {
15+
await element(by.id('explorer_search')).replaceText('<Picker>');
16+
await element(
17+
by.label(
18+
'<Picker> Provides multiple options to choose from, using either a dropdown menu or a dialog.',
19+
),
20+
).tap();
21+
});
22+
23+
afterAll(async () => {
24+
await element(by.label('Back')).tap();
25+
});
26+
27+
it('should be selectable by ID', async () => {
28+
await expect(element(by.id('basic-picker'))).toBeVisible();
29+
});
30+
});

RNTester/js/DatePickerIOSExample.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ class DatePickerExample extends React.Component<
4646
return (
4747
<View>
4848
<WithLabel label="Value:">
49-
<Text>
49+
<Text testID="date-and-time-indicator">
5050
{this.state.date.toLocaleDateString() +
5151
' ' +
52-
this.state.date.toLocaleTimeString()}
52+
this.state.date.toLocaleTimeString([], {
53+
hour: '2-digit',
54+
minute: '2-digit',
55+
})}
5356
</Text>
5457
</WithLabel>
5558
<WithLabel label="Timezone:">
@@ -62,20 +65,23 @@ class DatePickerExample extends React.Component<
6265
</WithLabel>
6366
<Heading label="Date + time picker" />
6467
<DatePickerIOS
68+
testID="date-and-time"
6569
date={this.state.date}
6670
mode="datetime"
6771
timeZoneOffsetInMinutes={this.state.timeZoneOffsetInHours * 60}
6872
onDateChange={this.onDateChange}
6973
/>
7074
<Heading label="Date picker" />
7175
<DatePickerIOS
76+
testID="date-only"
7277
date={this.state.date}
7378
mode="date"
7479
timeZoneOffsetInMinutes={this.state.timeZoneOffsetInHours * 60}
7580
onDateChange={this.onDateChange}
7681
/>
7782
<Heading label="Time picker, 10-minute interval" />
7883
<DatePickerIOS
84+
testID="time-only"
7985
date={this.state.date}
8086
mode="time"
8187
timeZoneOffsetInMinutes={this.state.timeZoneOffsetInHours * 60}

RNTester/js/PickerExample.js

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class PickerExample extends React.Component<{}, $FlowFixMeState> {
3838
<RNTesterPage title="<Picker>">
3939
<RNTesterBlock title="Basic Picker">
4040
<Picker
41+
testID="basic-picker"
4142
style={styles.picker}
4243
selectedValue={this.state.selected1}
4344
onValueChange={this.onValueChange.bind(this, 'selected1')}>

0 commit comments

Comments
 (0)