-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathclient.test.ts
119 lines (100 loc) · 3.11 KB
/
client.test.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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).toMatchInlineSnapshot(`
Headers {
Symbol(headers list): HeadersList {
"cookies": null,
Symbol(headers map): Map {
"x-some" => {
"name": "x-some",
"value": "header",
},
"content-type" => {
"name": "content-type",
"value": "application/json; charset=utf-8",
},
"accept" => {
"name": "accept",
"value": "application/graphql-response+json, application/json",
},
},
Symbol(headers map sorted): null,
},
Symbol(guard): "request",
Symbol(realm): {
"settingsObject": {
"baseUrl": undefined,
"origin": undefined,
"policyContainer": {
"referrerPolicy": "strict-origin-when-cross-origin",
},
},
},
}
`);
});
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);
});