@@ -468,6 +468,80 @@ describe('Accepts options', () => {
468468
469469 } ) ;
470470
471+ describe ( 'Accepts custom cacheMap instance' , ( ) => {
472+
473+ class SimpleMap {
474+ stash : Object ;
475+
476+ constructor ( ) {
477+ this . stash = { } ;
478+ }
479+ get ( key ) {
480+ return this . stash [ key ] ;
481+ }
482+ set ( key , value ) {
483+ this . stash [ key ] = value ;
484+ }
485+ delete ( key ) {
486+ delete this . stash [ key ] ;
487+ }
488+ clear ( ) {
489+ this . stash = { } ;
490+ }
491+ }
492+
493+ it ( 'Accepts a custom cache map implementation' , async ( ) => {
494+ var aCustomMap = new SimpleMap ( ) ;
495+ var identityLoadCalls = [ ] ;
496+ var identityLoader = new DataLoader ( keys => {
497+ identityLoadCalls . push ( keys ) ;
498+ return Promise . resolve ( keys ) ;
499+ } , { cacheMap : aCustomMap } ) ;
500+
501+ // Fetches as expected
502+
503+ var [ valueA , valueB1 ] = await Promise . all ( [
504+ identityLoader . load ( 'a' ) ,
505+ identityLoader . load ( 'b' ) ,
506+ ] ) ;
507+
508+ expect ( valueA ) . to . equal ( 'a' ) ;
509+ expect ( valueB1 ) . to . equal ( 'b' ) ;
510+
511+ expect ( identityLoadCalls ) . to . deep . equal ( [ [ 'a' , 'b' ] ] ) ;
512+ expect ( Object . keys ( aCustomMap . stash ) ) . to . deep . equal ( [ 'a' , 'b' ] ) ;
513+
514+ var [ valueC , valueB2 ] = await Promise . all ( [
515+ identityLoader . load ( 'c' ) ,
516+ identityLoader . load ( 'b' ) ,
517+ ] ) ;
518+
519+ expect ( valueC ) . to . equal ( 'c' ) ;
520+ expect ( valueB2 ) . to . equal ( 'b' ) ;
521+
522+ expect ( identityLoadCalls ) . to . deep . equal ( [ [ 'a' , 'b' ] , [ 'c' ] ] ) ;
523+ expect ( Object . keys ( aCustomMap . stash ) ) . to . deep . equal ( [ 'a' , 'b' , 'c' ] ) ;
524+
525+ // Supports clear
526+
527+ identityLoader . clear ( 'b' ) ;
528+ var valueB3 = await identityLoader . load ( 'b' ) ;
529+
530+ expect ( valueB3 ) . to . equal ( 'b' ) ;
531+ expect ( identityLoadCalls ) . to . deep . equal (
532+ [ [ 'a' , 'b' ] , [ 'c' ] , [ 'b' ] ]
533+ ) ;
534+ expect ( Object . keys ( aCustomMap . stash ) ) . to . deep . equal ( [ 'a' , 'c' , 'b' ] ) ;
535+
536+ // Supports clear all
537+
538+ identityLoader . clearAll ( ) ;
539+
540+ expect ( Object . keys ( aCustomMap . stash ) ) . to . deep . equal ( [ ] ) ;
541+ } ) ;
542+
543+ } ) ;
544+
471545} ) ;
472546
473547describe ( 'It is resilient to job queue ordering' , ( ) => {
0 commit comments