1
1
import gql from "graphql-tag" ;
2
2
import { MockLink , MockedResponse } from "../mockLink" ;
3
3
import { execute } from "../../../../link/core/execute" ;
4
+ import { ObservableStream , enableFakeTimers } from "../../../internal" ;
4
5
5
6
describe ( "MockedResponse.newData" , ( ) => {
6
7
const setup = ( ) => {
@@ -72,16 +73,15 @@ We've chosen this value as the MAXIMUM_DELAY since values that don't fit into a
72
73
const MAXIMUM_DELAY = 0x7f_ff_ff_ff ;
73
74
74
75
describe ( "mockLink" , ( ) => {
75
- beforeAll ( ( ) => jest . useFakeTimers ( ) ) ;
76
- afterAll ( ( ) => jest . useRealTimers ( ) ) ;
77
-
78
76
const query = gql `
79
77
query A {
80
78
a
81
79
}
82
80
` ;
83
81
84
82
it ( "should not require a result or error when delay equals Infinity" , async ( ) => {
83
+ using _fakeTimers = enableFakeTimers ( ) ;
84
+
85
85
const mockLink = new MockLink ( [
86
86
{
87
87
request : {
@@ -103,6 +103,8 @@ describe("mockLink", () => {
103
103
} ) ;
104
104
105
105
it ( "should require result or error when delay is just large" , ( done ) => {
106
+ using _fakeTimers = enableFakeTimers ( ) ;
107
+
106
108
const mockLink = new MockLink ( [
107
109
{
108
110
request : {
@@ -125,4 +127,80 @@ describe("mockLink", () => {
125
127
126
128
jest . advanceTimersByTime ( MAXIMUM_DELAY ) ;
127
129
} ) ;
130
+
131
+ it ( "should fill in default variables if they are missing in mocked requests" , async ( ) => {
132
+ const query = gql `
133
+ query GetTodo($done: Boolean = true, $user: String!) {
134
+ todo(user: $user, done: $done) {
135
+ id
136
+ title
137
+ }
138
+ }
139
+ ` ;
140
+ const mocks = [
141
+ {
142
+ // default should get filled in here
143
+ request : { query, variables : { user : "Tim" } } ,
144
+ result : {
145
+ data : { todo : { id : 1 } } ,
146
+ } ,
147
+ } ,
148
+ {
149
+ // we provide our own `done`, so it should not get filled in
150
+ request : { query, variables : { user : "Tim" , done : false } } ,
151
+ result : {
152
+ data : { todo : { id : 2 } } ,
153
+ } ,
154
+ } ,
155
+ {
156
+ // one more that has a different user variable and should never match
157
+ request : { query, variables : { user : "Tom" } } ,
158
+ result : {
159
+ data : { todo : { id : 2 } } ,
160
+ } ,
161
+ } ,
162
+ ] ;
163
+
164
+ // Apollo Client will always fill in default values for missing variables
165
+ // in the operation before calling the Link, so we have to do the same here
166
+ // when we call `execute`
167
+ const defaults = { done : true } ;
168
+ const link = new MockLink ( mocks , false , { showWarnings : false } ) ;
169
+ {
170
+ // Non-optional variable is missing, should not match.
171
+ const stream = new ObservableStream (
172
+ execute ( link , { query, variables : { ...defaults } } )
173
+ ) ;
174
+ await stream . takeError ( ) ;
175
+ }
176
+ {
177
+ // Execute called incorrectly without a default variable filled in.
178
+ // This will never happen in Apollo Client since AC always fills these
179
+ // before calling `execute`, so it's okay if it results in a "no match"
180
+ // scenario here.
181
+ const stream = new ObservableStream (
182
+ execute ( link , { query, variables : { user : "Tim" } } )
183
+ ) ;
184
+ await stream . takeError ( ) ;
185
+ }
186
+ {
187
+ // Expect default value to be filled in the mock request.
188
+ const stream = new ObservableStream (
189
+ execute ( link , { query, variables : { ...defaults , user : "Tim" } } )
190
+ ) ;
191
+ const result = await stream . takeNext ( ) ;
192
+ expect ( result ) . toEqual ( { data : { todo : { id : 1 } } } ) ;
193
+ }
194
+ {
195
+ // Test that defaults don't overwrite explicitly different values in a mock request.
196
+ const stream = new ObservableStream (
197
+ execute ( link , {
198
+ query,
199
+ variables : { ...defaults , user : "Tim" , done : false } ,
200
+ } )
201
+ ) ;
202
+ const result = await stream . takeNext ( ) ;
203
+ expect ( result ) . toEqual ( { data : { todo : { id : 2 } } } ) ;
204
+ }
205
+ } ) ;
128
206
} ) ;
0 commit comments