-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathevents.jsx
113 lines (92 loc) · 3.07 KB
/
events.jsx
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
// Note: you should use const createComponent = require('react-unit');
const createComponent = require('./react-unit');
const React = require('react');
class ToUppercaseInput extends React.Component {
constructor(props, ctx) {
super(props, ctx);
this.onChange = this.onChange.bind(this);
}
onChange(e) {
this.props.onChange({target:{
value: e.target.value.toUpperCase()
}});
}
render() {
return <input
onChange={this.onChange}
value={this.props.value.toUpperCase()} />
}
}
class OnlyUppercaseInput extends React.Component {
constructor(props, ctx) {
super(props, ctx);
this.onChange = this.onChange.bind(this);
}
onChange(e) {
if (e.target.value != e.target.value.toUpperCase()) return;
this.props.onChange({target:{ value: e.target.value }});
}
render() {
return <input
onChange={this.onChange}
value={this.props.value.toUpperCase()} />
}
}
class SimpleButton extends React.Component {
constructor(props, ctx) {
super(props, ctx);
this.onClick = this.onClick.bind(this);
}
onClick(e) {
// not using { target: { value: 'clicked' } } to show some variety
this.props.onClick('clicked');
}
render() {
return <button onClick={this.onClick}>Click me</button>
}
}
describe('events', () => {
it('should apply handler logic', () => {
let changedValue; // to be updated by the component
const component = createComponent(<ToUppercaseInput
value="HELLO"
onChange={e => changedValue = e.target.value}
/>);
// Trigger the change event
const input = component.findByQuery('input')[0];
input.onChange({target: {value: 'HELLO, world'}});
expect(changedValue).toEqual('HELLO, WORLD');
});
it('might be cancelled', () => {
let changedValue; // to be updated by the component
const component = createComponent(<OnlyUppercaseInput
value="HELLO"
onChange={e => changedValue = e.target.value}
/>);
// Trigger the change event
const input = component.findByQuery('input')[0];
input.onChange({target: {value: 'HELLO, world'}});
expect(changedValue).toBeUndefined();
});
it('can be called using shorthand methods (onClick and onChange)', () => {
let message; // to be updated by the component
const component = createComponent(<SimpleButton onClick={e => message = e}/>);
// Trigger the click event
component.findByQuery('button')[0].onClick();
expect(message).toBe('clicked');
});
it('can be called using on(event)', () => {
let message; // to be updated by the component
const component = createComponent(<SimpleButton onClick={e => message = e}/>);
// Trigger the click event
component.findByQuery('button')[0].on('click');
expect(message).toBe('clicked');
});
it('can be called directly using props', () => {
let message; // to be updated by the component
const component = createComponent(<SimpleButton onClick={e => message = e}/>);
// Trigger the click event
component.findByQuery('button')[0].props.onClick();
expect(message).toBe('clicked');
});
});