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

Commit 24c97fe

Browse files
author
Brandon Carroll
committed
fix(queries): fix boolean values for getBy* queries
56
1 parent c7348ec commit 24c97fe

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

src/__tests__/misc.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Button, Picker, Text, View } from 'react-native';
2+
import { Button, Picker, Switch, Text, View } from 'react-native';
33
import { toMatchDiffSnapshot } from 'snapshot-diff';
44

55
import { cleanup, fireEvent, render } from '../';
@@ -83,3 +83,9 @@ test('it finds only valid parents', () => {
8383
expect(getByText('hey').parentNode.parentNode.props.testID).toBe('view');
8484
expect(baseElement.parentNode).toBeNull();
8585
});
86+
87+
test('it finds <Switch /> by value={false}', () => {
88+
const { getByDisplayValue } = render(<Switch value={false} />);
89+
90+
getByDisplayValue(false);
91+
});

src/lib/__tests__/matches.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ test('matchers return false if text to match is not a string', () => {
2222
expect(matches(null, node, 'ABC', normalizer)).toBe(false);
2323
expect(fuzzyMatches(null, node, 'ABC', normalizer)).toBe(false);
2424
});
25+
26+
test('matchers return true if text to match is a boolean', () => {
27+
expect(matches(true, node, true, normalizer)).toBe(true);
28+
expect(fuzzyMatches(1, node, true, normalizer)).toBe(true);
29+
});

src/lib/__tests__/misc.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { View } from 'react-native';
2+
import { Picker, Switch, View } from 'react-native';
33

44
import { render, queryByProp, queryByTestId, cleanup } from '../../';
55

@@ -20,3 +20,16 @@ test('queryByProp', () => {
2020
/multiple elements/,
2121
);
2222
});
23+
24+
it('should render test', () => {
25+
const { getByDisplayValue } = render(
26+
<View>
27+
<Picker selectedValue="fr">
28+
<Picker.Item value="fr" label="French" />
29+
</Picker>
30+
<Switch value={true} />
31+
</View>,
32+
);
33+
34+
expect(getByDisplayValue(true)).toBeTruthy();
35+
});

src/lib/matches.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
function fuzzyMatches(textToMatch, node, matcher, normalizer) {
2-
if (typeof textToMatch !== 'string') {
2+
if (typeof matcher === 'boolean') {
33
return textToMatch == matcher;
44
}
55

6+
if (!textToMatch) {
7+
return false;
8+
}
9+
610
const normalizedText = normalizer(textToMatch);
711
if (typeof matcher === 'string') {
812
return normalizedText.toLowerCase().includes(matcher.toLowerCase());
@@ -14,10 +18,14 @@ function fuzzyMatches(textToMatch, node, matcher, normalizer) {
1418
}
1519

1620
function matches(textToMatch, node, matcher, normalizer) {
17-
if (typeof textToMatch !== 'string') {
21+
if (typeof matcher === 'boolean') {
1822
return textToMatch === matcher;
1923
}
2024

25+
if (!textToMatch) {
26+
return false;
27+
}
28+
2129
const normalizedText = normalizer(textToMatch);
2230
if (typeof matcher === 'string') {
2331
return normalizedText === matcher;

0 commit comments

Comments
 (0)