diff --git a/src/__tests__/dataloader-test.js b/src/__tests__/dataloader-test.js index 9dc0b1d..8f9722a 100644 --- a/src/__tests__/dataloader-test.js +++ b/src/__tests__/dataloader-test.js @@ -420,31 +420,47 @@ describe('Accepts options', () => { describe('Accepts object key in custom cacheKey function', () => { function cacheKey(key) { - var result; - if (typeof key === 'object') { - result = Object.keys(key).sort().map(k => k + ':' + key[k]).join('-'); - } else { - result = String(key); - } - return result; + return Object.keys(key).sort().map(k => k + ':' + key[k]).join(); } - it('Accepts objects with simple key', async () => { - var keyA = '1234'; + it('Accepts objects with a complex key', async () => { var identityLoadCalls = []; var identityLoader = new DataLoader(keys => { identityLoadCalls.push(keys); return Promise.resolve(keys); }, { cacheKeyFn: cacheKey }); - var valueA = await identityLoader.load(keyA); - expect(valueA).to.equal(keyA); + var key1 = { id: 123 }; + var key2 = { id: 123 }; + + var value1 = await identityLoader.load(key1); + var value2 = await identityLoader.load(key2); + + expect(identityLoadCalls).to.deep.equal([ [ key1 ] ]); + expect(value1).to.equal(key1); + expect(value2).to.equal(key1); }); - it('Accepts objects with different order of keys', async () => { - var keyA = { a: 123, b: 321 }; - var keyB = { b: 321, a: 123 }; + it('Clears objects with complex key', async () => { + var identityLoadCalls = []; + var identityLoader = new DataLoader(keys => { + identityLoadCalls.push(keys); + return Promise.resolve(keys); + }, { cacheKeyFn: cacheKey }); + + var key1 = { id: 123 }; + var key2 = { id: 123 }; + + var value1 = await identityLoader.load(key1); + identityLoader.clear(key2); // clear equivalent object key + var value2 = await identityLoader.load(key1); + + expect(identityLoadCalls).to.deep.equal([ [ key1 ], [ key1 ] ]); + expect(value1).to.equal(key1); + expect(value2).to.equal(key1); + }); + it('Accepts objects with different order of keys', async () => { var identityLoadCalls = []; var identityLoader = new DataLoader(keys => { identityLoadCalls.push(keys); @@ -453,6 +469,9 @@ describe('Accepts options', () => { // Fetches as expected + var keyA = { a: 123, b: 321 }; + var keyB = { b: 321, a: 123 }; + var [ valueA, valueB ] = await Promise.all([ identityLoader.load(keyA), identityLoader.load(keyB),