-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathsetup_chai_sinon.js
107 lines (99 loc) · 3.13 KB
/
setup_chai_sinon.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
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import stringify from 'json-stable-stringify';
import sinon from 'sinon'; // eslint-disable-line local/no-import
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
window.chai = chai;
window.should = chai.should();
window.expect = chai.expect;
window.assert = chai.assert;
window.sinon = sinon;
chai.use(chaiAsPromised);
chai.Assertion.addMethod('attribute', function (attr) {
const obj = this._obj;
const tagName = obj.tagName.toLowerCase();
this.assert(
obj.hasAttribute(attr),
"expected element '" + tagName + "' to have attribute #{exp}",
"expected element '" + tagName + "' to not have attribute #{act}",
attr,
attr
);
});
chai.Assertion.addMethod('class', function (className) {
const obj = this._obj;
const tagName = obj.tagName.toLowerCase();
this.assert(
obj.classList.contains(className),
"expected element '" + tagName + "' to have class #{exp}",
"expected element '" + tagName + "' to not have class #{act}",
className,
className
);
});
chai.Assertion.addProperty('visible', function () {
const obj = this._obj;
const computedStyle = window.getComputedStyle(obj);
const visibility = computedStyle.getPropertyValue('visibility');
const opacity = computedStyle.getPropertyValue('opacity');
const isOpaque = parseInt(opacity, 10) > 0;
const tagName = obj.tagName.toLowerCase();
this.assert(
visibility === 'visible' && isOpaque,
"expected element '" +
tagName +
"' to be #{exp}, got #{act}. with classes: " +
obj.className,
"expected element '" +
tagName +
"' not to be #{exp}, got #{act}. with classes: " +
obj.className,
'visible and opaque',
`visibility = ${visibility} and opacity = ${opacity}`
);
});
chai.Assertion.addProperty('hidden', function () {
const obj = this._obj;
const computedStyle = window.getComputedStyle(obj);
const visibility = computedStyle.getPropertyValue('visibility');
const opacity = computedStyle.getPropertyValue('opacity');
const tagName = obj.tagName.toLowerCase();
this.assert(
visibility === 'hidden' || parseInt(opacity, 10) == 0,
"expected element '" +
tagName +
"' to be #{exp}, got #{act}. with classes: " +
obj.className,
"expected element '" +
tagName +
"' not to be #{act}. with classes: " +
obj.className,
'hidden',
visibility
);
});
chai.Assertion.addMethod('display', function (display) {
const obj = this._obj;
const value = window.getComputedStyle(obj).getPropertyValue('display');
const tagName = obj.tagName.toLowerCase();
this.assert(
value === display,
"expected element '" + tagName + "' to be display #{exp}, got #{act}.",
"expected element '" + tagName + "' not to be display #{act}.",
display,
value
);
});
chai.Assertion.addMethod('jsonEqual', function (compare) {
const obj = this._obj;
const a = stringify(compare);
const b = stringify(obj);
this.assert(
a == b,
'expected JSON to be equal.\nExp: #{exp}\nAct: #{act}',
'expected JSON to not be equal.\nExp: #{exp}\nAct: #{act}',
a,
b
);
});