Skip to content

Commit

Permalink
fix: handle procedures with colliding names (#1)
Browse files Browse the repository at this point in the history
- add $ to path regexes

Co-authored-by: malo <[email protected]>
  • Loading branch information
maloguertin and malo authored Jan 31, 2023
1 parent 8c03b41 commit f89e400
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/createTRPCMsw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
44 changes: 28 additions & 16 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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')

Expand All @@ -55,20 +74,7 @@ describe('config', () => {
const mswTrpc = createTRPCMsw<AppRouter>({ baseUrl: 'http://localhost:3000/trpc' })
const nestedMswTrpc = createTRPCMsw<NestedAppRouter>({ 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())

Expand All @@ -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')

Expand Down
13 changes: 13 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f89e400

Please sign in to comment.