This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
84 lines (59 loc) · 2.04 KB
/
test.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
var tap = require('tap');
var condition = require('./index');
tap.test('throws when no settings or test function specified', function (t) {
t.plan(2);
function success() {
return Promise.resolve('success');
}
t.throws(condition()(success));
t.throws(condition({})(success));
});
function createResponse(status) {
return {
status: status
};
}
function test(data) {
return data.status === 'ok';
}
tap.test('resolves when condition is truthy', function (t) {
t.plan(1);
var response = createResponse('ok');
function belowStatus() {
return Promise.resolve(response);
}
condition({test: test})(belowStatus)().then(function (result) {
t.equal(result, response, 'result should be original promise result');
}).catch(function (err) {
t.bailout('the promise was unexpectedly rejected');
});
});
tap.test('rejects when condition is falsy', function (t) {
t.plan(4);
var response = createResponse('not good!');
function aboveStatus() {
return Promise.resolve(response);
}
condition({test:test})(aboveStatus)().then(function (res) {
t.bailout('the promise was unexpectedly resolved');
}).catch(function (error) {
t.ok(error instanceof condition.ConditionError, 'error should be instance of ConditionError');
t.equal(error.fn, aboveStatus, 'initial functon was not returned');
t.equal(error.message, 'Condition test failed', 'wrong error message');
t.equal(error.data, response, 'initial response was not returned');
});
});
tap.test('forward error if promise rejects', function (t) {
t.plan(1);
var expectedError = {
something: 'went wrong'
};
function willReject() {
return Promise.reject(expectedError);
}
condition({test:test})(willReject)().then(function (res) {
t.bailout('the promise was unexpectedly resolved');
}).catch(function (error) {
t.equal(error, expectedError, 'The promise was not reject with the original error');
});
});