File tree 2 files changed +64
-0
lines changed
2 files changed +64
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments