Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

Commit b9b3bf6

Browse files
author
Brandon Carroll
committed
3.0.0-beta.2
1 parent c91ee14 commit b9b3bf6

File tree

11 files changed

+69
-75
lines changed

11 files changed

+69
-75
lines changed

src/lib/__tests__/get-by-errors.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Text, TextInput, View } from 'react-native';
2+
import { Button, Text, TextInput, View } from 'react-native';
33
import cases from 'jest-in-case';
44

55
import { render } from '../';
@@ -83,6 +83,15 @@ cases(
8383
</View>
8484
),
8585
},
86+
getByTitle: {
87+
query: /his/,
88+
tree: (
89+
<View>
90+
<Button title="his" />
91+
<Button title="history" />
92+
</View>
93+
),
94+
},
8695
getByValue: {
8796
query: /his/,
8897
tree: (
@@ -174,6 +183,15 @@ cases(
174183
</View>
175184
),
176185
},
186+
queryByTitle: {
187+
query: /his/,
188+
tree: (
189+
<View>
190+
<Button title="his" />
191+
<Button title="history" />
192+
</View>
193+
),
194+
},
177195
queryByValue: {
178196
query: /his/,
179197
tree: (

src/lib/__tests__/misc.js

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import React from 'react';
2-
import { Text, View } from 'react-native';
2+
import { Picker, View } from 'react-native';
33

4-
import { render } from '../';
5-
import { queryByProp, queryByTestId } from '../';
4+
import { fireEvent, render, queryByProp, queryByTestId } from '../';
65

7-
// we used to use queryByProp internally, but we don't anymore. Some people
8-
// use it as an undocumented part of the API, so we'll keep it around.
96
test('queryByProp', () => {
107
const { container } = render(
118
<View>
@@ -22,33 +19,19 @@ test('queryByProp', () => {
2219
);
2320
});
2421

25-
test('toJSON stuff', () => {
26-
function Inner({ children, greeting }) {
27-
return (
28-
<Text>
29-
{greeting} {children}
30-
</Text>
31-
);
32-
}
33-
22+
test('picker', () => {
3423
function Wrapper() {
24+
const [value, setValue] = React.useState('js');
25+
3526
return (
36-
<View>
37-
<View>
38-
<Inner greeting="hello">world</Inner>
39-
</View>
40-
</View>
27+
<Picker selectedValue={value} onValueChange={itemValue => setValue(itemValue)}>
28+
<Picker.Item label="Java" value="java" />
29+
<Picker.Item label="JavaScript" value="js" />
30+
</Picker>
4131
);
4232
}
33+
const { findByValue, getByValue } = render(<Wrapper />);
4334

44-
const { baseElement } = render(<Wrapper />);
45-
46-
toJSON(baseElement);
35+
fireEvent.valueChange(getByValue('js'), 'java');
36+
expect(() => findByValue('js')).not.toThrow();
4737
});
48-
49-
function toJSON(node) {
50-
if (typeof node === 'string') return node;
51-
52-
if (node.type !== 'string') {
53-
}
54-
}

src/lib/__tests__/queries.find.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Image, Text, TextInput, View } from 'react-native';
2+
import { Button, Image, Text, TextInput, View } from 'react-native';
33

44
import { render } from '../.';
55

@@ -13,6 +13,7 @@ test('find asynchronously finds elements', async () => {
1313
findAllByPlaceholder,
1414
findAllByTestId,
1515
findAllByText,
16+
findAllByTitle,
1617
findAllByValue,
1718
findByA11yHint,
1819
findByA11yLabel,
@@ -22,12 +23,14 @@ test('find asynchronously finds elements', async () => {
2223
findByPlaceholder,
2324
findByTestId,
2425
findByText,
26+
findByTitle,
2527
findByValue,
2628
} = render(
2729
<View>
2830
<Text testID="test-id" accessibilityRole="text">
2931
test text content
3032
</Text>
33+
<Button title="button" />
3134
<TextInput placeholder="placeholder" />
3235
<TextInput value="value" />
3336
<TextInput accessibilityStates={['disabled']} />
@@ -56,6 +59,9 @@ test('find asynchronously finds elements', async () => {
5659
await expect(findByText('test text content')).resolves.toBeTruthy();
5760
await expect(findAllByText('test text content')).resolves.toHaveLength(1);
5861

62+
await expect(findByTitle('button')).resolves.toBeTruthy();
63+
await expect(findAllByTitle('button')).resolves.toHaveLength(1);
64+
5965
await expect(findByA11yRole('text')).resolves.toBeTruthy();
6066
await expect(findAllByA11yRole('text')).resolves.toHaveLength(1);
6167

@@ -76,6 +82,7 @@ test('find rejects when something cannot be found', async () => {
7682
findAllByPlaceholder,
7783
findAllByTestId,
7884
findAllByText,
85+
findAllByTitle,
7986
findAllByValue,
8087
findByA11yHint,
8188
findByA11yLabel,
@@ -85,6 +92,7 @@ test('find rejects when something cannot be found', async () => {
8592
findByPlaceholder,
8693
findByTestId,
8794
findByText,
95+
findByTitle,
8896
findByValue,
8997
} = render(<View />);
9098

@@ -112,6 +120,9 @@ test('find rejects when something cannot be found', async () => {
112120
await expect(findByText('x', qo, wo)).rejects.toThrow('x');
113121
await expect(findAllByText('x', qo, wo)).rejects.toThrow('x');
114122

123+
await expect(findByTitle('x', qo, wo)).rejects.toThrow('x');
124+
await expect(findAllByTitle('x', qo, wo)).rejects.toThrow('x');
125+
115126
await expect(findByValue('x', qo, wo)).rejects.toThrow('x');
116127
await expect(findAllByValue('x', qo, wo)).rejects.toThrow('x');
117128

src/lib/events.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const eventMap = {
2727
type: 'ChangeEvent',
2828
validTargets: ['TextInput'],
2929
},
30+
valueChange: {
31+
type: 'ChangeEvent',
32+
validTargets: ['Picker'],
33+
},
3034
contentSizeChange: {
3135
type: 'ContentSizeChangeEvent',
3236
validTargets: ['VirtualizedList', 'FlatList', 'SectionList', 'TextInput', 'ScrollView'],
@@ -51,7 +55,6 @@ const eventMap = {
5155
'DrawerLayoutAndroid',
5256
'Image',
5357
'Modal',
54-
'Picker',
5558
'RefreshControl',
5659
'ScrollView',
5760
'Switch',
@@ -182,19 +185,19 @@ function getEventHandlerName(key) {
182185
}
183186

184187
function validateElementType(list, element) {
185-
return (
186-
list.includes(element.type) ||
187-
list.includes(element.type.name) ||
188-
list.includes(element.type.displayName)
189-
);
188+
return list.includes(element.type) || list.includes(element.type.displayName);
190189
}
191190

192191
function isValidTarget(element, event) {
193192
return event.validTargets.length ? validateElementType(event.validTargets, element) : true;
194193
}
195194

196195
function isDisabled(element) {
197-
return element.props.disabled && validateElementType(disableableElements, element);
196+
const { accessibilityStates = [], disabled } = element.props;
197+
const propDisabled = disabled;
198+
const stateDisabled = Array.from(accessibilityStates).includes('disabled');
199+
200+
return (propDisabled || stateDisabled) && validateElementType(disableableElements, element);
198201
}
199202

200203
function findEventHandler(element, event) {

src/lib/queries/text.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,7 @@ function queryAllByText(
1717
const matcher = exact ? matches : fuzzyMatches;
1818
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
1919

20-
const baseArray = ['Text', 'TextInput'].reduce(
21-
(accumulator, currentValue) => [
22-
...accumulator,
23-
...baseElement.findAll(n => n.type === currentValue),
24-
],
25-
[],
26-
);
27-
28-
return baseArray
20+
return Array.from(baseElement.findAll(n => ['TextInput', 'Text'].includes(n.type)))
2921
.filter(node => matcher(getNodeText(node), node, text, matchNormalizer))
3022
.map(proxyUnsafeProperties);
3123
}

src/lib/queries/value.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ function queryAllByValue(
99
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
1010

1111
const baseElement = getBaseElement(container);
12-
return Array.from(baseElement.findAll(n => n.type === 'TextInput')).filter(node =>
13-
matcher(node.props.value, node, value, matchNormalizer),
12+
return Array.from(baseElement.findAll(n => ['TextInput', 'Picker'].includes(n.type))).filter(
13+
node => {
14+
if (node.type === 'Picker') {
15+
return matcher(node.props.selectedValue, node, value, matchNormalizer);
16+
}
17+
18+
return matcher(node.props.value, node, value, matchNormalizer);
19+
},
1420
);
1521
}
1622

src/preset/core-components.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
const CoreComponents = [
22
'ActivityIndicator',
3-
'FAKE',
43
'Button',
54
'DrawerLayoutAndroid',
65
'Image',
76
'Modal',
8-
'Picker',
97
'RefreshControl',
108
'ScrollView',
119
'Switch',
@@ -23,7 +21,7 @@ function setCoreComponents(components) {
2321
}
2422

2523
function getCoreComponents() {
26-
return CoreComponents;
24+
return [...CoreComponents, 'Picker'];
2725
}
2826

2927
export { getCoreComponents, setCoreComponents };

src/preset/mock-component.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ getCoreComponents().forEach(component => {
5757
}
5858
});
5959

60+
jest.doMock('Picker', () => {
61+
const Picker = mockComponent('Picker');
62+
Picker.Item = ({ children, ...props }) => React.createElement('Picker.Item', props, children);
63+
return Picker;
64+
});
65+
6066
export { mockComponent };

src/preset/require-native-component.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/preset/setup.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ require('./unmock');
44
// Mock native modules
55
require('./native-modules');
66

7-
// Mock requireNativeComponent
8-
require('./require-native-component');
9-
107
// Mock lean core components
118
require('./mock-component');

src/preset/unmock.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,3 @@ getCoreComponents().forEach(component => {
88
// do nothing if we can't
99
}
1010
});
11-
12-
jest.dontMock('requireNativeComponent');

0 commit comments

Comments
 (0)