-
Notifications
You must be signed in to change notification settings - Fork 2k
/
createTestClient.test.ts
96 lines (84 loc) 路 2.75 KB
/
createTestClient.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
import { ApolloServerBase, gql } from 'apollo-server-core';
import createTestClient from '../createTestClient';
describe('createTestClient', () => {
const typeDefs = gql`
type Query {
test(echo: String): String
# this resolver uses context
hello: String
}
type Mutation {
increment: Int!
}
`;
const resolvers = {
Query: {
test: (_, { echo }) => echo,
hello: (_, __, { person }) => {
return `hello ${person}`;
},
},
Mutation: {
increment: () => 1,
},
};
const myTestServer = new ApolloServerBase({
typeDefs,
context: () => ({ person: 'tom' }),
resolvers,
});
beforeAll(async () => {
await myTestServer.start();
});
it('allows queries', async () => {
const query = `{ test(echo: "foo") }`;
const client = createTestClient(myTestServer);
const res = await client.query({ query });
expect(res.data).toEqual({ test: 'foo' });
});
it('allows mutations', async () => {
const mutation = `mutation increment { increment }`;
const client = createTestClient(myTestServer);
const res = await client.mutate({ mutation });
expect(res.data).toEqual({ increment: 1 });
});
it('allows variables to be passed', async () => {
const query = `query test($echo: String){ test(echo: $echo) }`;
const client = createTestClient(myTestServer);
const res = await client.query({ query, variables: { echo: 'wow' } });
expect(res.data).toEqual({ test: 'wow' });
});
it('works with generic types', async () => {
const query = `query test($echo: String){ test(echo: $echo) }`;
type Data = { test?: string | null; };
type Variables = { echo: string; };
const client = createTestClient(myTestServer);
const res1 = await client.query<Data>({ query, variables: { echo: 'onlydata' } });
expect(res1.data).toEqual({ test: 'onlydata' });
const res2 = await client.query<Data, Variables>({ query, variables: { echo: 'data and variables' } });
expect(res2.data).toEqual({ test: 'data and variables' });
})
it('resolves with context', async () => {
const query = `{ hello }`;
const client = createTestClient(myTestServer);
const res = await client.query({ query });
expect(res.data).toEqual({ hello: 'hello tom' });
});
it('allows query documents as input', async () => {
const query = gql`
{
test(echo: "foo")
}
`;
const client = createTestClient(myTestServer);
const clientRes = await client.query({ query });
expect(clientRes.data).toEqual({ test: 'foo' });
const mutation = gql`
mutation increment {
increment
}
`;
const mutationRes = await client.mutate({ mutation });
expect(mutationRes.data).toEqual({ increment: 1 });
});
});