-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
350 lines (283 loc) · 9.16 KB
/
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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { shallow, mount, render } from 'enzyme';
import assume from 'assume';
import React from 'react';
import assyme from './';
/**
* Helper function which will render
*
* @param {Component} fixture The component we need to render.
* @param {Object} methods Render methods we want to use.
* @returns {Function} helper functions.
* @private
*/
function renderers(fixture, methods) {
const compile = (name, method, desc, fn) => {
it(`(${name}): ${desc}`, (next) => {
const wrapper = method(fixture);
if (fn.length > 1) return fn(wrapper, next);
fn(wrapper);
next();
});
};
//
// Define a default set of render methods we want to run.
//
if (!methods) methods = { shallow };
return (name, fn) => {
Object.keys(methods).forEach((key) => {
compile(key, methods[key], name, fn);
});
}
}
describe('assume-enzyme', function () {
it('works as a plugin', function () {
assume.use(assyme);
});
describe('.enzyme', function () {
function FixtureEnzyme() {
return (
<strong>Hello World</strong>
);
}
const enzymeWrapper = renderers(<FixtureEnzyme />);
it('is a function', function () {
const assumed = assume('what');
assume(assumed.enzyme).is.a('function');
});
enzymeWrapper('an enzyme instance', function (wrapper) {
assume(wrapper).is.enzyme();
});
enzymeWrapper('doesnt think that normal types is enzyme', function () {
assume('hi').is.not.enzyme();
assume({}).is.not.enzyme();
assume([]).is.not.enzyme();
assume(1).is.not.enzyme();
});
});
describe('.className', function () {
function FixtureClassName() {
return (
<div className='hello world'>
<span className='single-class-name'></span>
</div>
);
}
const classNameWrapper = renderers(<FixtureClassName />);
it('is a function', function () {
const assumed = assume('what');
assume(assumed.className).is.a('function');
assume(assumed.classNames).is.a('function');
});
classNameWrapper('finds multiple classNames on the root node', function (wrapper) {
assume(wrapper).has.className('hello');
assume(wrapper).has.className('world');
});
classNameWrapper('finds the single className on the child node', function (wrapper) {
assume(wrapper.find('span')).has.className('single-class-name');
});
classNameWrapper('throws assertion errors when it cannot find the class name', function (wrapper, next) {
assume(wrapper).does.not.have.className('moo');
try { assume(wrapper).has.className('moo'); }
catch (e) { return next(); }
throw new Error('I should fail hard');
});
classNameWrapper('finds classNames deeply in the tree using .anywhere', function (wrapper) {
assume(wrapper).anywhere.has.className('single-class-name');
assume(wrapper).does.not.anywhere.have.className('single-class-names');
});
});
describe('.contains', function () {
function UserContainer(props) {
return (
<div className={props.className}>
hello
</div>
);
}
function FixtureContainer() {
return (
<div className='hello world'>
<UserContainer className='holy' />
<UserContainer className='moly' />
</div>
);
}
function Anywhere() {
return (
<div className='hello world'>
<div id='we' className='need to go'>
<div className='deeper'>
<UserContainer className='holy' />
</div>
</div>
</div>
);
}
const containerWrapper = renderers(<FixtureContainer />);
const containerAnywhere = renderers(<Anywhere />);
it('is a function', function () {
const assumed = assume('what');
assume(assumed.contain).is.a('function');
assume(assumed.contains).is.a('function');
});
containerWrapper('finds the User component', function (wrapper) {
assume(wrapper).contains(<UserContainer className='moly' />);
assume(wrapper).contains(<UserContainer className='holy' />);
assume(wrapper).does.not.contain(<UserContainer className='what' />);
});
containerAnywhere('finds User component deeply in the tree using .anywhere', function (wrapper) {
assume(wrapper).anywhere.contains(<UserContainer className='holy' />);
assume(wrapper).does.not.anywhere.contain(<UserContainer className='holys' />);
});
});
describe('.tagName', function () {
function FixtureTagName() {
return (
<div>
<strong>hi</strong>
</div>
);
}
const tagNameWrapper = renderers(<FixtureTagName />);
it('is a function', function () {
const assumed = assume('what');
assume(assumed.tagName).is.a('function');
});
tagNameWrapper('comparing tagnames', function (wrapper) {
assume(wrapper).has.tagName('div');
assume(wrapper).does.not.have.tagName('strong');
});
});
describe('.checked', function () {
function FixtureChecked() {
return (
<div>
<input id='checkers' defaultChecked />
<input id='not-checked' defaultChecked={false} />
</div>
);
}
const checkedWrapper = renderers(<FixtureChecked />);
it('is a function', function () {
const assumed = assume('what');
assume(assumed.checked).is.a('function');
});
checkedWrapper('sees the input as checked', function (wrapper) {
assume(wrapper.find('#checkers')).is.checked();
assume(wrapper.find('#not-checked')).is.not.checked();
});
});
describe('.disabled', function () {
function FixtureDisabled() {
return (
<div>
<input id='disabled' disabled />
<input id='not-disabled' />
</div>
);
}
const disabledWrapper = renderers(<FixtureDisabled />);
it('is a function', function () {
const assumed = assume('what');
assume(assumed.checked).is.a('function');
});
disabledWrapper('sees the input as checked', function (wrapper) {
assume(wrapper.find('#disabled')).is.disabled();
assume(wrapper.find('#not-disabled')).is.not.disabled();
});
});
describe('#props', function () {
function UserProps(props) {
return (
<div>
hello { props.world }
</div>
);
}
function FixtureProps() {
return (
<div className='hello world'>
<UserProps world='moly' more='props' available={ 1 } required />
<UserProps world='holy' />
</div>
);
}
const propWrapper = renderers(<FixtureProps min={1} max={2} />);
it('is a function', function () {
const assumed = assume('what');
assume(assumed.props).is.a('function');
});
propWrapper('finding the props of the component', function (wrapper) {
assume(wrapper.find(UserProps).first()).to.have.props(['world']);
assume(wrapper.find(UserProps).first()).to.have.props({ 'world': 'moly' });
assume(wrapper.find(UserProps).first()).to.not.have.props(['hi']);
assume(wrapper.find(UserProps).first()).to.not.have.props({ world: 'what' });
});
propWrapper('finds the props on the supplied enzyme instance', function (wrapper) {
assume(wrapper).to.have.props({min: 1, max: 2});
});
});
describe.skip('#html', function () {
function FixtureHTML() {
return (
<div className='hello world'>
<span className='what'>is up</span>
</div>
);
}
const htmlWrapper = renderers(<FixtureHTML />);
it('is a function', function () {
const assumed = assume('what');
assume(assumed.html).is.a('function');
});
htmlWrapper('finding the props of the component', function (wrapper) {
assume(wrapper).to.have.html('<span class="what">is up</span>');
assume(wrapper).to.not.have.html('<strong>non existing tag</strong>');
});
});
describe('#empty', function () {
function Fixture() {
return (
<div className='hello world'>
<span id='empty-thing' className='what'></span>
</div>
);
}
const its = renderers(<Fixture />);
it('is a function', function () {
var assumed = assume('what');
assume(assumed.empty).is.a('function');
assume(assumed.blank).is.a('function');
});
its('finding the props of the component', function (wrapper) {
assume(wrapper).to.is.not.empty();
assume(wrapper.find('#empty-thing')).is.empty();
});
});
describe('#name', function () {
function User(props) {
return (
<div>
hello { props.world }
</div>
);
}
function Fixture() {
return (
<div className='hello world'>
<span id='empty-thing' className='what'></span>
<User world='domination' />
</div>
);
}
const its = renderers(<Fixture />);
it('is a function', function () {
var assumed = assume('what');
assume(assumed.name).is.a('function');
});
its('finding the props of the component', function (wrapper) {
assume(wrapper).to.have.name('div');
assume(wrapper.find(User)).to.have.name('User');
});
});
});