-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcreate-component-shallow.jsx
110 lines (82 loc) · 3.44 KB
/
create-component-shallow.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
const createComponent = require('./react-unit');
const React = require('react');
const R = require('ramda');
const Child = ({ title }) => <h1>{title}</h1>;
const Master = () => <div><Child title="First Child"/></div>;
const SuperMaster = () => <Master/>;
const MasterList = ({ titles }) =>
<ul>
{titles.map((t,i) => <li key={i}><Child title={t}/></li>)}
</ul>;
const titles = [
'Raiders of the Lost Ark',
'Temple of Doom',
'Last Crusade'
];
const Person = ({ name,children }) =>
<div>
<h1>{name}</h1>
<ul>{React.Children.map(children, (c,i) => <li key={i}>{c}</li>)}</ul>
</div>;
describe('createComponent.shallow', () => {
it('renders a single level of depth, preserving components', () => {
const component = createComponent.shallow(
<Person name="Homer">
<Person name="Bart"/>
<Person name="Lisa" />
<Person name="Maggie" />
</Person>);
const lisaByAttr = component.findByQuery('Person[name=Lisa]')[0];
const lisaByTagAndOrder = component.findByQuery('Person')[1];
const lisaByCompAndOrder = component.findByComponent(Person)[1];
expect(lisaByAttr.prop('name')).toEqual('Lisa');
expect(lisaByTagAndOrder.prop('name')).toEqual('Lisa');
expect(lisaByCompAndOrder.prop('name')).toEqual('Lisa');
});
it('should find direct descendent components', () => {
const component = createComponent.shallow(<Master/>);
const results = component.findByQuery('Child');
expect(results.length).toEqual(1);
});
it('should expose props from direct descendent components', () => {
const component = createComponent.shallow(<Master/>);
const results = component.findByQuery('Child')[0];
expect(results.prop('title')).toEqual('First Child');
});
it('should not render child elements of direct descendent components', () => {
const component = createComponent.shallow(<Master/>);
const results = component.findByQuery('h1');
expect(results.length).toEqual(0);
});
it('should render HTML between components', () => {
const component = createComponent.shallow(<Master />);
const child = component.findByQuery('div > Child')[0];
expect(child).not.toBeUndefined();
});
it('should find component rendering just a string as children', () => {
const Content = ({ children }) => <div>{children}</div>;
const Page = () => <Content>Test</Content>;
const component = createComponent.shallow(<Page/>);
expect(component.findByComponent(Content).length).toEqual(1);
});
it('should find component passing the children down to child component', () => {
const Content = ({ children }) => <div>{children}</div>;
const Page = ({ children }) => <Content>{children}</Content>;
const component = createComponent.shallow(<Page><div>Here</div></Page>);
expect(component.findByComponent(Content).length).toEqual(1);
});
it('should allow findByQuery in component props', () => {
const component = createComponent.shallow(<Master />);
const child = component.findByQuery('Child[title="First Child"]')[0];
expect(child).not.toBeUndefined();
});
it('should render lists of direct descendent components', () => {
const component = createComponent.shallow(<MasterList titles={titles} />);
const results = component.findByQuery('Child');
expect(results.length).toEqual(titles.length);
R.compose(
R.forEach(([t,r]) => expect(r.prop('title')).toEqual(t)),
R.zip(titles)
)(results);
});
});