|
| 1 | +/* eslint-disable */ |
| 2 | +import { beforeEach, describe, expect, test } from 'vitest' |
| 3 | +import { setupMockServer } from '../../tests/raw/__helpers.js' |
| 4 | +import type { Index } from '../../tests/ts/_/schema/generated/Index.js' |
| 5 | +import { $Index as schemaIndex } from '../../tests/ts/_/schema/generated/SchemaRuntime.js' |
| 6 | +import { create } from './client.js' |
| 7 | + |
| 8 | +const ctx = setupMockServer() |
| 9 | +const data = { fooBarUnion: { int: 1 } } |
| 10 | + |
| 11 | +// @ts-ignore infinite depth |
| 12 | +const client = () => create<Index>({ url: ctx.url, schemaIndex }) |
| 13 | + |
| 14 | +describe(`output`, () => { |
| 15 | + test(`query field`, async () => { |
| 16 | + ctx.res({ body: { data: { date: 0 } } }) |
| 17 | + expect(await client().query.$batch({ date: true })).toEqual({ date: new Date(0) }) |
| 18 | + }) |
| 19 | + test(`query field in non-null`, async () => { |
| 20 | + ctx.res({ body: { data: { dateNonNull: 0 } } }) |
| 21 | + expect(await client().query.$batch({ dateNonNull: true })).toEqual({ dateNonNull: new Date(0) }) |
| 22 | + }) |
| 23 | + test(`query field in list`, async () => { |
| 24 | + ctx.res({ body: { data: { dateList: [0, 1] } } }) |
| 25 | + expect(await client().query.$batch({ dateList: true })).toEqual({ dateList: [new Date(0), new Date(1)] }) |
| 26 | + }) |
| 27 | + test(`query field in list non-null`, async () => { |
| 28 | + ctx.res({ body: { data: { dateList: [0, 1] } } }) |
| 29 | + expect(await client().query.$batch({ dateList: true })).toEqual({ dateList: [new Date(0), new Date(1)] }) |
| 30 | + }) |
| 31 | + test(`object field`, async () => { |
| 32 | + ctx.res({ body: { data: { dateObject1: { date1: 0 } } } }) |
| 33 | + expect(await client().query.$batch({ dateObject1: { date1: true } })).toEqual({ |
| 34 | + dateObject1: { date1: new Date(0) }, |
| 35 | + }) |
| 36 | + }) |
| 37 | + test(`object field in interface`, async () => { |
| 38 | + ctx.res({ body: { data: { dateInterface1: { date1: 0 } } } }) |
| 39 | + expect(await client().query.$batch({ dateInterface1: { date1: true } })).toEqual({ |
| 40 | + dateInterface1: { date1: new Date(0) }, |
| 41 | + }) |
| 42 | + }) |
| 43 | + describe(`object field in union`, () => { |
| 44 | + test(`case 1 with __typename`, async () => { |
| 45 | + ctx.res({ body: { data: { dateUnion: { __typename: `DateObject1`, date1: 0 } } } }) |
| 46 | + expect(await client().query.$batch({ dateUnion: { __typename: true, onDateObject1: { date1: true } } })) |
| 47 | + .toEqual({ |
| 48 | + dateUnion: { __typename: `DateObject1`, date1: new Date(0) }, |
| 49 | + }) |
| 50 | + }) |
| 51 | + test(`case 1 without __typename`, async () => { |
| 52 | + ctx.res({ body: { data: { dateUnion: { date1: 0 } } } }) |
| 53 | + expect(await client().query.$batch({ dateUnion: { onDateObject1: { date1: true } } })).toEqual({ |
| 54 | + dateUnion: { date1: new Date(0) }, |
| 55 | + }) |
| 56 | + }) |
| 57 | + test(`case 2`, async () => { |
| 58 | + ctx.res({ body: { data: { dateUnion: { date2: 0 } } } }) |
| 59 | + expect( |
| 60 | + await client().query.$batch({ |
| 61 | + dateUnion: { onDateObject1: { date1: true }, onDateObject2: { date2: true } }, |
| 62 | + }), |
| 63 | + ) |
| 64 | + .toEqual({ dateUnion: { date2: new Date(0) } }) |
| 65 | + }) |
| 66 | + test(`case 2 miss`, async () => { |
| 67 | + ctx.res({ body: { data: { dateUnion: null } } }) |
| 68 | + expect(await client().query.$batch({ dateUnion: { onDateObject1: { date1: true } } })).toEqual({ |
| 69 | + dateUnion: null, |
| 70 | + }) // dprint-ignore |
| 71 | + }) |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe(`input`, () => { |
| 76 | + // needed to avoid runtime error but data ignored because we only test input below. |
| 77 | + beforeEach(() => { |
| 78 | + ctx.res({ body: { data: {} } }) |
| 79 | + }) |
| 80 | + const clientExpected = (expectedDocument: (document: any) => void) => { |
| 81 | + const client = create<Index>({ |
| 82 | + url: ctx.url, |
| 83 | + schemaIndex, |
| 84 | + hooks: { |
| 85 | + documentEncode: (input, run) => { |
| 86 | + const result = run(input) |
| 87 | + expectedDocument(result) |
| 88 | + return result |
| 89 | + }, |
| 90 | + }, |
| 91 | + }) |
| 92 | + return client |
| 93 | + } |
| 94 | + |
| 95 | + test(`arg field`, async () => { |
| 96 | + const client = clientExpected((doc) => expect(doc.dateArg.$.date).toEqual(new Date(0).getTime())) |
| 97 | + await client.query.$batch({ dateArg: { $: { date: new Date(0) } } }) |
| 98 | + }) |
| 99 | + test('arg field in non-null', async () => { |
| 100 | + const client = clientExpected((doc) => expect(doc.dateArgNonNull.$.date).toEqual(new Date(0).getTime())) |
| 101 | + await client.query.$batch({ dateArgNonNull: { $: { date: new Date(0) } } }) |
| 102 | + }) |
| 103 | + test('arg field in list', async () => { |
| 104 | + const client = clientExpected((doc) => |
| 105 | + expect(doc.dateArgList.$.date).toEqual([new Date(0).getTime(), new Date(1).getTime()]) |
| 106 | + ) |
| 107 | + await client.query.$batch({ dateArgList: { $: { date: [new Date(0), new Date(1)] } } }) |
| 108 | + }) |
| 109 | + test('arg field in list (null)', async () => { |
| 110 | + const client = clientExpected((doc) => expect(doc.dateArgList.$.date).toEqual(null)) |
| 111 | + await client.query.$batch({ dateArgList: { $: { date: null } } }) |
| 112 | + }) |
| 113 | + test('arg field in non-null list (with list)', async () => { |
| 114 | + const client = clientExpected((doc) => |
| 115 | + expect(doc.dateArgNonNullList.$.date).toEqual([new Date(0).getTime(), new Date(1).getTime()]) |
| 116 | + ) |
| 117 | + await client.query.$batch({ dateArgNonNullList: { $: { date: [new Date(0), new Date(1)] } } }) |
| 118 | + }) |
| 119 | + test('arg field in non-null list (with null)', async () => { |
| 120 | + const client = clientExpected((doc) => expect(doc.dateArgNonNullList.$.date).toEqual([null, new Date(0).getTime()])) |
| 121 | + await client.query.$batch({ dateArgNonNullList: { $: { date: [null, new Date(0)] } } }) |
| 122 | + }) |
| 123 | + test('arg field in non-null list non-null', async () => { |
| 124 | + const client = clientExpected((doc) => |
| 125 | + expect(doc.dateArgNonNullListNonNull.$.date).toEqual([new Date(0).getTime(), new Date(1).getTime()]) |
| 126 | + ) |
| 127 | + await client.query.$batch({ dateArgNonNullListNonNull: { $: { date: [new Date(0), new Date(1)] } } }) |
| 128 | + }) |
| 129 | + test(`input object field`, async () => { |
| 130 | + const client = clientExpected((doc) => { |
| 131 | + expect(doc.dateArgInputObject.$.input.dateRequired).toEqual(new Date(0).getTime()) |
| 132 | + expect(doc.dateArgInputObject.$.input.date).toEqual(new Date(1).getTime()) |
| 133 | + }) |
| 134 | + await client.query.$batch({ |
| 135 | + dateArgInputObject: { $: { input: { idRequired: '', dateRequired: new Date(0), date: new Date(1) } } }, |
| 136 | + }) |
| 137 | + }) |
| 138 | + test(`nested input object field`, async () => { |
| 139 | + const client = clientExpected((doc) => { |
| 140 | + expect(doc.InputObjectNested.$.input.InputObject.dateRequired).toEqual(new Date(0).getTime()) |
| 141 | + expect(doc.InputObjectNested.$.input.InputObject.date).toEqual(new Date(1).getTime()) |
| 142 | + }) |
| 143 | + await client.query.$batch({ |
| 144 | + InputObjectNested: { |
| 145 | + $: { input: { InputObject: { idRequired: '', dateRequired: new Date(0), date: new Date(1) } } }, |
| 146 | + }, |
| 147 | + }) |
| 148 | + }) |
| 149 | +}) |
0 commit comments