Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81159 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
describe('ReactMount', function() {
17
var React = require('React');
18
var ReactMount = require('ReactMount');
19
var ReactTestUtils = require('ReactTestUtils');
20
21
describe('constructAndRenderComponentByID', function() {
22
it('throws if given an id for a component that doesn\'t exist', function() {
23
expect(function() {
24
ReactMount.constructAndRenderComponentByID(
25
function dummyComponentConstructor() {},
26
{},
27
'SOME_ID_THAT_DOESNT_EXIST'
28
);
29
}).toThrow();
30
});
31
});
32
33
it('throws when given a string', function() {
34
expect(function() {
35
ReactTestUtils.renderIntoDocument('div');
36
}).toThrow(
37
'Invariant Violation: renderComponent(): Invalid component element. ' +
38
'Instead of passing an element string, make sure to instantiate it ' +
39
'by passing it to React.createElement.'
40
);
41
});
42
43
it('throws when given a factory', function() {
44
var Component = React.createClass({
45
render: function() {
46
return <div />;
47
}
48
});
49
expect(function() {
50
ReactTestUtils.renderIntoDocument(Component);
51
}).toThrow(
52
'Invariant Violation: renderComponent(): Invalid component element. ' +
53
'Instead of passing a component class, make sure to instantiate it ' +
54
'by passing it to React.createElement.'
55
);
56
});
57
58
it('should render different components in same root', function() {
59
var container = document.createElement('container');
60
document.documentElement.appendChild(container);
61
62
ReactMount.render(<div></div>, container);
63
expect(container.firstChild.nodeName).toBe('DIV');
64
65
ReactMount.render(<span></span>, container);
66
expect(container.firstChild.nodeName).toBe('SPAN');
67
});
68
69
it('should unmount and remount if the key changes', function() {
70
var container = document.createElement('container');
71
72
var mockMount = mocks.getMockFunction();
73
var mockUnmount = mocks.getMockFunction();
74
75
var Component = React.createClass({
76
componentDidMount: mockMount,
77
componentWillUnmount: mockUnmount,
78
render: function() {
79
return <span>{this.props.text}</span>;
80
}
81
});
82
83
expect(mockMount.mock.calls.length).toBe(0);
84
expect(mockUnmount.mock.calls.length).toBe(0);
85
86
ReactMount.render(<Component text="orange" key="A" />, container);
87
expect(container.firstChild.innerHTML).toBe('orange');
88
expect(mockMount.mock.calls.length).toBe(1);
89
expect(mockUnmount.mock.calls.length).toBe(0);
90
91
// If we change the key, the component is unmounted and remounted
92
ReactMount.render(<Component text="green" key="B" />, container);
93
expect(container.firstChild.innerHTML).toBe('green');
94
expect(mockMount.mock.calls.length).toBe(2);
95
expect(mockUnmount.mock.calls.length).toBe(1);
96
97
// But if we don't change the key, the component instance is reused
98
ReactMount.render(<Component text="blue" key="B" />, container);
99
expect(container.firstChild.innerHTML).toBe('blue');
100
expect(mockMount.mock.calls.length).toBe(2);
101
expect(mockUnmount.mock.calls.length).toBe(1);
102
});
103
104
it('should reuse markup if rendering to the same target twice', function() {
105
var container = document.createElement('container');
106
var instance1 = React.render(<div />, container);
107
var instance2 = React.render(<div />, container);
108
109
expect(instance1 === instance2).toBe(true);
110
});
111
});
112
113