1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.default = assertions;
|
9 |
|
10 | var _react = _interopRequireDefault(require("react"));
|
11 |
|
12 | var _reactDom = require("react-dom");
|
13 |
|
14 | var _uiTestQueries = require("@instructure/ui-test-queries");
|
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 | function assertions(chai, utils) {
|
40 | const flag = utils.flag,
|
41 | inspect = utils.inspect;
|
42 | const Assertion = chai.Assertion;
|
43 |
|
44 | function wrapObj(obj) {
|
45 | if (obj && typeof obj.getDOMNode === 'function') {
|
46 | return obj;
|
47 | }
|
48 |
|
49 | let node;
|
50 |
|
51 | if ((0, _uiTestQueries.isElement)(obj)) {
|
52 | node = obj;
|
53 | } else if ( _react.default.isValidElement(obj)) {
|
54 | node = (0, _reactDom.findDOMNode)(obj);
|
55 | }
|
56 |
|
57 | if (node) {
|
58 | return (0, _uiTestQueries.wrapQueryResult)(node);
|
59 | }
|
60 |
|
61 | return void 0;
|
62 | }
|
63 |
|
64 | function addAssertion(name, assertion) {
|
65 | if (Assertion.prototype[name]) {
|
66 | overwriteMethod(name, assertion);
|
67 | } else {
|
68 | addMethod(name, assertion);
|
69 | }
|
70 | }
|
71 |
|
72 | function overwriteProperty(name, assertion) {
|
73 | Assertion.overwriteProperty(name, function (_super) {
|
74 | return wrapOverwriteAssertion(assertion, _super);
|
75 | });
|
76 | }
|
77 |
|
78 | function overwriteMethod(name, assertion) {
|
79 | Assertion.overwriteMethod(name, function (_super) {
|
80 | return wrapOverwriteAssertion(assertion, _super);
|
81 | });
|
82 | }
|
83 |
|
84 | function addMethod(name, assertion) {
|
85 | Assertion.addMethod(name, wrapAssertion(assertion));
|
86 | }
|
87 |
|
88 | function addChainableMethod(name, assertion) {
|
89 | Assertion.addChainableMethod(name, wrapAssertion(assertion));
|
90 | }
|
91 |
|
92 | function overwriteChainableMethod(name, assertion) {
|
93 | Assertion.overwriteChainableMethod(name, function (_super) {
|
94 | return wrapOverwriteAssertion(assertion, _super);
|
95 | }, function (_super) {
|
96 | return function () {
|
97 | _super.call(this);
|
98 | };
|
99 | });
|
100 | }
|
101 |
|
102 | function wrapOverwriteAssertion(assertion, _super) {
|
103 | return function (arg1, arg2) {
|
104 | const wrapper = wrapObj(flag(this, 'object'));
|
105 |
|
106 | if (!wrapper) {
|
107 |
|
108 | return _super.apply(this, arguments);
|
109 | }
|
110 |
|
111 | assertion.call(this, {
|
112 | markup: () => wrapper.toString(),
|
113 | sig: inspect(wrapper.getDOMNode()),
|
114 | wrapper,
|
115 | arg1,
|
116 | arg2,
|
117 | flag,
|
118 | inspect
|
119 | });
|
120 | };
|
121 | }
|
122 |
|
123 | function wrapAssertion(assertion) {
|
124 | return function (arg1, arg2) {
|
125 | const wrapper = wrapObj(flag(this, 'object'));
|
126 | const config = {
|
127 | wrapper,
|
128 | arg1,
|
129 | flag,
|
130 | inspect
|
131 | };
|
132 |
|
133 | if (wrapper) {
|
134 | config.markup = () => wrapper.toString();
|
135 |
|
136 | config.sig = inspect(wrapper.getDOMNode());
|
137 | }
|
138 |
|
139 | if (arguments.length > 1) {
|
140 | config.arg2 = arg2;
|
141 | }
|
142 |
|
143 | assertion.call(this, config);
|
144 | };
|
145 | }
|
146 |
|
147 | overwriteProperty('not', function () {
|
148 | flag(this, 'negate', true);
|
149 | });
|
150 | addChainableMethod('exactly', function exactly(_ref) {
|
151 | let flag = _ref.flag,
|
152 | arg1 = _ref.arg1;
|
153 | flag(this, 'exactlyCount', arg1);
|
154 | });
|
155 | addAssertion('text', function text(_ref2) {
|
156 | let wrapper = _ref2.wrapper,
|
157 | markup = _ref2.markup,
|
158 | flag = _ref2.flag,
|
159 | arg1 = _ref2.arg1,
|
160 | arg2 = _ref2.arg2,
|
161 | sig = _ref2.sig;
|
162 | const actual = wrapper.text();
|
163 |
|
164 | if (typeof arg1 !== 'undefined') {
|
165 | if (flag(this, 'contains')) {
|
166 | this.assert(actual && actual.indexOf(String(arg1)) > -1, () => `expected ${sig} to contain text #{exp}, but it has #{act} ${markup()}`, () => `expected ${sig} not to contain text #{exp}, but it has #{act} ${markup()}`, arg1, actual);
|
167 | } else {
|
168 | this.assert(actual && (0, _uiTestQueries.matches)(actual, arg1, arg2), () => `expected ${sig} to have text #{exp}, but it has #{act} ${markup()}`, () => `expected ${sig} to not have text #{exp}, but it has #{act} ${markup()}`, arg1, actual);
|
169 | }
|
170 | }
|
171 |
|
172 | flag(this, 'object', actual);
|
173 | });
|
174 | overwriteChainableMethod('contain', function contain(_ref3) {
|
175 | let wrapper = _ref3.wrapper,
|
176 | markup = _ref3.markup,
|
177 | arg1 = _ref3.arg1,
|
178 | sig = _ref3.sig;
|
179 |
|
180 | if (arg1) {
|
181 | this.assert(wrapper && wrapper.contains(arg1), () => `expected ${sig} to contain ${(0, _uiTestQueries.elementToString)(arg1)} ${markup()}`, () => `expected ${sig} to not contain ${(0, _uiTestQueries.elementToString)(arg1)} ${markup()}`, arg1);
|
182 | }
|
183 | });
|
184 | addAssertion('className', function className(_ref4) {
|
185 | let wrapper = _ref4.wrapper,
|
186 | markup = _ref4.markup,
|
187 | arg1 = _ref4.arg1,
|
188 | sig = _ref4.sig;
|
189 | const actual = wrapper.classNames();
|
190 |
|
191 | this.assert(wrapper && wrapper.hasClass(arg1), () => `expected ${sig} to have a #{exp} class, but it has #{act} ${markup()}`, () => `expected ${sig} to not have a #{exp} class, but it has #{act} ${markup()}`, arg1, actual);
|
192 | });
|
193 | addAssertion('match', function match(_ref5) {
|
194 | let wrapper = _ref5.wrapper,
|
195 | markup = _ref5.markup,
|
196 | arg1 = _ref5.arg1,
|
197 | sig = _ref5.sig;
|
198 | this.assert(wrapper && wrapper.matches(arg1), () => `expected ${sig} to match #{exp} ${markup()}`, () => `expected ${sig} to not match #{exp} ${markup()}`, arg1);
|
199 | });
|
200 | addAssertion('descendants', listAndCountAssertion('descendants', 'descendants'));
|
201 | addAssertion('children', listAndCountAssertion('children', 'children'));
|
202 | addAssertion('ancestors', listAndCountAssertion('ancestors', 'ancestors'));
|
203 | addAssertion('parents', listAndCountAssertion('parents', 'parents'));
|
204 | addAssertion('attribute', propAndValueAssertion('attribute', 'attribute'));
|
205 | addAssertion('style', propAndValueAssertion('style', 'computed CSS style'));
|
206 | addAssertion('bounds', propAndValueAssertion('bounds', 'bounding client rect'));
|
207 | addAssertion('tagName', valueAssertion('tagName', 'tag name'));
|
208 | addAssertion('id', valueAssertion('id', 'id'));
|
209 | addAssertion('visible', booleanAssertion('visible', 'visible'));
|
210 | addAssertion('clickable', booleanAssertion('clickable', 'clickable'));
|
211 | addAssertion('focus', booleanAssertion('containsFocus', 'focused or contain the focused element'));
|
212 | addAssertion('focused', booleanAssertion('focused', 'focused'));
|
213 | addAssertion('focusable', booleanAssertion('focusable', 'focusable'));
|
214 | addAssertion('tabbable', booleanAssertion('tabbable', 'tabbable'));
|
215 | addAssertion('checked', booleanAssertion('checked', 'checked'));
|
216 | addAssertion('selected', booleanAssertion('selected', 'selected'));
|
217 | addAssertion('disabled', booleanAssertion('disabled', 'disabled'));
|
218 | addAssertion('enabled', booleanAssertion('enabled', 'enabled'));
|
219 | addAssertion('readonly', booleanAssertion('readonly', 'readonly'));
|
220 | addAssertion('accessible', booleanAssertion('accessible', 'accessible'));
|
221 | addAssertion('role', valueAssertion('role', 'role'));
|
222 | addAssertion('title', valueAssertion('title', 'title'));
|
223 | addAssertion('value', valueAssertion('value', 'value'));
|
224 | addAssertion('label', valueAssertion('label', 'label'));
|
225 | }
|
226 |
|
227 | function getActual(wrapper, assertion) {
|
228 | const methodOrProperty = wrapper ? wrapper[assertion] : void 0;
|
229 |
|
230 | if (typeof methodOrProperty === 'function') {
|
231 | for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
232 | args[_key - 2] = arguments[_key];
|
233 | }
|
234 |
|
235 | return methodOrProperty(...args);
|
236 | } else {
|
237 | return methodOrProperty;
|
238 | }
|
239 | }
|
240 |
|
241 | function propAndValueAssertion(assertion, desc) {
|
242 | return function (args) {
|
243 | const wrapper = args.wrapper,
|
244 | markup = args.markup,
|
245 | flag = args.flag,
|
246 | inspect = args.inspect,
|
247 | arg1 = args.arg1,
|
248 | arg2 = args.arg2,
|
249 | arg3 = args.arg3,
|
250 | sig = args.sig;
|
251 | const actual = getActual(wrapper, assertion, arg1);
|
252 |
|
253 | if (arg2) {
|
254 | this.assert(actual && (0, _uiTestQueries.matches)(actual, arg2, arg3), () => `expected ${sig} to have a ${inspect(arg1)} ${desc} with the value #{exp}, but the value was #{act} ${markup()}`, () => `expected ${sig} to not have a ${inspect(arg1)} ${desc} with the value #{act} ${markup()}`, arg2, actual);
|
255 | } else {
|
256 | this.assert(typeof actual !== 'undefined' && actual !== null, () => `expected ${sig} to have a #{exp} ${desc} ${markup()}`, () => `expected ${sig} to not have a #{exp} ${desc} ${markup()}`, arg1, actual);
|
257 | }
|
258 |
|
259 | flag(this, 'object', actual);
|
260 | };
|
261 | }
|
262 |
|
263 | function booleanAssertion(assertion, desc) {
|
264 | return function (_ref6) {
|
265 | let wrapper = _ref6.wrapper,
|
266 | markup = _ref6.markup,
|
267 | sig = _ref6.sig;
|
268 | const actual = getActual(wrapper, assertion);
|
269 | this.assert(actual, () => `expected ${sig} to be ${desc} ${markup()}`, () => `expected ${sig} to not be ${desc} ${markup()}`, void 0);
|
270 | };
|
271 | }
|
272 |
|
273 | function valueAssertion(assertion, desc) {
|
274 | return function (_ref7) {
|
275 | let wrapper = _ref7.wrapper,
|
276 | markup = _ref7.markup,
|
277 | arg1 = _ref7.arg1,
|
278 | arg2 = _ref7.arg2,
|
279 | sig = _ref7.sig;
|
280 | const actual = getActual(wrapper, assertion);
|
281 | this.assert((0, _uiTestQueries.matches)(actual, arg1, arg2), () => `expected ${sig} to have a #{exp} ${desc}, but it has #{act} ${markup()}`, () => `expected ${sig} to not have a #{exp} ${desc}, but it has #{act} ${markup()}`, arg1, actual);
|
282 | };
|
283 | }
|
284 |
|
285 | function listAndCountAssertion(assertion, desc) {
|
286 | return function (_ref8) {
|
287 | let wrapper = _ref8.wrapper,
|
288 | markup = _ref8.markup,
|
289 | arg1 = _ref8.arg1,
|
290 | sig = _ref8.sig,
|
291 | flag = _ref8.flag;
|
292 | const exactlyCount = flag(this, 'exactlyCount');
|
293 | const actual = getActual(wrapper, assertion, arg1);
|
294 | const count = actual.length;
|
295 |
|
296 | if (exactlyCount || exactlyCount === 0) {
|
297 | this.assert(count === exactlyCount, () => `expected ${sig} to have ${exactlyCount} ${desc} #{exp} but actually found ${count} ${markup()}`, () => `expected ${sig} to not have ${exactlyCount} ${desc} #{exp} but actually found ${count} ${markup()}`, arg1);
|
298 | } else {
|
299 | this.assert(count > 0, () => `expected ${sig} to have ${desc} #{exp} ${markup()}`, () => `expected ${sig} to not have ${desc} #{exp} ${markup()}`, arg1);
|
300 | }
|
301 | };
|
302 | } |
\ | No newline at end of file |