From dd2d3b6b055818e72ba8e374bdf705dc014921ed Mon Sep 17 00:00:00 2001 From: Jason Kuhrt Date: Sun, 16 Apr 2023 23:47:32 -0400 Subject: [PATCH] chore(tests): test that custom config is passed to fetch closes #506 --- examples/configuration-fetch-options.ts | 7 ++++++- src/types.ts | 8 ++++---- tests/fetch.test.ts | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 tests/fetch.test.ts diff --git a/examples/configuration-fetch-options.ts b/examples/configuration-fetch-options.ts index ac69bf6ce..1330e0b8c 100644 --- a/examples/configuration-fetch-options.ts +++ b/examples/configuration-fetch-options.ts @@ -19,7 +19,12 @@ const query = gql` ` interface Data { - Movie: { releaseDate: string; actors: Array<{ name: string }> } + Movie: { + releaseDate: string + actors: { + name: string + }[] + } } const data = await graphQLClient.request(query) diff --git a/src/types.ts b/src/types.ts index 6d91c8d08..8352a52b9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -81,12 +81,12 @@ export type MaybeLazy = T | (() => T) export type RequestDocument = string | DocumentNode -export interface GraphQLClientResponse { - data: T - extensions?: unknown +export interface GraphQLClientResponse { + status: number headers: Headers + data: Data + extensions?: unknown errors?: GraphQLError[] - status: number } export type HTTPMethodInput = 'GET' | 'POST' | 'get' | 'post' diff --git a/tests/fetch.test.ts b/tests/fetch.test.ts new file mode 100644 index 000000000..c87fe5255 --- /dev/null +++ b/tests/fetch.test.ts @@ -0,0 +1,18 @@ +import { gql, GraphQLClient } from '../src/index.js' +import { Headers } from 'cross-fetch' +import { expect, test, vitest } from 'vitest' + +test(`custom fetch configuration is passed through`, async () => { + const fetch = vitest.fn().mockResolvedValue({ ok: true, headers: new Headers(), text: () => ``, data: {} }) + const client = new GraphQLClient(`https://foobar`, { + fetch, + // @ts-expect-error extended fetch options + next: { + revalidate: 1, + }, + }) + await client.request(gql`foo`).catch(() => { + /* ignore */ + }) + expect(fetch.mock.calls).toMatchObject([[expect.stringMatching(`.*`), { next: { revalidate: 1 } }]]) +})