forked from testing-library/native-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
132 lines (106 loc) · 4.08 KB
/
events.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
import React from 'react';
import '@testing-library/jest-native/extend-expect';
import { Button, Image, Text, TextInput } from 'react-native';
import { render, fireEvent, eventMap, getEventHandlerName, wait, cleanup } from '../';
afterEach(cleanup);
Object.keys(eventMap).forEach(key => {
describe(`${key} events`, () => {
const config = eventMap[key];
config.forEach(event => {
const spy = jest.fn();
const handlerName = getEventHandlerName(event);
const {
container: {
children: [target],
},
} = render(
React.createElement(key, {
[handlerName]: spy,
}),
);
fireEvent[event](target);
expect(spy).toHaveBeenCalledTimes(1);
});
});
});
test('onChange works', () => {
const handleChange = jest.fn();
const {
container: {
children: [input],
},
} = render(<TextInput onChange={handleChange} />);
fireEvent.change(input, { target: { value: 'a' } });
expect(handleChange).toHaveBeenCalledTimes(1);
});
test('onChangeText works', () => {
function OnChangeText() {
const [text, setText] = React.useState('first');
return <TextInput onChangeText={setText} value={text} testID="input" />;
}
const { getByTestId } = render(<OnChangeText />);
const input = getByTestId('input');
expect(input.props.value).toBe('first');
fireEvent.changeText(input, 'second');
expect(input.props.value).toBe('second');
});
test('assigns target properties', async () => {
class MyComponent extends React.Component {
state = { value: '' };
onChange = ({ nativeEvent }) => {
this.setState({ value: nativeEvent.text });
this.props.onChange();
};
render() {
return <TextInput testID="input" value={this.state.value} onChange={this.onChange} />;
}
}
const spy = jest.fn();
const value = 'a';
const { getByTestId } = render(<MyComponent onChange={spy} />);
const input = getByTestId('input');
fireEvent.change(input, { nativeEvent: { text: value } });
expect(spy).toHaveBeenCalledTimes(1);
await wait(() => expect(input.props.value).toBe(value));
});
test('calling `fireEvent` directly works too', () => {
const handleEvent = jest.fn();
const { container } = render(<Button onPress={handleEvent} title="test" />);
fireEvent(container.children[0], new NativeTestEvent('press'));
expect(handleEvent).toBeCalledTimes(1);
});
test('calling a custom event works as well', () => {
const event = { nativeEvent: { value: 'testing' } };
const onMyEvent = jest.fn(({ nativeEvent }) => expect(nativeEvent).toEqual({ value: 'testing' }));
const MyComponent = ({ onMyEvent }) => <TextInput value="test" onChange={onMyEvent} />;
const {
container: {
children: [input],
},
} = render(<MyComponent onMyEvent={onMyEvent} />);
fireEvent(input, new NativeTestEvent('myEvent', event));
expect(onMyEvent).toHaveBeenCalledWith({ nativeEvent: { value: 'testing' } });
});
test('calling a handler when there is no valid target does not work', () => {
const handleEvent = jest.fn();
const { getByTestId } = render(<Image onPress={handleEvent} testID="image" />);
expect(() => fireEvent.press(getByTestId('image'))).not.toThrow();
expect(handleEvent).toBeCalledTimes(0);
});
test('calling a handler if a Button is disabled does not work', () => {
const handleEvent = jest.fn();
const { getByText } = render(<Button disabled onPress={handleEvent} title="button" />);
expect(() => fireEvent.press(getByText('button'))).not.toThrow();
expect(handleEvent).toBeCalledTimes(0);
});
test('calling an event that has no defined handler throws', () => {
const { getByText } = render(<Text>test</Text>);
const text = getByText('test');
expect(() => fireEvent.changeText(text).toThrow());
});
test('calling an event sets nativeEvent properly', () => {
const event = { nativeEvent: { value: 'testing' } };
const onChange = jest.fn(({ nativeEvent }) => expect(nativeEvent).toEqual({ value: 'testing' }));
const { getByDisplayValue } = render(<TextInput value="test" onChange={onChange} />);
fireEvent.change(getByDisplayValue('test'), event);
});