forked from Vincit/objection.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestUtils.js
50 lines (47 loc) · 1.21 KB
/
testUtils.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
const _ = require('lodash');
const expect = require('expect.js');
/**
* Expect that `result` contains all attributes of `partial` and their values equal.
*
* Example:
*
* ```js
* // doesn't throw.
* expectPartialEqual({a: 1, b: 2}, {a: 1});
* // doesn't throw.
* expectPartialEqual([{a: 1, b: 2}, {a: 2, b: 4}], [{a: 1}, {b: 4}]);
* // Throws
* expectPartialEqual({a: 1}, {b: 1});
* // Throws
* expectPartialEqual({a: 1}, {a: 2});
* ```
*/
function expectPartialEqual(result, partial) {
if (Array.isArray(result) && Array.isArray(partial)) {
expect(result).to.have.length(partial.length);
result.forEach((value, idx) => {
expectPartialEqual(result[idx], partial[idx]);
});
} else if (
_.isObject(result) &&
!Array.isArray(partial) &&
_.isObject(partial) &&
!Array.isArray(result)
) {
var partialKeys = _.keys(partial);
expect(_.pick(result, partialKeys)).to.eql(partial);
} else {
throw new Error('result and partial must both be arrays or objects');
}
}
function createRejectionReflection(err) {
return {
isRejected: () => true,
isFulfilled: () => false,
reason: () => err
};
}
module.exports = {
expectPartialEqual,
createRejectionReflection
};