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
22global . XMLHttpRequest = null ;
33
44let { mockRandom} = require ( 'jest-mock-random' ) ;
@@ -27,33 +27,33 @@ function mockXHR(status, data) {
2727 return xhrMockObj ;
2828}
2929
30+ /* global client, method, url, fetchPost, fetchComments */
3031describe ( '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 ,
3637 asJSON : true ,
3738 fragments : {
3839 user : 'on User {name}' ,
3940 auth : {
4041 user : 'on User {token, ...user}'
4142 }
4243 }
43- } ) ;
44- client . fragment ( {
45- auth : {
46- error : 'on Error {messages}'
47- }
48- } ) ;
49- } ) ;
44+ } ) ) ;
5045
5146 it ( 'client should be a function' , ( ) => {
5247 expect ( typeof client ) . toBe ( 'function' ) ;
5348 } ) ;
5449
5550 describe ( '.fragment()' , ( ) => {
5651 it ( 'registers a new fragment' , ( ) => {
52+ client . fragment ( {
53+ auth : {
54+ error : 'on Error {messages}'
55+ }
56+ } ) ;
5757
5858 expect ( client . fragment ( 'auth.error' ) ) . toBe (
5959 'fragment auth_error on Error {messages}'
@@ -77,7 +77,13 @@ describe('graphql.js', () => {
7777 ) ;
7878 } ) ;
7979
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+
8187 expect ( client . fragments ( ) ) . toStrictEqual (
8288 expect . objectContaining ( {
8389 auth_error : '\nfragment auth_error on Error {messages}' ,
@@ -98,6 +104,12 @@ describe('graphql.js', () => {
98104}` ;
99105
100106 it ( 'mixes in the requested fragments and sets the data types' , ( ) => {
107+ client . fragment ( {
108+ auth : {
109+ error : 'on Error {messages}'
110+ }
111+ } ) ;
112+
101113 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!]!) {
102114 user(name: $name, bool: $bool, int: $int, id: $id) {
103115 ... auth_user
@@ -170,28 +182,23 @@ fragment auth_error on Error {messages}`;
170182 } ) ;
171183
172184 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 ( `{
179186 post(id: $id) {
180187 id
181188 title
182189 text
183190 }
184- }` ) ;
185-
186- fetchComments = client . query ( `{
191+ }` ) ) ;
192+ set ( 'fetchComments' , ( ) => client . query ( `{
187193 commentsOfPost: comments(postId: $postId) {
188194 comment
189195 owner {
190196 name
191197 }
192198 }
193- }` ) ;
194- } ) ;
199+ }` ) ) ;
200+
201+ set ( 'url' , ( ) => 'https://example.org' ) ;
195202
196203 describe ( 'when executing the queries normally' , ( ) => {
197204 it ( 'sends a network request right away' , ( ) => {
0 commit comments