-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchai-react.js
225 lines (181 loc) · 6.53 KB
/
chai-react.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
(function (chaiReact) {
// Module systems magic dance.
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// NodeJS
module.exports = function (chai, utils) {
return chaiReact(chai, utils, require('react'), require('react-dom/test-utils'));
};
} else if (typeof define === "function" && define.amd) {
// AMD
define(['react', 'react-dom/test-utils'], function (React, TestUtils) {
return function (chai, utils) {
return chaiReact(chai, utils, React, TestUtils);
};
});
} else {
// Other environment (usually <script> tag): plug in to global chai instance directly.
chai.use(function (chai, utils) {
return chaiReact(chai, utils, React, ReactTestUtils);
});
}
}(function (chai, utils, React, TestUtils) {
var flag = utils.flag;
function inspectify (component) {
component.inspect = function () {
return 'React component';
};
}
function getComponentProp (component, prop) {
if (TestUtils.isDOMComponent(component)) {
if (prop === 'className') {
return component.className;
} else {
return component.getAttribute(prop);
}
} else if (component.nodeType === Node.TEXT_NODE) {
// Skip text nodes as they don't have any props
} else {
return component.props[prop];
}
}
function componentHasProp (component, prop) {
if (TestUtils.isDOMComponent(component)) {
return component.hasAttribute(prop);
} else {
return component.props && prop in component.props;
}
}
chai.Assertion.addMethod('state', function (name, value) {
var component = flag(this, 'object'),
state = component.state || {},
actual = state[name];
new chai.Assertion(component).is.a.component;
inspectify(component);
if (!flag(this, 'negate') || undefined === value) {
this.assert(
undefined !== actual,
'expected component to have state \'' + name + '\' #{exp}',
'expected component not to have state \'' + name + '\' #{exp}'
);
}
if (undefined !== value) {
this.assert(
value === actual,
'expected component to have state \'' + name + '\' with the value #{exp}, but the value was #{act}',
'expected component not to have state \'' + name + '\' with the value #{act}',
value,
actual
);
}
flag(this, 'object', actual);
});
chai.Assertion.addMethod('prop', function (name, value) {
var actual,
component = flag(this, 'object');
new chai.Assertion(component).is.a.component;
actual = getComponentProp(component, name);
inspectify(component);
if (!flag(this, 'negate') || undefined === value) {
this.assert(
undefined !== actual,
'expected component to have prop \'' + name + '\' defined',
'expected component not to have prop \'' + name + '\' defined'
);
}
if (undefined !== value) {
this.assert(
value === actual,
'expected component to have prop \'' + name + '\' with the value #{exp}, but the value was #{act}',
'expected component not to have prop \'' + name + '\' with the value #{act}',
value,
actual
);
}
flag(this, 'object', actual);
});
chai.Assertion.addMethod('componentsWithProp', function (name, value, match) {
var components,
component = flag(this, 'object');
new chai.Assertion(component).is.a.component;
inspectify(component);
components = TestUtils.findAllInRenderedTree(component, function (comp) {
var prop;
if (value !== undefined) {
prop = getComponentProp(comp, name);
switch (match) {
case 'contains':
return typeof prop === 'string' && prop.indexOf(value) !== -1;
default:
return prop === value;
}
}
return componentHasProp(comp, name);
});
if (undefined !== value) {
this.assert(
components.length > 0,
'expected component tree to have a component with prop \'' + name + '\' with the value #{exp}',
'expected component tree not to have prop \'' + name + '\' with the value #{exp}',
value
);
} else {
this.assert(
components.length > 0,
'expected component tree to have at least one component with prop \'' + name + '\' defined',
'expected component tree not to have a component with prop \'' + name + '\' defined'
);
}
flag(this, 'object', components);
});
chai.Assertion.addMethod('componentsOfType', function (type) {
var components = [];
var component = flag(this, 'object');
new chai.Assertion(component).is.a.component;
inspectify(component);
components = TestUtils.scryRenderedComponentsWithType(component, type);
flag(this, 'object', components);
});
chai.Assertion.addMethod('componentOfType', function (type) {
var component = flag(this, 'object');
new chai.Assertion(component).is.a.component;
inspectify(component);
var found = TestUtils.findRenderedComponentWithType(component, type);
flag(this, 'object', found);
});
chai.Assertion.addMethod('componentsWithTag', function (tag) {
var components = [];
var component = flag(this, 'object');
new chai.Assertion(component).is.a.component;
inspectify(component);
components = TestUtils.scryRenderedDOMComponentsWithTag(component, tag);
flag(this, 'object', components);
});
chai.Assertion.addMethod('componentWithTag', function (tag) {
var found,
component = flag(this, 'object');
if (TestUtils.isDOMComponent(component)) {
found = component.tagName.toLowerCase() === tag.toLowerCase();
} else {
new chai.Assertion(component).is.a.component;
inspectify(component);
found = TestUtils.findRenderedDOMComponentWithTag(component, tag);
}
flag(this, 'object', found);
});
chai.Assertion.addProperty('component', function () {
var component = flag(this, 'object');
this.assert(
component && (TestUtils.isDOMComponent(component) || TestUtils.isCompositeComponent(component)),
'expected #{this} to be a valid React component, but it is not',
'expected #{this} to not be a valid React component, but it is'
);
});
chai.Assertion.addProperty('element', function () {
var element = flag(this, 'object');
this.assert(
React.isValidElement(element),
'expected #{this} to be a valid React element, but it is not',
'expected #{this} to not be a valid React element, but it is'
);
});
}));