11import { it , expect } from 'vitest' ;
2- import fetch from 'node-fetch ' ;
2+ import { createTHandler } from './thandler ' ;
33import { RequestHeaders } from '../src/handler' ;
44import { createClient , NetworkError } from '../src/client' ;
5- import { startTServer } from './utils/tserver' ;
6- import { texecute } from './utils/texecute' ;
5+ import { ExecutionResult } from 'graphql' ;
6+ import { RequestParams } from '../src/common' ;
7+ import { Client } from '../src/client' ;
8+
9+ function texecute < D = unknown , E = unknown > (
10+ client : Client ,
11+ params : RequestParams ,
12+ ) : [ request : Promise < ExecutionResult < D , E > > , cancel : ( ) => void ] {
13+ let cancel ! : ( ) => void ;
14+ const request = new Promise < ExecutionResult < D , E > > ( ( resolve , reject ) => {
15+ let result : ExecutionResult < D , E > ;
16+ cancel = client . subscribe < D , E > ( params , {
17+ next : ( data ) => ( result = data ) ,
18+ error : reject ,
19+ complete : ( ) => resolve ( result ) ,
20+ } ) ;
21+ } ) ;
22+ return [ request , cancel ] ;
23+ }
724
825it ( 'should use the provided headers' , async ( ) => {
926 let headers : RequestHeaders = { } ;
10- const server = startTServer ( {
27+ const { fetch } = createTHandler ( {
1128 onSubscribe : ( req ) => {
1229 headers = req . headers ;
1330 } ,
1431 } ) ;
1532
1633 const client = createClient ( {
17- url : server . url ,
34+ url : 'http://localhost' ,
1835 fetchFn : fetch ,
1936 headers : async ( ) => {
2037 return { 'x-some' : 'header' } ;
@@ -24,14 +41,45 @@ it('should use the provided headers', async () => {
2441 const [ request ] = texecute ( client , { query : '{ hello }' } ) ;
2542 await request ;
2643
27- expect ( headers [ 'x-some' ] ) . toBe ( 'header' ) ;
44+ expect ( headers ) . toMatchInlineSnapshot ( `
45+ Headers {
46+ Symbol(headers list): HeadersList {
47+ "cookies": null,
48+ Symbol(headers map): Map {
49+ "x-some" => {
50+ "name": "x-some",
51+ "value": "header",
52+ },
53+ "content-type" => {
54+ "name": "content-type",
55+ "value": "application/json; charset=utf-8",
56+ },
57+ "accept" => {
58+ "name": "accept",
59+ "value": "application/graphql-response+json, application/json",
60+ },
61+ },
62+ Symbol(headers map sorted): null,
63+ },
64+ Symbol(guard): "request",
65+ Symbol(realm): {
66+ "settingsObject": {
67+ "baseUrl": undefined,
68+ "origin": undefined,
69+ "policyContainer": {
70+ "referrerPolicy": "strict-origin-when-cross-origin",
71+ },
72+ },
73+ },
74+ }
75+ ` ) ;
2876} ) ;
2977
3078it ( 'should execute query, next the result and then complete' , async ( ) => {
31- const server = startTServer ( ) ;
79+ const { fetch } = createTHandler ( ) ;
3280
3381 const client = createClient ( {
34- url : server . url ,
82+ url : 'http://localhost' ,
3583 fetchFn : fetch ,
3684 } ) ;
3785
@@ -43,10 +91,10 @@ it('should execute query, next the result and then complete', async () => {
4391} ) ;
4492
4593it ( 'should execute mutation, next the result and then complete' , async ( ) => {
46- const server = startTServer ( ) ;
94+ const { fetch } = createTHandler ( ) ;
4795
4896 const client = createClient ( {
49- url : server . url ,
97+ url : 'http://localhost' ,
5098 fetchFn : fetch ,
5199 } ) ;
52100
@@ -58,10 +106,10 @@ it('should execute mutation, next the result and then complete', async () => {
58106} ) ;
59107
60108it ( 'should report invalid request' , async ( ) => {
61- const server = startTServer ( ) ;
109+ const { fetch } = createTHandler ( ) ;
62110
63111 const client = createClient ( {
64- url : server . url ,
112+ url : 'http://localhost' ,
65113 fetchFn : fetch ,
66114 } ) ;
67115
0 commit comments