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

Commit 4754e0a

Browse files
committed
refactor baseElement/container
1 parent 0998aab commit 4754e0a

File tree

10 files changed

+50
-98
lines changed

10 files changed

+50
-98
lines changed

examples/__tests__/react-intl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const renderWithReactIntl = component => {
3939
setupTests();
4040

4141
test('it should render FormattedDate and have a formatted pt date', () => {
42-
const { baseElement } = renderWithReactIntl(<FormatDateView />);
42+
const { container, rootInstance } = renderWithReactIntl(<FormatDateView />);
4343

44-
getByText({ container: baseElement }, '11/03/2019');
44+
getByText({ container, rootInstance }, '11/03/2019');
4545
});

mockComponent.js

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

src/__tests__/events.js

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

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

1414
test('calling `fireEvent` directly works too', () => {
1515
const handleEvent = jest.fn();
16-
const { baseElement } = render(<Button onPress={handleEvent} title="test" />);
17-
fireEvent(baseElement, new NativeEvent('press'));
16+
const { rootInstance } = render(<Button onPress={handleEvent} title="test" />);
17+
fireEvent(rootInstance, new NativeEvent('press'));
1818
});
1919

2020
test('calling a handler when there is no valid target throws', () => {

src/__tests__/queries.find.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { render } from '../.';
55

66
test('find asynchronously finds elements', async () => {
77
const {
8-
baseElement,
8+
rootInstance,
99

1010
findByA11yLabel,
1111
findAllByA11yLabel,
@@ -102,7 +102,7 @@ test('find rejects when something cannot be found', async () => {
102102
});
103103

104104
test('actually works with async code', async () => {
105-
const { container, findByTestId, rerender } = render(<View />);
105+
const { findByTestId, rerender, container } = render(<View />);
106106
setTimeout(() => rerender(<Text testID="text">correct dom</Text>), 20);
107107
await expect(findByTestId('text', {}, { container })).resolves.toBeTruthy();
108108
});

src/__tests__/render.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { render } from '../';
44

55
test('renders View', () => {
66
const ref = React.createRef();
7-
const { baseElement } = render(<View ref={ref} />);
8-
expect(baseElement.instance).toBe(ref.current);
7+
const { rootInstance } = render(<View ref={ref} />);
8+
expect(rootInstance.instance).toBe(ref.current);
99
});
1010

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

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

src/get-queries-for-element.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as defaultQueries from './queries';
1212
function getQueriesForElement(container, queries = defaultQueries) {
1313
return Object.keys(queries).reduce((helpers, key) => {
1414
const fn = queries[key];
15-
helpers[key] = fn.bind(null, { testInstance: container, container: container.root });
15+
helpers[key] = fn.bind(null, { container: container, rootInstance: container.root });
1616
return helpers;
1717
}, {});
1818
}

src/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ function render(ui, { options = {}, wrapper: WrapperComponent } = {}) {
1212
const wrapUiIfNeeded = innerElement =>
1313
WrapperComponent ? <WrapperComponent>{innerElement}</WrapperComponent> : innerElement;
1414

15-
let instance = {};
15+
let container = {};
1616

1717
act(() => {
18-
instance = TR.create(wrapUiIfNeeded(ui), options);
18+
container = TR.create(wrapUiIfNeeded(ui), options);
1919
});
2020

2121
return {
22-
baseElement: instance.root,
23-
container: instance,
24-
debug: (el = instance) => console.log(prettyPrint(el.toJSON())),
25-
unmount: () => instance.unmount(),
22+
container,
23+
rootInstance: container.root,
24+
debug: (el = container) => console.log(prettyPrint(el.toJSON())),
25+
unmount: () => container.unmount(),
2626
rerender: rerenderUi => {
27-
instance.update(wrapUiIfNeeded(rerenderUi));
27+
container.update(wrapUiIfNeeded(rerenderUi));
2828
// Intentionally do not return anything to avoid unnecessarily complicating the API.
2929
// folks can use all the same utilities we return in the first place that are bound to the container
3030
},
31-
...getQueriesForElement(instance),
31+
...getQueriesForElement(container),
3232
};
3333
}
3434

src/queries.js

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ const queryByA11yRole = queryByProp.bind(null, 'accessibilityRole');
1818
const queryAllByA11yRole = queryAllByProp.bind(null, 'accessibilityRole');
1919

2020
function queryAllByText(
21-
{ container },
21+
{ rootInstance },
2222
text,
2323
{ types = [Text, TextInput], exact = true, collapseWhitespace, trim, normalizer } = {},
2424
) {
2525
const matcher = exact ? matches : fuzzyMatches;
2626
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
2727

2828
const baseArray = types.reduce(
29-
(accumulator, currentValue) => [...accumulator, ...container.findAllByType(currentValue)],
29+
(accumulator, currentValue) => [...accumulator, ...rootInstance.findAllByType(currentValue)],
3030
[],
3131
);
3232

@@ -42,10 +42,10 @@ function queryByText(...args) {
4242
// 1. The error messages are specific to each one and depend on arguments
4343
// 2. The stack trace will look better because it'll have a helpful method name.
4444

45-
function getAllByTestId({ container, testInstance }, id, ...rest) {
46-
const els = queryAllByTestId({ container, testInstance }, id, ...rest);
45+
function getAllByTestId({ rootInstance, container }, id, ...rest) {
46+
const els = queryAllByTestId({ rootInstance, container }, id, ...rest);
4747
if (!els.length) {
48-
throw getElementError(`Unable to find an element with the testID: ${id}`, testInstance);
48+
throw getElementError(`Unable to find an element with the testID: ${id}`, container);
4949
}
5050
return els;
5151
}
@@ -54,13 +54,10 @@ function getByTestId(...args) {
5454
return firstResultOrNull(getAllByTestId, ...args);
5555
}
5656

57-
function getAllByA11yRole({ container, testInstance }, value, ...rest) {
58-
const els = queryAllByA11yRole({ container, testInstance }, value, ...rest);
57+
function getAllByA11yRole({ rootInstance, container }, value, ...rest) {
58+
const els = queryAllByA11yRole({ rootInstance, container }, value, ...rest);
5959
if (!els.length) {
60-
throw getElementError(
61-
`Unable to find an element by accessibilityRole="${value}".`,
62-
testInstance,
63-
);
60+
throw getElementError(`Unable to find an element by accessibilityRole="${value}".`, container);
6461
}
6562
return els;
6663
}
@@ -69,10 +66,10 @@ function getByA11yRole(...args) {
6966
return firstResultOrNull(getAllByA11yRole, ...args);
7067
}
7168

72-
function getAllByValue({ container, testInstance }, value, ...rest) {
73-
const els = queryAllByValue({ container, testInstance }, value, ...rest);
69+
function getAllByValue({ rootInstance, container }, value, ...rest) {
70+
const els = queryAllByValue({ rootInstance, container }, value, ...rest);
7471
if (!els.length) {
75-
throw getElementError(`Unable to find an element with the value: ${value}.`, testInstance);
72+
throw getElementError(`Unable to find an element with the value: ${value}.`, container);
7673
}
7774
return els;
7875
}
@@ -81,13 +78,10 @@ function getByValue(...args) {
8178
return firstResultOrNull(getAllByValue, ...args);
8279
}
8380

84-
function getAllByA11yLabel({ container, testInstance }, text, ...rest) {
85-
const els = queryAllByA11yLabel({ container, testInstance }, text, ...rest);
81+
function getAllByA11yLabel({ rootInstance, container }, text, ...rest) {
82+
const els = queryAllByA11yLabel({ rootInstance, container }, text, ...rest);
8683
if (!els.length) {
87-
throw getElementError(
88-
`Unable to find an element by accessibilityLabel="${text}"`,
89-
testInstance,
90-
);
84+
throw getElementError(`Unable to find an element by accessibilityLabel="${text}"`, container);
9185
}
9286
return els;
9387
}
@@ -96,12 +90,12 @@ function getByA11yLabel(...args) {
9690
return firstResultOrNull(getAllByA11yLabel, ...args);
9791
}
9892

99-
function getAllByPlaceholder({ container, testInstance }, text, ...rest) {
100-
const els = queryAllByPlaceholder({ container, testInstance }, text, ...rest);
93+
function getAllByPlaceholder({ rootInstance, container }, text, ...rest) {
94+
const els = queryAllByPlaceholder({ rootInstance, container }, text, ...rest);
10195
if (!els.length) {
10296
throw getElementError(
10397
`Unable to find an element with the placeholder text of: ${text}`,
104-
testInstance,
98+
container,
10599
);
106100
}
107101
return els;
@@ -111,12 +105,12 @@ function getByPlaceholder(...args) {
111105
return firstResultOrNull(getAllByPlaceholder, ...args);
112106
}
113107

114-
function getAllByText({ container, testInstance }, text, ...rest) {
115-
const els = queryAllByText({ container, testInstance }, text, ...rest);
108+
function getAllByText({ rootInstance, container }, text, ...rest) {
109+
const els = queryAllByText({ rootInstance, container }, text, ...rest);
116110
if (!els.length) {
117111
throw getElementError(
118112
`Unable to find an element with the text: ${text}. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.`,
119-
testInstance,
113+
container,
120114
);
121115
}
122116
return els;
@@ -127,9 +121,9 @@ function getByText(...args) {
127121
}
128122

129123
function makeFinder(getter) {
130-
return (defaultInstance, text, options, waitForElementOptions) =>
124+
return (testInstance, text, options, waitForElementOptions) =>
131125
waitForElement(
132-
(instance = defaultInstance) => getter(instance, text, options),
126+
(instance = testInstance) => getter(instance, text, options),
133127
waitForElementOptions,
134128
);
135129
}

src/query-helpers.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,19 @@ function defaultFilter(node) {
6666

6767
function queryAllByProp(
6868
attribute,
69-
{ container, testInstance },
69+
{ container, rootInstance },
7070
text,
7171
{ filter, exact = true, collapseWhitespace, trim, normalizer } = {},
7272
) {
7373
const matcher = exact ? matches : fuzzyMatches;
7474
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
75-
76-
const allNodes = Array.from(container.findAll(c => c.props[attribute]));
75+
const allNodes = Array.from(rootInstance.findAll(c => c.props[attribute]));
7776

7877
return (
7978
allNodes
8079
// Then make sure to only match the odd numbered ones
8180
.filter((node, index) => (filter ? filter(node, index) : defaultFilter(node, index)))
82-
.filter(node => matcher(node.props[attribute], container, text, matchNormalizer))
81+
.filter(node => matcher(node.props[attribute], rootInstance, text, matchNormalizer))
8382
);
8483
}
8584

src/wait-for-element.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ function waitForElement(callback, { container, interval = 50, timeout = 4500 } =
2222

2323
function onMutation() {
2424
try {
25-
const result = container ? callback({ container: container.root }) : callback();
25+
const result = container
26+
? callback({ container, rootInstance: container.root })
27+
: callback();
2628
if (result) {
2729
onDone(null, result);
2830
}

0 commit comments

Comments
 (0)