Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): no server #103

Merged
merged 5 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 60 additions & 12 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import { it, expect } from 'vitest';
import fetch from 'node-fetch';
import { createTHandler } from './thandler';
import { RequestHeaders } from '../src/handler';
import { createClient, NetworkError } from '../src/client';
import { startTServer } from './utils/tserver';
import { texecute } from './utils/texecute';
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 server = startTServer({
const { fetch } = createTHandler({
onSubscribe: (req) => {
headers = req.headers;
},
});

const client = createClient({
url: server.url,
url: 'http://localhost',
fetchFn: fetch,
headers: async () => {
return { 'x-some': 'header' };
Expand All @@ -24,14 +41,45 @@ it('should use the provided headers', async () => {
const [request] = texecute(client, { query: '{ hello }' });
await request;

expect(headers['x-some']).toBe('header');
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 server = startTServer();
const { fetch } = createTHandler();

const client = createClient({
url: server.url,
url: 'http://localhost',
fetchFn: fetch,
});

Expand All @@ -43,10 +91,10 @@ it('should execute query, next the result and then complete', async () => {
});

it('should execute mutation, next the result and then complete', async () => {
const server = startTServer();
const { fetch } = createTHandler();

const client = createClient({
url: server.url,
url: 'http://localhost',
fetchFn: fetch,
});

Expand All @@ -58,10 +106,10 @@ it('should execute mutation, next the result and then complete', async () => {
});

it('should report invalid request', async () => {
const server = startTServer();
const { fetch } = createTHandler();

const client = createClient({
url: server.url,
url: 'http://localhost',
fetchFn: fetch,
});

Expand Down
Loading