-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinon-react.js
More file actions
167 lines (134 loc) · 3.92 KB
/
sinon-react.js
File metadata and controls
167 lines (134 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module.exports = exports = function (sinon) {
return new SinonReact(sinon)
}
exports.SinonReact = SinonReact
var React = require('react')
var cheerio = require('cheerio')
var isBrowser = global.document ? true : false
var defaultContainer
function SinonReact(sinon) {
this.sinon = sinon
this._rendered = []
this._mutated = []
}
function getDefaultContainer() {
if (defaultContainer) {
return defaultContainer
}
var el = document.createElement('div')
var body = document.getElementsByTagName('body')[0]
body.appendChild(el)
defaultContainer = el
return el
}
function renderInBrowser(sr, comp, cont, cb) {
if (isFunc(cont)) {
cb = cont
cont = undefined
}
if (!cont) cont = getDefaultContainer()
var rc = isFunc(cb)
? React.render(comp, cont, cb)
: React.render(comp, cont)
sr.rendered.push(rc)
return rc
}
function isFunc(obj) { return 'function' === typeof obj }
SinonReact.prototype.render = function (component, container, cb) {
return isBrowser
? renderInBrowser(this, component, container, cb)
: React.renderToStaticMarkup(component, container)
}
function reactClassCtor(reactClass) {
return reactClass.type // React 0.11.1
|| reactClass.componentConstructor // React 0.8.0
}
function reactClassProto(reactClass) {
var ctor = reactClassCtor(reactClass)
if (!ctor) {
throw new Error(
'A component constructor could not be found for this class. '
+ 'Are you sure you passed in a the component definition for a React component?'
)
}
return ctor.prototype
}
function stubOrSpy(sr, which, obj, attr, orig, spy) {
sr._mutated.push({
obj: obj
, attr: attr
, orig: orig
, spy: spy
})
return spy;
}
function stubOrSpyReactClass(sr, which, reactClass, method) {
var proto = reactClassProto(reactClass)
var orig = proto[method]
var spy = sr.sinon[which](proto, method)
// react.js will autobind `this` to the correct value and it caches that
// result on a __reactAutoBindMap for performance reasons.
if(proto.__reactAutoBindMap)
proto.__reactAutoBindMap[method] = spy;
return stubOrSpy(sr, which, proto, method, orig, spy)
}
SinonReact.prototype.stubClassMethod = function (reactClass, method) {
return stubOrSpyReactClass(this, 'stub', reactClass, method)
}
SinonReact.prototype.spyOnClassMethod = function (reactClass, method) {
return stubOrSpyReactClass(this, 'spy', reactClass, method)
}
SinonReact.prototype.setClassAttribute = function (reactClass, attribute, value) {
reactClassProto(reactClass)[attribute] = value
return reactClass
}
SinonReact.prototype.stubComponent = function (obj, attr) {
var comp = React.createClass({
render: function () {
return React.DOM.div()
}
})
stubOrSpy(this, 'stub', obj, attr, comp, obj[attr])
return comp
}
function restore(item) {
var obj = item.obj
var spy = item.spy
if (obj.__reactAutoBindMap)
obj.__reactAutoBindMap[item.attr] = item.orig
obj[item.attr] = item.orig
if (spy && isFunc(spy.restore)) spy.restore()
}
SinonReact.prototype.restore = function () {
// we need to do this one by one even if
// `this.sinon` is a sandbox because we also
// potentially need to reset `obj.__reactAutoBindMap`
for (var i = 0; i < this._mutated.length; i++)
restore(this._mutated[i])
this._mutated = []
unmountAll(this._rendered)
this._rendered = []
// if `this.sinon` is a sandbox
if (isFunc(this.sinon.restore))
this.sinon.restore()
}
function unmountAll(rendered) {
for (var i = 0; i < rendered.length; i++)
unmount(rendered[i]);
}
function isMounted(comp) {
return !!component
&& isFunc(comp.isMounted)
&& comp.isMounted()
}
function unmount(component){
return isMounted(component)
? React.unmountComponentAtNode(component.getDOMNode().parentNode)
: false
}
SinonReact.$ = function (el) {
el = isFunc(el.getDOMNode)
? el.getDOMNode().outerHTML
: el
return cheerio.load(el)
}