@@ -86,6 +86,11 @@ describe('service.all()', () => {
86
86
await booksService . clearCacheMemory ( ) ;
87
87
test_response_subject . complete ( ) ;
88
88
test_response_subject = new BehaviorSubject ( new HttpResponse ( ) ) ;
89
+
90
+ // clear cachememory on every test
91
+ let cachememory = CacheMemory . getInstance ( ) ;
92
+ ( cachememory as any ) . resources = { } ;
93
+ ( cachememory as any ) . collections = { } ;
89
94
} ) ;
90
95
91
96
it ( `without cached collection emits source ^new-server|` , async ( ) => {
@@ -179,13 +184,14 @@ describe('service.all()', () => {
179
184
test_response_subject . next ( new HttpResponse ( { body : TestFactory . getCollectionDocumentData ( Book ) } ) ) ;
180
185
booksService . collections_ttl = 5 ; // live
181
186
await booksService . all ( ) . toPromise ( ) ;
182
- CacheMemory . getInstance ( ) . deprecateCollections ( '' ) ; // kill only memory cache
187
+ let cachememory = CacheMemory . getInstance ( ) ; // kill only memory cache
188
+ ( cachememory as any ) . resources = { } ; // kill memory cache
189
+ ( cachememory as any ) . collections = { } ; // kill memory cache
183
190
184
191
let http_request_spy = spyOn ( HttpClient . prototype , 'request' ) . and . callThrough ( ) ;
185
192
let expected = [
186
193
// expected emits
187
- // source_resource: 'server' because we dont touch child elements
188
- { builded : true , loaded : false , source : 'new' , source_resource : 'server' } ,
194
+ { builded : false , loaded : false , source : 'new' , source_resource : undefined } ,
189
195
{ builded : true , loaded : true , source : 'store' , source_resource : 'store' }
190
196
] ;
191
197
@@ -199,7 +205,44 @@ describe('service.all()', () => {
199
205
}
200
206
} ) ,
201
207
map ( emit => {
202
- return { builded : emit . builded , loaded : emit . loaded , source : emit . source , source_resource : emit . data [ 0 ] . source } ;
208
+ if ( emit . data . length > 0 ) {
209
+ return { builded : emit . builded , loaded : emit . loaded , source : emit . source , source_resource : emit . data [ 0 ] . source } ;
210
+ } else {
211
+ return { builded : emit . builded , loaded : emit . loaded , source : emit . source , source_resource : undefined } ;
212
+ }
213
+ } ) ,
214
+ toArray ( )
215
+ )
216
+ . toPromise ( ) ;
217
+ expect ( emits ) . toMatchObject ( expected ) ;
218
+ expect ( http_request_spy ) . toHaveBeenCalledTimes ( 0 ) ;
219
+ } ) ;
220
+
221
+ it ( `with cached on store (live) collection wihtout includes emits source ^new-store|` , async ( ) => {
222
+ // caching collection
223
+ test_response_subject . next ( new HttpResponse ( { body : TestFactory . getCollectionDocumentData ( Book ) } ) ) ;
224
+ booksService . collections_ttl = 5 ; // live
225
+ await booksService . all ( { include : [ 'author' ] } ) . toPromise ( ) ;
226
+ let cachememory = CacheMemory . getInstance ( ) ; // kill only memory cache
227
+ ( cachememory as any ) . resources = { } ; // kill memory cache
228
+ ( cachememory as any ) . collections = { } ; // kill memory cache
229
+
230
+ let http_request_spy = spyOn ( HttpClient . prototype , 'request' ) . and . callThrough ( ) ;
231
+ let expected = [
232
+ // expected emits
233
+ { builded : false , loaded : false , source : 'new' , source_resource : undefined } ,
234
+ { builded : true , loaded : true , source : 'store' , source_resource : 'store' }
235
+ ] ;
236
+
237
+ let emits = await booksService
238
+ . all ( )
239
+ . pipe (
240
+ map ( emit => {
241
+ if ( emit . data . length > 0 ) {
242
+ return { builded : emit . builded , loaded : emit . loaded , source : emit . source , source_resource : emit . data [ 0 ] . source } ;
243
+ } else {
244
+ return { builded : emit . builded , loaded : emit . loaded , source : emit . source , source_resource : undefined } ;
245
+ }
203
246
} ) ,
204
247
toArray ( )
205
248
)
@@ -213,13 +256,39 @@ describe('service.all()', () => {
213
256
test_response_subject . next ( new HttpResponse ( { body : TestFactory . getCollectionDocumentData ( Book ) } ) ) ;
214
257
booksService . collections_ttl = 0 ; // dead
215
258
await booksService . all ( ) . toPromise ( ) ;
216
- booksService . clearCacheMemory ( ) ; // kill only memory cache
259
+ CacheMemory . getInstance ( ) . deprecateCollections ( '' ) ;
260
+
261
+ let http_request_spy = spyOn ( HttpClient . prototype , 'request' ) . and . callThrough ( ) ;
262
+ let expected = [
263
+ // expected emits
264
+ { builded : true , loaded : false , source : 'new' } , // @TODO : builded should be false
265
+ { builded : true , loaded : true , source : 'server' }
266
+ ] ;
267
+
268
+ let emits = await booksService
269
+ . all ( )
270
+ . pipe (
271
+ map ( emit => {
272
+ return { builded : emit . builded , loaded : emit . loaded , source : emit . source } ;
273
+ } ) ,
274
+ toArray ( )
275
+ )
276
+ . toPromise ( ) ;
277
+ expect ( emits ) . toMatchObject ( expected ) ;
278
+ expect ( http_request_spy ) . toHaveBeenCalledTimes ( 1 ) ;
279
+ } ) ;
280
+
281
+ it ( `with cached on store (dead, no collection_ttl defined) collection emits source ^new-store-server|` , async ( ) => {
282
+ // caching collection
283
+ test_response_subject . next ( new HttpResponse ( { body : TestFactory . getCollectionDocumentData ( Book ) } ) ) ;
284
+ delete booksService . collections_ttl ; // dead
285
+ await booksService . all ( ) . toPromise ( ) ;
286
+ CacheMemory . getInstance ( ) . deprecateCollections ( '' ) ;
217
287
218
288
let http_request_spy = spyOn ( HttpClient . prototype , 'request' ) . and . callThrough ( ) ;
219
289
let expected = [
220
290
// expected emits
221
291
{ builded : true , loaded : false , source : 'new' } , // @TODO : builded should be false
222
- // { builded: true, loaded: false, source: 'store' }, // @todo
223
292
{ builded : true , loaded : true , source : 'server' }
224
293
] ;
225
294
@@ -320,6 +389,7 @@ describe('service.all() and next service.get()', () => {
320
389
} ) ;
321
390
322
391
it ( `with cached collection on memory and next request get() without include` , async ( ) => {
392
+ Author . test_ttl = 100000 ;
323
393
let http_request_spy = spyOn ( HttpClient . prototype , 'request' ) . and . callThrough ( ) ;
324
394
let all_authors_body = TestFactory . getCollectionDocumentData ( Author , 1 , [ 'books' ] ) ;
325
395
test_response_subject . next ( new HttpResponse ( { body : all_authors_body } ) ) ;
@@ -346,8 +416,7 @@ describe('service.all() and next service.get()', () => {
346
416
)
347
417
. toPromise ( ) ;
348
418
349
- // @TODO : fix this error!!!
350
- // expect(author_emits).toMatchObject(expected); // ERROR!!! [{ loaded: false, source: 'memory' }, { loaded: false, source: 'store' }]
419
+ expect ( author_emits ) . toMatchObject ( expected ) ;
351
420
// expect(received_author.relationships.books.data[0].attributes).toBeFalsy(); // ERROR!!!
352
421
expect ( http_request_spy ) . toHaveBeenCalledTimes ( 1 ) ; // on all() request
353
422
} ) ;
@@ -385,7 +454,7 @@ describe('service.all() and next service.get()', () => {
385
454
. toPromise ( ) ;
386
455
387
456
// @TODO : fix this error!!!
388
- // expect(author_emits).toMatchObject(expected); // ERROR!!! [{ loaded: false, source: 'new' }, { loaded: false, source: 'store' }]
457
+ expect ( author_emits ) . toMatchObject ( expected ) ;
389
458
// expect(received_author.relationships.books.data[0].attributes).toBeFalsy(); // ERROR!!!
390
459
expect ( http_request_spy ) . toHaveBeenCalledTimes ( 1 ) ; // on all() request
391
460
} ) ;
@@ -395,6 +464,7 @@ describe('service.get()', () => {
395
464
let core : Core ;
396
465
let booksService : BooksService ;
397
466
let authorsService : AuthorsService ;
467
+ let photosService : PhotosService ;
398
468
beforeEach ( async ( ) => {
399
469
core = new Core (
400
470
new JsonapiConfig ( ) ,
@@ -406,6 +476,8 @@ describe('service.get()', () => {
406
476
await booksService . clearCacheMemory ( ) ;
407
477
authorsService = new AuthorsService ( ) ;
408
478
authorsService . register ( ) ;
479
+ photosService = new PhotosService ( ) ;
480
+ photosService . register ( ) ;
409
481
await authorsService . clearCacheMemory ( ) ;
410
482
test_response_subject . complete ( ) ;
411
483
test_response_subject = new BehaviorSubject ( new HttpResponse ( ) ) ;
@@ -589,17 +661,16 @@ describe('service.get()', () => {
589
661
{ loaded : true , source : 'server' }
590
662
] ;
591
663
let emits = await booksService
592
- . get ( '1' , { ttl : 1000 } )
664
+ . get ( '1' )
593
665
. pipe (
594
666
map ( emit => {
595
667
return { loaded : emit . loaded , source : emit . source } ;
596
668
} ) ,
597
669
toArray ( )
598
670
)
599
671
. toPromise ( ) ;
600
- // @TODO : fix library
601
- // expect(emits).toMatchObject(expected); // ERROR!!! [{ loaded: true, source: 'memory' }, { loaded: true, source: 'server' }]
602
- // expect(http_request_spy).toHaveBeenCalledTimes(1); // ERROR: receiving 0...
672
+ expect ( emits ) . toMatchObject ( expected ) ;
673
+ expect ( http_request_spy ) . toHaveBeenCalledTimes ( 1 ) ;
603
674
} ) ;
604
675
605
676
it ( `with cached on store (live) resource emits source ^new-store|` , async ( ) => {
0 commit comments