|
| 1 | +import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider"; |
| 2 | + |
1 | 3 | import {
|
2 |
| - NODE_USE_ARN_REGION_CONFIG_OPTIONS as loadOptions, |
| 4 | + NODE_USE_ARN_REGION_CONFIG_OPTIONS, |
3 | 5 | NODE_USE_ARN_REGION_ENV_NAME,
|
4 | 6 | NODE_USE_ARN_REGION_INI_NAME,
|
5 | 7 | } from "./NodeUseArnRegionConfigOptions";
|
6 | 8 |
|
7 |
| -describe("Node useArnRegion config loader", () => { |
8 |
| - describe("environment variable selector", () => { |
9 |
| - const env: { [NODE_USE_ARN_REGION_ENV_NAME]: any } = {} as any; |
| 9 | +jest.mock("@aws-sdk/node-config-provider"); |
10 | 10 |
|
11 |
| - beforeEach(() => { |
12 |
| - delete env[NODE_USE_ARN_REGION_ENV_NAME]; |
13 |
| - }); |
| 11 | +describe("NODE_USE_ARN_REGION_CONFIG_OPTIONS", () => { |
| 12 | + afterEach(() => { |
| 13 | + jest.clearAllMocks(); |
| 14 | + }); |
14 | 15 |
|
15 |
| - it(`should return undefined if the environment variable doesn't have ${NODE_USE_ARN_REGION_ENV_NAME}`, () => { |
16 |
| - expect(loadOptions.environmentVariableSelector(env)).toBeUndefined(); |
| 16 | + const test = (func: Function, obj: { [key: string]: string }, key: string, type: SelectorType) => { |
| 17 | + it.each([true, false, undefined])("returns %s", (output) => { |
| 18 | + (booleanSelector as jest.Mock).mockReturnValueOnce(output); |
| 19 | + expect(func(obj)).toEqual(output); |
| 20 | + expect(booleanSelector).toBeCalledWith(obj, key, type); |
17 | 21 | });
|
18 | 22 |
|
19 |
| - [ |
20 |
| - { envValue: "true", parsed: true }, |
21 |
| - { envValue: "false", parsed: false }, |
22 |
| - ].forEach(({ envValue, parsed }) => { |
23 |
| - it(`should return boolean if the environment variable ${NODE_USE_ARN_REGION_ENV_NAME} is ${envValue}`, () => { |
24 |
| - env[NODE_USE_ARN_REGION_ENV_NAME] = envValue; |
25 |
| - expect(loadOptions.environmentVariableSelector(env)).toBe(parsed); |
| 23 | + it("throws error", () => { |
| 24 | + const mockError = new Error("error"); |
| 25 | + (booleanSelector as jest.Mock).mockImplementationOnce(() => { |
| 26 | + throw mockError; |
26 | 27 | });
|
| 28 | + expect(() => { |
| 29 | + func(obj); |
| 30 | + }).toThrow(mockError); |
27 | 31 | });
|
| 32 | + }; |
28 | 33 |
|
29 |
| - ["0", "1", "yes", "no", undefined, null, void 0, ""].forEach((envValue) => { |
30 |
| - it(`should throw if the environment variable ${NODE_USE_ARN_REGION_ENV_NAME} is ${envValue}`, () => { |
31 |
| - env[NODE_USE_ARN_REGION_ENV_NAME] = envValue as any; |
32 |
| - expect(() => loadOptions.environmentVariableSelector(env)).toThrow( |
33 |
| - `Cannot load env ${NODE_USE_ARN_REGION_ENV_NAME}. Expected "true" or "false", got ${envValue}.` |
34 |
| - ); |
35 |
| - }); |
36 |
| - }); |
| 34 | + describe("calls booleanSelector for environmentVariableSelector", () => { |
| 35 | + const env: { [NODE_USE_ARN_REGION_ENV_NAME]: any } = {} as any; |
| 36 | + const { environmentVariableSelector } = NODE_USE_ARN_REGION_CONFIG_OPTIONS; |
| 37 | + test(environmentVariableSelector, env, NODE_USE_ARN_REGION_ENV_NAME, SelectorType.ENV); |
37 | 38 | });
|
38 | 39 |
|
39 |
| - describe("shared INI files selector", () => { |
| 40 | + describe("calls booleanSelector for configFileSelector", () => { |
40 | 41 | const profileContent: { [NODE_USE_ARN_REGION_INI_NAME]: any } = {} as any;
|
| 42 | + const { configFileSelector } = NODE_USE_ARN_REGION_CONFIG_OPTIONS; |
| 43 | + test(configFileSelector, profileContent, NODE_USE_ARN_REGION_INI_NAME, SelectorType.CONFIG); |
| 44 | + }); |
41 | 45 |
|
42 |
| - beforeEach(() => { |
43 |
| - delete profileContent[NODE_USE_ARN_REGION_INI_NAME]; |
44 |
| - }); |
45 |
| - |
46 |
| - it(`should return undefined if shared config profile doesn't have ${NODE_USE_ARN_REGION_INI_NAME}`, () => { |
47 |
| - expect(loadOptions.configFileSelector(profileContent)).toBeUndefined(); |
48 |
| - }); |
49 |
| - |
50 |
| - [ |
51 |
| - { value: "true", parsed: true }, |
52 |
| - { value: "false", parsed: false }, |
53 |
| - ].forEach(({ value, parsed }) => { |
54 |
| - it(`should return boolean if the shared config profile ${NODE_USE_ARN_REGION_INI_NAME} is ${value}`, () => { |
55 |
| - profileContent[NODE_USE_ARN_REGION_INI_NAME] = value; |
56 |
| - expect(loadOptions.configFileSelector(profileContent)).toBe(parsed); |
57 |
| - }); |
58 |
| - }); |
59 |
| - |
60 |
| - ["0", "1", "yes", "no", undefined, null, void 0, ""].forEach((value) => { |
61 |
| - it(`should throw if the shared config profile ${NODE_USE_ARN_REGION_INI_NAME} is ${value}`, () => { |
62 |
| - profileContent[NODE_USE_ARN_REGION_INI_NAME] = value as any; |
63 |
| - expect(() => loadOptions.configFileSelector(profileContent)).toThrow( |
64 |
| - `Cannot load shared config entry ${NODE_USE_ARN_REGION_INI_NAME}. Expected "true" or "false", got ${value}.` |
65 |
| - ); |
66 |
| - }); |
67 |
| - }); |
| 46 | + it("returns false for default", () => { |
| 47 | + const { default: defaultValue } = NODE_USE_ARN_REGION_CONFIG_OPTIONS; |
| 48 | + expect(defaultValue).toEqual(false); |
68 | 49 | });
|
69 | 50 | });
|
0 commit comments