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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
odoo.define('web.qunit_asserts', function (require) {
"use strict";
/**
* In this file, we extend QUnit by adding some specialized assertions. The goal
* of these new assertions is twofold:
* - ease of use: they should allow us to simplify some common complex assertions
* - safer: these assertions will fail when some preconditions are not met.
*
* For example, the assert.isVisible assertion will also check that the target
* matches exactly one element.
*/
const Widget = require('web.Widget');
/** @todo use testUtilsDom.getNode to extract the element from the 'w' argument */
//-------------------------------------------------------------------------
// Private functions
//-------------------------------------------------------------------------
/**
* Helper function, to check if a given element
* - is unique (if it is a jquery node set)
* - has (or has not) a css class
*
* @private
* @param {Widget|jQuery|HTMLElement|owl.Component} w
* @param {string} className
* @param {boolean} shouldHaveClass
* @param {string} [msg]
*/
function _checkClass(w, className, shouldHaveClass, msg) {
if (w instanceof jQuery && w.length !== 1) {
const assertion = shouldHaveClass ? 'hasClass' : 'doesNotHaveClass';
QUnit.assert.ok(false, `Assertion '${assertion} ${className}' targets ${w.length} elements instead of 1`);
}
const el = w instanceof Widget || w instanceof owl.Component ? w.el :
w instanceof jQuery ? w[0] : w;
msg = msg || `target should ${shouldHaveClass ? 'have' : 'not have'} class ${className}`;
const isFalse = className.split(" ").some(cls => {
const hasClass = el.classList.contains(cls);
return shouldHaveClass ? !hasClass : hasClass;
});
QUnit.assert.ok(!isFalse, msg);
}
/**
* Helper function, to check if a given element
* - is unique (if it is a jquery node set)
* - is (or not) visible
*
* @private
* @param {Widget|jQuery|HTMLElement|owl.Component} w
* @param {boolean} shouldBeVisible
* @param {string} [msg]
*/
function _checkVisible(w, shouldBeVisible, msg) {
if (w instanceof jQuery && w.length !== 1) {
const assertion = shouldBeVisible ? 'isVisible' : 'isNotVisible';
QUnit.assert.ok(false, `Assertion '${assertion}' targets ${w.length} elements instead of 1`);
}
const el = w instanceof Widget || w instanceof owl.Component ? w.el :
w instanceof jQuery ? w[0] : w;
msg = msg || `target should ${shouldBeVisible ? '' : 'not'} be visible`;
let isVisible = el &&
el.offsetWidth &&
el.offsetHeight;
if (isVisible) {
// This computation is a little more heavy and we only want to perform it
// if the above assertion has failed.
const rect = el.getBoundingClientRect();
isVisible = rect.width + rect.height;
}
const condition = shouldBeVisible ? isVisible : !isVisible;
QUnit.assert.ok(condition, msg);
}
//-------------------------------------------------------------------------
// Public functions
//-------------------------------------------------------------------------
/**
* Checks that the target element (described by widget/jquery or html element)
* contains exactly n matches for the selector.
*
* Example: assert.containsN(document.body, '.modal', 0)
*
* @param {Widget|jQuery|HTMLElement|owl.Component} w
* @param {string} selector
* @param {number} n
* @param {string} [msg]
*/
function containsN(w, selector, n, msg) {
if (typeof n !== 'number') {
throw new Error("containsN assert should be called with a number as third argument");
}
let matches = [];
if (w instanceof owl.Component) {
if (!w.el) {
throw new Error(`containsN assert with selector '${selector}' called on an unmounted component`);
}
matches = w.el.querySelectorAll(selector);
} else {
const $el = w instanceof Widget ? w.$el :
w instanceof HTMLElement ? $(w) :
w; // jquery element
matches = $el.find(selector);
}
if (!msg) {
msg = `Selector '${selector}' should have exactly ${n} matches (inside the target)`;
}
QUnit.assert.strictEqual(matches.length, n, msg);
}
/**
* Checks that the target element (described by widget/jquery or html element)
* contains exactly 0 match for the selector.
*
* @param {Widget|jQuery|HTMLElement|owl.Component} w
* @param {string} selector
* @param {string} [msg]
*/
function containsNone(w, selector, msg) {
containsN(w, selector, 0, msg);
}
/**
* Checks that the target element (described by widget/jquery or html element)
* contains exactly 1 match for the selector.
*
* @param {Widget|jQuery|HTMLElement|owl.Component} w
* @param {string} selector
* @param {string} [msg]
*/
function containsOnce(w, selector, msg) {
containsN(w, selector, 1, msg);
}
/**
* Checks that the target element (described by widget/jquery or html element)
* - exists
* - is unique
* - has the given class (specified by className)
*
* Note that it uses the hasClass jQuery method, so it can be used to check the
* presence of more than one class ('some-class other-class'), but it is a
* little brittle, because it depends on the order of these classes:
*
* div.a.b.c: has class 'a b c', but does not have class 'a c b'
*
* @param {Widget|jQuery|HTMLElement|owl.Component} w
* @param {string} className
* @param {string} [msg]
*/
function hasClass(w, className, msg) {
_checkClass(w, className, true, msg);
}
/**
* Checks that the target element (described by widget/jquery or html element)
* - exists
* - is unique
* - does not have the given class (specified by className)
*
* @param {Widget|jQuery|HTMLElement|owl.Component} w
* @param {string} className
* @param {string} [msg]
*/
function doesNotHaveClass(w, className, msg) {
_checkClass(w, className, false, msg);
}
/**
* Checks that the target element (described by widget/jquery or html element)
* - exists
* - is unique
* - has the given attribute with the proper value
*
* @param {Widget|jQuery|HTMLElement|owl.Component} w
* @param {string} attr
* @param {string} value
* @param {string} [msg]
*/
function hasAttrValue(w, attr, value, msg) {
const $el = w instanceof Widget ? w.$el :
w instanceof HTMLElement ? $(w) :
w; // jquery element
if ($el.length !== 1) {
const descr = `hasAttrValue (${attr}: ${value})`;
QUnit.assert.ok(false,
`Assertion '${descr}' targets ${$el.length} elements instead of 1`
);
} else {
msg = msg || `attribute '${attr}' of target should be '${value}'`;
QUnit.assert.strictEqual($el.attr(attr), value, msg);
}
}
/**
* Checks that the target element (described by widget/jquery or html element)
* - exists
* - is visible (as far as jQuery can tell: not in display none, ...)
*
* @param {Widget|jQuery|HTMLElement|owl.Component} w
* @param {string} [msg]
*/
function isVisible(w, msg) {
_checkVisible(w, true, msg);
}
/**
* Checks that the target element (described by widget/jquery or html element)
* - exists
* - is not visible (as far as jQuery can tell: display none, ...)
*
* @param {Widget|jQuery|HTMLElement|owl.Component} w
* @param {string} [msg]
*/
function isNotVisible(w, msg) {
_checkVisible(w, false, msg);
}
//-------------------------------------------------------------------------
// Exposed API
//-------------------------------------------------------------------------
QUnit.assert.containsN = containsN;
QUnit.assert.containsNone = containsNone;
QUnit.assert.containsOnce = containsOnce;
QUnit.assert.hasClass = hasClass;
QUnit.assert.doesNotHaveClass = doesNotHaveClass;
QUnit.assert.hasAttrValue = hasAttrValue;
QUnit.assert.isVisible = isVisible;
QUnit.assert.isNotVisible = isNotVisible;
});
|