Skip to content

Commit

Permalink
fix(config-resolver): replace non-null assertions (#4279)
Browse files Browse the repository at this point in the history
* chore(test): show failing unit tests

* fix(config-resolver): replace non-null assertions

Replace non-null assertions with fallback
to `false` value.

Co-authored-by: Eduardo Rodrigues <eduardomourar@users.noreply.github.com>
  • Loading branch information
eduardomourar and eduardomourar committed Dec 13, 2022
1 parent ebc7261 commit e5e753c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ describe(resolveCustomEndpointsConfig.name, () => {
});

afterEach(() => {
expect(normalizeProvider).toHaveBeenCalledTimes(2);
expect(normalizeProvider).toHaveBeenNthCalledWith(2, mockInput.useDualstackEndpoint);
jest.clearAllMocks();
});

describe("tls", () => {
afterEach(() => {
expect(normalizeProvider).toHaveBeenCalledTimes(2);
expect(normalizeProvider).toHaveBeenNthCalledWith(2, mockInput.useDualstackEndpoint);
});

it.each([true, false])("returns %s when the value is passed", (tls) => {
expect(resolveCustomEndpointsConfig({ ...mockInput, tls }).tls).toStrictEqual(tls);
});
Expand All @@ -43,9 +46,19 @@ describe(resolveCustomEndpointsConfig.name, () => {
expect(resolveCustomEndpointsConfig(mockInput).isCustomEndpoint).toStrictEqual(true);
});

it("returns false when useDualstackEndpoint is not defined", async () => {
const useDualstackEndpoint = await resolveCustomEndpointsConfig({
...mockInput,
useDualstackEndpoint: undefined,
}).useDualstackEndpoint();
expect(useDualstackEndpoint).toStrictEqual(false);
});

describe("returns normalized endpoint", () => {
afterEach(() => {
expect(normalizeProvider).toHaveBeenCalledTimes(2);
expect(normalizeProvider).toHaveBeenNthCalledWith(1, mockInput.endpoint);
expect(normalizeProvider).toHaveBeenNthCalledWith(2, mockInput.useDualstackEndpoint);
});

it("calls urlParser endpoint is of type string", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export const resolveCustomEndpointsConfig = <T>(
tls: input.tls ?? true,
endpoint: normalizeProvider(typeof endpoint === "string" ? urlParser(endpoint) : endpoint),
isCustomEndpoint: true,
useDualstackEndpoint: normalizeProvider(input.useDualstackEndpoint!),
useDualstackEndpoint: normalizeProvider(input.useDualstackEndpoint ?? false),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ describe(resolveEndpointsConfig.name, () => {
});

afterEach(() => {
expect(normalizeProvider).toHaveBeenNthCalledWith(1, mockInput.useDualstackEndpoint);
jest.clearAllMocks();
});

describe("tls", () => {
afterEach(() => {
expect(normalizeProvider).toHaveBeenNthCalledWith(1, mockInput.useDualstackEndpoint);
});

it.each([true, false])("returns %s when it's %s", (tls) => {
expect(resolveEndpointsConfig({ ...mockInput, tls }).tls).toStrictEqual(tls);
});
Expand All @@ -43,6 +46,10 @@ describe(resolveEndpointsConfig.name, () => {
});

describe("isCustomEndpoint", () => {
afterEach(() => {
expect(normalizeProvider).toHaveBeenNthCalledWith(1, mockInput.useDualstackEndpoint);
});

it("returns true when endpoint is defined", () => {
expect(resolveEndpointsConfig(mockInput).isCustomEndpoint).toStrictEqual(true);
});
Expand All @@ -53,7 +60,19 @@ describe(resolveEndpointsConfig.name, () => {
});
});

it("returns false when useDualstackEndpoint is not defined", async () => {
const useDualstackEndpoint = await resolveEndpointsConfig({
...mockInput,
useDualstackEndpoint: undefined,
}).useDualstackEndpoint();
expect(useDualstackEndpoint).toStrictEqual(false);
});

describe("endpoint", () => {
afterEach(() => {
expect(normalizeProvider).toHaveBeenNthCalledWith(1, mockInput.useDualstackEndpoint);
});

describe("returns from normalizeProvider when endpoint is defined", () => {
afterEach(() => {
expect(normalizeProvider).toHaveBeenCalledTimes(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface EndpointsResolvedConfig extends Required<EndpointsInputConfig>
export const resolveEndpointsConfig = <T>(
input: T & EndpointsInputConfig & PreviouslyResolved
): T & EndpointsResolvedConfig => {
const useDualstackEndpoint = normalizeProvider(input.useDualstackEndpoint!);
const useDualstackEndpoint = normalizeProvider(input.useDualstackEndpoint ?? false);
const { endpoint, useFipsEndpoint, urlParser } = input;
return {
...input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ describe("RegionConfig", () => {
it("throw if region is not supplied", () => {
expect(() => resolveRegionConfig({ useFipsEndpoint: mockUseFipsEndpoint })).toThrow();
});

it("returns false when useFipsEndpoint is not defined", async () => {
const useFipsEndpoint = await resolveRegionConfig({
region: () => Promise.resolve(mockRegion),
}).useFipsEndpoint();
expect(useFipsEndpoint).toStrictEqual(false);
});
});

describe("useFipsEndpoint", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export const resolveRegionConfig = <T>(input: T & RegionInputConfig & Previously
if (isFipsRegion(providedRegion)) {
return true;
}
return typeof useFipsEndpoint === "boolean" ? Promise.resolve(useFipsEndpoint) : useFipsEndpoint!();
if (!useFipsEndpoint) {
return Promise.resolve(false);
}
return typeof useFipsEndpoint === "boolean" ? Promise.resolve(useFipsEndpoint) : useFipsEndpoint();
},
};
};

0 comments on commit e5e753c

Please sign in to comment.