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
var mocks = require('mocks');
15
16
var React;
17
var ReactTestUtils;
18
var reactComponentExpect;
19
20
var TestComponent;
21
var TestComponentWithPropTypes;
22
var TestComponentWithReverseSpec;
23
var mixinPropValidator;
24
var componentPropValidator;
25
26
describe('ReactCompositeComponent-mixin', function() {
27
28
beforeEach(function() {
29
React = require('React');
30
ReactTestUtils = require('ReactTestUtils');
31
reactComponentExpect = require('reactComponentExpect');
32
mixinPropValidator = mocks.getMockFunction();
33
componentPropValidator = mocks.getMockFunction();
34
35
var MixinA = {
36
propTypes: {
37
propA: function() {}
38
},
39
componentDidMount: function() {
40
this.props.listener('MixinA didMount');
41
}
42
};
43
44
var MixinB = {
45
mixins: [MixinA],
46
propTypes: {
47
propB: function() {}
48
},
49
componentDidMount: function() {
50
this.props.listener('MixinB didMount');
51
}
52
};
53
54
var MixinBWithReverseSpec = {
55
componentDidMount: function() {
56
this.props.listener('MixinBWithReverseSpec didMount');
57
},
58
mixins: [MixinA]
59
};
60
61
var MixinC = {
62
statics: {
63
staticC: function() {}
64
},
65
componentDidMount: function() {
66
this.props.listener('MixinC didMount');
67
}
68
};
69
70
var MixinD = {
71
propTypes: {
72
value: mixinPropValidator
73
}
74
};
75
76
TestComponent = React.createClass({
77
mixins: [MixinB, MixinC, MixinD],
78
statics: {
79
staticComponent: function() {}
80
},
81
propTypes: {
82
propComponent: function() {}
83
},
84
componentDidMount: function() {
85
this.props.listener('Component didMount');
86
},
87
render: function() {
88
return <div />;
89
}
90
});
91
92
TestComponentWithReverseSpec = React.createClass({
93
render: function() {
94
return <div />;
95
},
96
componentDidMount: function() {
97
this.props.listener('Component didMount');
98
},
99
mixins: [MixinBWithReverseSpec, MixinC, MixinD]
100
});
101
102
TestComponentWithPropTypes = React.createClass({
103
mixins: [MixinD],
104
propTypes: {
105
value: componentPropValidator
106
},
107
render: function() {
108
return <div />;
109
}
110
});
111
});
112
113
it('should support merging propTypes and statics', function() {
114
var listener = mocks.getMockFunction();
115
var instance = <TestComponent listener={listener} />;
116
instance = ReactTestUtils.renderIntoDocument(instance);
117
118
var instancePropTypes = instance.constructor.propTypes;
119
120
expect('propA' in instancePropTypes).toBe(true);
121
expect('propB' in instancePropTypes).toBe(true);
122
expect('propComponent' in instancePropTypes).toBe(true);
123
124
expect('staticC' in TestComponent).toBe(true);
125
expect('staticComponent' in TestComponent).toBe(true);
126
});
127
128
it('should support chaining delegate functions', function() {
129
var listener = mocks.getMockFunction();
130
var instance = <TestComponent listener={listener} />;
131
instance = ReactTestUtils.renderIntoDocument(instance);
132
133
expect(listener.mock.calls).toEqual([
134
['MixinA didMount'],
135
['MixinB didMount'],
136
['MixinC didMount'],
137
['Component didMount']
138
]);
139
});
140
141
it('should chain functions regardless of spec property order', function() {
142
var listener = mocks.getMockFunction();
143
var instance = <TestComponentWithReverseSpec listener={listener} />;
144
instance = ReactTestUtils.renderIntoDocument(instance);
145
146
expect(listener.mock.calls).toEqual([
147
['MixinA didMount'],
148
['MixinBWithReverseSpec didMount'],
149
['MixinC didMount'],
150
['Component didMount']
151
]);
152
});
153
154
it('should validate prop types via mixins', function() {
155
expect(TestComponent.type.propTypes).toBeDefined();
156
expect(TestComponent.type.propTypes.value)
157
.toBe(mixinPropValidator);
158
});
159
160
it('should override mixin prop types with class prop types', function() {
161
// Sanity check...
162
expect(componentPropValidator).toNotBe(mixinPropValidator);
163
// Actually check...
164
expect(TestComponentWithPropTypes.type.propTypes)
165
.toBeDefined();
166
expect(TestComponentWithPropTypes.type.propTypes.value)
167
.toNotBe(mixinPropValidator);
168
expect(TestComponentWithPropTypes.type.propTypes.value)
169
.toBe(componentPropValidator);
170
});
171
});
172
173