Skip to content

Commit f47fbe2

Browse files
MerlierMerlier
andauthored
Add prop optionsTestIDPrefix for test automation. Support testID key in data options (#148)
Co-authored-by: Merlier <[email protected]>
1 parent b8e8fb0 commit f47fbe2

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,14 @@ The selector accepts a specific format of data:
106106
[{ key: 5, label: 'Red Apples' }]
107107
```
108108

109-
Optionally provide a `component` key which overrides the default label text:
109+
Optionally provide a `component` key which overrides the default label text. Optionally provide a unique `testID` for each item:
110110
```javascript
111111
[{
112112
key: 5,
113113
label: 'Red Apples',
114-
component: <View style={{backgroundColor: 'red'}}><Text style={{color: 'white'}}>Red Apples custom component ☺</Text></View>
114+
// The next keys are optional --
115+
component: <View style={{backgroundColor: 'red'}}><Text style={{color: 'white'}}>Red Apples custom component ☺</Text></View>,
116+
testID: '5-red-apples'
115117
}]
116118
```
117119

@@ -135,7 +137,7 @@ return (
135137
### Props
136138
Prop | Type | Optional | Default | Description
137139
------------------- | -------- | -------- | ------------ | -----------
138-
`data` | array | No | [] | array of objects with a unique `key` and `label` to select in the modal. Optional `component` overrides label text.
140+
`data` | array | No | [] | array of objects with a unique `key` and `label` to select in the modal. Optional `component` overrides label text. Optional unique `testID` for each item.
139141
`onChange` | function | Yes | () => {} | callback function, when the users has selected an option
140142
`onModalOpen` | function | Yes | () => {} | callback function, when modal is opening
141143
`onModalClose` | function | Yes | (item) => {} | callback function, when modal is closing. Returns the selected item.
@@ -183,6 +185,7 @@ Prop | Type | Optional | Default | Description
183185
`selectedKey` | any | Yes | '' | Key of the item to be initially selected
184186
`enableShortPress` | bool | Yes | true | enables short press. This is regular touch behavior.
185187
`enableLongPress` | bool | Yes | false | enables long press. When true, `onModalOpen` returns `{longPress: true}`
188+
`optionsTestIDPrefix` | string | Yes | `'default'` | This prefixes each selectable option's testID prop if no testID keys are provided in `props.data` array objects. Default for each option's testID: 'default-\<optionLabel\>'
186189

187190
### Methods
188191

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const propTypes = {
7575
selectedKey: PropTypes.any,
7676
enableShortPress: PropTypes.bool,
7777
enableLongPress: PropTypes.bool,
78+
optionsTestIDPrefix: PropTypes.string,
7879
};
7980

8081
const defaultProps = {
@@ -126,6 +127,7 @@ const defaultProps = {
126127
selectedKey: '',
127128
enableShortPress: true,
128129
enableLongPress: false,
130+
optionsTestIDPrefix: 'default',
129131
};
130132

131133
export default class ModalSelector extends React.Component {
@@ -230,6 +232,7 @@ export default class ModalSelector extends React.Component {
230232
return (
231233
<TouchableOpacity
232234
key={this.props.keyExtractor(option)}
235+
testID={option.testID || this.props.optionsTestIDPrefix + '-' + optionLabel}
233236
onPress={() => this.onChange(option)}
234237
activeOpacity={this.props.touchableActiveOpacity}
235238
accessible={this.props.listItemAccessible}

0 commit comments

Comments
 (0)