Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
myty committed Sep 24, 2022
1 parent 098056c commit 715a8a7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
42 changes: 42 additions & 0 deletions promise-chain.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import {
assert,
assertEquals,
assertRejects,
} from "https://deno.land/std@0.154.0/testing/asserts.ts";
import {
assertSpyCalls,
spy,
} from "https://deno.land/std@0.157.0/testing/mock.ts";
import PromiseChain, { chain } 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() {
Expand Down Expand Up @@ -76,3 +82,39 @@ Deno.test(async function whenChainedPromiseIsReusedItReturnsCachedResult() {
assertEquals(resultTwo.propertyOne, 1);
assertEquals(resultTwo.propertyTwo, 6);
});

Deno.test(function whenPromiseChainHasExceptionItIsRejected() {
// Arrange
const testClassWithException = new TestClassWithException();

// Act, Assert
assertRejects(() => chain(testClassWithException).throwException());
});

Deno.test(async function whenPromiseChainHasExceptionItIsCaught() {
// Arrange
const catchSpy = spy();
const testClassWithException = new TestClassWithException();

// Act
await chain(testClassWithException)
.throwException()
.catch(catchSpy);

// Assert
assertSpyCalls(catchSpy, 1);
});

Deno.test(async function whenPromiseChainPromiseIsFinalized() {
// Arrange
const finallySpy = spy();
const testClass = new TestClass();

// Act
await chain(testClass)
.asyncIncrementOne()
.finally(finallySpy);

// Assert
assertSpyCalls(finallySpy, 1);
});
5 changes: 5 additions & 0 deletions stubs/test-class-with-exceptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class TestClassWithException {
throwException(): Promise<TestClassWithException> {
return Promise.reject();
}
}

0 comments on commit 715a8a7

Please sign in to comment.