Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for behavior when process.nextTick does not exist #221

Merged
merged 1 commit into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/__tests__/browser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2019-present, GraphQL Foundation
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

// Mock out process.nextTick as not existing for this test before requiring.
process.nextTick = (null: any);
const DataLoader = require('..');

describe('Browser support', () => {
it('batches multiple requests without process.nextTick', async () => {
const loadCalls = [];
const identityLoader = new DataLoader<number, number>(async keys => {
loadCalls.push(keys);
return keys;
});

const promise1 = identityLoader.load(1);
const promise2 = identityLoader.load(2);

const [ value1, value2 ] = await Promise.all([ promise1, promise2 ]);
expect(value1).toBe(1);
expect(value2).toBe(2);

expect(loadCalls).toEqual([ [ 1, 2 ] ]);
});
});
33 changes: 33 additions & 0 deletions src/__tests__/oldbrowser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2019-present, GraphQL Foundation
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

// Mock out process.nextTick and setImmediate as not existing for this test
// before requiring.
process.nextTick = (null: any);
global.setImmediate = (null: any);
const DataLoader = require('..');

describe('Old browser support', () => {
it('batches multiple requests without setImmediate', async () => {
const loadCalls = [];
const identityLoader = new DataLoader<number, number>(async keys => {
loadCalls.push(keys);
return keys;
});

const promise1 = identityLoader.load(1);
const promise2 = identityLoader.load(2);

const [ value1, value2 ] = await Promise.all([ promise1, promise2 ]);
expect(value1).toBe(1);
expect(value2).toBe(2);

expect(loadCalls).toEqual([ [ 1, 2 ] ]);
});
});