diff --git a/packages/app/universal-testing-utils/src/MswHelper.spec.ts b/packages/app/universal-testing-utils/src/MswHelper.spec.ts index 51533d6b3..24a441756 100644 --- a/packages/app/universal-testing-utils/src/MswHelper.spec.ts +++ b/packages/app/universal-testing-utils/src/MswHelper.spec.ts @@ -1,9 +1,14 @@ import { mapRouteToPath } from '@lokalise/api-contracts' -import { sendByGetRoute, sendByPayloadRoute } from '@lokalise/frontend-http-client' +import { + sendByDeleteRoute, + sendByGetRoute, + sendByPayloadRoute, +} from '@lokalise/frontend-http-client' import { setupServer } from 'msw/node' import { afterAll, afterEach, beforeEach, describe, expect, it } from 'vitest' import wretch from 'wretch' import { + deleteContract, getContract, getContractWithPathParams, getContractWithQueryParams, @@ -248,6 +253,23 @@ describe('MswHelper', () => { `) }) + it('mocks POST request custom void implementation', async () => { + const mock = vi.fn() + + mswHelper.mockValidResponseWithImplementation(deleteContract, server, { + handleRequest: () => { + mock() + }, + }) + + const response = await sendByDeleteRoute(wretchClient, deleteContract, { + pathParams: { userId: ':userId' }, + }) + + expect(mock).toHaveBeenCalledOnce() + expect(response).toMatchInlineSnapshot('null') + }) + it('mocks POST request with path params with custom implementation', async () => { const mock = vi.fn() diff --git a/packages/app/universal-testing-utils/src/MswHelper.ts b/packages/app/universal-testing-utils/src/MswHelper.ts index 7c194141e..58b73aab9 100644 --- a/packages/app/universal-testing-utils/src/MswHelper.ts +++ b/packages/app/universal-testing-utils/src/MswHelper.ts @@ -1,5 +1,6 @@ import { type CommonRouteDefinition, + type DeleteRouteDefinition, type InferSchemaInput, type InferSchemaOutput, type PayloadRouteDefinition, @@ -151,7 +152,8 @@ export class MswHelper { IsNonJSONResponseExpected, IsEmptyResponseExpected > - | PayloadRouteDefinition, RequestBodySchema>, + | PayloadRouteDefinition, RequestBodySchema> + | DeleteRouteDefinition, RequestBodySchema>, server: SetupServerApi, params: PathParamsSchema extends undefined ? MockWithImplementationParamsNoPath< @@ -177,20 +179,21 @@ export class MswHelper { server.use( http[method](resolvedPath, async (requestInfo) => { - return HttpResponse.json( - await params.handleRequest( - requestInfo as Parameters< - HttpResponseResolver< - InferSchemaInput, - InferSchemaInput, - InferSchemaInput - > - >[0], - ), - { - status: params.responseCode, - }, + const response = await params.handleRequest( + requestInfo as Parameters< + HttpResponseResolver< + InferSchemaInput, + InferSchemaInput, + InferSchemaInput + > + >[0], ) + + if (!response) return new HttpResponse(null, { status: params.responseCode }) + + return HttpResponse.json(response, { + status: params.responseCode, + }) }), ) } diff --git a/packages/app/universal-testing-utils/test/testContracts.ts b/packages/app/universal-testing-utils/test/testContracts.ts index 952a76a68..eac061983 100644 --- a/packages/app/universal-testing-utils/test/testContracts.ts +++ b/packages/app/universal-testing-utils/test/testContracts.ts @@ -1,4 +1,4 @@ -import { buildGetRoute, buildPayloadRoute } from '@lokalise/api-contracts' +import { buildDeleteRoute, buildGetRoute, buildPayloadRoute } from '@lokalise/api-contracts' import { z } from 'zod' const REQUEST_BODY_SCHEMA = z.object({ @@ -34,6 +34,15 @@ export const getContract = buildGetRoute({ pathResolver: () => '/', }) +export const deleteContract = buildDeleteRoute({ + successResponseBodySchema: z.void(), + description: 'some description', + responseSchemasByStatusCode: { + 201: z.void(), + }, + pathResolver: () => '/', +}) + export const getContractWithQueryParams = buildGetRoute({ successResponseBodySchema: RESPONSE_BODY_SCHEMA, description: 'some description',