Skip to content

Commit 4212c9e

Browse files
authored
Add tests for behavior when process.nextTick does not exist (graphql#221)
1 parent 44977c0 commit 4212c9e

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/__tests__/browser.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (c) 2019-present, GraphQL Foundation
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
// Mock out process.nextTick as not existing for this test before requiring.
11+
process.nextTick = (null: any);
12+
const DataLoader = require('..');
13+
14+
describe('Browser support', () => {
15+
it('batches multiple requests without process.nextTick', async () => {
16+
const loadCalls = [];
17+
const identityLoader = new DataLoader<number, number>(async keys => {
18+
loadCalls.push(keys);
19+
return keys;
20+
});
21+
22+
const promise1 = identityLoader.load(1);
23+
const promise2 = identityLoader.load(2);
24+
25+
const [ value1, value2 ] = await Promise.all([ promise1, promise2 ]);
26+
expect(value1).toBe(1);
27+
expect(value2).toBe(2);
28+
29+
expect(loadCalls).toEqual([ [ 1, 2 ] ]);
30+
});
31+
});

src/__tests__/oldbrowser.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) 2019-present, GraphQL Foundation
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
// Mock out process.nextTick and setImmediate as not existing for this test
11+
// before requiring.
12+
process.nextTick = (null: any);
13+
global.setImmediate = (null: any);
14+
const DataLoader = require('..');
15+
16+
describe('Old browser support', () => {
17+
it('batches multiple requests without setImmediate', async () => {
18+
const loadCalls = [];
19+
const identityLoader = new DataLoader<number, number>(async keys => {
20+
loadCalls.push(keys);
21+
return keys;
22+
});
23+
24+
const promise1 = identityLoader.load(1);
25+
const promise2 = identityLoader.load(2);
26+
27+
const [ value1, value2 ] = await Promise.all([ promise1, promise2 ]);
28+
expect(value1).toBe(1);
29+
expect(value2).toBe(2);
30+
31+
expect(loadCalls).toEqual([ [ 1, 2 ] ]);
32+
});
33+
});

0 commit comments

Comments
 (0)