-
Notifications
You must be signed in to change notification settings - Fork 9
/
promiseThatSpec.js
141 lines (115 loc) · 3.65 KB
/
promiseThatSpec.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
'use strict';
const _ = require('lodash');
const AssertionError = require('assertion-error');
const __ = require('../..');
const TestMatcher = require('./TestMatcher');
describe('promiseThat', () => {
it('should return promise', () => {
const result = __.promiseThat('a value', new TestMatcher(() => true));
__.assertThat(result, __.is(__.promise()));
});
it('should pass given value to matcher', async () => {
let passedValue;
await __.promiseThat('a value', new TestMatcher((value) => {
passedValue = value;
return true;
}));
__.assertThat(passedValue, __.is('a value'));
});
it('should fulfill promise if matcher matches', async () => {
const input = 'expected value';
await __.promiseThat(input, __.is('expected value'));
});
it('should resolve if matcher returns promise resolving to true', async () => {
await __.promiseThat('a value', new TestMatcher(() => {
return Promise.resolve(true);
}));
});
it('should reject promise with AssertionError if matcher resolves to false', async () => {
try {
await __.promiseThat('a value', new TestMatcher(() => Promise.resolve(false)));
throw new Error('Should not be fulfilled');
} catch (e) {
__.assertThat(e, __.instanceOf(AssertionError));
}
});
it('should defer result until matcher promise resolves', (done) => {
let resolveFn;
const deferred = new Promise((resolve) => {
resolveFn = resolve;
});
__.promiseThat('a value', new TestMatcher(() => {
return deferred;
}))
.then(done, done);
resolveFn(true);
});
it('should allow a message as first parameter', async () => {
const message = 'Some explanation';
try {
await __.promiseThat(message, 'different value', __.is('expected value'));
throw new Error('Should not be fulfilled');
} catch (e) {
__.assertThat(e, __.instanceOf(AssertionError));
__.assertThat(e.message, __.containsString(message));
}
});
it('should forward rejection if matcher returns rejecting promise', async () => {
const rejectionValue = new Error('some reason');
const rejectingMatcher = new TestMatcher(() => Promise.reject(rejectionValue));
try {
await __.promiseThat('a value', rejectingMatcher);
throw new Error('Should not be fulfilled');
} catch (e) {
__.assertThat(e, __.is(rejectionValue));
}
});
it('should allow matchers to describe mismatch asynchronously', (done) => {
let resolveFn;
const deferred = new Promise((resolve) => {
resolveFn = resolve;
});
const matcher = _.create(new __.Matcher(), {
matches() {
return false;
},
describeTo(description) {
description.append('Matcher description');
},
describeMismatch(__actual, description) {
return deferred.then(() => {
description.append('Deferred mismatch description');
});
}
});
__.promiseThat('a value', matcher).then(
() => {
throw new Error('Should not be fulfilled');
},
(reason) => {
__.assertThat(reason, __.instanceOf(AssertionError));
__.assertThat(reason.message, __.containsString('Deferred mismatch description'));
}
)
.then(done, done);
resolveFn();
});
it('should pass diff representations to AssertionError', async () => {
const testMatcher = new TestMatcher(() => { return false; });
testMatcher.getExpectedForDiff = () => {
return 'expected for diff';
};
testMatcher.formatActualForDiff = function (actual) {
return Promise.resolve('actual for diff: ' + actual);
};
try {
await __.promiseThat('actual value', testMatcher);
throw new Error('Should not be fulfilled');
} catch (e) {
__.assertThat(e, __.hasProperties({
expected: 'expected for diff',
actual: 'actual for diff: actual value'
}));
}
});
});