@@ -12,8 +12,16 @@ const createClient = (link: ApolloLink) =>
12
12
cache : new InMemoryCache ( ) ,
13
13
} ) ;
14
14
15
+ type Result = {
16
+ heroes : { name : string } [ ] ;
17
+ } ;
18
+
19
+ type Variables = {
20
+ foo ?: number ;
21
+ } ;
22
+
15
23
const heroesOperation = {
16
- query : gql `
24
+ query : gql < Result , Variables > `
17
25
query allHeroes {
18
26
heroes {
19
27
name
@@ -40,8 +48,7 @@ const Batman = {
40
48
describe ( 'QueryRef' , ( ) => {
41
49
let ngZone : NgZone ;
42
50
let client : ApolloClient < any > ;
43
- let obsQuery : ObservableQuery < any > ;
44
- let queryRef : QueryRef < any > ;
51
+ let obsQuery : ObservableQuery < Result , Variables > ;
45
52
46
53
beforeEach ( ( ) => {
47
54
ngZone = { run : jest . fn ( cb => cb ( ) ) } as any ;
@@ -58,10 +65,14 @@ describe('QueryRef', () => {
58
65
59
66
client = createClient ( mockedLink ) ;
60
67
obsQuery = client . watchQuery ( heroesOperation ) ;
61
- queryRef = new QueryRef < any > ( obsQuery , ngZone , { } as any ) ;
62
68
} ) ;
63
69
70
+ function createQueryRef ( obsQuery : ObservableQuery < Result > ) : QueryRef < Result , Variables > {
71
+ return new QueryRef ( obsQuery , ngZone , { query : heroesOperation . query } ) ;
72
+ }
73
+
64
74
test ( 'should listen to changes' , done => {
75
+ const queryRef = createQueryRef ( obsQuery ) ;
65
76
queryRef . valueChanges . subscribe ( {
66
77
next : result => {
67
78
expect ( result . data ) . toBeDefined ( ) ;
@@ -77,6 +88,7 @@ describe('QueryRef', () => {
77
88
const mockCallback = jest . fn ( ) ;
78
89
obsQuery . refetch = mockCallback ;
79
90
91
+ const queryRef = createQueryRef ( obsQuery ) ;
80
92
queryRef . refetch ( ) ;
81
93
82
94
expect ( mockCallback . mock . calls . length ) . toBe ( 1 ) ;
@@ -85,6 +97,7 @@ describe('QueryRef', () => {
85
97
test ( 'should be able refetch and receive new results' , done => {
86
98
let calls = 0 ;
87
99
100
+ const queryRef = createQueryRef ( obsQuery ) ;
88
101
queryRef . valueChanges . subscribe ( {
89
102
next : result => {
90
103
calls ++ ;
@@ -110,6 +123,7 @@ describe('QueryRef', () => {
110
123
111
124
test ( 'should be able refetch and receive new results after using rxjs operator' , done => {
112
125
let calls = 0 ;
126
+ const queryRef = createQueryRef ( obsQuery ) ;
113
127
const obs = queryRef . valueChanges ;
114
128
115
129
obs . pipe ( map ( result => result . data ) ) . subscribe ( {
@@ -139,9 +153,10 @@ describe('QueryRef', () => {
139
153
140
154
test ( 'should be able to call updateQuery()' , ( ) => {
141
155
const mockCallback = jest . fn ( ) ;
142
- const mapFn = ( ) => ( { } ) ;
156
+ const mapFn = ( ) => undefined ;
143
157
obsQuery . updateQuery = mockCallback ;
144
158
159
+ const queryRef = createQueryRef ( obsQuery ) ;
145
160
queryRef . updateQuery ( mapFn ) ;
146
161
147
162
expect ( mockCallback . mock . calls . length ) . toBe ( 1 ) ;
@@ -152,6 +167,7 @@ describe('QueryRef', () => {
152
167
const mockCallback = jest . fn ( ) ;
153
168
obsQuery . result = mockCallback . mockReturnValue ( 'expected' ) ;
154
169
170
+ const queryRef = createQueryRef ( obsQuery ) ;
155
171
const result = queryRef . result ( ) ;
156
172
157
173
expect ( result ) . toBe ( 'expected' ) ;
@@ -160,6 +176,7 @@ describe('QueryRef', () => {
160
176
161
177
test ( 'should be able to call getCurrentResult() and get updated results' , done => {
162
178
let calls = 0 ;
179
+ const queryRef = createQueryRef ( obsQuery ) ;
163
180
const obs = queryRef . valueChanges ;
164
181
165
182
obs . pipe ( map ( result => result . data ) ) . subscribe ( {
@@ -189,6 +206,7 @@ describe('QueryRef', () => {
189
206
const mockCallback = jest . fn ( ) ;
190
207
obsQuery . getLastResult = mockCallback . mockReturnValue ( 'expected' ) ;
191
208
209
+ const queryRef = createQueryRef ( obsQuery ) ;
192
210
const result = queryRef . getLastResult ( ) ;
193
211
194
212
expect ( result ) . toBe ( 'expected' ) ;
@@ -199,6 +217,7 @@ describe('QueryRef', () => {
199
217
const mockCallback = jest . fn ( ) ;
200
218
obsQuery . getLastError = mockCallback . mockReturnValue ( 'expected' ) ;
201
219
220
+ const queryRef = createQueryRef ( obsQuery ) ;
202
221
const result = queryRef . getLastError ( ) ;
203
222
204
223
expect ( result ) . toBe ( 'expected' ) ;
@@ -209,6 +228,7 @@ describe('QueryRef', () => {
209
228
const mockCallback = jest . fn ( ) ;
210
229
obsQuery . resetLastResults = mockCallback . mockReturnValue ( 'expected' ) ;
211
230
231
+ const queryRef = createQueryRef ( obsQuery ) ;
212
232
const result = queryRef . resetLastResults ( ) ;
213
233
214
234
expect ( result ) . toBe ( 'expected' ) ;
@@ -217,10 +237,11 @@ describe('QueryRef', () => {
217
237
218
238
test ( 'should be able to call fetchMore()' , ( ) => {
219
239
const mockCallback = jest . fn ( ) ;
220
- const opts = { foo : 1 } ;
240
+ const opts = { variables : { foo : 1 } } ;
221
241
obsQuery . fetchMore = mockCallback . mockReturnValue ( 'expected' ) ;
222
242
223
- const result = queryRef . fetchMore ( opts as any ) ;
243
+ const queryRef = createQueryRef ( obsQuery ) ;
244
+ const result = queryRef . fetchMore ( opts ) ;
224
245
225
246
expect ( result ) . toBe ( 'expected' ) ;
226
247
expect ( mockCallback . mock . calls . length ) . toBe ( 1 ) ;
@@ -229,10 +250,11 @@ describe('QueryRef', () => {
229
250
230
251
test ( 'should be able to call subscribeToMore()' , ( ) => {
231
252
const mockCallback = jest . fn ( ) ;
232
- const opts = { foo : 1 } ;
253
+ const opts = { document : heroesOperation . query } ;
233
254
obsQuery . subscribeToMore = mockCallback ;
234
255
235
- queryRef . subscribeToMore ( opts as any ) ;
256
+ const queryRef = createQueryRef ( obsQuery ) ;
257
+ queryRef . subscribeToMore ( opts ) ;
236
258
237
259
expect ( mockCallback . mock . calls . length ) . toBe ( 1 ) ;
238
260
expect ( mockCallback . mock . calls [ 0 ] [ 0 ] ) . toBe ( opts ) ;
@@ -242,6 +264,7 @@ describe('QueryRef', () => {
242
264
const mockCallback = jest . fn ( ) ;
243
265
obsQuery . stopPolling = mockCallback ;
244
266
267
+ const queryRef = createQueryRef ( obsQuery ) ;
245
268
queryRef . stopPolling ( ) ;
246
269
247
270
expect ( mockCallback . mock . calls . length ) . toBe ( 1 ) ;
@@ -251,6 +274,7 @@ describe('QueryRef', () => {
251
274
const mockCallback = jest . fn ( ) ;
252
275
obsQuery . startPolling = mockCallback ;
253
276
277
+ const queryRef = createQueryRef ( obsQuery ) ;
254
278
queryRef . startPolling ( 3000 ) ;
255
279
256
280
expect ( mockCallback . mock . calls . length ) . toBe ( 1 ) ;
@@ -262,6 +286,7 @@ describe('QueryRef', () => {
262
286
const opts = { } ;
263
287
obsQuery . setOptions = mockCallback . mockReturnValue ( 'expected' ) ;
264
288
289
+ const queryRef = createQueryRef ( obsQuery ) ;
265
290
const result = queryRef . setOptions ( opts ) ;
266
291
267
292
expect ( result ) . toBe ( 'expected' ) ;
@@ -274,6 +299,7 @@ describe('QueryRef', () => {
274
299
const variables = { } ;
275
300
obsQuery . setVariables = mockCallback . mockReturnValue ( 'expected' ) ;
276
301
302
+ const queryRef = createQueryRef ( obsQuery ) ;
277
303
const result = queryRef . setVariables ( variables ) ;
278
304
279
305
expect ( result ) . toBe ( 'expected' ) ;
@@ -282,6 +308,7 @@ describe('QueryRef', () => {
282
308
} ) ;
283
309
284
310
test ( 'should handle multiple subscribers' , done => {
311
+ const queryRef = createQueryRef ( obsQuery ) ;
285
312
const obsFirst = queryRef . valueChanges ;
286
313
const obsSecond = queryRef . valueChanges ;
287
314
@@ -338,6 +365,7 @@ describe('QueryRef', () => {
338
365
} ) ;
339
366
340
367
test ( 'should unsubscribe' , done => {
368
+ const queryRef = createQueryRef ( obsQuery ) ;
341
369
const obs = queryRef . valueChanges ;
342
370
const id = queryRef . queryId ;
343
371
@@ -356,6 +384,7 @@ describe('QueryRef', () => {
356
384
357
385
test ( 'should unsubscribe based on rxjs operators' , done => {
358
386
const gate = new Subject < void > ( ) ;
387
+ const queryRef = createQueryRef ( obsQuery ) ;
359
388
const obs = queryRef . valueChanges . pipe ( takeUntil ( gate ) ) ;
360
389
const id = queryRef . queryId ;
361
390
0 commit comments