Skip to content

Commit e74e928

Browse files
authored
Add basic tests for render (#8)
1 parent 61623dd commit e74e928

File tree

5 files changed

+898
-7
lines changed

5 files changed

+898
-7
lines changed

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['module:metro-react-native-babel-preset'],
3+
};

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
"license": "MIT",
99
"private": false,
1010
"devDependencies": {
11+
"@babel/core": "^7.1.2",
12+
"@babel/runtime": "^7.1.2",
1113
"@callstack/eslint-config": "^2.0.0",
14+
"babel-core": "7.0.0-bridge.0",
1215
"babel-jest": "^23.6.0",
1316
"eslint": "^5.6.1",
1417
"flow-bin": "^0.81.0",
1518
"jest": "^23.6.0",
19+
"metro-react-native-babel-preset": "^0.47.1",
1620
"pretty-format": "^23.6.0",
1721
"react": "^16.5.2",
1822
"react-test-renderer": "^16.5.2"

src/__tests__/render.test.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/* eslint-disable react/no-multi-comp */
2+
import React from 'react';
3+
import { View, Text, TouchableOpacity } from 'react-native'; // eslint-disable-line import/no-unresolved
4+
import { render } from '..';
5+
6+
jest.mock(
7+
'react-native',
8+
() => ({
9+
View(props) {
10+
return props.children;
11+
},
12+
Text(props) {
13+
return props.children;
14+
},
15+
TouchableOpacity(props) {
16+
return props.children;
17+
},
18+
}),
19+
{ virtual: true }
20+
);
21+
22+
class Button extends React.Component {
23+
render() {
24+
return (
25+
<TouchableOpacity onPress={this.props.onPress}>
26+
<Text>{this.props.children}</Text>
27+
</TouchableOpacity>
28+
);
29+
}
30+
}
31+
32+
class Banana extends React.Component {
33+
state = {
34+
fresh: false,
35+
};
36+
37+
componentDidUpdate() {
38+
if (this.props.onUpdate) {
39+
this.props.onUpdate();
40+
}
41+
}
42+
43+
componentWillUnmount() {
44+
if (this.props.onUnmount) {
45+
this.props.onUnmount();
46+
}
47+
}
48+
49+
changeFresh = () => {
50+
this.setState(state => ({
51+
fresh: !state.fresh,
52+
}));
53+
};
54+
55+
render() {
56+
return (
57+
<View>
58+
<Text>Is the banana fresh?</Text>
59+
<Text testID="bananaFresh">
60+
{this.state.fresh ? 'fresh' : 'not fresh'}
61+
</Text>
62+
<Button onPress={this.changeFresh} type="primary">
63+
Change freshness!
64+
</Button>
65+
</View>
66+
);
67+
}
68+
}
69+
70+
test('getByTestId', () => {
71+
const { getByTestId } = render(<Banana />);
72+
const component = getByTestId('bananaFresh');
73+
74+
expect(component.props.children).toBe('not fresh');
75+
expect(() => {
76+
getByTestId('InExistent');
77+
}).toThrow();
78+
});
79+
80+
test('getByName', () => {
81+
const { getByTestId, getByName } = render(<Banana />);
82+
const bananaFresh = getByTestId('bananaFresh');
83+
const button = getByName('Button');
84+
85+
button.props.onPress();
86+
87+
expect(bananaFresh.props.children).toBe('fresh');
88+
89+
const sameButton = getByName(Button);
90+
sameButton.props.onPress();
91+
92+
expect(bananaFresh.props.children).toBe('not fresh');
93+
94+
expect(() => {
95+
getByName('InExistent');
96+
}).toThrow();
97+
});
98+
99+
test('getAllByName', () => {
100+
const { getAllByName } = render(<Banana />);
101+
const [text, status, button] = getAllByName('Text');
102+
103+
expect(text.props.children).toBe('Is the banana fresh?');
104+
expect(status.props.children).toBe('not fresh');
105+
expect(button.props.children).toBe('Change freshness!');
106+
expect(getAllByName('InExistent')).toHaveLength(0);
107+
});
108+
109+
test('getByText', () => {
110+
const { getByText } = render(<Banana />);
111+
const button = getByText(/change/i);
112+
113+
expect(button.props.children).toBe('Change freshness!');
114+
115+
const sameButton = getByText('not fresh');
116+
117+
expect(sameButton.props.children).toBe('not fresh');
118+
expect(() => {
119+
getByText('InExistent');
120+
}).toThrow();
121+
});
122+
123+
test('getAllByText', () => {
124+
const { getAllByText } = render(<Banana />);
125+
const button = getAllByText(/fresh/i);
126+
127+
expect(button).toHaveLength(3);
128+
expect(getAllByText('InExistent')).toHaveLength(0);
129+
});
130+
131+
test('getByProps', () => {
132+
const { getByProps } = render(<Banana />);
133+
const primaryType = getByProps({ type: 'primary' });
134+
135+
expect(primaryType.props.children).toBe('Change freshness!');
136+
expect(() => {
137+
getByProps({ type: 'inexistent' });
138+
}).toThrow();
139+
});
140+
141+
test('getAllByProps', () => {
142+
const { getAllByProps } = render(<Banana />);
143+
const primaryTypes = getAllByProps({ type: 'primary' });
144+
145+
expect(primaryTypes).toHaveLength(1);
146+
expect(getAllByProps({ type: 'inexistent' })).toHaveLength(0);
147+
});
148+
149+
test('update', () => {
150+
const fn = jest.fn();
151+
const { getByName, update } = render(<Banana onUpdate={fn} />);
152+
const button = getByName('Button');
153+
154+
button.props.onPress();
155+
156+
update(<Banana onUpdate={fn} />);
157+
158+
expect(fn).toHaveBeenCalledTimes(2);
159+
});
160+
161+
test('unmount', () => {
162+
const fn = jest.fn();
163+
const { unmount } = render(<Banana onUnmount={fn} />);
164+
unmount();
165+
expect(fn).toHaveBeenCalled();
166+
});

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ export const debug = (
8383
prettyFormat(output, {
8484
plugins: [plugins.ReactTestComponent, plugins.ReactElement],
8585
}),
86-
message
86+
message || ''
8787
);
8888
};

0 commit comments

Comments
 (0)