forked from bvaughn/jasmine-es6-promise-matchers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jasmine-es6-promise-matchers.js
108 lines (99 loc) · 3.14 KB
/
jasmine-es6-promise-matchers.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
/**
* Matcher helpers for tests involving Promises.
*
* <p>Note that this library depends on the ES6Promise polyfill library.
* @see https://github.com/jakearchibald/es6-promise
*
* @example
* expect(promise).toBeRejected();
* expect(promise).toBeRejectedWith(someValue);
* expect(promise).toBeResolved();
* expect(promise).toBeResolvedWith(someValue);
*/
(window || global).JasminePromiseMatchers = new function() {
var windowOrGlobal = window || global;
var OriginalPromise;
/**
* Install the JasminePromiseMatchers library.
*/
this.install = function() {
OriginalPromise = windowOrGlobal.Promise;
// Polyfill if necessary for browsers like Phantom JS.
windowOrGlobal.Promise = windowOrGlobal.Promise || ES6Promise.Promise;
};
/**
* Uninstall the JasminePromiseMatchers library.
*/
this.uninstall = function() {
windowOrGlobal.Promise = OriginalPromise;
};
var PROMISE_STATE = {
PENDING: 'pending',
REJECTED: 'rejected',
RESOLVED: 'resolved',
};
var verifyData = function(actualData, expectedData, done) {
if (expectedData !== undefined) {
if (expectedData.asymmetricMatch instanceof Function) {
if (!expectedData.asymmetricMatch(actualData)) {
done.fail('Expected ' + actualData + ' to contain ' + expectedData);
}
} else if (actualData !== expectedData) {
done.fail('Expected ' + actualData + ' to be ' + expectedData);
}
}
};
var verifyState = function(actualState, expectedState, done) {
if (actualState !== expectedState) {
done.fail('Expected ' + actualState + ' to be ' + expectedState);
}
};
// Helper method to verify expectations and return a Jasmine-friendly info-object
var verifyPromiseExpectations = function(done, promise, expectedState, expectedData) {
promise.then(
function(data) {
verifyState(PROMISE_STATE.RESOLVED, expectedState, done);
verifyData(data, expectedData, done);
done();
},
function(data) {
verifyState(PROMISE_STATE.REJECTED, expectedState, done);
verifyData(data, expectedData, done);
done();
});
return {pass: true};
};
// Install the matchers
beforeEach(function() {
jasmine.addMatchers({
toBeRejected: function() {
return {
compare: function(promise, done) {
return verifyPromiseExpectations(done, promise, PROMISE_STATE.REJECTED);
}
};
},
toBeRejectedWith: function() {
return {
compare: function(promise, expectedData, done) {
return verifyPromiseExpectations(done, promise, PROMISE_STATE.REJECTED, expectedData);
}
};
},
toBeResolved: function() {
return {
compare: function(promise, done) {
return verifyPromiseExpectations(done, promise, PROMISE_STATE.RESOLVED);
}
};
},
toBeResolvedWith: function() {
return {
compare: function(promise, expectedData, done) {
return verifyPromiseExpectations(done, promise, PROMISE_STATE.RESOLVED, expectedData);
}
};
}
});
});
}();