import * as nock from 'nock'; import { graphql, GraphqlClient, IGraphqlInitOptions } from '..'; const URL_BASE = 'http://localhost:1234'; const URL_PATH = `/graphql`; const URL = `${URL_BASE}${URL_PATH}`; export function client(options?: IGraphqlInitOptions) { return graphql(URL, options) as GraphqlClient; } export interface IMockCallbackArgs { req: { headers: any }; headers: any; data: any; } export type MockCallback = (args: IMockCallbackArgs) => void; export function mock(data: any, callback?: MockCallback) { nock(URL_BASE) .post(URL_PATH, () => true) .reply(200, function(uri: string, reqBody: any) { if (callback) { callback({ req: this.req, headers: this.req.headers, data, }); } return { data }; }); } export function mockError(status: number, callback?: MockCallback) { nock(URL_BASE) .post(URL_PATH, () => true) .reply(status, function(uri: string, reqBody: any) { const data = {}; if (callback) { callback({ req: this.req, headers: this.req.headers, data, }); } return { data }; }); }