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

Commit 2ba51ba

Browse files
committed
refactor: rename rootInstance to baseElement
1 parent f45d4c5 commit 2ba51ba

File tree

6 files changed

+54
-41
lines changed

6 files changed

+54
-41
lines changed

src/__tests__/events.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { NativeEvent } from '../events';
66

77
test('onChange works', () => {
88
const handleChange = jest.fn();
9-
const { rootInstance } = render(<TextInput onChange={handleChange} />);
10-
fireEvent.change(rootInstance, { target: { value: 'a' } });
9+
const { baseElement } = render(<TextInput onChange={handleChange} />);
10+
fireEvent.change(baseElement, { target: { value: 'a' } });
1111
expect(handleChange).toHaveBeenCalledTimes(1);
1212
});
1313

@@ -28,9 +28,9 @@ test('onChangeText works', () => {
2828

2929
test('calling `fireEvent` directly works too', () => {
3030
const handleEvent = jest.fn();
31-
const { rootInstance } = render(<Button onPress={handleEvent} title="test" />);
31+
const { baseElement } = render(<Button onPress={handleEvent} title="test" />);
3232

33-
fireEvent(rootInstance, new NativeEvent('press'));
33+
fireEvent(baseElement, new NativeEvent('press'));
3434
expect(handleEvent).toBeCalledTimes(1);
3535
});
3636

@@ -39,8 +39,8 @@ test('calling a custom event works as well', () => {
3939
const onMyEvent = jest.fn(({ nativeEvent }) => expect(nativeEvent).toEqual({ value: 'testing' }));
4040
const MyComponent = ({ onMyEvent }) => <TextInput value="test" onChange={onMyEvent} />;
4141

42-
const { rootInstance } = render(<MyComponent onMyEvent={onMyEvent} />);
43-
fireEvent(rootInstance, new NativeEvent('myEvent', event));
42+
const { baseElement } = render(<MyComponent onMyEvent={onMyEvent} />);
43+
fireEvent(baseElement, new NativeEvent('myEvent', event));
4444

4545
expect(onMyEvent).toHaveBeenCalledWith({
4646
nativeEvent: { value: 'testing' },

src/__tests__/render.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import { View } from 'react-native';
33
import { render } from '../';
44

55
test('renders View', () => {
6-
const ref = React.createRef();
7-
const { rootInstance } = render(<View ref={ref} />);
8-
expect(rootInstance.instance).toBe(ref.current);
6+
const { baseElement } = render(<View />);
7+
expect(baseElement).not.toBeNull();
98
});
109

11-
test('returns rootInstance', () => {
12-
const { rootInstance } = render(<View />);
13-
expect(rootInstance).toBeTruthy();
10+
test('returns baseElement', () => {
11+
const { baseElement } = render(<View />);
12+
expect(baseElement).toBeTruthy();
1413
});
1514

1615
test('renders options.wrapper around node', () => {

src/__tests__/wait-for-element-to-be-removed.fake-timers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ test('requires an unempty array of elements to exist first', () => {
2525
});
2626

2727
test('times out after 4500ms by default', () => {
28-
const { rootInstance } = render(<View />);
28+
const { baseElement } = render(<View />);
2929
const promise = expect(
30-
waitForElementToBeRemoved(() => rootInstance),
30+
waitForElementToBeRemoved(() => baseElement),
3131
).rejects.toThrowErrorMatchingInlineSnapshot(`"Timed out in waitForElementToBeRemoved."`);
3232

3333
jest.advanceTimersByTime(4501);

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ function render(ui, { options = {}, wrapper: WrapperComponent } = {}) {
1919
container = TR.create(wrapUiIfNeeded(ui), options);
2020
});
2121

22+
const baseElement = queryHelpers.removeBadProperties(container.root);
23+
2224
return {
2325
container,
24-
rootInstance: container.root,
26+
baseElement,
2527
debug: (el = container) => console.log(prettyPrint(el)),
2628
unmount: () => container.unmount(),
2729
rerender: rerenderUi => {

src/queries.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { Text, TextInput } from 'react-native';
44
import { getNodeText } from './get-node-text';
55
import { waitForElement } from './wait-for-element';
66
import { fuzzyMatches, makeNormalizer, matches } from './matches';
7-
import { getElementError, firstResultOrNull, queryAllByProp, queryByProp } from './query-helpers';
7+
import {
8+
getElementError,
9+
firstResultOrNull,
10+
queryAllByProp,
11+
queryByProp,
12+
removeBadProperties,
13+
} from './query-helpers';
814

915
const queryByA11yLabel = queryByProp.bind(null, 'accessibilityLabel');
1016
const queryAllByA11yLabel = queryAllByProp.bind(null, 'accessibilityLabel');
@@ -22,37 +28,21 @@ function queryAllByText(
2228
text,
2329
{ types = ['Text', 'TextInput'], exact = true, collapseWhitespace, trim, normalizer } = {},
2430
) {
25-
const rootInstance = container.root;
31+
const baseElement = container.root;
2632
const matcher = exact ? matches : fuzzyMatches;
2733
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
2834

2935
const baseArray = types.reduce(
30-
(accumulator, currentValue) => [...accumulator, ...rootInstance.findAllByType(currentValue)],
36+
(accumulator, currentValue) => [
37+
...accumulator,
38+
...baseElement.findAll(n => n.type === currentValue),
39+
],
3140
[],
3241
);
3342

3443
return [...baseArray]
3544
.filter(node => matcher(getNodeText(node), node, text, matchNormalizer))
36-
.map(node => {
37-
// We take the guiding principles seriously around these parts. These methods just let
38-
// you do too much unfortunately, and they make it hard to follow the rules of the
39-
// testing-library. It's not that we don't trust you, in fact we do trust you! We've
40-
// left `findAll` on the instance for you as an emergency escape hatch for when
41-
// you're really in a bind. We do want you to test successfully, after all ☺️
42-
//
43-
// The main intent is to:
44-
// 1) Make it hard to assert on implementation details
45-
// 2) Force you to consider how to test from a user's perspective
46-
//
47-
// Of course if you can't figure that out, we still want you to be able to use this library,
48-
// so use `findAll`, just use it sparingly! We really believe you can do everything you
49-
// need using the query methods provided on the `render` API.
50-
['find', 'findAllByProps', 'findAllByType', 'findByProps', 'findByType', 'instance'].forEach(
51-
op => Object.defineProperty(node, op, { get: undefined }),
52-
);
53-
54-
return node;
55-
});
45+
.map(removeBadProperties);
5646
}
5747

5848
function queryByText(...args) {

src/query-helpers.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,41 @@ function defaultFilter(node) {
3838
return mockedTypes.includes(node.type);
3939
}
4040

41+
function removeBadProperties(node) {
42+
// We take the guiding principles seriously around these parts. These methods just let
43+
// you do too much unfortunately, and they make it hard to follow the rules of the
44+
// testing-library. It's not that we don't trust you, in fact we do trust you! We've
45+
// left `findAll` on the instance for you as an emergency escape hatch for when
46+
// you're really in a bind. We do want you to test successfully, after all ☺️
47+
//
48+
// The main intent is to:
49+
// 1) Make it hard to assert on implementation details
50+
// 2) Force you to consider how to test from a user's perspective
51+
//
52+
// Of course if you can't figure that out, we still want you to be able to use this library,
53+
// so use `findAll`, just use it sparingly! We really believe you can do everything you
54+
// need using the query methods provided on the `render` API.
55+
['find', 'findAllByProps', 'findAllByType', 'findByProps', 'findByType', 'instance'].forEach(op =>
56+
Object.defineProperty(node, op, { get: undefined }),
57+
);
58+
59+
return node;
60+
}
61+
4162
function queryAllByProp(
4263
attribute,
4364
container,
4465
text,
4566
{ filter, exact = true, collapseWhitespace, trim, normalizer } = {},
4667
) {
47-
const rootInstance = container.root;
68+
const baseElement = container.root;
4869
const matcher = exact ? matches : fuzzyMatches;
4970
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
50-
const allNodes = Array.from(rootInstance.findAll(c => c.props[attribute]));
71+
const allNodes = Array.from(baseElement.findAll(c => c.props[attribute]));
5172

5273
return allNodes
5374
.filter((node, index) => (filter ? filter(node, index) : defaultFilter(node, index)))
54-
.filter(node => matcher(node.props[attribute], rootInstance, text, matchNormalizer));
75+
.filter(node => matcher(node.props[attribute], baseElement, text, matchNormalizer));
5576
}
5677

5778
function queryByProp(...args) {
@@ -65,4 +86,5 @@ export {
6586
filterNodeByType,
6687
queryAllByProp,
6788
queryByProp,
89+
removeBadProperties,
6890
};

0 commit comments

Comments
 (0)