-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease-1.2.0.js
More file actions
240 lines (196 loc) · 5.88 KB
/
Copy pathrelease-1.2.0.js
File metadata and controls
240 lines (196 loc) · 5.88 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import React, { PropTypes } from 'react';
import renderer from 'react-test-renderer';
import forge, { ThemeProp } from '../lib';
describe('Release 1.2.0: Theme property', () => {
it('Feature should returns only object', () => {
const featureMock1 = jest.fn(() => {});
const AwesomeComponent = () => <div>Hello</div>;
const features = forge(featureMock1);
const ForgedComponent = features(AwesomeComponent);
expect(() => {
renderer.create(<ForgedComponent />);
}).toThrow(`Forgekit <${AwesomeComponent.name}/>: "${featureMock1.name}" feature should return Object`);
});
it('Should throw Error if theme is wrong type', () => {
const featureMock1 = jest.fn(() => {});
featureMock1.propTypes = {
theme: PropTypes.shape({
foo: PropTypes.string,
}),
};
const features = forge(featureMock1);
const AwesomeComponent = () => <div>Hello</div>;
/**
* except to throw Error becasue propType theme should be only ThemeProp type
* import { ThemeProp } from 'forgekit'
*/
expect(() => {
features(AwesomeComponent);
}).toThrow(`Forgekit <${AwesomeComponent.name}/>: property "theme" should be the ThemeProp type`);
});
it('Should merge all theme prop from all features', () => {
const featureMock1 = jest.fn(() => {});
featureMock1.propTypes = {
theme: ThemeProp({
foo: PropTypes.string,
}),
};
const featureMock2 = jest.fn(() => {});
featureMock2.propTypes = {
theme: ThemeProp({
bar: PropTypes.string,
}),
};
const featureMock3 = jest.fn(() => {});
featureMock3.propTypes = {
theme: ThemeProp({
baz: PropTypes.string,
}),
};
const AwesomeComponent = () => <div>Hello</div>;
AwesomeComponent.propTypes = {
theme: ThemeProp({
base: PropTypes.string,
}),
};
const features = forge(featureMock1, featureMock2, featureMock3);
const ForgedComponent = features(AwesomeComponent);
expect(ForgedComponent.propTypes.theme.themeKeys).toEqual([
'base',
'foo',
'bar',
'baz',
]);
});
it('Should pick only defined theme keys for feature', () => {
const featureMock1 = jest.fn(() => ({}));
featureMock1.propTypes = {
theme: ThemeProp({
foo: PropTypes.string,
}),
};
const featureMock2 = jest.fn(() => ({}));
featureMock2.propTypes = {
theme: ThemeProp({
bar: PropTypes.string,
}),
};
const features = forge(featureMock1, featureMock2);
const AwesomeComponent = jest.fn(() => <div>Hello</div>);
AwesomeComponent.propTypes = {
theme: ThemeProp({
baz: PropTypes.string,
}),
};
const ForgedComponent = features(AwesomeComponent);
renderer.create(<ForgedComponent theme={{ foo: 'foo', bar: 'bar', baz: 'baz' }} />);
expect(featureMock1.mock.calls.length).toEqual(1);
expect(featureMock2.mock.calls.length).toEqual(1);
expect(AwesomeComponent.mock.calls[0][0].theme).toEqual({
baz: 'baz',
});
// expect that only requested theme keys should be passed to feature function
expect(featureMock1.mock.calls[0][0].theme).toEqual({
foo: 'foo',
});
// expect that only requested theme keys should be passed to feature function
expect(featureMock2.mock.calls[0][0].theme).toEqual({
bar: 'bar',
});
});
it('Should pass default theme values', () => {
const featureMock1 = jest.fn(() => ({}));
featureMock1.propTypes = {
theme: ThemeProp({
foo: PropTypes.string,
bar: PropTypes.string,
}),
};
featureMock1.defaultProps = {
theme: {
foo: 'defaultFoo',
bar: 'defaultBar',
},
};
const features = forge(featureMock1);
const AwesomeComponent = () => <div>Hello</div>;
const ForgedComponent = features(AwesomeComponent);
renderer.create(<ForgedComponent />);
expect(featureMock1.mock.calls[0][0].theme).toEqual({
/**
* All valus are defained at featureMock1.defaultProps.theme
*/
foo: 'defaultFoo',
bar: 'defaultBar',
});
});
it('Should mere all theme keys from Component and all features', () => {
const featureMock1 = jest.fn(() => ({}));
featureMock1.propTypes = {
theme: ThemeProp({
foo1: PropTypes.string,
bar1: PropTypes.string,
}),
};
featureMock1.defaultProps = {
theme: {
foo1: 'defaultFoo1',
bar1: 'defaultBar1',
},
};
const featureMock2 = jest.fn(() => ({}));
featureMock2.propTypes = {
theme: ThemeProp({
foo2: PropTypes.string,
}),
};
featureMock2.defaultProps = {
theme: {
foo2: 'defaultFoo2',
},
};
const features = forge(featureMock1, featureMock2);
const AwesomeComponent = jest.fn(() => <div>Hello</div>);
AwesomeComponent.propTypes = {
theme: ThemeProp({
base: PropTypes.string,
style: PropTypes.string,
}),
};
AwesomeComponent.defaultProps = {
theme: {
base: 'base',
},
};
const ForgedComponent = features(AwesomeComponent, 'AwesomeComponent', {
theme: {
style: 'style-from-forge',
},
});
expect(ForgedComponent.propTypes.theme.themeKeys).toEqual([
'base',
'style',
'foo1',
'bar1',
'foo2',
]);
expect(ForgedComponent.defaultProps.theme).toEqual({
foo1: 'defaultFoo1',
bar1: 'defaultBar1',
foo2: 'defaultFoo2',
base: 'base',
});
renderer.create(<ForgedComponent />);
expect(AwesomeComponent.mock.calls[0][0].theme).toEqual({
base: 'base',
style: 'style-from-forge',
});
expect(featureMock1.mock.calls[0][0].theme).toEqual({
foo1: 'defaultFoo1',
bar1: 'defaultBar1',
});
expect(featureMock2.mock.calls[0][0].theme).toEqual({
foo2: 'defaultFoo2',
});
});
});