Skip to content

Commit 0b7d4c1

Browse files
committed
handle empty response
1 parent 565141a commit 0b7d4c1

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

packages/app/universal-testing-utils/src/MswHelper.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { mapRouteToPath } from '@lokalise/api-contracts'
2-
import { sendByGetRoute, sendByPayloadRoute } from '@lokalise/frontend-http-client'
2+
import {
3+
sendByDeleteRoute,
4+
sendByGetRoute,
5+
sendByPayloadRoute,
6+
} from '@lokalise/frontend-http-client'
37
import { setupServer } from 'msw/node'
48
import { afterAll, afterEach, beforeEach, describe, expect, it } from 'vitest'
59
import wretch from 'wretch'
610
import {
11+
deleteContract,
712
getContract,
813
getContractWithPathParams,
914
getContractWithQueryParams,
@@ -248,6 +253,23 @@ describe('MswHelper', () => {
248253
`)
249254
})
250255

256+
it('mocks POST request custom void implementation', async () => {
257+
const mock = vi.fn()
258+
259+
mswHelper.mockValidResponseWithImplementation(deleteContract, server, {
260+
handleRequest: () => {
261+
mock()
262+
},
263+
})
264+
265+
const response = await sendByDeleteRoute(wretchClient, deleteContract, {
266+
pathParams: { userId: ':userId' },
267+
})
268+
269+
expect(mock).toHaveBeenCalledOnce()
270+
expect(response).toMatchInlineSnapshot('null')
271+
})
272+
251273
it('mocks POST request with path params with custom implementation', async () => {
252274
const mock = vi.fn()
253275

packages/app/universal-testing-utils/src/MswHelper.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
type CommonRouteDefinition,
3+
type DeleteRouteDefinition,
34
type InferSchemaInput,
45
type InferSchemaOutput,
56
type PayloadRouteDefinition,
@@ -151,7 +152,8 @@ export class MswHelper {
151152
IsNonJSONResponseExpected,
152153
IsEmptyResponseExpected
153154
>
154-
| PayloadRouteDefinition<InferSchemaOutput<PathParamsSchema>, RequestBodySchema>,
155+
| PayloadRouteDefinition<InferSchemaOutput<PathParamsSchema>, RequestBodySchema>
156+
| DeleteRouteDefinition<InferSchemaOutput<PathParamsSchema>, RequestBodySchema>,
155157
server: SetupServerApi,
156158
params: PathParamsSchema extends undefined
157159
? MockWithImplementationParamsNoPath<
@@ -177,20 +179,22 @@ export class MswHelper {
177179

178180
server.use(
179181
http[method](resolvedPath, async (requestInfo) => {
180-
return HttpResponse.json(
181-
await params.handleRequest(
182-
requestInfo as Parameters<
183-
HttpResponseResolver<
184-
InferSchemaInput<PathParamsSchema>,
185-
InferSchemaInput<RequestBodySchema>,
186-
InferSchemaInput<ResponseBodySchema>
187-
>
188-
>[0],
189-
),
190-
{
191-
status: params.responseCode,
192-
},
182+
const response = await params.handleRequest(
183+
requestInfo as Parameters<
184+
HttpResponseResolver<
185+
InferSchemaInput<PathParamsSchema>,
186+
InferSchemaInput<RequestBodySchema>,
187+
InferSchemaInput<ResponseBodySchema>
188+
>
189+
>[0],
193190
)
191+
192+
throw new Error(response)
193+
if (!response) return new HttpResponse(null, { status: params.responseCode })
194+
195+
return HttpResponse.json(response, {
196+
status: params.responseCode,
197+
})
194198
}),
195199
)
196200
}

packages/app/universal-testing-utils/test/testContracts.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { buildGetRoute, buildPayloadRoute } from '@lokalise/api-contracts'
1+
import { buildDeleteRoute, buildGetRoute, buildPayloadRoute } from '@lokalise/api-contracts'
22
import { z } from 'zod'
33

44
const REQUEST_BODY_SCHEMA = z.object({
@@ -34,6 +34,15 @@ export const getContract = buildGetRoute({
3434
pathResolver: () => '/',
3535
})
3636

37+
export const deleteContract = buildDeleteRoute({
38+
successResponseBodySchema: z.void(),
39+
description: 'some description',
40+
responseSchemasByStatusCode: {
41+
201: z.void(),
42+
},
43+
pathResolver: () => '/',
44+
})
45+
3746
export const getContractWithQueryParams = buildGetRoute({
3847
successResponseBodySchema: RESPONSE_BODY_SCHEMA,
3948
description: 'some description',

0 commit comments

Comments
 (0)