Skip to content

Commit d8c3b30

Browse files
author
Brandon Carroll
committed
3.0.0-beta.5
1 parent 6faa701 commit d8c3b30

40 files changed

+238
-152
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 { testRenderer } = renderWithReactIntl(<FormatDateView />);
4343

44-
getByText(baseElement, '11/03/2019');
44+
getByText(testRenderer, '11/03/2019');
4545
});

examples/__tests__/react-navigation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function renderWithNavigation({ screens = {}, navigatorConfig = {} } = {}) {
5151

5252
const App = createAppContainer(AppNavigator);
5353

54-
return { ...render(<App detached />), navigationBaseElement: App };
54+
return { ...render(<App detached />), navigationTestRenderer: App };
5555
}
5656

5757
test('full app rendering/navigating', async () => {

examples/__tests__/update-props.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class NumberDisplay extends React.Component {
1717
}
1818
}
1919

20-
test('calling render with the same component on the same baseElement does not remount', () => {
20+
test('calling render with the same component on the same testRenderer does not remount', () => {
2121
const { getByTestId, rerender } = render(<NumberDisplay number={1} />);
2222
expect(getByTestId('number-display')).toHaveTextContent(1);
2323

File renamed without changes.

src/lib/__tests__/debug.js renamed to src/__tests__/debug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ afterEach(() => {
1111
console.log.mockRestore();
1212
});
1313

14-
test('debug pretty prints the baseElement', () => {
14+
test('debug pretty prints the testRenderer', () => {
1515
const HelloWorld = () => <Text>Hello World</Text>;
1616
const { debug } = render(<HelloWorld />);
1717
debug();
File renamed without changes.

src/lib/__tests__/events.js renamed to src/__tests__/events.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import React from 'react';
22
import 'jest-native/extend-expect';
33
import { Button, Image, Text, TextInput, TouchableHighlight } from 'react-native';
44

5-
import { render, fireEvent, getEventHandlerName, wait } from '../';
6-
import { eventMap, NativeEvent } from '../events';
5+
import { render, fireEvent, eventMap, NativeEvent, getEventHandlerName, wait } from '../';
76

87
Object.keys(eventMap).forEach(key => {
98
const handlerName = getEventHandlerName(key);

src/lib/__tests__/fetch.js renamed to src/__tests__/fetch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Fetch extends React.Component {
3434
test('Fetch makes an API call and displays the greeting when load-greeting is clicked', async () => {
3535
fetch.mockResponseOnce(JSON.stringify({ data: { greeting: 'hello there' } }));
3636
const url = '/greeting';
37-
const { baseElement, getByText } = render(<Fetch url={url} />);
37+
const { testRenderer, getByText } = render(<Fetch url={url} />);
3838

3939
fireEvent.press(getByText('Fetch'));
4040

@@ -44,5 +44,5 @@ test('Fetch makes an API call and displays the greeting when load-greeting is cl
4444
expect(fetch).toHaveBeenCalledWith(url);
4545

4646
expect(getByText('hello there')).toHaveTextContent('hello there');
47-
expect(baseElement).toMatchSnapshot();
47+
expect(testRenderer).toMatchSnapshot();
4848
});
File renamed without changes.

src/lib/__tests__/no-act.js renamed to src/__tests__/no-act.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { act } from '..';
1+
import { act } from '../';
22

33
jest.mock('react-test-renderer', () => ({}));
44

File renamed without changes.

src/lib/__tests__/render.js renamed to src/__tests__/render.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 { baseElement, getByTestId } = render(<View testID="inner" />, {
18+
const { testRenderer, getByTestId } = render(<View testID="inner" />, {
1919
wrapper: WrapperComponent,
2020
});
2121

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

src/lib/__tests__/stopwatch.js renamed to src/__tests__/stopwatch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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, baseElement } = render(<StopWatch />);
44+
const { unmount, getByTitle, testRenderer } = render(<StopWatch />);
4545
fireEvent.press(getByTitle('Start'));
4646

4747
unmount();
4848

49-
expect(baseElement.toJSON()).toBeNull();
49+
expect(testRenderer.toJSON()).toBeNull();
5050
await wait(() => expect(console.error).not.toHaveBeenCalled());
5151
});
File renamed without changes.

src/index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,67 @@
1+
import React from 'react';
2+
import TR from 'react-test-renderer';
3+
4+
import {
5+
configure as configureNTL,
6+
fireEvent as rntlFireEvent,
7+
getQueriesForElement,
8+
NativeEvent,
9+
prettyPrint,
10+
proxyUnsafeProperties,
11+
} 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+
});
23+
24+
function render(ui, { options = {}, wrapper: WrapperComponent } = {}) {
25+
const wrapUiIfNeeded = innerElement =>
26+
WrapperComponent ? <WrapperComponent>{innerElement}</WrapperComponent> : innerElement;
27+
28+
let testRenderer;
29+
30+
act(() => {
31+
testRenderer = TR.create(wrapUiIfNeeded(ui), options);
32+
});
33+
34+
return {
35+
testRenderer,
36+
container: proxyUnsafeProperties(testRenderer.root),
37+
debug: () => console.log(prettyPrint(testRenderer.toJSON())),
38+
unmount: () => testRenderer.unmount(),
39+
rerender: rerenderUi => {
40+
act(() => {
41+
testRenderer.update(wrapUiIfNeeded(rerenderUi));
42+
});
43+
},
44+
...getQueriesForElement(testRenderer),
45+
};
46+
}
47+
48+
function fireEvent(...args) {
49+
let returnValue;
50+
act(() => {
51+
returnValue = rntlFireEvent(...args);
52+
});
53+
return returnValue;
54+
}
55+
56+
Object.keys(rntlFireEvent).forEach(key => {
57+
fireEvent[key] = (...args) => {
58+
let returnValue;
59+
act(() => {
60+
returnValue = rntlFireEvent[key](...args);
61+
});
62+
return returnValue;
63+
};
64+
});
65+
166
export * from './lib';
67+
export { act, fireEvent, render, NativeEvent };

src/lib/__tests__/config.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { configure, getConfig } from '../config';
2+
3+
describe('configuration API', () => {
4+
let originalConfig;
5+
beforeEach(() => {
6+
// Grab the existing configuration so we can restore
7+
// it at the end of the test
8+
configure(existingConfig => {
9+
originalConfig = existingConfig;
10+
// Don't change the existing config
11+
return {};
12+
});
13+
});
14+
afterEach(() => {
15+
configure(originalConfig);
16+
});
17+
18+
beforeEach(() => {
19+
configure({ foo: '123', bar: '456' });
20+
});
21+
22+
describe('getConfig', () => {
23+
test('returns existing configuration', () => {
24+
const conf = getConfig();
25+
expect(conf.foo).toEqual('123');
26+
});
27+
});
28+
29+
describe('configure', () => {
30+
test('merges a delta rather than replacing the whole config', () => {
31+
const conf = getConfig();
32+
expect(conf).toMatchObject({ foo: '123', bar: '456' });
33+
});
34+
35+
test('overrides existing values', () => {
36+
configure({ foo: '789' });
37+
const conf = getConfig();
38+
expect(conf.foo).toEqual('789');
39+
});
40+
41+
test('passes existing config out to config function', () => {
42+
// Create a new config key based on the value of an existing one
43+
configure(existingConfig => ({
44+
foo: `${existingConfig.foo}-derived`,
45+
}));
46+
const conf = getConfig();
47+
48+
// The new value should be there, and existing values should be
49+
// untouched
50+
expect(conf).toMatchObject({
51+
foo: '123-derived',
52+
});
53+
});
54+
});
55+
});

src/lib/__tests__/get-by-errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { Button, Text, TextInput, View } from 'react-native';
33
import cases from 'jest-in-case';
44

5-
import { render } from '../';
5+
import { render } from '../../';
66

77
cases(
88
'getBy* queries throw an error when there are multiple elements returned',

src/lib/__tests__/misc.js

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

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

66
test('queryByProp', () => {
7-
const { baseElement } = render(
7+
const { testRenderer } = render(
88
<View>
99
<View testID="foo" importantForAccessibility="no" />
1010
<View importantForAccessibility="no" />
1111
<View importantForAccessibility="no-hide-descendants" />
1212
</View>,
1313
);
1414

15-
expect(queryByTestId(baseElement, 'foo')).not.toBeNull();
16-
expect(queryByProp('importantForAccessibility', baseElement, 'auto')).toBeNull();
17-
expect(() => queryByProp('importantForAccessibility', baseElement, /no/)).toThrow(
15+
expect(queryByTestId(testRenderer, 'foo')).not.toBeNull();
16+
expect(queryByProp('importantForAccessibility', testRenderer, 'auto')).toBeNull();
17+
expect(() => queryByProp('importantForAccessibility', testRenderer, /no/)).toThrow(
1818
/multiple elements/,
1919
);
2020
});

src/lib/__tests__/pretty-print.js

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

4-
import { render } from '../';
4+
import { render } from '../../';
55
import { prettyPrint } from '../pretty-print';
66

77
test('it prints correctly with no children', () => {
8-
const { baseElement } = render(<View />);
8+
const { testRenderer } = render(<View />);
99

10-
expect(prettyPrint(baseElement.toJSON())).toMatchInlineSnapshot(`"[36m<View />[39m"`);
10+
expect(prettyPrint(testRenderer.toJSON())).toMatchInlineSnapshot(`"[36m<View />[39m"`);
1111
});
1212

1313
test('it prints correctly with one child', () => {
14-
const { baseElement } = render(
14+
const { testRenderer } = render(
1515
<View>
1616
<Text>Hello World!</Text>
1717
</View>,
1818
);
1919

20-
expect(prettyPrint(baseElement.toJSON())).toMatchInlineSnapshot(`
20+
expect(prettyPrint(testRenderer.toJSON())).toMatchInlineSnapshot(`
2121
"<View>
2222
<Text>
2323
Hello World!
@@ -27,14 +27,14 @@ test('it prints correctly with one child', () => {
2727
});
2828

2929
test('it prints correctly with multiple children', () => {
30-
const { baseElement } = render(
30+
const { testRenderer } = render(
3131
<View>
3232
<Text>Hello</Text>
3333
<Text>World!</Text>
3434
</View>,
3535
);
3636

37-
expect(prettyPrint(baseElement.toJSON())).toMatchInlineSnapshot(`
37+
expect(prettyPrint(testRenderer.toJSON())).toMatchInlineSnapshot(`
3838
"<View>
3939
<Text>
4040
Hello
@@ -47,11 +47,11 @@ test('it prints correctly with multiple children', () => {
4747
});
4848

4949
test('it supports truncating the output length', () => {
50-
const { baseElement } = render(
50+
const { testRenderer } = render(
5151
<View>
5252
<Text>Hello World!</Text>
5353
</View>,
5454
);
5555

56-
expect(prettyPrint(baseElement.toJSON(), 5)).toMatch(/\.\.\./);
56+
expect(prettyPrint(testRenderer.toJSON(), 5)).toMatch(/\.\.\./);
5757
});

src/lib/__tests__/queries.find.js

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

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

66
test('find asynchronously finds elements', async () => {
77
const {

src/lib/__tests__/text-matchers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import cases from 'jest-in-case';
33
import { Button, Image, Text, TextInput, TouchableOpacity } from 'react-native';
44

5-
import { render } from '../';
5+
import { render } from '../../';
66
import { getDefaultNormalizer } from '../matches';
77

88
cases(

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

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

4-
import { render } from '../';
4+
import { render } from '../../';
55
import { waitForElementToBeRemoved } from '../wait-for-element-to-be-removed';
66

77
jest.useFakeTimers();

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

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

4-
import { render } from '../';
4+
import { render } from '../../';
55
import { waitForElementToBeRemoved } from '../wait-for-element-to-be-removed';
66

77
test('resolves only when the element is removed', async () => {

src/lib/__tests__/wait-for-element.js

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

4-
import { render } from '../';
4+
import { render } from '../../';
55
import { waitForElement } from '../wait-for-element';
66

77
test('waits for element to appear in the document', async () => {
@@ -12,7 +12,7 @@ test('waits for element to appear in the document', async () => {
1212
expect(element).toBeTruthy();
1313
});
1414

15-
test('waits for element to appear in a specified baseElement', async () => {
15+
test('waits for element to appear in a specified testRenderer', async () => {
1616
const { rerender, getByTestId } = render(<View />);
1717
const promise = waitForElement(() => getByTestId('test'));
1818
setTimeout(() => rerender(<View testID="test" />));

src/lib/__tests__/wait.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { wait } from '../wait';
1+
import { wait } from '../../';
22

33
test('it waits for the data to be loaded', async () => {
44
const spy = jest.fn();

src/lib/__tests__/within.js

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

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

66
test('it works when scoping to a smaller set of elements', () => {
77
const { getByTestId } = render(

0 commit comments

Comments
 (0)