4
4
* This source code is licensed under the MIT license found in the
5
5
* LICENSE file in the root directory of this source tree.
6
6
*
7
- * @no - flow
7
+ * @flow
8
8
*/
9
9
10
- const { expect } = require ( 'chai' ) ;
11
- const { describe, it } = require ( 'mocha' ) ;
12
- const DataLoader = require ( '../' ) ;
10
+ const DataLoader = require ( '..' ) ;
13
11
14
12
describe ( 'Provides descriptive error messages for API abuse' , ( ) => {
15
13
16
14
it ( 'Loader creation requires a function' , ( ) => {
17
15
expect ( ( ) => {
16
+ // $FlowExpectError
18
17
new DataLoader ( ) ; // eslint-disable-line no-new
19
- } ) . to . throw (
18
+ } ) . toThrow (
20
19
'DataLoader must be constructed with a function which accepts ' +
21
20
'Array<key> and returns Promise<Array<value>>, but got: undefined.'
22
21
) ;
23
22
24
23
expect ( ( ) => {
24
+ // $FlowExpectError
25
25
new DataLoader ( { } ) ; // eslint-disable-line no-new
26
- } ) . to . throw (
26
+ } ) . toThrow (
27
27
'DataLoader must be constructed with a function which accepts ' +
28
28
'Array<key> and returns Promise<Array<value>>, but got: [object Object].'
29
29
) ;
30
30
} ) ;
31
31
32
32
it ( 'Load function requires an key' , ( ) => {
33
- var idLoader = new DataLoader ( keys => Promise . resolve ( keys ) ) ;
33
+ const idLoader = new DataLoader ( keys => Promise . resolve ( keys ) ) ;
34
34
35
35
expect ( ( ) => {
36
36
idLoader . load ( ) ;
37
- } ) . to . throw (
37
+ } ) . toThrow (
38
38
'The loader.load() function must be called with a value,' +
39
39
'but got: undefined.'
40
40
) ;
41
41
42
42
expect ( ( ) => {
43
43
idLoader . load ( null ) ;
44
- } ) . to . throw (
44
+ } ) . toThrow (
45
45
'The loader.load() function must be called with a value,' +
46
46
'but got: null.'
47
47
) ;
48
48
49
49
// Falsey values like the number 0 is acceptable
50
50
expect ( ( ) => {
51
51
idLoader . load ( 0 ) ;
52
- } ) . not . to . throw ( ) ;
52
+ } ) . not . toThrow ( ) ;
53
53
} ) ;
54
54
55
55
it ( 'LoadMany function requires a list of key' , ( ) => {
56
- var idLoader = new DataLoader ( keys => Promise . resolve ( keys ) ) ;
56
+ const idLoader = new DataLoader ( keys => Promise . resolve ( keys ) ) ;
57
57
58
58
expect ( ( ) => {
59
+ // $FlowExpectError
59
60
idLoader . loadMany ( ) ;
60
- } ) . to . throw (
61
+ } ) . toThrow (
61
62
'The loader.loadMany() function must be called with Array<key> ' +
62
63
'but got: undefined.'
63
64
) ;
64
65
65
66
expect ( ( ) => {
67
+ // $FlowExpectError
66
68
idLoader . loadMany ( 1 , 2 , 3 ) ;
67
- } ) . to . throw (
69
+ } ) . toThrow (
68
70
'The loader.loadMany() function must be called with Array<key> ' +
69
71
'but got: 1.'
70
72
) ;
71
73
72
74
// Empty array is acceptable
73
75
expect ( ( ) => {
74
76
idLoader . loadMany ( [ ] ) ;
75
- } ) . not . to . throw ( ) ;
77
+ } ) . not . toThrow ( ) ;
76
78
} ) ;
77
79
78
80
it ( 'Batch function must return a Promise, not null' , async ( ) => {
79
- var badLoader = new DataLoader ( ( ) => null ) ;
81
+ // $FlowExpectError
82
+ const badLoader = new DataLoader ( ( ) => null ) ;
80
83
81
- var caughtError ;
84
+ let caughtError ;
82
85
try {
83
86
await badLoader . load ( 1 ) ;
84
87
} catch ( error ) {
85
88
caughtError = error ;
86
89
}
87
- expect ( caughtError ) . to . be . instanceof ( Error ) ;
88
- expect ( caughtError . message ) . to . equal (
90
+ expect ( caughtError ) . toBeInstanceOf ( Error ) ;
91
+ expect ( ( caughtError : any ) . message ) . toBe (
89
92
'DataLoader must be constructed with a function which accepts ' +
90
93
'Array<key> and returns Promise<Array<value>>, but the function did ' +
91
94
'not return a Promise: null.'
@@ -94,16 +97,17 @@ describe('Provides descriptive error messages for API abuse', () => {
94
97
95
98
it ( 'Batch function must return a Promise, not a value' , async ( ) => {
96
99
// Note: this is returning the keys directly, rather than a promise to keys.
97
- var badLoader = new DataLoader ( keys => keys ) ;
100
+ // $FlowExpectError
101
+ const badLoader = new DataLoader ( keys => keys ) ;
98
102
99
- var caughtError ;
103
+ let caughtError ;
100
104
try {
101
105
await badLoader . load ( 1 ) ;
102
106
} catch ( error ) {
103
107
caughtError = error ;
104
108
}
105
- expect ( caughtError ) . to . be . instanceof ( Error ) ;
106
- expect ( caughtError . message ) . to . equal (
109
+ expect ( caughtError ) . toBeInstanceOf ( Error ) ;
110
+ expect ( ( caughtError : any ) . message ) . toBe (
107
111
'DataLoader must be constructed with a function which accepts ' +
108
112
'Array<key> and returns Promise<Array<value>>, but the function did ' +
109
113
'not return a Promise: 1.'
@@ -112,16 +116,17 @@ describe('Provides descriptive error messages for API abuse', () => {
112
116
113
117
it ( 'Batch function must return a Promise of an Array, not null' , async ( ) => {
114
118
// Note: this resolves to undefined
115
- var badLoader = new DataLoader ( ( ) => Promise . resolve ( ) ) ;
119
+ // $FlowExpectError
120
+ const badLoader = new DataLoader ( ( ) => Promise . resolve ( undefined ) ) ;
116
121
117
- var caughtError ;
122
+ let caughtError ;
118
123
try {
119
124
await badLoader . load ( 1 ) ;
120
125
} catch ( error ) {
121
126
caughtError = error ;
122
127
}
123
- expect ( caughtError ) . to . be . instanceof ( Error ) ;
124
- expect ( caughtError . message ) . to . equal (
128
+ expect ( caughtError ) . toBeInstanceOf ( Error ) ;
129
+ expect ( ( caughtError : any ) . message ) . toBe (
125
130
'DataLoader must be constructed with a function which accepts ' +
126
131
'Array<key> and returns Promise<Array<value>>, but the function did ' +
127
132
'not return a Promise of an Array: undefined.'
@@ -130,16 +135,16 @@ describe('Provides descriptive error messages for API abuse', () => {
130
135
131
136
it ( 'Batch function must promise an Array of correct length' , async ( ) => {
132
137
// Note: this resolves to empty array
133
- var badLoader = new DataLoader ( ( ) => Promise . resolve ( [ ] ) ) ;
138
+ const badLoader = new DataLoader ( ( ) => Promise . resolve ( [ ] ) ) ;
134
139
135
- var caughtError ;
140
+ let caughtError ;
136
141
try {
137
142
await badLoader . load ( 1 ) ;
138
143
} catch ( error ) {
139
144
caughtError = error ;
140
145
}
141
- expect ( caughtError ) . to . be . instanceof ( Error ) ;
142
- expect ( caughtError . message ) . to . equal (
146
+ expect ( caughtError ) . toBeInstanceOf ( Error ) ;
147
+ expect ( ( caughtError : any ) . message ) . toBe (
143
148
'DataLoader must be constructed with a function which accepts ' +
144
149
'Array<key> and returns Promise<Array<value>>, but the function did ' +
145
150
'not return a Promise of an Array of the same length as the Array ' +
@@ -155,10 +160,11 @@ describe('Provides descriptive error messages for API abuse', () => {
155
160
}
156
161
157
162
expect ( ( ) => {
158
- var incompleteMap = new IncompleteMap ( ) ;
159
- var options = { cacheMap : incompleteMap } ;
160
- new DataLoader ( keys => keys , options ) ; // eslint-disable-line no-new
161
- } ) . to . throw (
163
+ // $FlowExpectError
164
+ const incompleteMap = new IncompleteMap ( ) ;
165
+ const options = { cacheMap : incompleteMap } ;
166
+ new DataLoader ( async keys => keys , options ) ; // eslint-disable-line no-new
167
+ } ) . toThrow (
162
168
'Custom cacheMap missing methods: set, delete, clear'
163
169
) ;
164
170
} ) ;
0 commit comments