Skip to content

Commit

Permalink
feat: use new createCallerFactory function for tests (#304)
Browse files Browse the repository at this point in the history
old function is deprecated
  • Loading branch information
karrui authored Jun 27, 2024
1 parent 2e15005 commit b8cf312
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/server/modules/auth/email/__tests__/email.router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import * as mailLib from '~/lib/mail'
import { prisma } from '~/server/prisma'
import { createTokenHash } from '../../auth.util'
import { emailSessionRouter } from '../email.router'
import { createCallerFactory } from '~/server/trpc'

const createCaller = createCallerFactory(emailSessionRouter)

describe('auth.email', async () => {
let caller: Awaited<ReturnType<typeof emailSessionRouter.createCaller>>
let caller: Awaited<ReturnType<typeof createCaller>>
let session: ReturnType<typeof applySession>

beforeEach(async () => {
session = applySession()
const ctx = await createMockRequest(session)
caller = emailSessionRouter.createCaller(ctx)
caller = createCaller(ctx)
})

describe('login', async () => {
Expand Down
15 changes: 9 additions & 6 deletions src/server/modules/post/__tests__/post.router.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { type User } from '@prisma/client'
import { it, expect } from 'vitest'
import { type RouterInput } from '~/utils/trpc'
import { appRouter } from '../../_app'
import {
applyAuthedSession,
applySession,
createMockRequest,
} from 'tests/integration/helpers/iron-session'
import { createCallerFactory } from '~/server/trpc'
import { postRouter } from '../post.router'

const createCaller = createCallerFactory(postRouter)

describe('post.add', async () => {
it('unauthed user should not be able to create a post', async () => {
const ctx = await createMockRequest(applySession())
const caller = appRouter.createCaller(ctx)
const caller = createCaller(ctx)

const input: RouterInput['post']['add'] = {
title: 'hello test',
content: 'hello test with a long input',
contentHtml: '<p>hello test with a long input</p>',
}

await expect(caller.post.add(input)).rejects.toThrowError()
await expect(caller.add(input)).rejects.toThrowError()
})

it('post should be retrievable after creation', async () => {
Expand All @@ -33,16 +36,16 @@ describe('post.add', async () => {
image: null,
}
const ctx = await createMockRequest(await applyAuthedSession(defaultUser))
const caller = appRouter.createCaller(ctx)
const caller = createCaller(ctx)

const input: RouterInput['post']['add'] = {
title: 'hello test',
content: 'hello test with a long input',
contentHtml: '<p>hello test with a long input</p>',
}

const post = await caller.post.add(input)
const byId = await caller.post.byId({ id: post.id })
const post = await caller.add(input)
const byId = await caller.byId({ id: post.id })

expect(byId).toMatchObject(input)
})
Expand Down
7 changes: 7 additions & 0 deletions src/server/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,10 @@ export const middleware = t.middleware
* @see https://trpc.io/docs/v10/merging-routers
*/
export const mergeRouters = t.mergeRouters

/**
* Create a server-side caller
* @link https://trpc.io/docs/v11/server/server-side-calls
* Mostly used for test invocation
*/
export const createCallerFactory = t.createCallerFactory

0 comments on commit b8cf312

Please sign in to comment.