forked from graphql/graphql-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.test.ts
More file actions
96 lines (77 loc) · 2.59 KB
/
Copy pathclient.test.ts
File metadata and controls
96 lines (77 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { it, expect } from 'vitest';
import { createTHandler } from './thandler';
import { RequestHeaders } from '../src/handler';
import { createClient, NetworkError } from '../src/client';
import { ExecutionResult } from 'graphql';
import { RequestParams } from '../src/common';
import { Client } from '../src/client';
function texecute<D = unknown, E = unknown>(
client: Client,
params: RequestParams,
): [request: Promise<ExecutionResult<D, E>>, cancel: () => void] {
let cancel!: () => void;
const request = new Promise<ExecutionResult<D, E>>((resolve, reject) => {
let result: ExecutionResult<D, E>;
cancel = client.subscribe<D, E>(params, {
next: (data) => (result = data),
error: reject,
complete: () => resolve(result),
});
});
return [request, cancel];
}
it('should use the provided headers', async () => {
let headers: RequestHeaders = {};
const { fetch } = createTHandler({
onSubscribe: (req) => {
headers = req.headers;
},
});
const client = createClient({
url: 'http://localhost',
fetchFn: fetch,
headers: async () => {
return { 'x-some': 'header' };
},
});
const [request] = texecute(client, { query: '{ hello }' });
await request;
expect(headers).toBeInstanceOf(Headers);
const actualHeaders = headers as unknown as Headers;
expect(actualHeaders.get('x-some')).toBe('header');
expect(actualHeaders.get('content-type')).toBe(
'application/json; charset=utf-8',
);
expect(actualHeaders.get('accept')).toBe(
'application/graphql-response+json, application/json',
);
});
it('should execute query, next the result and then complete', async () => {
const { fetch } = createTHandler();
const client = createClient({
url: 'http://localhost',
fetchFn: fetch,
});
const [request] = texecute(client, { query: '{ hello }' });
const result = await request;
expect(result).toEqual({ data: { hello: 'world' } });
});
it('should execute mutation, next the result and then complete', async () => {
const { fetch } = createTHandler();
const client = createClient({
url: 'http://localhost',
fetchFn: fetch,
});
const [request] = texecute(client, { query: 'mutation { dontChange }' });
const result = await request;
expect(result).toEqual({ data: { dontChange: 'didntChange' } });
});
it('should report invalid request', async () => {
const { fetch } = createTHandler();
const client = createClient({
url: 'http://localhost',
fetchFn: fetch,
});
const [request] = texecute(client, { query: 'query {' });
await expect(request).rejects.toBeInstanceOf(NetworkError);
});