findUnique.ts•1.7 kB
import testMatrix from './_matrix'
import { setup } from './_setup'
// @ts-ignore
import type { PrismaClient } from './generated/prisma/client'
declare let prisma: PrismaClient
let vars: Awaited<ReturnType<typeof setup>>
testMatrix.setupTestSuite(() => {
  beforeAll(async () => {
    vars = await setup(prisma)
  })
  test('findUnique with where 1 unique (PK)', async () => {
    const data = await prisma.user.findUnique({
      where: {
        id: vars.userId,
      },
    })
    expect(data?.id).toBe(vars.userId)
  })
  test('findUnique with where 2 uniques (PK & non-PK)', async () => {
    const data = await prisma.post.findUnique({
      where: {
        id: vars.postId1,
        title: 'Hello World 1',
      },
    })
    expect(data?.id).toBe(vars.postId1)
  })
  test('findUnique with where 1 unique (non-PK)', async () => {
    const data = await prisma.post.findUnique({
      where: {
        title: 'Hello World 2',
      },
    })
    expect(data?.id).toBe(vars.postId2)
  })
  test('findUnique with nested where on optional 1:1 not found', async () => {
    const data = await prisma.user.findUnique({
      where: {
        id: vars.userId,
      },
      include: {
        payment: {
          where: {
            ccn: 'not there',
          },
        },
      },
    })
    expect(data?.payment).toBeNull()
  })
  test('findUnique with nested where on optional 1:1 found', async () => {
    const data = await prisma.user.findUnique({
      where: {
        id: vars.userId,
      },
      include: {
        payment: {
          where: {
            ccn: vars.ccn,
          },
        },
      },
    })
    expect(data?.payment).not.toBeNull()
  })
})