11import { InMemoryLRUCache } from 'apollo-server-caching'
22import wait from 'waait'
3+ import { ObjectId } from 'mongodb'
4+ import { EJSON } from 'bson'
35
4- import { createCachingMethods } from '../cache'
6+ import { createCachingMethods , idToString } from '../cache'
57
68const docs = {
79 id1 : {
810 _id : 'id1'
11+ // _id: ObjectId()
912 } ,
1013 id2 : {
11- _id : 'id2'
12- } ,
13- id3 : {
14- _id : 'id3'
14+ _id : ObjectId ( )
1515 }
1616}
1717
1818const collectionName = 'test'
19- const cacheKey = id => `mongo-${ collectionName } -${ id } `
19+ const cacheKey = id => `mongo-${ collectionName } -${ idToString ( id ) } `
2020
2121describe ( 'createCachingMethods' , ( ) => {
2222 let collection
@@ -29,7 +29,13 @@ describe('createCachingMethods', () => {
2929 find : jest . fn ( ( { _id : { $in : ids } } ) => ( {
3030 toArray : ( ) =>
3131 new Promise ( resolve => {
32- setTimeout ( ( ) => resolve ( ids . map ( id => docs [ id ] ) ) , 0 )
32+ setTimeout (
33+ ( ) =>
34+ resolve (
35+ ids . map ( id => ( id === docs . id1 . _id ? docs . id1 : docs . id2 ) )
36+ ) ,
37+ 0
38+ )
3339 } )
3440 } ) )
3541 }
@@ -46,61 +52,61 @@ describe('createCachingMethods', () => {
4652 } )
4753
4854 it ( 'finds one' , async ( ) => {
49- const doc = await api . findOneById ( ' id1' )
55+ const doc = await api . findOneById ( docs . id1 . _id )
5056 expect ( doc ) . toBe ( docs . id1 )
5157 expect ( collection . find . mock . calls . length ) . toBe ( 1 )
5258 } )
5359
5460 it ( 'finds two with batching' , async ( ) => {
55- const foundDocs = await api . findManyByIds ( [ 'id2' , 'id3' ] )
61+ const foundDocs = await api . findManyByIds ( [ docs . id1 . _id , docs . id2 . _id ] )
5662
57- expect ( foundDocs [ 0 ] ) . toBe ( docs . id2 )
58- expect ( foundDocs [ 1 ] ) . toBe ( docs . id3 )
63+ expect ( foundDocs [ 0 ] ) . toBe ( docs . id1 )
64+ expect ( foundDocs [ 1 ] ) . toBe ( docs . id2 )
5965
6066 expect ( collection . find . mock . calls . length ) . toBe ( 1 )
6167 } )
6268
6369 // TODO why doesn't this pass?
6470 // it.only(`doesn't cache without ttl`, async () => {
65- // await api.findOneById(' id1' )
66- // await api.findOneById(' id1' )
71+ // await api.findOneById(docs. id1._id )
72+ // await api.findOneById(docs. id1._id )
6773
6874 // expect(collection.find.mock.calls.length).toBe(2)
6975 // })
7076
7177 it ( `doesn't cache without ttl` , async ( ) => {
72- await api . findOneById ( ' id1' )
78+ await api . findOneById ( docs . id1 . _id )
7379
74- const value = await cache . get ( cacheKey ( ' id1' ) )
80+ const value = await cache . get ( cacheKey ( docs . id1 . _id ) )
7581 expect ( value ) . toBeUndefined ( )
7682 } )
7783
7884 it ( `caches` , async ( ) => {
79- await api . findOneById ( ' id1' , { ttl : 1 } )
80- const value = await cache . get ( cacheKey ( ' id1' ) )
81- expect ( JSON . parse ( value ) ) . toEqual ( docs . id1 )
85+ await api . findOneById ( docs . id1 . _id , { ttl : 1 } )
86+ const value = await cache . get ( cacheKey ( docs . id1 . _id ) )
87+ expect ( value ) . toEqual ( EJSON . stringify ( docs . id1 ) )
8288
83- await api . findOneById ( ' id1' )
89+ await api . findOneById ( docs . id1 . _id )
8490 expect ( collection . find . mock . calls . length ) . toBe ( 1 )
8591 } )
8692
8793 it ( `caches with ttl` , async ( ) => {
88- await api . findOneById ( ' id1' , { ttl : 1 } )
94+ await api . findOneById ( docs . id1 . _id , { ttl : 1 } )
8995 await wait ( 1001 )
9096
91- const value = await cache . get ( cacheKey ( ' id1' ) )
97+ const value = await cache . get ( cacheKey ( docs . id1 . _id ) )
9298 expect ( value ) . toBeUndefined ( )
9399 } )
94100
95101 it ( `deletes from cache` , async ( ) => {
96- await api . findOneById ( ' id1' , { ttl : 1 } )
102+ await api . findOneById ( docs . id1 . _id , { ttl : 1 } )
97103
98- const valueBefore = await cache . get ( cacheKey ( ' id1' ) )
99- expect ( JSON . parse ( valueBefore ) ) . toEqual ( docs . id1 )
104+ const valueBefore = await cache . get ( cacheKey ( docs . id1 . _id ) )
105+ expect ( valueBefore ) . toEqual ( EJSON . stringify ( docs . id1 ) )
100106
101- await api . deleteFromCacheById ( ' id1' )
107+ await api . deleteFromCacheById ( docs . id1 . _id )
102108
103- const valueAfter = await cache . get ( cacheKey ( ' id1' ) )
109+ const valueAfter = await cache . get ( cacheKey ( docs . id1 . _id ) )
104110 expect ( valueAfter ) . toBeUndefined ( )
105111 } )
106112} )
0 commit comments