1
- // to fake out graphql to use XHR so we can stub it
1
+ // to fake out graphql to use XHR, so we can stub it
2
2
global . XMLHttpRequest = null ;
3
3
4
4
let { mockRandom} = require ( 'jest-mock-random' ) ;
@@ -27,33 +27,33 @@ function mockXHR(status, data) {
27
27
return xhrMockObj ;
28
28
}
29
29
30
+ /* global client, method, url, fetchPost, fetchComments */
30
31
describe ( 'graphql.js' , ( ) => {
31
- let client = null ;
32
-
33
- beforeEach ( ( ) => {
34
- client = graphql ( null , {
35
- method : 'put' ,
32
+ set ( 'url' , ( ) => null ) ;
33
+ set ( 'method' , ( ) => 'put' ) ;
34
+ set ( 'client' , ( ) =>
35
+ graphql ( url , {
36
+ method : method ,
36
37
asJSON : true ,
37
38
fragments : {
38
39
user : 'on User {name}' ,
39
40
auth : {
40
41
user : 'on User {token, ...user}'
41
42
}
42
43
}
43
- } ) ;
44
- client . fragment ( {
45
- auth : {
46
- error : 'on Error {messages}'
47
- }
48
- } ) ;
49
- } ) ;
44
+ } ) ) ;
50
45
51
46
it ( 'client should be a function' , ( ) => {
52
47
expect ( typeof client ) . toBe ( 'function' ) ;
53
48
} ) ;
54
49
55
50
describe ( '.fragment()' , ( ) => {
56
51
it ( 'registers a new fragment' , ( ) => {
52
+ client . fragment ( {
53
+ auth : {
54
+ error : 'on Error {messages}'
55
+ }
56
+ } ) ;
57
57
58
58
expect ( client . fragment ( 'auth.error' ) ) . toBe (
59
59
'fragment auth_error on Error {messages}'
@@ -77,7 +77,13 @@ describe('graphql.js', () => {
77
77
) ;
78
78
} ) ;
79
79
80
- it ( 'returns returns new fragments registered as well' , ( ) => {
80
+ it ( 'returns new registered fragments as well' , ( ) => {
81
+ client . fragment ( {
82
+ auth : {
83
+ error : 'on Error {messages}'
84
+ }
85
+ } ) ;
86
+
81
87
expect ( client . fragments ( ) ) . toStrictEqual (
82
88
expect . objectContaining ( {
83
89
auth_error : '\nfragment auth_error on Error {messages}' ,
@@ -98,6 +104,12 @@ describe('graphql.js', () => {
98
104
}` ;
99
105
100
106
it ( 'mixes in the requested fragments and sets the data types' , ( ) => {
107
+ client . fragment ( {
108
+ auth : {
109
+ error : 'on Error {messages}'
110
+ }
111
+ } ) ;
112
+
101
113
var expectedQuery = `query ($name: String!, $bool: Boolean!, $int: Int!, $float: Float!, $id: ID!, $user_id: Int!, $postID: ID!, $custom_id: CustomType!, $customId: ID!, $target: [ID!]!) {
102
114
user(name: $name, bool: $bool, int: $int, id: $id) {
103
115
... auth_user
@@ -170,28 +182,23 @@ fragment auth_error on Error {messages}`;
170
182
} ) ;
171
183
172
184
describe ( 'query testing' , ( ) => {
173
- let fetchPost = null ;
174
- let fetchComments = null ;
175
-
176
- beforeEach ( ( ) => {
177
- client . setUrl ( 'https://example.org' ) ;
178
- fetchPost = client . query ( `{
185
+ set ( 'fetchPost' , ( ) => client . query ( `{
179
186
post(id: $id) {
180
187
id
181
188
title
182
189
text
183
190
}
184
- }` ) ;
185
-
186
- fetchComments = client . query ( `{
191
+ }` ) ) ;
192
+ set ( 'fetchComments' , ( ) => client . query ( `{
187
193
commentsOfPost: comments(postId: $postId) {
188
194
comment
189
195
owner {
190
196
name
191
197
}
192
198
}
193
- }` ) ;
194
- } ) ;
199
+ }` ) ) ;
200
+
201
+ set ( 'url' , ( ) => 'https://example.org' ) ;
195
202
196
203
describe ( 'when executing the queries normally' , ( ) => {
197
204
it ( 'sends a network request right away' , ( ) => {
0 commit comments