Created
September 9, 2020 06:33
-
-
Save mbergal/5f8c6bf7ac902c96fcd1b9440eab66e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Jest; | |
let getData = (): Promise.promise(string) => Promise.resolved("some initial data"); | |
type firstStepResult = {first: string}; | |
type secondStepResult = {second: string}; | |
let doFirstStep = (v: string) => { | |
Js.log(v); | |
Promise.resolved(Ok({first: "first"})); | |
}; | |
let doSecondStep = (v: firstStepResult) => { | |
Js.log(v); | |
Promise.resolved(Ok({second: "second"})); | |
}; | |
let errorOnFirstStep = (v: string) => { | |
Promise.resolved(Error(v)); | |
}; | |
describe("Async workflow", () => { | |
testPromise("Ok", () => { | |
Expect.( | |
getData() | |
->Promise.flatMap(doFirstStep) | |
->Promise.flatMapOk(doSecondStep) | |
->Promise.flatMap(x=>{ | |
Promise.resolved(switch x { | |
| Ok(x) => expect(x) |> toEqual({second: "second"}) | |
| _ => fail("error") | |
}) | |
}) | |
-> Promise.Js.toBsPromise | |
) | |
}); | |
testPromise("Errors", () => { | |
Expect.( | |
getData() | |
->Promise.flatMap(errorOnFirstStep) | |
->Promise.flatMapOk(doSecondStep) | |
->Promise.flatMap(x=> { | |
Promise.resolved( | |
switch (x) { | |
| Error(x) => expect(x) |> toBe("some initial data") | |
| _ => fail("Was not Error") | |
}, | |
); | |
}) | |
-> Promise.Js.toBsPromise | |
) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment