-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextensions.js
197 lines (163 loc) · 4.77 KB
/
extensions.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
"use strict";
global.Mocha = require("mocha");
global.Chai = require("chai");
global.expect = Chai.expect;
/** Check if an element contains one or more CSS classes */
addToChai(["class", "classes"], function(...classNames){
Chai.util.flag(this, "negate");
let subjects = Chai.util.flag(this, "object");
subjects = "length" in subjects ? Array.from(subjects) : [subjects];
for(const subject of subjects){
const classList = subject.classList;
for(let names of classNames){
if(!Array.isArray(names))
names = names.split(/\s+/g);
for(let name of names){
const list = subject.classList.length
? `classList "${subject.className}"`
: "empty classList";
this.assert(
classList.contains(name),
`expected ${list} to include "${name}"`,
`expected ${list} not to include "${name}"`
);
}
}
}
});
/** Assert that two filesystem paths are the same */
Chai.Assertion.addMethod("equalPath", function(path){
const normalise = require("path").normalize;
const subject = Chai.util.flag(this, "object");
const expected = normalise(path);
const actual = normalise(subject);
this.assert(
actual === expected,
"expected path #{act} to equal #{exp}",
"expected path #{act} not to equal #{exp}",
expected,
actual,
false
);
});
/** Assert that a file exists in the filesystem */
Chai.Assertion.addProperty("existOnDisk", function(){
const path = Chai.util.flag(this, "object");
let exists = false;
try{
require("fs").statSync(path);
exists = true;
} catch(e){}
this.assert(
exists,
`expected "${path}" to exist in filesystem`,
`expected "${path}" not to exist in filesystem`
);
});
/** Assert that an HTML element has user focus */
Chai.Assertion.addProperty("focus", function(){
const ae = document.activeElement;
let subject = Chai.util.flag(this, "object");
if(subject.jquery)
subject = subject[0];
this.assert(
ae === subject || ae.contains(subject),
"expected element to have focus",
"expected element not to have focus"
);
});
/** Assert that an HTML element is rendered in the DOM tree */
Chai.Assertion.addProperty("drawn", function(){
let subject = Chai.util.flag(this, "object");
if(subject.jquery)
subject = subject[0];
const bounds = subject.getBoundingClientRect();
const {top, right, bottom, left} = bounds;
this.assert(
!(0 === top && 0 === right && 0 === bottom && 0 === left),
"expected element to be drawn",
"expected element not to be drawn"
);
});
/** Attach one or more HTML elements to the spec-runner window */
global.attachToDOM = function(...elements){
if(isHeadless()){
const {body} = document;
for(const el of elements)
if(!body.contains(el))
body.appendChild(el);
}
else{
const mocha = document.querySelector("#mocha");
const body = mocha.parentElement;
for(const el of elements)
if(el !== mocha && !body.contains(el))
body.insertBefore(el, mocha);
}
};
/** Remove previously-added HTML elements */
global.resetDOM = function(){
if(isHeadless()){
while(document.body.firstChild)
document.body.removeChild(document.body.firstChild);
}
else{
const mocha = document.querySelector("#mocha");
for(const el of Array.from(document.body.children))
if(el !== mocha) document.body.removeChild(el);
}
};
/**
* @global - Predicate to skip POSIX-only tests.
* This avoids marking skipped tests as "pending" if run on Windows.
*
* @example
* unlessOnWindows.describe("Symlinks", …);
* unlessOnWindows.it("tests hard-links", …);
* unlessOnWindows(posixOnlyFunc => …);
*
* @property {Function} describe
* @property {Function} specify
* @property {Function} it
* @type {Function}
*/
global.unlessOnWindows = (mochFn => {
const noope = () => {};
const isWin = "win32" === process.platform;
const output = cb => (cb && !isWin) ? cb() : noope();
for(const name of mochFn){
const value = isWin ? noope : (...args) => global[name](...args);
Object.defineProperty(output, name, {value});
}
return output;
})(["describe", "it", "specify"]);
/**
* Call `describe()` with "When " prepended to its description.
*
* Complements the `autoIt` setting and helps specs read more naturally.
* Only globalised if `when` doesn't exist in global scope.
*
* @example when("it loads the page", () => it("does this", fn)));
* @param {String} text
* @param {...*} args
* @type {Function}
*/
function when(text, ...args){
return describe("When " + text, ...args);
}
if(null == global.when)
global.when = when;
/** Thin wrapper around Chai.Assertion.addMethod to permit plugin aliases */
function addToChai(names, fn){
for(const name of names)
Chai.Assertion.addMethod(name, fn);
}
/**
* Determine whether specs are being run headlessly.
*
* @return {Boolean}
* @internal
*/
function isHeadless(){
return global.atom.getLoadSettings().headless;
}