From 4212c9e48d0580b9b28a31d6c1098d1a61260bfb Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Thu, 14 Nov 2019 13:23:39 -0800 Subject: [PATCH] Add tests for behavior when process.nextTick does not exist (#221) --- src/__tests__/browser.test.js | 31 ++++++++++++++++++++++++++++++ src/__tests__/oldbrowser.test.js | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/__tests__/browser.test.js create mode 100644 src/__tests__/oldbrowser.test.js diff --git a/src/__tests__/browser.test.js b/src/__tests__/browser.test.js new file mode 100644 index 0000000..e602cb6 --- /dev/null +++ b/src/__tests__/browser.test.js @@ -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(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 ] ]); + }); +}); diff --git a/src/__tests__/oldbrowser.test.js b/src/__tests__/oldbrowser.test.js new file mode 100644 index 0000000..8a579d8 --- /dev/null +++ b/src/__tests__/oldbrowser.test.js @@ -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(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 ] ]); + }); +});