|
1 | 1 | import { chain } from "./chain";
|
2 |
| -import { fromStatic } from "./fromStatic"; |
3 | 2 | import { ProviderError } from "./ProviderError";
|
4 | 3 |
|
| 4 | +const resolveStatic = (staticValue: unknown) => jest.fn().mockResolvedValue(staticValue); |
| 5 | +const rejectWithError = (errorMsg: string) => jest.fn().mockRejectedValue(new Error(errorMsg)); |
| 6 | +const rejectWithProviderError = (errorMsg: string) => jest.fn().mockRejectedValue(new ProviderError(errorMsg)); |
| 7 | + |
5 | 8 | describe("chain", () => {
|
6 | 9 | it("should distill many credential providers into one", async () => {
|
7 |
| - const provider = chain(fromStatic("foo"), fromStatic("bar")); |
8 |
| - |
| 10 | + const provider = chain(resolveStatic("foo"), resolveStatic("bar")); |
9 | 11 | expect(typeof (await provider())).toBe("string");
|
10 | 12 | });
|
11 | 13 |
|
12 | 14 | it("should return the resolved value of the first successful promise", async () => {
|
13 |
| - const provider = chain( |
14 |
| - () => Promise.reject(new ProviderError("Move along")), |
15 |
| - () => Promise.reject(new ProviderError("Nothing to see here")), |
16 |
| - fromStatic("foo") |
17 |
| - ); |
| 15 | + const expectedOutput = "foo"; |
| 16 | + const providers = [ |
| 17 | + rejectWithProviderError("Move along"), |
| 18 | + rejectWithProviderError("Nothing to see here"), |
| 19 | + resolveStatic(expectedOutput), |
| 20 | + ]; |
18 | 21 |
|
19 |
| - expect(await provider()).toBe("foo"); |
| 22 | + try { |
| 23 | + const result = await chain(...providers)(); |
| 24 | + expect(result).toBe(expectedOutput); |
| 25 | + } catch (error) { |
| 26 | + throw error; |
| 27 | + } |
| 28 | + |
| 29 | + expect(providers[0]).toHaveBeenCalledTimes(1); |
| 30 | + expect(providers[1]).toHaveBeenCalledTimes(1); |
| 31 | + expect(providers[2]).toHaveBeenCalledTimes(1); |
20 | 32 | });
|
21 | 33 |
|
22 | 34 | it("should not invoke subsequent providers once one resolves", async () => {
|
| 35 | + const expectedOutput = "foo"; |
23 | 36 | const providers = [
|
24 |
| - jest.fn().mockRejectedValue(new ProviderError("Move along")), |
25 |
| - jest.fn().mockResolvedValue("foo"), |
26 |
| - jest.fn(() => fail("This provider should not be invoked")), |
| 37 | + rejectWithProviderError("Move along"), |
| 38 | + resolveStatic(expectedOutput), |
| 39 | + rejectWithProviderError("This provider should not be invoked"), |
27 | 40 | ];
|
28 | 41 |
|
29 |
| - expect(await chain(...providers)()).toBe("foo"); |
30 |
| - expect(providers[0].mock.calls.length).toBe(1); |
31 |
| - expect(providers[1].mock.calls.length).toBe(1); |
32 |
| - expect(providers[2].mock.calls.length).toBe(0); |
| 42 | + try { |
| 43 | + const result = await chain(...providers)(); |
| 44 | + expect(result).toBe(expectedOutput); |
| 45 | + } catch (error) { |
| 46 | + throw error; |
| 47 | + } |
| 48 | + |
| 49 | + expect(providers[0]).toHaveBeenCalledTimes(1); |
| 50 | + expect(providers[1]).toHaveBeenCalledTimes(1); |
| 51 | + expect(providers[2]).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + describe("should throw if no provider resolves", () => { |
| 55 | + const expectedErrorMsg = "Last provider failed"; |
| 56 | + |
| 57 | + it.each([ |
| 58 | + [ProviderError, rejectWithProviderError(expectedErrorMsg)], |
| 59 | + [Error, rejectWithError(expectedErrorMsg)], |
| 60 | + ])("case %p", async (errorType, errorProviderMockFn) => { |
| 61 | + const firstProviderWhichRejects = rejectWithProviderError("Move along"); |
| 62 | + try { |
| 63 | + await chain(firstProviderWhichRejects, errorProviderMockFn)(); |
| 64 | + throw new Error("Should not get here"); |
| 65 | + } catch (error) { |
| 66 | + expect(error).toEqual(new errorType(expectedErrorMsg)); |
| 67 | + } |
| 68 | + expect(firstProviderWhichRejects).toHaveBeenCalledTimes(1); |
| 69 | + expect(errorProviderMockFn).toHaveBeenCalledTimes(1); |
| 70 | + }); |
33 | 71 | });
|
34 | 72 |
|
35 | 73 | it("should halt if an unrecognized error is encountered", async () => {
|
36 |
| - const provider = chain( |
37 |
| - () => Promise.reject(new ProviderError("Move along")), |
38 |
| - () => Promise.reject(new Error("Unrelated failure")), |
39 |
| - fromStatic("foo") |
40 |
| - ); |
| 74 | + const expectedErrorMsg = "Unrelated failure"; |
| 75 | + const providers = [rejectWithProviderError("Move along"), rejectWithError(expectedErrorMsg), resolveStatic("foo")]; |
| 76 | + |
| 77 | + try { |
| 78 | + await chain(...providers)(); |
| 79 | + throw new Error("Should not get here"); |
| 80 | + } catch (error) { |
| 81 | + expect(error).toEqual(new Error(expectedErrorMsg)); |
| 82 | + } |
| 83 | + |
| 84 | + expect(providers[0]).toHaveBeenCalledTimes(1); |
| 85 | + expect(providers[1]).toHaveBeenCalledTimes(1); |
| 86 | + expect(providers[2]).not.toHaveBeenCalled(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should halt if ProviderError explicitly requests it", async () => { |
| 90 | + const expectedError = new ProviderError("ProviderError with tryNextLink set to false", false); |
| 91 | + const providers = [ |
| 92 | + rejectWithProviderError("Move along"), |
| 93 | + jest.fn().mockRejectedValue(expectedError), |
| 94 | + resolveStatic("foo"), |
| 95 | + ]; |
| 96 | + |
| 97 | + try { |
| 98 | + await chain(...providers)(); |
| 99 | + throw new Error("Should not get here"); |
| 100 | + } catch (error) { |
| 101 | + expect(error).toEqual(expectedError); |
| 102 | + } |
41 | 103 |
|
42 |
| - await expect(provider()).rejects.toMatchObject(new Error("Unrelated failure")); |
| 104 | + expect(providers[0]).toHaveBeenCalledTimes(1); |
| 105 | + expect(providers[1]).toHaveBeenCalledTimes(1); |
| 106 | + expect(providers[2]).not.toHaveBeenCalled(); |
43 | 107 | });
|
44 | 108 |
|
45 | 109 | it("should reject chains with no links", async () => {
|
46 |
| - await expect(chain()()).rejects.toMatchObject(new Error("No providers in chain")); |
| 110 | + try { |
| 111 | + await chain()(); |
| 112 | + throw new Error("Should not get here"); |
| 113 | + } catch (error) { |
| 114 | + expect(error).toEqual(new Error("No providers in chain")); |
| 115 | + } |
47 | 116 | });
|
48 | 117 | });
|
0 commit comments