import { Async } from "."; const Path = Async.Path; describe("Given", () => { describe("a promise", () => { test("an async path can be created", () => { const promise = new Promise<{}>(() => { }); const result = Path.fromPromise(promise); expect(result).toBeInstanceOf(Async.Path); }) describe("when the promise is resolved", () => { test("then wer'e on a happy path", done => { const expectedStr = "Promise resolved!"; const promise = new Promise((resolve) => { resolve(expectedStr); }) const callback = (str: string) => { expect(str).toBe(expectedStr); done(); } Path.fromPromise(promise) .onHappyPath(callback); }) describe("where", () => { test("only happy continuations gets executed", done => { expect.assertions(3); const expectedStr = "Promise was resolved!"; const promise = new Promise((resolve) => { resolve(""); }) const firstCallback = (str: string) => { expect(str).toBe(""); return "Promise"; } const secondCallback = (str: string) => { expect(str).toBe("Promise"); return `${str} was`; } const finalCallback = (str: string) => { const result = `${str} resolved!`; expect(result).toBe(expectedStr); done(); } return Path.fromPromise(promise) .onHappyPath(firstCallback) .onHappyPath(secondCallback) .onSadPath(() => { expect(true).toBe(true); }) .onHappyPath(finalCallback); }) }) }) describe("when the promise is rejected", () => { describe("then wer'e on a sad path", () => { const expectedErrorMessage = "Expected error!"; const promise = new Promise<{}>((resolve, reject) => { reject(new Error("Expected error!")); }) test("that handles the rejected error", done => { Path.fromPromise(promise) .onSadPath((error) => { expect(error.message).toBe(expectedErrorMessage); done(); }) }) test("that ignores the rejected error", done => { Path.fromPromise(promise) .onSadPath(() => { expect(true).toBe(true) done(); }); }) describe("where", () => { test("only sad continuations gets executed", done => { expect.assertions(2); Path.fromPromise(promise) .onHappyPath(() => { expect(true).toBe(true)}) .onHappyPath(() => { expect(true).toBe(true)}) .onSadPath(() => { expect(true).toBe(true); }) .onSadPath(() => { expect(true).toBe(true); done(); }) }) }) }) }) }) })