@@ -418,6 +418,56 @@ describe('Accepts options', () => {
418418 ) ;
419419 } ) ;
420420
421+ describe ( 'Accepts object key in custom cacheKey function' , ( ) => {
422+ function cacheKey ( key ) {
423+ var result ;
424+ if ( typeof key === 'object' ) {
425+ result = Object . keys ( key ) . sort ( ) . map ( k => k + ':' + key [ k ] ) . join ( '-' ) ;
426+ } else {
427+ result = String ( key ) ;
428+ }
429+ return result ;
430+ }
431+
432+ it ( 'Accepts objects with simple key' , async ( ) => {
433+ var keyA = '1234' ;
434+ var identityLoadCalls = [ ] ;
435+ var identityLoader = new DataLoader ( keys => {
436+ identityLoadCalls . push ( keys ) ;
437+ return Promise . resolve ( keys ) ;
438+ } , { cacheKeyFn : cacheKey } ) ;
439+
440+ var valueA = await identityLoader . load ( keyA ) ;
441+ expect ( valueA ) . to . equal ( keyA ) ;
442+ } ) ;
443+
444+ it ( 'Accepts objects with different order of keys' , async ( ) => {
445+ var keyA = { a : 123 , b : 321 } ;
446+ var keyB = { b : 321 , a : 123 } ;
447+
448+ var identityLoadCalls = [ ] ;
449+ var identityLoader = new DataLoader ( keys => {
450+ identityLoadCalls . push ( keys ) ;
451+ return Promise . resolve ( keys ) ;
452+ } , { cacheKeyFn : cacheKey } ) ;
453+
454+ // Fetches as expected
455+
456+ var [ valueA , valueB ] = await Promise . all ( [
457+ identityLoader . load ( keyA ) ,
458+ identityLoader . load ( keyB ) ,
459+ ] ) ;
460+
461+ expect ( valueA ) . to . equal ( keyA ) ;
462+ expect ( valueB ) . to . equal ( valueA ) ;
463+
464+ expect ( identityLoadCalls ) . to . have . length ( 1 ) ;
465+ expect ( identityLoadCalls [ 0 ] ) . to . have . length ( 1 ) ;
466+ expect ( identityLoadCalls [ 0 ] [ 0 ] ) . to . equal ( keyA ) ;
467+ } ) ;
468+
469+ } ) ;
470+
421471} ) ;
422472
423473describe ( 'It is resilient to job queue ordering' , ( ) => {
0 commit comments