Skip to content

Commit

Permalink
refactor(test): no mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed May 16, 2024
1 parent f1fd315 commit 39dfb37
Showing 1 changed file with 52 additions and 56 deletions.
108 changes: 52 additions & 56 deletions src/layers/5_client/client.customScalar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,67 @@ import { describe, expect } from 'vitest'
import { db } from '../../../tests/_/db.js'
import { createResponse, test } from '../../../tests/_/helpers.js'
import { Graffle } from '../../../tests/_/schema/generated/__.js'
import { setupMockServer } from '../../../tests/legacy/__helpers.js'

const ctx = setupMockServer()
const date0Encoded = db.date0.toISOString()
const date1Encoded = db.date1.toISOString()

const client = () => Graffle.create({ schema: ctx.url })
const client = Graffle.create({ schema: 'https://foo' })

describe(`output`, () => {
test(`query field`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { date: date0Encoded } }))
expect(await client().query.$batch({ date: true })).toEqual({ date: db.date0 })
})
test(`query field in non-null`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateNonNull: date0Encoded } }))
expect(await client().query.$batch({ dateNonNull: true })).toEqual({ dateNonNull: db.date0 })
test(`query field`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { date: date0Encoded } }))
expect(await client.query.$batch({ date: true })).toEqual({ date: db.date0 })
})
test(`query field in non-null`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateNonNull: date0Encoded } }))
expect(await client.query.$batch({ dateNonNull: true })).toEqual({ dateNonNull: db.date0 })
})
test(`query field in list`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateList: [0, 1] } }))
expect(await client.query.$batch({ dateList: true })).toEqual({ dateList: [db.date0, new Date(1)] })
})
test(`query field in list non-null`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateList: [0, 1] } }))
expect(await client.query.$batch({ dateList: true })).toEqual({ dateList: [db.date0, new Date(1)] })
})
test(`object field`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateObject1: { date1: 0 } } }))
expect(await client.query.$batch({ dateObject1: { date1: true } })).toEqual({
dateObject1: { date1: db.date0 },
})
test(`query field in list`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateList: [0, 1] } }))
expect(await client().query.$batch({ dateList: true })).toEqual({ dateList: [db.date0, new Date(1)] })
})
test(`object field in interface`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateInterface1: { date1: 0 } } }))
expect(await client.query.$batch({ dateInterface1: { date1: true } })).toEqual({
dateInterface1: { date1: db.date0 },
})
test(`query field in list non-null`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateList: [0, 1] } }))
expect(await client().query.$batch({ dateList: true })).toEqual({ dateList: [db.date0, new Date(1)] })
})

describe(`object field in union`, () => {
test(`case 1 with __typename`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateUnion: { __typename: `DateObject1`, date1: 0 } } }))
expect(await client.query.$batch({ dateUnion: { __typename: true, onDateObject1: { date1: true } } }))
.toEqual({
dateUnion: { __typename: `DateObject1`, date1: db.date0 },
})
})
test(`object field`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateObject1: { date1: 0 } } }))
expect(await client().query.$batch({ dateObject1: { date1: true } })).toEqual({
dateObject1: { date1: db.date0 },
test(`case 1 without __typename`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateUnion: { date1: date0Encoded } } }))
expect(await client.query.$batch({ dateUnion: { onDateObject1: { date1: true } } })).toEqual({
dateUnion: { date1: db.date0 },
})
})
test(`object field in interface`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateInterface1: { date1: 0 } } }))
expect(await client().query.$batch({ dateInterface1: { date1: true } })).toEqual({
dateInterface1: { date1: db.date0 },
})
test(`case 2`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateUnion: { date2: date0Encoded } } }))
expect(
await client.query.$batch({
dateUnion: { onDateObject1: { date1: true }, onDateObject2: { date2: true } },
}),
)
.toEqual({ dateUnion: { date2: db.date0 } })
})
describe(`object field in union`, () => {
test(`case 1 with __typename`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateUnion: { __typename: `DateObject1`, date1: 0 } } }))
expect(await client().query.$batch({ dateUnion: { __typename: true, onDateObject1: { date1: true } } }))
.toEqual({
dateUnion: { __typename: `DateObject1`, date1: db.date0 },
})
})
test(`case 1 without __typename`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateUnion: { date1: date0Encoded } } }))
expect(await client().query.$batch({ dateUnion: { onDateObject1: { date1: true } } })).toEqual({
dateUnion: { date1: db.date0 },
})
})
test(`case 2`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateUnion: { date2: date0Encoded } } }))
expect(
await client().query.$batch({
dateUnion: { onDateObject1: { date1: true }, onDateObject2: { date2: true } },
}),
)
.toEqual({ dateUnion: { date2: db.date0 } })
})
test(`case 2 miss`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateUnion: null } }))
expect(await client().query.$batch({ dateUnion: { onDateObject1: { date1: true } } })).toEqual({
dateUnion: null,
}) // dprint-ignore
})
test(`case 2 miss`, async ({ fetch }) => {
fetch.mockResolvedValueOnce(createResponse({ data: { dateUnion: null } }))
expect(await client.query.$batch({ dateUnion: { onDateObject1: { date1: true } } })).toEqual({
dateUnion: null,
}) // dprint-ignore
})
})

0 comments on commit 39dfb37

Please sign in to comment.