Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @emails react-core
10
*/
11
12
"use strict";
13
14
describe('onlyChild', function() {
15
16
var React;
17
var onlyChild;
18
var WrapComponent;
19
20
beforeEach(function() {
21
React = require('React');
22
onlyChild = require('onlyChild');
23
WrapComponent = React.createClass({
24
render: function() {
25
return (
26
<div>
27
{onlyChild(this.props.children, this.props.mapFn, this)}
28
</div>
29
);
30
}
31
});
32
});
33
34
it('should fail when passed two children', function() {
35
expect(function() {
36
var instance =
37
<WrapComponent>
38
<div />
39
<span />
40
</WrapComponent>;
41
onlyChild(instance.props.children);
42
}).toThrow();
43
});
44
45
it('should fail when passed nully values', function() {
46
expect(function() {
47
var instance =
48
<WrapComponent>
49
{null}
50
</WrapComponent>;
51
onlyChild(instance.props.children);
52
}).toThrow();
53
54
expect(function() {
55
var instance =
56
<WrapComponent>
57
{undefined}
58
</WrapComponent>;
59
onlyChild(instance.props.children);
60
}).toThrow();
61
});
62
63
it('should fail when key/value objects', function() {
64
expect(function() {
65
var instance =
66
<WrapComponent>
67
{{oneThing: <span />}}
68
</WrapComponent>;
69
onlyChild(instance.props.children);
70
}).toThrow();
71
});
72
73
74
it('should not fail when passed interpolated single child', function() {
75
expect(function() {
76
var instance =
77
<WrapComponent>
78
{<span />}
79
</WrapComponent>;
80
onlyChild(instance.props.children);
81
}).not.toThrow();
82
});
83
84
85
it('should return the only child', function() {
86
expect(function() {
87
var instance =
88
<WrapComponent>
89
<span />
90
</WrapComponent>;
91
onlyChild(instance.props.children);
92
}).not.toThrow();
93
});
94
95
});
96
97