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

Commit d2bd926

Browse files
author
Brandon Carroll
committed
feat: decouple lib from preset
1 parent 64b60d4 commit d2bd926

21 files changed

+145
-117
lines changed

src/__tests__/render.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
2-
import { Text, View } from 'react-native';
3-
import { queryAllByProp, render } from '../';
2+
import { View } from 'react-native';
3+
import { toJSON, render } from '../';
44

55
test('renders View', () => {
66
const { container } = render(<View />);
@@ -15,12 +15,12 @@ test('returns container', () => {
1515
test('renders options.wrapper around node', () => {
1616
const WrapperComponent = ({ children }) => <View testID="wrapper">{children}</View>;
1717

18-
const { testRenderer, getByTestId } = render(<View testID="inner" />, {
18+
const { container, getByTestId } = render(<View testID="inner" />, {
1919
wrapper: WrapperComponent,
2020
});
2121

2222
expect(getByTestId('wrapper')).toBeTruthy();
23-
expect(testRenderer.toJSON()).toMatchInlineSnapshot(`
23+
expect(toJSON(container)).toMatchInlineSnapshot(`
2424
<View
2525
testID="wrapper"
2626
>

src/__tests__/stopwatch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { Button, Text, View } from 'react-native';
33

4-
import { render, fireEvent } from '../';
4+
import { render, fireEvent, toJSON } from '../';
55

66
class StopWatch extends React.Component {
77
state = { lapse: 0, running: false };
@@ -41,11 +41,11 @@ const wait = time => new Promise(resolve => setTimeout(resolve, time));
4141

4242
test('unmounts a component', async () => {
4343
jest.spyOn(console, 'error').mockImplementation(() => {});
44-
const { unmount, getByTitle, testRenderer } = render(<StopWatch />);
44+
const { unmount, getByTitle, container } = render(<StopWatch />);
4545
fireEvent.press(getByTitle('Start'));
4646

4747
unmount();
4848

49-
expect(testRenderer.toJSON()).toBeNull();
49+
expect(toJSON(container)).toBeNull();
5050
await wait(() => expect(console.error).not.toHaveBeenCalled());
5151
});

src/index.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,14 @@ import React from 'react';
22
import TR from 'react-test-renderer';
33

44
import {
5-
configure as configureNTL,
5+
toJSON,
66
fireEvent as rntlFireEvent,
77
getQueriesForElement,
88
NativeEvent,
99
prettyPrint,
1010
proxyUnsafeProperties,
1111
} from './lib';
12-
import act, { asyncAct } from './act-compat';
13-
14-
configureNTL({
15-
asyncWrapper: async cb => {
16-
let result;
17-
await asyncAct(async () => {
18-
result = await cb();
19-
});
20-
return result;
21-
},
22-
});
12+
import act from './act-compat';
2313

2414
function render(ui, { options = {}, wrapper: WrapperComponent, queries } = {}) {
2515
const wrapUiIfNeeded = innerElement =>
@@ -34,7 +24,7 @@ function render(ui, { options = {}, wrapper: WrapperComponent, queries } = {}) {
3424
return {
3525
testRenderer,
3626
container: proxyUnsafeProperties(testRenderer.root),
37-
debug: () => console.log(prettyPrint(testRenderer.toJSON())),
27+
debug: (el = testRenderer.root) => console.log(prettyPrint(toJSON(el))),
3828
unmount: () => testRenderer.unmount(),
3929
rerender: rerenderUi => {
4030
act(() => {

src/lib/__tests__/config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,11 @@ describe('configuration API', () => {
5151
foo: '123-derived',
5252
});
5353
});
54+
55+
test('asyncWrapper callback exists by default', () => {
56+
const callback = jest.fn();
57+
getConfig().asyncWrapper(callback);
58+
expect(callback).toHaveBeenCalledTimes(1);
59+
});
5460
});
5561
});

src/lib/__tests__/pretty-print.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import React from 'react';
22
import { Text, View } from 'react-native';
33

4-
import { render } from '../../';
5-
import { prettyPrint } from '../pretty-print';
4+
import { render, prettyPrint, toJSON } from '../../';
65

76
test('it prints correctly with no children', () => {
8-
const { testRenderer } = render(<View />);
7+
const { container } = render(<View />);
98

10-
expect(prettyPrint(testRenderer.toJSON())).toMatchInlineSnapshot(`"[36m<View />[39m"`);
9+
expect(prettyPrint(toJSON(container))).toMatchInlineSnapshot(`"[36m<View />[39m"`);
1110
});
1211

1312
test('it prints correctly with one child', () => {
14-
const { testRenderer } = render(
13+
const { container } = render(
1514
<View>
1615
<Text>Hello World!</Text>
1716
</View>,
1817
);
1918

20-
expect(prettyPrint(testRenderer.toJSON())).toMatchInlineSnapshot(`
19+
expect(prettyPrint(toJSON(container))).toMatchInlineSnapshot(`
2120
"<View>
2221
<Text>
2322
Hello World!
@@ -27,14 +26,14 @@ test('it prints correctly with one child', () => {
2726
});
2827

2928
test('it prints correctly with multiple children', () => {
30-
const { testRenderer } = render(
29+
const { container } = render(
3130
<View>
3231
<Text>Hello</Text>
3332
<Text>World!</Text>
3433
</View>,
3534
);
3635

37-
expect(prettyPrint(testRenderer.toJSON())).toMatchInlineSnapshot(`
36+
expect(prettyPrint(toJSON(container))).toMatchInlineSnapshot(`
3837
"<View>
3938
<Text>
4039
Hello
@@ -47,11 +46,11 @@ test('it prints correctly with multiple children', () => {
4746
});
4847

4948
test('it supports truncating the output length', () => {
50-
const { testRenderer } = render(
49+
const { container } = render(
5150
<View>
5251
<Text>Hello World!</Text>
5352
</View>,
5453
);
5554

56-
expect(prettyPrint(testRenderer.toJSON(), 5)).toMatch(/\.\.\./);
55+
expect(prettyPrint(toJSON(container), 5)).toMatch(/\.\.\./);
5756
});

src/lib/__tests__/query-helpers.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { defaultFilter, proxyUnsafeProperties } from '../query-helpers';
1+
import { configure } from '../config';
2+
import { validComponentFilter, proxyUnsafeProperties } from '../query-helpers';
23

3-
test('defaultFilter returns `true` when node.type is in the mocked type list', () => {
4-
expect(defaultFilter({ type: 'Text' })).toEqual(true);
4+
test('validComponentFilter returns `true` when node.type is in the mocked type list', () => {
5+
configure({ coreComponents: ['Text'] });
6+
expect(validComponentFilter({ type: 'Text' })).toEqual(true);
57
});
6-
test('defaultFilter returns `false` when node.type is not in the mocked type list', () => {
7-
expect(defaultFilter({ type: 'Test' })).toEqual(false);
8+
test('validComponentFilter returns `false` when node.type is not in the mocked type list', () => {
9+
configure({ coreComponents: ['Text'] });
10+
expect(validComponentFilter({ type: 'Test' })).toEqual(false);
11+
});
12+
test('validComponentFilter always returns `true` when no mocked components are provided', () => {
13+
configure({ coreComponents: null });
14+
expect(validComponentFilter({ type: 'BadComponent' })).toEqual(true);
815
});
916

1017
test('proxyUnsafeProperties ignores what it should', () => {

src/lib/__tests__/to-json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ test('it converts to json', () => {
3030
</ParentComponent>,
3131
);
3232

33-
console.log(prettyPrint(toJSON(container)));
33+
expect(prettyPrint(toJSON(container))).toMatchSnapshot();
3434
});

src/lib/config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
/* istanbul ignore next */
12
let config = {
2-
// no default config in NTL...
3+
asyncWrapper: cb => cb(),
34
};
45

56
function configure(newConfig) {
@@ -16,8 +17,8 @@ function configure(newConfig) {
1617
};
1718
}
1819

19-
function getConfig() {
20-
return config;
20+
function getConfig(key) {
21+
return key ? config[key] : config;
2122
}
2223

2324
export { configure, getConfig };

src/lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { getQueriesForElement } from './get-queries-for-element';
22
import * as queries from './queries';
33
import * as queryHelpers from './query-helpers';
44

5+
export * from './config';
6+
export * from './to-json';
57
export * from './queries';
68
export * from './wait';
79
export * from './wait-for-element';
@@ -12,7 +14,6 @@ export * from './events';
1214
export * from './get-queries-for-element';
1315
export * from './query-helpers';
1416
export * from './pretty-print';
15-
export { configure } from './config';
1617

1718
export {
1819
getQueriesForElement as within,

src/lib/pretty-print.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import prettyFormat from 'pretty-format';
22
import React from 'react';
3+
34
const { ReactTestComponent, ReactElement } = prettyFormat.plugins;
45

56
function prettyPrint(element, maxLength, options) {

src/lib/queries/display-value.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { matches, fuzzyMatches, makeNormalizer, buildQueries, getContainer } from './all-utils';
1+
import {
2+
matches,
3+
fuzzyMatches,
4+
makeNormalizer,
5+
buildQueries,
6+
getContainer,
7+
validComponentFilter,
8+
} from './all-utils';
29

310
function queryAllByDisplayValue(
411
testRenderer,
@@ -9,7 +16,7 @@ function queryAllByDisplayValue(
916
const matcher = exact ? matches : fuzzyMatches;
1017
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
1118

12-
return Array.from(container.findAll(n => ['TextInput', 'Picker'].includes(n.type)))
19+
return Array.from(container.findAll(node => validComponentFilter(node, 'displayValueComponents')))
1320
.filter(filter)
1421
.filter(node => {
1522
if (node.type === 'Picker') {

src/lib/queries/text.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getNodeText,
66
buildQueries,
77
getContainer,
8+
validComponentFilter,
89
} from './all-utils';
910

1011
function queryAllByText(
@@ -16,7 +17,7 @@ function queryAllByText(
1617
const matcher = exact ? matches : fuzzyMatches;
1718
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
1819

19-
return Array.from(container.findAll(n => ['Button', 'TextInput', 'Text'].includes(n.type)))
20+
return Array.from(container.findAll(node => validComponentFilter(node, 'textComponents')))
2021
.filter(filter)
2122
.filter(node => matcher(getNodeText(node), node, text, matchNormalizer));
2223
}

src/lib/queries/title.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { buildQueries, matches, fuzzyMatches, makeNormalizer, getContainer } from './all-utils';
1+
import {
2+
buildQueries,
3+
matches,
4+
fuzzyMatches,
5+
makeNormalizer,
6+
getContainer,
7+
validComponentFilter,
8+
} from './all-utils';
29

310
function queryAllByTitle(
411
testRenderer,
@@ -9,7 +16,7 @@ function queryAllByTitle(
916
const matcher = exact ? matches : fuzzyMatches;
1017
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
1118

12-
return Array.from(container.findAll(n => ['Button', 'RefreshControl'].includes(n.type)))
19+
return Array.from(container.findAll(node => validComponentFilter(node, 'titleComponents')))
1320
.filter(filter)
1421
.filter(node => matcher(node.getProp('title'), node, value, matchNormalizer));
1522
}

src/lib/query-helpers.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { getCoreComponents } from '../preset/core-components';
2-
1+
import { toJSON } from './to-json';
2+
import { getConfig } from './config';
33
import { prettyPrint } from './pretty-print';
44
import { waitForElement } from './wait-for-element';
55
import { fuzzyMatches, makeNormalizer, matches } from './matches';
66

77
function debugTree(testRenderer) {
88
const limit = process.env.DEBUG_PRINT_LIMIT || 7000;
99

10-
return prettyPrint(testRenderer.toJSON(), limit);
10+
return prettyPrint(toJSON(testRenderer.root), limit);
1111
}
1212

1313
function getElementError(message, testRenderer) {
@@ -21,8 +21,9 @@ function getMultipleElementsFoundError(message, testRenderer) {
2121
);
2222
}
2323

24-
function defaultFilter(node) {
25-
return getCoreComponents().includes(node.type);
24+
function validComponentFilter(node, key = 'coreComponents') {
25+
const validComponents = getConfig(key);
26+
return validComponents ? validComponents.includes(node.type) : true;
2627
}
2728

2829
function getContainer(testRenderer) {
@@ -59,7 +60,7 @@ function proxyUnsafeProperties(node) {
5960
return function(...args) {
6061
return ref
6162
.apply(this, args)
62-
.filter(defaultFilter)
63+
.filter(node => validComponentFilter(node))
6364
.map(proxyUnsafeProperties);
6465
};
6566
} else if (key === 'getProp') {
@@ -148,7 +149,6 @@ function buildQueries(queryAllBy, getMultipleError, getMissingError) {
148149

149150
export {
150151
buildQueries,
151-
defaultFilter,
152152
getContainer,
153153
getElementError,
154154
getMultipleElementsFoundError,
@@ -158,4 +158,5 @@ export {
158158
queryAllByProp,
159159
queryByProp,
160160
proxyUnsafeProperties,
161+
validComponentFilter,
161162
};

0 commit comments

Comments
 (0)