From f89e400b670bd7660d5df4de9302f721e62fdb90 Mon Sep 17 00:00:00 2001 From: Malo Guertin Date: Tue, 31 Jan 2023 10:54:53 -0500 Subject: [PATCH] fix: handle procedures with colliding names (#1) - add $ to path regexes Co-authored-by: malo --- package.json | 2 +- src/createTRPCMsw.ts | 2 +- test/integration.test.ts | 44 +++++++++++++++++++++++++--------------- test/setup.ts | 13 ++++++++++++ 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index d39f14d..0a21bd9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "msw-trpc", - "version": "1.1.0", + "version": "1.1.1", "description": "Trpc API for Mock Service Worker (MSW).", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/createTRPCMsw.ts b/src/createTRPCMsw.ts index 03e594a..86e5cd5 100644 --- a/src/createTRPCMsw.ts +++ b/src/createTRPCMsw.ts @@ -28,7 +28,7 @@ const getRegexpAsString = (baseUrl: string | RegExp) => { return baseUrlAsString } -const buildUrlFromPathParts = (pathParts: string[]) => new RegExp(pathParts.map(getRegexpAsString).join('[/.|.]')) +const buildUrlFromPathParts = (pathParts: string[]) => new RegExp(pathParts.map(getRegexpAsString).join('[/.|.]') + '$') // @ts-expect-error any const createUntypedTRPCMsw = ( diff --git a/test/integration.test.ts b/test/integration.test.ts index d9eafe0..202b011 100644 --- a/test/integration.test.ts +++ b/test/integration.test.ts @@ -3,21 +3,34 @@ import { AppRouter, mswTrpc, NestedAppRouter, nestedMswTrpc, nestedTrpc, trpc } import { setupServer } from 'msw/node' import { createTRPCMsw } from '../src' -describe('queries and mutations', () => { - const server = setupServer( +type MswTrpc = typeof mswTrpc +type NestedMswTrpc = typeof nestedMswTrpc + +const setupServerWithQueries = (mswTrpc: MswTrpc, nestedMswTrpc: NestedMswTrpc) => { + return setupServer( mswTrpc.userById.query((req, res, ctx) => { return res(ctx.status(200), ctx.data({ id: '1', name: 'Malo' })) }), + mswTrpc.userByIdAndPost.query((req, res, ctx) => { + return res(ctx.status(200), ctx.data({ id: '1', name: 'Malo', posts: ['1'] })) + }), mswTrpc.createUser.mutation(async (req, res, ctx) => { return res(ctx.status(200), ctx.data({ id: '2', name: await req.json() })) }), nestedMswTrpc.users.userById.query((req, res, ctx) => { return res(ctx.status(200), ctx.data({ id: '1', name: 'Malo' })) }), + nestedMswTrpc.users.userByIdAndPost.query((req, res, ctx) => { + return res(ctx.status(200), ctx.data({ id: '1', name: 'Malo', posts: ['1'] })) + }), nestedMswTrpc.users.createUser.mutation(async (req, res, ctx) => { return res(ctx.status(200), ctx.data({ id: '2', name: await req.json() })) }) ) +} + +describe('queries and mutations', () => { + const server = setupServerWithQueries(mswTrpc, nestedMswTrpc) beforeAll(() => server.listen()) @@ -42,6 +55,12 @@ describe('queries and mutations', () => { expect(user).toEqual({ id: '1', name: 'Malo' }) }) + test('msw server setup from msw-trpc query handle should handle queries with same starting string properly', async () => { + const user = await nestedTrpc.users.userByIdAndPost.query('1') + + expect(user).toEqual({ id: '1', name: 'Malo', posts: ['1'] }) + }) + test('msw server setup from msw-trpc query handle should handle mutations properly', async () => { const user = await nestedTrpc.users.createUser.mutate('Robert') @@ -55,20 +74,7 @@ describe('config', () => { const mswTrpc = createTRPCMsw({ baseUrl: 'http://localhost:3000/trpc' }) const nestedMswTrpc = createTRPCMsw({ baseUrl: 'http://localhost:3000/trpc' }) - const server = setupServer( - mswTrpc.userById.query((req, res, ctx) => { - return res(ctx.status(200), ctx.data({ id: '1', name: 'Malo' })) - }), - mswTrpc.createUser.mutation(async (req, res, ctx) => { - return res(ctx.status(200), ctx.data({ id: '2', name: await req.json() })) - }), - nestedMswTrpc.users.userById.query((req, res, ctx) => { - return res(ctx.status(200), ctx.data({ id: '1', name: 'Malo' })) - }), - nestedMswTrpc.users.createUser.mutation(async (req, res, ctx) => { - return res(ctx.status(200), ctx.data({ id: '2', name: await req.json() })) - }) - ) + const server = setupServerWithQueries(mswTrpc, nestedMswTrpc) beforeAll(() => server.listen()) @@ -93,6 +99,12 @@ describe('config', () => { expect(user).toEqual({ id: '1', name: 'Malo' }) }) + test('msw server setup from msw-trpc query handle should handle queries with same starting string properly', async () => { + const user = await nestedTrpc.users.userByIdAndPost.query('1') + + expect(user).toEqual({ id: '1', name: 'Malo', posts: ['1'] }) + }) + test('msw server setup from msw-trpc query handle should handle mutations properly', async () => { const user = await nestedTrpc.users.createUser.mutate('Robert') diff --git a/test/setup.ts b/test/setup.ts index 9a73e22..7f6eb16 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -28,6 +28,19 @@ const appRouter = t.router({ return user }), + userByIdAndPost: t.procedure + .input((val: unknown) => { + if (typeof val === 'string') return val + + throw new Error(`Invalid input: ${typeof val}`) + }) + .query(req => { + const { input } = req + + const user = userList.find(u => u.id === input) + + return { ...user, posts: ['1'] } + }), createUser: t.procedure .input((val: unknown) => { if (typeof val === 'string') return val