-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdeep-render.test.js
61 lines (51 loc) · 1.55 KB
/
deep-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
const {h, Component} = require('preact');
const {render} = require('./preact-render-spy');
it('componentWillReceiveProps', () => {
class Child extends Component {
constructor(props) {
super(props);
this.state = {value: props.value};
}
componentWillReceiveProps(newProps) {
this.setState({value: `_${newProps.value}_`});
}
render(props, {value}) {
return (
<div>{value}</div>
);
}
}
class Parent extends Component {
constructor(props) {
super(props);
this.state = {value: 'default'};
}
render(props, {value}) {
return (
<Child
onClick={() => this.setState({value: 'clicked'})}
value={value}
/>
);
}
}
const context = render(<Parent />);
const getChild = () => context.find('Child').at(0);
expect(getChild().text()).toBe('default');
getChild().simulate('click');
expect(getChild().text()).toBe('_clicked_');
});
it(`renders components with null children`, () => {
const Null = () => null;
const Node = () => <div><Null />text</div>;
const context = render(<Node />);
expect(context.find('div').text()).toBe('text');
});
it('renders to a specified depth', () => {
const ErrorIfRendered = () => { throw new Error('this should not render'); };
const Second = () => <ErrorIfRendered />;
const First = () => <div><Second /><Second /></div>;
const context = render(<First />, { depth: 2 });
expect(context.find('ErrorIfRendered').length).toBe(2);
expect(() => render(<First />, { depth: 3 })).toThrow();
});