-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpromise-chain.test.ts
More file actions
119 lines (99 loc) · 3.1 KB
/
promise-chain.test.ts
File metadata and controls
119 lines (99 loc) · 3.1 KB
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
import {
assert,
assertEquals,
assertRejects,
assertSpyCalls,
spy,
} from "./deps.ts";
import PromiseChain from "./promise-chain.ts";
import { TestClassWithException } from "./stubs/test-class-with-exceptions.ts";
import { TestClass } from "./stubs/test-class.ts";
Deno.test(async function whenTraditionalAsyncChainingItReturnsResult() {
// Arrange
const testClass = new TestClass();
// Act
const result = await testClass
.asyncIncrement("propertyOne", 3)
.then((t) => t.asyncIncrementTwo())
.then((t) => t.asyncIncrementOne())
.then((t) => t.increment("propertyTwo", 5))
.then((t) => t.increment("propertyOne", 2))
.then((t) => t.asyncIncrementOne());
// Assert
assertEquals(result.propertyOne, 7);
assertEquals(result.propertyTwo, 6);
});
Deno.test(async function whenAsyncChainingItReturnsResult() {
// Arrange
const testClass = new TestClass();
// Act
const result = await PromiseChain(testClass)
.asyncIncrement("propertyOne", 3)
.asyncIncrementTwo()
.asyncIncrementOne()
.increment("propertyTwo", 5)
.increment("propertyOne", 2)
.asyncIncrementOne();
// Assert
assertEquals(result.propertyOne, 7);
assertEquals(result.propertyTwo, 6);
});
Deno.test(function whenComposableAsyncItIsPromiseLike() {
// Arrange
const testClass = new TestClass();
// Act
const result = PromiseChain(testClass);
// Assert
assert("then" in result && typeof result.then === "function");
assert("catch" in result && typeof result.catch === "function");
assert("finally" in result && typeof result.finally === "function");
});
Deno.test(async function whenChainedPromiseIsReusedItReturnsCachedResult() {
// Arrange
const testClass = new TestClass();
const durationExpectedMs = 250;
const resultTask = PromiseChain(testClass)
.asyncIncrement("propertyTwo", 3)
.asyncIncrementOneLongRunningTask(durationExpectedMs);
await resultTask;
// Act
const startTime = Date.now();
const resultTwo = await resultTask
.asyncIncrement("propertyTwo", 3);
const durationActualMs = Date.now() - startTime;
// Assert
assert(
durationActualMs < durationExpectedMs,
`durationActual was actually ${durationActualMs}`,
);
assertEquals(resultTwo.propertyOne, 1);
assertEquals(resultTwo.propertyTwo, 6);
});
Deno.test(function whenPromiseChainHasExceptionItIsRejected() {
// Arrange
const testClassWithException = new TestClassWithException();
// Act, Assert
assertRejects(() => PromiseChain(testClassWithException).throwException());
});
Deno.test(async function whenPromiseChainHasExceptionItIsCaught() {
// Arrange
const catchSpy = spy();
const testClassWithException = new TestClassWithException();
// Act
await PromiseChain(testClassWithException)
.throwException()
.catch(catchSpy);
// Assert
assertSpyCalls(catchSpy, 1);
});
Deno.test(async function whenPromiseChainPromiseIsFinalized() {
// Arrange
const finallySpy = spy();
const testClass = new TestClass();
// Act
await PromiseChain(testClass)
.asyncIncrementOne()
.finally(finallySpy);
// Assert
assertSpyCalls(finallySpy, 1);
});