Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test case: using custom decoder #224

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions e2e/src/__tests__/test-api/request-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import * as requestTypes from "../../generated/testapi/requestTypes";

// @ts-ignore because leaked-handles doesn't ship type defintions
import * as leaked from "leaked-handles";
import {
createFetchRequestForApi,
ReplaceRequestParams,
RequestParams,
TypeofApiCall
} from "@pagopa/ts-commons/lib/requests";
import { options } from "yargs";
import { right } from "fp-ts/lib/Either";
leaked.set({ debugSockets: true });

const { isSpecEnabled } = config.specs.testapi;
Expand Down Expand Up @@ -188,4 +196,65 @@ describeSuite("Request types generated from Test API spec", () => {
}
);
});

describe("use custom decoder", () => {
beforeEach(() => {
jest.clearAllMocks();
});

// just a fetch instance that always return the default success status
const defaultSuccessStatus = 201;
const mockFetch = ((() => ({
status: defaultSuccessStatus,
json: async () => ({})
})) as unknown) as typeof fetch;

// just pick one of the response decoders we generate, any will do
const aDecoderFactory = requestTypes.testParameterWithReferenceDecoder;
type aRequestType = requestTypes.TestParameterWithReferenceT;

// just a mock decoder that always successfully decode
// only constraint: DecoderType must extend response decoder for the default status code
type DecoderType = undefined;
const mockIs = jest.fn<boolean, [unknown]>(_ => true);
const mockDecode = jest.fn(e => right<t.Errors, DecoderType>(e));
const mockEncode = jest.fn(e => e);
const aCustomDecoder = new t.Type<DecoderType, DecoderType, unknown>(
"CustomDecoder",
(mockIs as unknown) as t.Is<DecoderType>,
mockDecode,
mockEncode
);

it.each`
testName | decoder
${"with explicit declaration"} | ${aDecoderFactory({ [defaultSuccessStatus]: aCustomDecoder })}
${"with implicit declaration"} | ${aDecoderFactory(aCustomDecoder)}
`("should use custom decoder $testName", async ({ decoder }) => {
const apiRequestT: ReplaceRequestParams<
aRequestType,
RequestParams<aRequestType>
> = {
method: "post",
headers: () => ({
"Content-Type": "application/json"
}),
response_decoder: decoder,
url: ({}) => `/any/path`,
body: () => "any body",
query: () => ({})
};

const apiRequest: TypeofApiCall<aRequestType> = createFetchRequestForApi(
apiRequestT,
{
fetchApi: mockFetch
}
);

const result = await apiRequest({});
expect(mockDecode).toBeCalled();
expect(result.isRight()).toBe(true);
});
});
});