Skip to content

Commit

Permalink
updated crud test utils, now, if no expectedResponse is provided to c…
Browse files Browse the repository at this point in the history
…heckMethod(), an empty object of type T is created and request is nocked with it
  • Loading branch information
tusharpandey13 committed Nov 8, 2024
1 parent 23af401 commit b6cd08e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions test/management/tests.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,17 @@ export function checkRequestInterceptor<T>(operation: Promise<T>, request: nock.
export function checkOperation<T>(operation: Promise<ApiResponse<T>>, expectedResponse: T): void {
it('should test the method', async () => {
const result = await operation;
expect(result.data).toEqual(expectedResponse);
expect(result.data).toEqual(
// only compare if the expected response is defined (api returns data), in this case
// expectedResponse is an empty object
result.data ? expectedResponse : undefined
);
});
}

export type CheckMethodParams<T> = {
operation: Promise<ApiResponse<T>>;
expectedResponse: any;
expectedResponse?: T;
uri: string | RegExp | { (uri: string): boolean };
method: string;
requestBody?: RequestBodyMatcher | any;
Expand All @@ -102,22 +106,25 @@ export type CheckMethodParams<T> = {
* @param {string} params.method - The HTTP method to intercept (e.g., 'GET', 'POST').
* @param {RequestBodyMatcher | any} [params.requestBody] - The optional request body to match.
*/
export const checkMethod = <T>({
export const checkMethod = <T, MethodParams extends CheckMethodParams<T>>({
operation,
expectedResponse,
uri,
method,
requestBody,
}: CheckMethodParams<T>): void => {
expectedResponse,
}: MethodParams): void => {
// set the expected response to an empty object if it is not provided
const finalExpectedResponse: T = expectedResponse ?? <T>{};

// nock the API with success scenario
let request: nock.Scope = nock(API_URL)
.intercept(uri, method, requestBody)
.reply(200, expectedResponse);
.reply(200, finalExpectedResponse as any);

// check for various success checks
checkForPromise(operation);
checkRequestInterceptor(operation, request);
checkOperation(operation, expectedResponse);
checkOperation(operation, finalExpectedResponse);

// nock the API with error scenario
request = nock(API_URL).intercept(uri, method, requestBody).reply(500);
Expand Down

0 comments on commit b6cd08e

Please sign in to comment.