@@ -9,12 +9,13 @@ customGlobal.fetch = require("jest-fetch-mock");
9
9
customGlobal . fetchMock = customGlobal . fetch ;
10
10
11
11
const fakeFetch = global . fetch as FetchMock ;
12
-
12
+ const fakeRevolve = jest . fn ( ) ;
13
13
// Mock a custom element of `any-provider`.
14
14
customElements . define (
15
15
"any-provider" ,
16
16
class Tmp extends HTMLElement {
17
17
resolve ( ) : string {
18
+ fakeRevolve ( ) ;
18
19
return "resolved" ;
19
20
}
20
21
}
@@ -59,18 +60,20 @@ describe("fetch", () => {
59
60
60
61
afterEach ( ( ) : void => {
61
62
fakeFetch . resetMocks ( ) ;
63
+ fakeRevolve . mockClear ( ) ;
62
64
} ) ;
63
65
64
66
it ( "should resolve normal provider" , async ( ) => {
65
- const result = await fetch ( "any-provider" , true , [ "abc" ] ) ;
67
+ const result = await fetch ( "any-provider" , true , ... [ "abc" ] ) ;
66
68
67
69
expect ( result ) . toBe ( "resolved" ) ;
70
+ expect ( fakeRevolve ) . toHaveBeenCalledTimes ( 1 ) ;
68
71
} ) ;
69
72
70
73
it ( "should throw error " , async ( ) => {
71
74
expect . assertions ( 1 ) ;
72
75
try {
73
- await fetch ( "error-provider" , true , [ "abc" ] ) ;
76
+ await fetch ( "error-provider" , true , ... [ "abc" ] ) ;
74
77
} catch ( e ) {
75
78
expect ( e . message ) . toBe ( "oops" ) ;
76
79
}
@@ -79,32 +82,36 @@ describe("fetch", () => {
79
82
it ( "should throw error when not defined provider" , async ( ) => {
80
83
expect . assertions ( 1 ) ;
81
84
try {
82
- await fetch ( "undefined-provider" , true , [ "abc" ] ) ;
85
+ await fetch ( "undefined-provider" , true , "abc" ) ;
83
86
} catch ( e ) {
84
87
expect ( e . message ) . toContain ( 'Provider not defined: "undefined-provider"' ) ;
85
88
}
86
89
} ) ;
87
90
88
91
it ( "should resolve flow api provider" , async ( ) => {
89
- const result = await fetch ( "easyops.custom_api@test" , false , [
90
- "abc" ,
91
- { a : "a" , c : "c" , b : "b" } ,
92
- ] ) ;
92
+ const result = await fetch ( "easyops.custom_api@test" , false , "abc" , {
93
+ a : "a" ,
94
+ c : "c" ,
95
+ b : "b" ,
96
+ } ) ;
93
97
94
98
expect ( result ) . toEqual ( expected ) ;
95
99
expect ( fakeFetch ) . toHaveBeenCalledTimes ( 1 ) ;
96
100
97
- const result2 = await fetch ( "easyops.custom_api@test" , false , [
98
- "abc" ,
99
- { a : "a" , c : "c" , b : "b" } ,
100
- ] ) ;
101
+ const result2 = await fetch ( "easyops.custom_api@test" , false , "abc" , {
102
+ a : "a" ,
103
+ c : "c" ,
104
+ b : "b" ,
105
+ } ) ;
101
106
102
107
expect ( result2 ) . toEqual ( expected ) ;
103
108
expect ( fakeFetch ) . toHaveBeenCalledTimes ( 2 ) ;
104
109
} ) ;
105
110
106
111
it ( "should resolve flow api provider by cache" , async ( ) => {
107
- const result = await fetch ( "easyops.custom_api@test" , true , [
112
+ const result = await fetch (
113
+ "easyops.custom_api@test" ,
114
+ true ,
108
115
{
109
116
url : "api/gateway/easyops.api.cmdb.cmdb_object.ListObjectBasic@1.1.0/object_basic" ,
110
117
originalUri : "/object_basic" ,
@@ -116,13 +123,15 @@ describe("fetch", () => {
116
123
a : "a" ,
117
124
c : "c" ,
118
125
b : "b" ,
119
- } ,
120
- ] ) ;
126
+ }
127
+ ) ;
121
128
122
129
expect ( result ) . toEqual ( expected ) ;
123
130
expect ( fakeFetch ) . toHaveBeenCalledTimes ( 1 ) ;
124
131
125
- const result2 = await fetch ( "easyops.custom_api@test" , true , [
132
+ const result2 = await fetch (
133
+ "easyops.custom_api@test" ,
134
+ true ,
126
135
{
127
136
isFileType : false ,
128
137
responseWrapper : true ,
@@ -134,8 +143,8 @@ describe("fetch", () => {
134
143
c : "c" ,
135
144
b : "b" ,
136
145
a : "a" ,
137
- } ,
138
- ] ) ;
146
+ }
147
+ ) ;
139
148
140
149
// use cached response data
141
150
expect ( result2 ) . toEqual ( expected ) ;
@@ -160,4 +169,79 @@ describe("fetch", () => {
160
169
expect ( result3 ) . toEqual ( expected ) ;
161
170
expect ( fakeFetch ) . toHaveBeenCalledTimes ( 2 ) ;
162
171
} ) ;
172
+
173
+ it ( "should resolve normal provider by cache" , async ( ) => {
174
+ const result = await fetch (
175
+ "providers-of-cmbd.a" ,
176
+ true ,
177
+ ...[
178
+ "_DASHBOARD_PROVIDER" ,
179
+ {
180
+ fields : {
181
+ provider : 1 ,
182
+ args : 1 ,
183
+ name : 1 ,
184
+ instanceId : 1 ,
185
+ transform : 1 ,
186
+ } ,
187
+ query : {
188
+ $or : [
189
+ {
190
+ name : {
191
+ $like : "%alexabcdes%" ,
192
+ } ,
193
+ } ,
194
+ {
195
+ provider : {
196
+ $like : "%alexabcdes%" ,
197
+ } ,
198
+ } ,
199
+ ] ,
200
+ } ,
201
+ page : 1 ,
202
+ page_size : 30 ,
203
+ } ,
204
+ { } ,
205
+ ]
206
+ ) ;
207
+
208
+ expect ( result ) . toEqual ( "resolved" ) ;
209
+ expect ( fakeRevolve ) . toHaveBeenCalledTimes ( 1 ) ;
210
+
211
+ const result2 = await fetch (
212
+ "providers-of-cmbd.a" ,
213
+ true ,
214
+ "_DASHBOARD_PROVIDER" ,
215
+ {
216
+ fields : {
217
+ provider : 1 ,
218
+ args : 1 ,
219
+ name : 1 ,
220
+ instanceId : 1 ,
221
+ transform : 1 ,
222
+ } ,
223
+ query : {
224
+ $or : [
225
+ {
226
+ name : {
227
+ $like : "%alexabcdes%" ,
228
+ } ,
229
+ } ,
230
+ {
231
+ provider : {
232
+ $like : "%alexabcdes%" ,
233
+ } ,
234
+ } ,
235
+ ] ,
236
+ } ,
237
+ page : 1 ,
238
+ page_size : 30 ,
239
+ } ,
240
+ { }
241
+ ) ;
242
+
243
+ // cached response data
244
+ expect ( result2 ) . toEqual ( "resolved" ) ;
245
+ expect ( fakeRevolve ) . toHaveBeenCalledTimes ( 1 ) ;
246
+ } ) ;
163
247
} ) ;
0 commit comments