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