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