Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed Jan 12, 2016
1 parent 06a3bf1 commit 96144f2
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions src/__tests__/dataloader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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),
Expand Down

0 comments on commit 96144f2

Please sign in to comment.