|
1 |
| -import { defaultProvider } from "./defaultProvider"; |
2 |
| -import { ProviderError } from "@aws-sdk/property-provider"; |
| 1 | +import { fromEnv } from "./fromEnv"; |
| 2 | +import { fromSharedConfigFiles } from "./fromSharedConfigFiles"; |
| 3 | +import { chain, memoize } from "@aws-sdk/property-provider"; |
| 4 | +import { |
| 5 | + defaultProvider, |
| 6 | + RegionProviderConfiguration |
| 7 | +} from "./defaultProvider"; |
3 | 8 |
|
4 |
| -jest.mock("./fromEnv", () => { |
5 |
| - const envProvider = jest.fn(); |
6 |
| - return { |
7 |
| - fromEnv: jest.fn().mockReturnValue(envProvider) |
8 |
| - }; |
9 |
| -}); |
10 |
| -import { fromEnv, EnvConfiguration } from "./fromEnv"; |
| 9 | +jest.mock("./fromEnv", () => ({ |
| 10 | + fromEnv: jest.fn() |
| 11 | +})); |
11 | 12 |
|
12 |
| -jest.mock("./fromSharedConfigFiles", () => { |
13 |
| - const iniProvider = jest.fn(); |
14 |
| - return { |
15 |
| - fromSharedConfigFiles: jest.fn().mockReturnValue(iniProvider) |
16 |
| - }; |
17 |
| -}); |
18 |
| -import { |
19 |
| - fromSharedConfigFiles, |
20 |
| - SharedConfigInit |
21 |
| -} from "./fromSharedConfigFiles"; |
| 13 | +jest.mock("./fromSharedConfigFiles", () => ({ |
| 14 | + fromSharedConfigFiles: jest.fn() |
| 15 | +})); |
22 | 16 |
|
23 |
| -beforeEach(() => { |
24 |
| - (fromEnv() as any).mockClear(); |
25 |
| - (fromSharedConfigFiles() as any).mockClear(); |
26 |
| - (fromEnv as any).mockClear(); |
27 |
| - (fromSharedConfigFiles as any).mockClear(); |
28 |
| -}); |
| 17 | +jest.mock("@aws-sdk/property-provider", () => ({ |
| 18 | + chain: jest.fn(), |
| 19 | + memoize: jest.fn() |
| 20 | +})); |
29 | 21 |
|
30 | 22 | describe("defaultProvider", () => {
|
31 |
| - it("should stop after the environmental provider if a region has been found", async () => { |
32 |
| - const region = "foo"; |
33 |
| - (fromEnv() as any).mockImplementation(() => Promise.resolve(region)); |
| 23 | + const configuration: RegionProviderConfiguration = { |
| 24 | + profile: "profile", |
| 25 | + environmentVariableName: "environmentVariableName" |
| 26 | + }; |
34 | 27 |
|
35 |
| - expect(await defaultProvider()()).toEqual(region); |
36 |
| - expect((fromEnv() as any).mock.calls.length).toBe(1); |
37 |
| - expect((fromSharedConfigFiles() as any).mock.calls.length).toBe(0); |
| 28 | + afterEach(() => { |
| 29 | + jest.clearAllMocks(); |
38 | 30 | });
|
39 | 31 |
|
40 |
| - it("should continue on to the ini provider if no environment variable region has been found", async () => { |
41 |
| - const region = "foo"; |
| 32 | + it("passes fromEnv() and fromSharedConfigFiles() to chain", () => { |
| 33 | + const mockFromEnvReturn = "mockFromEnvReturn"; |
| 34 | + (fromEnv as jest.Mock).mockReturnValueOnce(mockFromEnvReturn); |
42 | 35 |
|
43 |
| - (fromEnv() as any).mockImplementation(() => |
44 |
| - Promise.reject(new ProviderError("Nothing here!")) |
| 36 | + const mockFromSharedConfigFilesReturn = "mockFromSharedConfigFilesReturn"; |
| 37 | + (fromSharedConfigFiles as jest.Mock).mockReturnValueOnce( |
| 38 | + mockFromSharedConfigFilesReturn |
45 | 39 | );
|
46 |
| - (fromSharedConfigFiles() as any).mockImplementation(() => |
47 |
| - Promise.resolve(region) |
48 |
| - ); |
49 |
| - |
50 |
| - expect(await defaultProvider()()).toEqual(region); |
51 |
| - expect((fromEnv() as any).mock.calls.length).toBe(1); |
52 |
| - expect((fromSharedConfigFiles() as any).mock.calls.length).toBe(1); |
53 |
| - }); |
54 |
| - |
55 |
| - it("should pass configuration on to the env provider", async () => { |
56 |
| - const envConfig: EnvConfiguration = { |
57 |
| - environmentVariableName: "foo" |
58 |
| - }; |
59 |
| - |
60 |
| - (fromEnv() as any).mockImplementation(() => Promise.resolve("region")); |
61 |
| - (fromEnv as any).mockClear(); |
62 | 40 |
|
63 |
| - await expect(defaultProvider(envConfig)()).resolves; |
64 |
| - |
65 |
| - expect((fromEnv as any).mock.calls.length).toBe(1); |
66 |
| - expect((fromEnv as any).mock.calls[0][0]).toBe(envConfig); |
67 |
| - }); |
| 41 | + defaultProvider(configuration); |
68 | 42 |
|
69 |
| - it("should pass configuration on to the ini provider", async () => { |
70 |
| - const iniConfig: SharedConfigInit = { |
71 |
| - profile: "foo", |
72 |
| - filepath: "/home/user/.secrets/credentials.ini", |
73 |
| - configFilepath: "/home/user/.secrets/credentials.ini" |
74 |
| - }; |
| 43 | + expect(fromEnv).toHaveBeenCalledTimes(1); |
| 44 | + expect(fromEnv).toHaveBeenCalledWith(configuration); |
| 45 | + expect(fromSharedConfigFiles).toHaveBeenCalledTimes(1); |
| 46 | + expect(fromSharedConfigFiles).toHaveBeenCalledWith(configuration); |
75 | 47 |
|
76 |
| - (fromEnv() as any).mockImplementation(() => |
77 |
| - Promise.reject(new ProviderError("Keep moving!")) |
| 48 | + expect(chain).toHaveBeenCalledTimes(1); |
| 49 | + expect(chain).toHaveBeenCalledWith( |
| 50 | + mockFromEnvReturn, |
| 51 | + mockFromSharedConfigFilesReturn |
78 | 52 | );
|
79 |
| - (fromSharedConfigFiles() as any).mockImplementation(() => |
80 |
| - Promise.resolve("region") |
81 |
| - ); |
82 |
| - |
83 |
| - (fromSharedConfigFiles as any).mockClear(); |
84 |
| - |
85 |
| - await expect(defaultProvider(iniConfig)()).resolves; |
86 |
| - |
87 |
| - expect((fromSharedConfigFiles as any).mock.calls.length).toBe(1); |
88 |
| - expect((fromSharedConfigFiles as any).mock.calls[0][0]).toBe(iniConfig); |
89 | 53 | });
|
90 | 54 |
|
91 |
| - it("should return the same promise across invocations", async () => { |
92 |
| - const region = "foo"; |
93 |
| - (fromEnv() as any).mockImplementation(() => Promise.resolve(region)); |
| 55 | + it("passes output of chain to memoize", () => { |
| 56 | + const mockChainReturn = "mockChainReturn"; |
| 57 | + (chain as jest.Mock).mockReturnValueOnce(mockChainReturn); |
94 | 58 |
|
95 |
| - const provider = defaultProvider(); |
| 59 | + defaultProvider(configuration); |
96 | 60 |
|
97 |
| - expect(await provider()).toEqual(region); |
| 61 | + expect(chain).toHaveBeenCalledTimes(1); |
| 62 | + expect(memoize).toHaveBeenCalledTimes(1); |
| 63 | + expect(memoize).toHaveBeenCalledWith(mockChainReturn); |
| 64 | + }); |
98 | 65 |
|
99 |
| - expect(provider()).toBe(provider()); |
| 66 | + it("returns output memoize", () => { |
| 67 | + const mockMemoizeReturn = "mockMemoizeReturn"; |
| 68 | + (memoize as jest.Mock).mockReturnValueOnce(mockMemoizeReturn); |
100 | 69 |
|
101 |
| - expect(await provider()).toEqual(region); |
102 |
| - expect((fromEnv() as any).mock.calls.length).toBe(1); |
| 70 | + expect(defaultProvider(configuration)).toEqual(mockMemoizeReturn); |
103 | 71 | });
|
104 | 72 | });
|
0 commit comments