Skip to content

Commit

Permalink
chore: refactor test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Oct 3, 2024
1 parent 624e684 commit fccd6eb
Showing 1 changed file with 42 additions and 58 deletions.
100 changes: 42 additions & 58 deletions src/layers/6_client/raw/client.raw.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,53 @@
/* eslint-disable */
import { describe, expectTypeOf } from 'vitest'
import { test } from '../../../../tests/_/helpers.js'
import { Graffle } from '../../../../tests/_/schemas/kitchen-sink/graffle/__.js'
import { schema } from '../../../../tests/_/schemas/kitchen-sink/schema.js'
import { gql } from '../../6_helpers/gql.js'
import type { RawResolveOutputReturnRootType } from '../handleOutput.js'

const g = Graffle.create({ schema })

describe(`TypedQueryDocumentNode with just data (no variables)`, () => {
const document = gql<{ id: string }, {}>`
query {
id
}
`
test(`envelope data is typed`, () => {
expectTypeOf(g.raw({ document })).resolves.toEqualTypeOf<
RawResolveOutputReturnRootType<typeof g._.context.config, { id: string }>
>()
})
test('variables are not allowed to be passed in', () => {
// @ts-expect-error - variables not allowed.
g.raw({ document: document, variables: {} })
describe(`TypedQueryDocumentNode`, () => {
describe(`data`, () => {
const document = gql<{ id: string }, {}>`query { id }`
test(`envelope data is typed`, ({ kitchenSink: g }) => {
type ExpectedType = RawResolveOutputReturnRootType<typeof g._.context.config, { id: string }>
expectTypeOf(g.raw({ document })).resolves.toEqualTypeOf<ExpectedType>()
})
test(`variables are not allowed to be passed in`, async ({ kitchenSink: g }) => {
// @ts-expect-error - variables not allowed.
await g.raw({ document: document, variables: {} })
})
})
})

describe(`TypedQueryDocumentNode with data and optional variables`, () => {
const document = gql<{ id: string }, { filter?: boolean }>`
query {
id
}
`
test(`envelope data is typed`, () => {
expectTypeOf(g.raw({ document })).resolves.toEqualTypeOf<
RawResolveOutputReturnRootType<typeof g._.context.config, { id: string }>
>()
})
test('variables are typed and allowed to be passed in or omitted', () => {
// Expect no type errors below
g.raw({ document })
g.raw({ document, variables: {} })
g.raw({ document, variables: { filter: true } })
g.raw({ document, variables: { filter: false } })
// @ts-expect-error - wrong type
g.raw({ document, variables: { filter: 'wrong type' } })
describe(`data + optional variables`, () => {
const document = gql<{ id: string }, { filter?: boolean }>`query { id }`
test(`envelope data is typed`, ({ kitchenSink: g }) => {
expectTypeOf(g.raw({ document })).resolves.toEqualTypeOf<
RawResolveOutputReturnRootType<typeof g._.context.config, { id: string }>
>()
})
test(`variables are typed and allowed to be passed in or omitted`, async ({ kitchenSink: g }) => {
// Expect no type errors below
await g.raw({ document })
await g.raw({ document, variables: {} })
await g.raw({ document, variables: { filter: true } })
await g.raw({ document, variables: { filter: false } })
// @ts-expect-error - wrong type
await g.raw({ document, variables: { filter: `wrong type` } })
})
})
})

describe(`TypedQueryDocumentNode with data and one or more required variables`, () => {
const document = gql<{ id: string }, { filter: boolean }>`
query {
id
}
`
test('valid variables can be given', () => {
g.raw({ document, variables: { filter: true } })
})
test('variables property cannot be omitted', () => {
// @ts-expect-error - variables missing
g.raw({ document })
// @ts-expect-error - variables filter property missing
g.raw({ document, variables: {} })
})
test('given variables must be valid types', () => {
// @ts-expect-error - wrong type
g.raw({ document, variables: { filter: 'wrong type' } })
describe(`data + 1+ variables required`, () => {
const document = gql<{ id: string }, { filter: boolean }>`query { id }`
test(`valid variables can be given`, async ({ kitchenSink: g }) => {
await g.raw({ document, variables: { filter: true } })
})
test(`variables property cannot be omitted`, async ({ kitchenSink: g }) => {
// @ts-expect-error - variables missing
await g.raw({ document })
// @ts-expect-error - variables filter property missing
await g.raw({ document, variables: {} })
})
test(`given variables must be valid types`, async ({ kitchenSink: g }) => {
// @ts-expect-error - wrong type
await g.raw({ document, variables: { filter: `wrong type` } })
})
})
})

0 comments on commit fccd6eb

Please sign in to comment.