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
/*jslint evil: true */
13
14
"use strict";
15
16
var React = require('React');
17
var ReactDOM = require('ReactDOM');
18
var ReactMount = require('ReactMount');
19
var ReactTestUtils = require('ReactTestUtils');
20
var div = React.createFactory('div');
21
22
describe('ReactDOM', function() {
23
// TODO: uncomment this test once we can run in phantom, which
24
// supports real submit events.
25
/*
26
it('should bubble onSubmit', function() {
27
var count = 0;
28
var form;
29
var Parent = React.createClass({
30
handleSubmit: function() {
31
count++;
32
return false;
33
},
34
render: function() {
35
return <Child />;
36
}
37
});
38
var Child = React.createClass({
39
render: function() {
40
return <form><input type="submit" value="Submit" /></form>;
41
},
42
componentDidMount: function() {
43
form = this.getDOMNode();
44
}
45
});
46
var instance = ReactTestUtils.renderIntoDocument(<Parent />);
47
form.submit();
48
expect(count).toEqual(1);
49
});
50
*/
51
52
it("should allow children to be passed as an argument", function() {
53
var argDiv = ReactTestUtils.renderIntoDocument(
54
div(null, 'child')
55
);
56
var argNode = ReactMount.getNode(argDiv._rootNodeID);
57
expect(argNode.innerHTML).toBe('child');
58
});
59
60
it("should overwrite props.children with children argument", function() {
61
var conflictDiv = ReactTestUtils.renderIntoDocument(
62
div({children: 'fakechild'}, 'child')
63
);
64
var conflictNode = ReactMount.getNode(conflictDiv._rootNodeID);
65
expect(conflictNode.innerHTML).toBe('child');
66
});
67
68
/**
69
* We need to make sure that updates occur to the actual node that's in the
70
* DOM, instead of a stale cache.
71
*/
72
it("should purge the DOM cache when removing nodes", function() {
73
var myDiv = ReactTestUtils.renderIntoDocument(
74
<div>{{
75
theDog: <div className="dog" />,
76
theBird: <div className="bird" />
77
}}</div>
78
);
79
// Warm the cache with theDog
80
myDiv.setProps({
81
children: {
82
theDog: <div className="dogbeforedelete" />,
83
theBird: <div className="bird" />
84
}
85
});
86
// Remove theDog - this should purge the cache
87
myDiv.setProps({
88
children: {
89
theBird: <div className="bird" />
90
}
91
});
92
// Now, put theDog back. It's now a different DOM node.
93
myDiv.setProps({
94
children: {
95
theDog: <div className="dog" />,
96
theBird: <div className="bird" />
97
}
98
});
99
// Change the className of theDog. It will use the same element
100
myDiv.setProps({
101
children: {
102
theDog: <div className="bigdog" />,
103
theBird: <div className="bird" />
104
}
105
});
106
var root = ReactMount.getNode(myDiv._rootNodeID);
107
var dog = root.childNodes[0];
108
expect(dog.className).toBe('bigdog');
109
});
110
111
it('allow React.DOM factories to be called without warnings', function() {
112
spyOn(console, 'warn');
113
var element = React.DOM.div();
114
expect(element.type).toBe('div');
115
expect(console.warn.argsForCall.length).toBe(0);
116
});
117
118
it('warns but allow dom factories to be used in createFactory', function() {
119
spyOn(console, 'warn');
120
var factory = React.createFactory(React.DOM.div);
121
expect(factory().type).toBe('div');
122
expect(console.warn.argsForCall.length).toBe(1);
123
expect(console.warn.argsForCall[0][0]).toContain(
124
'Do not pass React.DOM.div'
125
);
126
});
127
128
it('warns but allow dom factories to be used in createElement', function() {
129
spyOn(console, 'warn');
130
var element = React.createElement(React.DOM.div);
131
expect(element.type).toBe('div');
132
expect(console.warn.argsForCall.length).toBe(1);
133
expect(console.warn.argsForCall[0][0]).toContain(
134
'Do not pass React.DOM.div'
135
);
136
});
137
});
138
139