UNPKG

11.4 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = assertions;
8var _react = _interopRequireDefault(require("react"));
9var _reactDom = require("react-dom");
10var _uiTestQueries = require("@instructure/ui-test-queries");
11/*
12 * The MIT License (MIT)
13 *
14 * Copyright (c) 2015 - present Instructure, Inc.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this software and associated documentation files (the "Software"), to deal
18 * in the Software without restriction, including without limitation the rights
19 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 * copies of the Software, and to permit persons to whom the Software is
21 * furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in all
24 * copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35function assertions(chai, utils) {
36 const flag = utils.flag,
37 inspect = utils.inspect;
38 const Assertion = chai.Assertion;
39 function wrapObj(obj) {
40 if (obj && typeof obj.getDOMNode === 'function') {
41 return obj;
42 }
43 let node;
44 if ((0, _uiTestQueries.isElement)(obj)) {
45 node = obj;
46 } else if ( /*#__PURE__*/_react.default.isValidElement(obj)) {
47 node = (0, _reactDom.findDOMNode)(obj);
48 }
49 if (node) {
50 return (0, _uiTestQueries.wrapQueryResult)(node);
51 }
52 return void 0;
53 }
54 function addAssertion(name, assertion) {
55 // @ts-expect-error don't know how to type this..
56 if (Assertion.prototype[name]) {
57 overwriteMethod(name, assertion);
58 } else {
59 addMethod(name, assertion);
60 }
61 }
62 function overwriteProperty(name, assertion) {
63 Assertion.overwriteProperty(name, function (_super) {
64 return wrapOverwriteAssertion(assertion, _super);
65 });
66 }
67 function overwriteMethod(name, assertion) {
68 Assertion.overwriteMethod(name, function (_super) {
69 return wrapOverwriteAssertion(assertion, _super);
70 });
71 }
72 function addMethod(name, assertion) {
73 Assertion.addMethod(name, wrapAssertion(assertion));
74 }
75 function addChainableMethod(name, assertion) {
76 Assertion.addChainableMethod(name, wrapAssertion(assertion));
77 }
78 function overwriteChainableMethod(name, assertion) {
79 Assertion.overwriteChainableMethod(name, function (_super) {
80 return wrapOverwriteAssertion(assertion, _super);
81 }, function (_super) {
82 return function () {
83 _super.call(this);
84 };
85 });
86 }
87 function wrapOverwriteAssertion(assertion, _super) {
88 return function (arg1, arg2) {
89 const wrapper = wrapObj(flag(this, 'object'));
90 if (!wrapper) {
91 // @ts-expect-error TODO: this needs new syntax
92 // eslint-disable-next-line prefer-rest-params
93 return _super.apply(this, arguments);
94 }
95 assertion.call(this, {
96 markup: () => wrapper.toString(),
97 sig: inspect(wrapper.getDOMNode()),
98 wrapper,
99 arg1,
100 arg2,
101 flag,
102 inspect
103 });
104 };
105 }
106 function wrapAssertion(assertion) {
107 return function (arg1, arg2) {
108 const wrapper = wrapObj(flag(this, 'object'));
109 const config = {
110 wrapper,
111 arg1,
112 flag,
113 inspect
114 };
115 if (wrapper) {
116 config.markup = () => wrapper.toString();
117 config.sig = inspect(wrapper.getDOMNode());
118 }
119 if (arguments.length > 1) {
120 config.arg2 = arg2;
121 }
122 assertion.call(this, config);
123 };
124 }
125 overwriteProperty('not', function () {
126 flag(this, 'negate', true);
127 });
128 addChainableMethod('exactly', function exactly(_ref) {
129 let flag = _ref.flag,
130 arg1 = _ref.arg1;
131 flag(this, 'exactlyCount', arg1);
132 });
133 addAssertion('text', function text(_ref2) {
134 let wrapper = _ref2.wrapper,
135 markup = _ref2.markup,
136 flag = _ref2.flag,
137 arg1 = _ref2.arg1,
138 arg2 = _ref2.arg2,
139 sig = _ref2.sig;
140 const actual = wrapper.text(); // TODO check if this is this never null
141
142 if (typeof arg1 !== 'undefined') {
143 if (flag(this, 'contains')) {
144 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);
145 } else {
146 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);
147 }
148 }
149 flag(this, 'object', actual);
150 });
151 overwriteChainableMethod('contain', function contain(_ref3) {
152 let wrapper = _ref3.wrapper,
153 markup = _ref3.markup,
154 arg1 = _ref3.arg1,
155 sig = _ref3.sig;
156 if (arg1) {
157 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);
158 }
159 });
160 addAssertion('className', function className(_ref4) {
161 let wrapper = _ref4.wrapper,
162 markup = _ref4.markup,
163 arg1 = _ref4.arg1,
164 sig = _ref4.sig;
165 const actual = wrapper.classNames(); // TODO check if this is this never null
166
167 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);
168 });
169 addAssertion('match', function match(_ref5) {
170 let wrapper = _ref5.wrapper,
171 markup = _ref5.markup,
172 arg1 = _ref5.arg1,
173 sig = _ref5.sig;
174 this.assert(wrapper && wrapper.matches(arg1), () => `expected ${sig} to match #{exp} ${markup()}`, () => `expected ${sig} to not match #{exp} ${markup()}`, arg1);
175 });
176 addAssertion('descendants', listAndCountAssertion('descendants', 'descendants'));
177 addAssertion('children', listAndCountAssertion('children', 'children'));
178 addAssertion('ancestors', listAndCountAssertion('ancestors', 'ancestors'));
179 addAssertion('parents', listAndCountAssertion('parents', 'parents'));
180 addAssertion('attribute', propAndValueAssertion('attribute', 'attribute'));
181 addAssertion('style', propAndValueAssertion('style', 'computed CSS style'));
182 addAssertion('bounds', propAndValueAssertion('bounds', 'bounding client rect'));
183 addAssertion('tagName', valueAssertion('tagName', 'tag name'));
184 addAssertion('id', valueAssertion('id', 'id'));
185 addAssertion('visible', booleanAssertion('visible', 'visible'));
186 addAssertion('clickable', booleanAssertion('clickable', 'clickable'));
187 addAssertion('focus', booleanAssertion('containsFocus', 'focused or contain the focused element'));
188 addAssertion('focused', booleanAssertion('focused', 'focused'));
189 addAssertion('focusable', booleanAssertion('focusable', 'focusable'));
190 addAssertion('tabbable', booleanAssertion('tabbable', 'tabbable'));
191 addAssertion('checked', booleanAssertion('checked', 'checked'));
192 addAssertion('selected', booleanAssertion('selected', 'selected'));
193 addAssertion('disabled', booleanAssertion('disabled', 'disabled'));
194 addAssertion('enabled', booleanAssertion('enabled', 'enabled'));
195 addAssertion('readonly', booleanAssertion('readonly', 'readonly'));
196 addAssertion('accessible', booleanAssertion('accessible', 'accessible'));
197 addAssertion('role', valueAssertion('role', 'role'));
198 addAssertion('title', valueAssertion('title', 'title'));
199 addAssertion('value', valueAssertion('value', 'value'));
200 addAssertion('label', valueAssertion('label', 'label'));
201}
202function getActual(wrapper, assertion) {
203 const methodOrProperty = wrapper ? wrapper[assertion] : void 0;
204 if (typeof methodOrProperty === 'function') {
205 for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
206 args[_key - 2] = arguments[_key];
207 }
208 return methodOrProperty(...args);
209 } else {
210 return methodOrProperty;
211 }
212}
213function propAndValueAssertion(assertion, desc) {
214 return function (args) {
215 const wrapper = args.wrapper,
216 markup = args.markup,
217 flag = args.flag,
218 inspect = args.inspect,
219 arg1 = args.arg1,
220 arg2 = args.arg2,
221 arg3 = args.arg3,
222 sig = args.sig;
223 const actual = getActual(wrapper, assertion, arg1);
224 if (arg2) {
225 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);
226 } else {
227 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);
228 }
229 flag(this, 'object', actual);
230 };
231}
232function booleanAssertion(assertion, desc) {
233 return function (_ref6) {
234 let wrapper = _ref6.wrapper,
235 markup = _ref6.markup,
236 sig = _ref6.sig;
237 const actual = getActual(wrapper, assertion);
238 this.assert(actual, () => `expected ${sig} to be ${desc} ${markup()}`, () => `expected ${sig} to not be ${desc} ${markup()}`, void 0);
239 };
240}
241function valueAssertion(assertion, desc) {
242 return function (_ref7) {
243 let wrapper = _ref7.wrapper,
244 markup = _ref7.markup,
245 arg1 = _ref7.arg1,
246 arg2 = _ref7.arg2,
247 sig = _ref7.sig;
248 const actual = getActual(wrapper, assertion);
249 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);
250 };
251}
252function listAndCountAssertion(assertion, desc) {
253 return function (_ref8) {
254 let wrapper = _ref8.wrapper,
255 markup = _ref8.markup,
256 arg1 = _ref8.arg1,
257 sig = _ref8.sig,
258 flag = _ref8.flag;
259 const exactlyCount = flag(this, 'exactlyCount');
260 const actual = getActual(wrapper, assertion, arg1);
261 const count = actual.length;
262 if (exactlyCount || exactlyCount === 0) {
263 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);
264 } else {
265 this.assert(count > 0, () => `expected ${sig} to have ${desc} #{exp} ${markup()}`, () => `expected ${sig} to not have ${desc} #{exp} ${markup()}`, arg1);
266 }
267 };
268}
\No newline at end of file