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

Commit b42b38b

Browse files
committed
feat: export within helper from library
1 parent 3012b5d commit b42b38b

8 files changed

+41
-22
lines changed

src/__tests__/queries.find.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ test('find rejects when something cannot be found', async () => {
120120
});
121121

122122
test('actually works with async code', async () => {
123-
const { findByTestId, rerender, container } = render(<View />);
123+
const { findByTestId, rerender } = render(<View />);
124124
setTimeout(() => rerender(<Text testID="text">correct tree</Text>), 20);
125-
await expect(findByTestId('text', {}, { container })).resolves.toBeTruthy();
125+
await expect(findByTestId('text', {})).resolves.toBeTruthy();
126126
});

src/__tests__/wait-for-element.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ test('waits for element to appear in the document', async () => {
1313
});
1414

1515
test('waits for element to appear in a specified container', async () => {
16-
const { rerender, container, getByTestId } = render(<View />);
17-
const promise = waitForElement(() => getByTestId('test'), { container });
16+
const { rerender, getByTestId } = render(<View />);
17+
const promise = waitForElement(() => getByTestId('test'));
1818
setTimeout(() => rerender(<View testID="test" />));
1919
const element = await promise;
2020
expect(element).toBeTruthy();
@@ -44,7 +44,7 @@ test('can specify our own timeout time', async () => {
4444
});
4545

4646
test('throws last thrown error', async () => {
47-
const { rerender, container } = render(<View />);
47+
const { rerender } = render(<View />);
4848
let error;
4949
setTimeout(() => {
5050
error = new Error('first error');
@@ -58,14 +58,14 @@ test('throws last thrown error', async () => {
5858
() => {
5959
throw error;
6060
},
61-
{ container, timeout: 50 },
61+
{ timeout: 50 },
6262
);
6363
await expect(promise).rejects.toThrow(error);
6464
});
6565

6666
test('waits until callback does not return null', async () => {
67-
const { rerender, container, queryByTestId } = render(<View />);
68-
const promise = waitForElement(() => queryByTestId('text'), { container });
67+
const { rerender, queryByTestId } = render(<View />);
68+
const promise = waitForElement(() => queryByTestId('text'));
6969
rerender(<View testID="text" />);
7070
const element = await promise;
7171
expect(element).toBeTruthy();

src/__tests__/within.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import { Button, View } from 'react-native';
3+
4+
import { render, within } from '../';
5+
6+
test('it works when scoping to a smaller set of elements', () => {
7+
const { getByTestId } = render(
8+
<View>
9+
<View testID="filter-box">
10+
<Button testID="search-button" onPress={jest.fn()} title="press me" />
11+
</View>
12+
</View>,
13+
);
14+
15+
within(getByTestId('filter-box')).getByTestId('search-button');
16+
});

src/get-queries-for-element.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ function getQueriesForElement(container, queries = defaultQueries) {
88
}, {});
99
}
1010

11-
export { getQueriesForElement };
11+
export { getQueriesForElement, getQueriesForElement as within };

src/queries.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getNodeText } from './get-node-text';
55
import { waitForElement } from './wait-for-element';
66
import { fuzzyMatches, makeNormalizer, matches } from './matches';
77
import {
8+
getBaseElement,
89
getElementError,
910
firstResultOrNull,
1011
queryAllByProp,
@@ -175,7 +176,7 @@ function queryAllByText(
175176
text,
176177
{ types = ['Text', 'TextInput'], exact = true, collapseWhitespace, trim, normalizer } = {},
177178
) {
178-
const baseElement = container.root;
179+
const baseElement = getBaseElement(container);
179180
const matcher = exact ? matches : fuzzyMatches;
180181
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
181182

@@ -187,7 +188,7 @@ function queryAllByText(
187188
[],
188189
);
189190

190-
return [...baseArray]
191+
return baseArray
191192
.filter(node => matcher(getNodeText(node), node, text, matchNormalizer))
192193
.map(removeBadProperties);
193194
}
@@ -202,11 +203,8 @@ function queryByText(...args) {
202203
|--------------------------------------------------------------------------
203204
*/
204205
function makeFinder(getter) {
205-
return (testContainer, text, options, waitForElementOptions) =>
206-
waitForElement(
207-
(container = testContainer) => getter(container, text, options),
208-
waitForElementOptions,
209-
);
206+
return (container, text, options, waitForElementOptions) =>
207+
waitForElement(() => getter(container, text, options), waitForElementOptions);
210208
}
211209

212210
/*

src/query-helpers.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ function defaultFilter(node) {
3838
return mockedTypes.includes(node.type);
3939
}
4040

41+
function getBaseElement(container) {
42+
return container.root ? container.root : container;
43+
}
44+
4145
function removeBadProperties(node) {
4246
// We take the guiding principles seriously around these parts. These methods just let
4347
// you do too much unfortunately, and they make it hard to follow the rules of the
@@ -65,7 +69,7 @@ function queryAllByProp(
6569
match,
6670
{ filter, exact = true, collapseWhitespace, trim, normalizer } = {},
6771
) {
68-
const baseElement = container.root;
72+
const baseElement = getBaseElement(container);
6973
const matcher = exact ? matches : fuzzyMatches;
7074
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
7175
const allNodes = Array.from(baseElement.findAll(c => c.props[attribute]));
@@ -86,6 +90,7 @@ function queryByProp(...args) {
8690

8791
export {
8892
defaultFilter,
93+
getBaseElement,
8994
getElementError,
9095
firstResultOrNull,
9196
filterNodeByType,

src/wait-for-element-to-be-removed.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getConfig } from './config';
22
import { getSetImmediate } from './helpers';
33

4-
function waitForElementToBeRemoved(callback, { container, interval = 50, timeout = 4500 } = {}) {
4+
function waitForElementToBeRemoved(callback, { interval = 50, timeout = 4500 } = {}) {
55
return new Promise((resolve, reject) => {
66
if (typeof callback !== 'function') {
77
reject(new Error('waitForElementToBeRemoved requires a callback as the first parameter'));
@@ -12,7 +12,7 @@ function waitForElementToBeRemoved(callback, { container, interval = 50, timeout
1212

1313
// Check if the element is not present
1414
/* istanbul ignore next */
15-
const result = container ? callback(container) : callback();
15+
const result = callback();
1616
if (!result || (Array.isArray(result) && !result.length)) {
1717
onDone(
1818
new Error(
@@ -38,7 +38,7 @@ function waitForElementToBeRemoved(callback, { container, interval = 50, timeout
3838
function onMutation() {
3939
try {
4040
/* istanbul ignore next */
41-
const result = container ? callback(container) : callback();
41+
const result = callback();
4242
if (!result || (Array.isArray(result) && !result.length)) {
4343
onDone(null, true);
4444
} else {

src/wait-for-element.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getConfig } from './config';
22
import { getSetImmediate } from './helpers';
33

4-
function waitForElement(callback, { container, interval = 50, timeout = 4500 } = {}) {
4+
function waitForElement(callback, { interval = 50, timeout = 4500 } = {}) {
55
return new Promise((resolve, reject) => {
66
if (typeof callback !== 'function') {
77
reject(new Error('waitForElement requires a callback as the first parameter'));
@@ -23,7 +23,7 @@ function waitForElement(callback, { container, interval = 50, timeout = 4500 } =
2323

2424
function onMutation() {
2525
try {
26-
const result = container ? callback(container) : callback();
26+
const result = callback();
2727
if (result) {
2828
onDone(null, result);
2929
}

0 commit comments

Comments
 (0)