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('sliceChildren', function() {
15
16
var React;
17
var ReactTestUtils;
18
19
var sliceChildren;
20
var reactComponentExpect;
21
22
var Partial;
23
24
beforeEach(function() {
25
React = require('React');
26
ReactTestUtils = require('ReactTestUtils');
27
28
sliceChildren = require('sliceChildren');
29
reactComponentExpect = require('reactComponentExpect');
30
31
Partial = React.createClass({
32
render: function() {
33
return (
34
<div>
35
{sliceChildren(
36
this.props.children,
37
this.props.start,
38
this.props.end
39
)}
40
</div>
41
);
42
}
43
});
44
});
45
46
function renderAndSlice(set, start, end) {
47
var instance = <Partial start={start} end={end}>{set}</Partial>;
48
instance = ReactTestUtils.renderIntoDocument(instance);
49
var rendered = reactComponentExpect(instance)
50
.expectRenderedChild()
51
.instance();
52
return rendered.props.children;
53
}
54
55
it('should render the whole set if start zero is supplied', function() {
56
var fullSet = [
57
<div key="A" />,
58
<div key="B" />,
59
<div key="C" />
60
];
61
var children = renderAndSlice(fullSet, 0);
62
expect(children).toEqual({
63
'.$A': fullSet[0],
64
'.$B': fullSet[1],
65
'.$C': fullSet[2]
66
});
67
});
68
69
it('should render the remaining set if no end index is supplied', function() {
70
var fullSet = [
71
<div key="A" />,
72
<div key="B" />,
73
<div key="C" />
74
];
75
var children = renderAndSlice(fullSet, 1);
76
expect(children).toEqual({
77
'.$B': fullSet[1],
78
'.$C': fullSet[2]
79
});
80
});
81
82
it('should exclude everything at or after the end index', function() {
83
var fullSet = [
84
<div key="A" />,
85
<div key="B" />,
86
<div key="C" />,
87
<div key="D" />
88
];
89
var children = renderAndSlice(fullSet, 1, 2);
90
expect(children).toEqual({
91
'.$B': fullSet[1]
92
});
93
});
94
95
it('should allow static children to be sliced', function() {
96
var a = <div />;
97
var b = <div />;
98
var c = <div />;
99
100
var instance = <Partial start={1} end={2}>{a}{b}{c}</Partial>;
101
instance = ReactTestUtils.renderIntoDocument(instance);
102
var rendered = reactComponentExpect(instance)
103
.expectRenderedChild()
104
.instance();
105
106
expect(rendered.props.children).toEqual({
107
'.1': b
108
});
109
});
110
111
});
112
113