-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqunit-promises.js
114 lines (101 loc) · 3.86 KB
/
qunit-promises.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
(function (QUnit) {
// jQuery promise objects have .then and .always methods
// Q promise objects have .then and .finally methods
function verifyPromise(promise) {
if (!promise) {
throw new Error('expected a promise object');
}
if (typeof promise.then !== 'function') {
throw new Error('promise object does not have .then function');
}
if (typeof promise.always !== 'function' &&
typeof promise['finally'] !== 'function') {
throw new Error('promise object does not have .always or .finally');
}
return alwaysName(promise);
}
function alwaysName(promise) {
if (typeof promise.always === 'function') {
return 'always';
} else if (typeof promise['finally'] === 'function') {
return 'finally';
} else {
throw new Error('promise object does not have "always" method');
}
}
function T() {
return true;
}
QUnit.extend(QUnit.assert, {
// resolved promises
will: function (promise, expectation, message) {
var always = verifyPromise(promise);
if (typeof expectation == 'string') {
message = expectation;
expectation = T;
}
if (arguments.length === 1) {
expectation = T;
}
QUnit.stop();
promise.then(function (actual) {
QUnit.push(expectation(actual), undefined, undefined, message);
}, function () {
QUnit.push(false, undefined, undefined, 'promise rejected (but should have been resolved)');
})[always](QUnit.start).done();
},
willEqual: function (promise, expected, message, actualTransform) {
var always = verifyPromise(promise);
actualTransform = actualTransform || function(actual) {return actual;};
QUnit.stop();
promise.then(function (actual) {
actual = actualTransform(actual);
QUnit.push(actual == expected, actual, expected, message);
}, function (actual) {
QUnit.push(false, actual, expected, 'promise rejected (but should have been resolved)');
})[always](QUnit.start).done();
},
willDeepEqual: function (promise, expected, message, actualTransform) {
var always = verifyPromise(promise);
actualTransform = actualTransform || function(actual) {return actual;};
QUnit.stop();
promise.then(function (actual) {
if (typeof QUnit.equiv !== 'function') {
throw new Error('Missing QUnit.equiv function');
}
actual = actualTransform(actual);
QUnit.push(QUnit.equiv(actual, expected), actual, expected, message);
}, function (actual) {
QUnit.push(false, actual, expected, 'promise rejected (but should have been resolved)');
})[always](QUnit.start).done();
},
// rejected promises
wont: function (promise, message) {
var always = verifyPromise(promise);
QUnit.stop();
promise.then(function () {
QUnit.push(false, undefined, undefined, 'promise resolved (but should have been rejected)');
}, function () {
QUnit.push(true, undefined, undefined, message);
})[always](QUnit.start).done();
},
wontEqual: function (promise, expected, message) {
var always = verifyPromise(promise);
QUnit.stop();
promise.then(function (actual) {
QUnit.push(false, actual, expected, 'promise resolved (but should have been rejected)');
}, function (actual) {
QUnit.push(actual == expected, actual, expected, message);
})[always](QUnit.start).done();
},
wontDeepEqual: function (promise, expected, message) {
var always = verifyPromise(promise);
QUnit.stop();
promise.then(function (actual) {
QUnit.push(false, actual, expected, 'promise resolved (but should have been rejected)');
}, function (actual) {
QUnit.push(QUnit.equiv(actual, expected), actual, expected, message);
})[always](QUnit.start).done();
}
});
}(QUnit));