-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathindex.test.ts
More file actions
112 lines (108 loc) · 4.03 KB
/
index.test.ts
File metadata and controls
112 lines (108 loc) · 4.03 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { ApolloClient } from '@apollo/client';
import { GraphQLError } from 'graphql';
import gql from 'graphql-tag';
import buildDataProvider, { BuildQueryFactory } from './index';
describe('GraphQL data provider', () => {
describe('mutate', () => {
describe('with error', () => {
it('sets ApolloError in body', async () => {
const mockClient = {
mutate: async () => {
throw new GraphQLError('some error');
},
};
const mockBuildQueryFactory = () => {
return () => ({
query: gql`
mutation {
updateMyResource {
result
}
}
`,
parseResponse: () => ({}),
});
};
const dataProvider = await buildDataProvider({
client: mockClient as unknown as ApolloClient,
introspection: false,
buildQuery:
mockBuildQueryFactory as unknown as BuildQueryFactory,
});
try {
await dataProvider.update('myResource', {
id: 1,
previousData: { id: 1 },
data: {},
});
} catch (error) {
expect(error.body).not.toBeNull();
expect(error.body.graphQLErrors).toBeDefined();
expect(error.body.graphQLErrors).toHaveLength(1);
return;
}
fail('expected data provider to throw an error');
});
});
});
describe('getIntrospection', () => {
it('returns introspection result', async () => {
const schema = {
queryType: { name: 'Query' },
mutationType: { name: 'Mutation' },
types: [
{
name: 'Query',
fields: [{ name: 'allPosts' }, { name: 'Post' }],
},
{
name: 'Mutation',
fields: [
{ name: 'createPost' },
{ name: 'updatePost' },
{ name: 'deletePost' },
],
},
{ name: 'Post' },
],
};
const client = {
query: jest.fn(() =>
Promise.resolve({
data: {
__schema: schema,
},
})
),
};
const dataProvider = buildDataProvider({
client: client as unknown as ApolloClient,
buildQuery: () => () => undefined,
});
const introspection = await dataProvider.getIntrospection();
expect(introspection).toEqual({
queries: [
{ name: 'allPosts' },
{ name: 'Post' },
{ name: 'createPost' },
{ name: 'updatePost' },
{ name: 'deletePost' },
],
types: [{ name: 'Post' }],
resources: [
{
type: { name: 'Post' },
GET_LIST: { name: 'allPosts' },
GET_MANY: { name: 'allPosts' },
GET_MANY_REFERENCE: { name: 'allPosts' },
GET_ONE: { name: 'Post' },
CREATE: { name: 'createPost' },
UPDATE: { name: 'updatePost' },
DELETE: { name: 'deletePost' },
},
],
schema,
});
});
});
});