-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraptor.test.js
172 lines (160 loc) · 4.72 KB
/
raptor.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
167
168
169
170
171
172
import React from 'react';
import Raptor from './raptor';
import renderer from 'react-test-renderer';
class HelloWorld extends React.Component {
render() {
const {data} = this.props;
return (
<div className="hello-world" foo={data.foo}>
{this.props.children}
</div>
);
}
}
class UserList extends React.Component {
render() {
return <div className="user-list">{this.props.children}</div>;
}
}
class UserItem extends React.Component {
render() {
const {data} = this.props;
return (
<div className="user-item">
<div className="avatar">{data.name[0]}</div>
<div className="primary">{data.name}</div>
<div className="secondary">{data.secondary}</div>
</div>
);
}
}
describe('raptor.views', () => {
describe('addView()', () => {
it('Adds a single view to raptor.views.', () => {
const raptor = new Raptor();
raptor.addView('UserList', UserList);
raptor.addView('UserItem', UserItem);
expect(raptor.views).toEqual({UserList, UserItem});
});
});
describe('addViews()', () => {
it('Adds multiple views to raptor.views.', () => {
const raptor = new Raptor();
raptor.addViews({UserList, UserItem});
expect(raptor.views).toEqual({UserList, UserItem});
});
});
});
describe('raptor.layouts', () => {
const Foo = {herp: 'derp'};
const Bar = {derp: 'herp'};
describe('addLayout()', () => {
it('Adds a single layout to raptor.layouts.', () => {
const raptor = new Raptor();
raptor.addLayout('Foo', Foo);
raptor.addLayout('Bar', Bar);
expect(raptor.layouts).toEqual({Foo, Bar});
});
});
describe('addLayouts()', () => {
it('Adds multiple layouts to raptor.layouts.', () => {
const raptor = new Raptor();
raptor.addLayouts({Foo, Bar});
expect(raptor.layouts).toEqual({Foo, Bar});
});
});
});
describe('raptor.render()', () => {
it('Renders correct JSX from a simple layout and view model.', () => {
const raptor = new Raptor();
raptor.addViews({UserList, UserItem});
raptor.addLayout('Users', {
users: {
$view: 'UserList',
$children: 'UserItem',
},
});
const fragment = raptor.render('Users', {
users: [
{id: '1', name: 'Foo Guy', secondary: '(619) 555-7380'},
{id: '2', name: 'Bar Guy', secondary: '(858) 555-7380'},
{id: '3', name: 'Herp Guy', secondary: '(415) 555-7380'},
{id: '4', name: 'Derp Guy', secondary: '(650) 555-7380'},
],
});
const rendered = renderer.create(fragment);
expect(rendered.toJSON()).toMatchSnapshot();
});
it('Maps model keys to view prop keys according to $props.', () => {
const raptor = new Raptor();
raptor.addViews({UserList, UserItem});
raptor.addLayout('Users', {
users: {
$view: 'UserList',
$children: {
$view: 'UserItem',
$props: {secondary: 'phone'},
},
},
});
const fragment = raptor.render('Users', {
users: [
{id: '1', name: 'Foo Guy', phone: '(619) 555-7380'},
{id: '2', name: 'Bar Guy', phone: '(858) 555-7380'},
],
});
const rendered = renderer.create(fragment);
expect(rendered.toJSON()).toMatchSnapshot();
});
it('Supports reducer functions for $props.', () => {
const raptor = new Raptor();
raptor.addViews({UserList, UserItem});
raptor.addLayout('Users', {
users: {
$view: 'UserList',
$children: {
$view: 'UserItem',
$props: {secondary: (data) => data.phones[0].number},
},
},
});
const fragment = raptor.render('Users', {
users: [
{id: '1', name: 'Foo Guy', phones: [{number: '(619) 555-7380'}]},
{id: '2', name: 'Bar Guy', phones: [{number: '(858) 555-7380'}]},
],
});
const rendered = renderer.create(fragment);
expect(rendered.toJSON()).toMatchSnapshot();
});
it('Renders deeply nested views OK.', () => {
const raptor = new Raptor();
raptor.addViews({HelloWorld});
raptor.addLayout('Home', {
heroCarousel: {
$view: 'HelloWorld',
carouselItems: {
$view: 'HelloWorld',
$children: {
$view: 'HelloWorld',
message: {
$view: 'HelloWorld',
$props: {foo: 'text'},
},
},
},
},
});
const fragment = raptor.render('Home', {
heroCarousel: {
foo: 'Hero Carousel',
carouselItems: [
{foo: 'Carousel Item 1', message: {text: 'Message 1'}},
{foo: 'Carousel Item 2', message: {text: 'Message 2'}},
],
},
});
const rendered = renderer.create(fragment);
expect(rendered.toJSON()).toMatchSnapshot();
});
});