@@ -8,16 +8,28 @@ type Method = "get" | "post" | "patch" | "delete"
8
8
9
9
type PartialAxiosResponse = Pick < AxiosResponse , "data" | "status" >
10
10
11
- const alwaysError = (
12
- method : string ,
11
+ type RequestMaker = (
12
+ method : Method ,
13
13
url : string ,
14
- _body ?: unknown ,
15
- ) : Promise < PartialAxiosResponse > => {
14
+ body ?: unknown ,
15
+ ) => Promise < PartialAxiosResponse >
16
+
17
+ const alwaysError : RequestMaker = ( method , url , _body ) => {
16
18
const msg = `No response specified for ${ method } ${ url } `
17
19
console . error ( msg )
18
20
throw new Error ( msg )
19
21
}
20
22
23
+ const standardizeUrl = ( url : string ) => {
24
+ if ( ! url . includes ( "?" ) ) {
25
+ return url
26
+ }
27
+ const [ path , queryString ] = url . split ( "?" )
28
+ const query = new URLSearchParams ( queryString )
29
+ query . sort ( )
30
+ return `${ path } ?${ query . toString ( ) } `
31
+ }
32
+
21
33
/**
22
34
* A jest mock function that makes fake network requests.
23
35
*
@@ -29,17 +41,23 @@ const alwaysError = (
29
41
* '/some/url/to/thing',
30
42
* expect.objectContaining({ some: 'value' }) // request body
31
43
* ])
44
+ *
45
+ * NOTE: URLs called by this function are first
32
46
* ```
33
47
*/
34
48
const makeRequest = jest . fn ( alwaysError )
49
+ const makeSortedRequest : RequestMaker = ( method , url , body ) =>
50
+ makeRequest ( method , standardizeUrl ( url ) , body )
35
51
36
52
const mockAxiosInstance = {
37
- get : jest . fn ( ( url : string ) => makeRequest ( "get" , url , undefined ) ) ,
38
- post : jest . fn ( ( url : string , body : unknown ) => makeRequest ( "post" , url , body ) ) ,
53
+ get : jest . fn ( ( url : string ) => makeSortedRequest ( "get" , url , undefined ) ) ,
54
+ post : jest . fn ( ( url : string , body : unknown ) =>
55
+ makeSortedRequest ( "post" , url , body ) ,
56
+ ) ,
39
57
patch : jest . fn ( ( url : string , body : unknown ) =>
40
- makeRequest ( "patch" , url , body ) ,
58
+ makeSortedRequest ( "patch" , url , body ) ,
41
59
) ,
42
- delete : jest . fn ( ( url : string ) => makeRequest ( "delete" , url , undefined ) ) ,
60
+ delete : jest . fn ( ( url : string ) => makeSortedRequest ( "delete" , url , undefined ) ) ,
43
61
request : jest . fn (
44
62
(
45
63
{
@@ -57,7 +75,11 @@ const mockAxiosInstance = {
57
75
// on object shape.
58
76
const deserialized =
59
77
typeof data === "string" ? JSON . parse ( data ) : undefined
60
- return makeRequest ( method . toLowerCase ( ) , url , deserialized )
78
+ return makeSortedRequest (
79
+ method . toLowerCase ( ) as Method ,
80
+ url ,
81
+ deserialized ,
82
+ )
61
83
} ,
62
84
) ,
63
85
defaults : { } , // OpenAPI Generator accesses this, so it needs to exist
@@ -72,24 +94,16 @@ const expectAnythingOrNil = expect.toBeOneOf([
72
94
expect . toBeNil ( ) ,
73
95
] )
74
96
75
- const standardizeUrl = < T > ( url : T ) => {
76
- if ( ! ( typeof url === "string" ) ) return url
77
- if ( ! url . includes ( "?" ) ) return url
78
- const [ path , queryString ] = url . split ( "?" )
79
- const query = new URLSearchParams ( queryString )
80
- query . sort ( )
81
- return `${ path } ?${ query . toString ( ) } `
82
- }
83
-
84
97
const mockRequest = < T , U > (
85
98
method : Method ,
86
99
url : string ,
87
100
requestBody : T = expectAnythingOrNil ,
88
101
responseBody : U | ( ( req : T ) => U ) | undefined = undefined ,
89
102
code : number ,
90
103
) => {
104
+ const urlMatcher = typeof url === "string" ? standardizeUrl ( url ) : url
91
105
when ( makeRequest )
92
- . calledWith ( method , standardizeUrl ( url ) , requestBody )
106
+ . calledWith ( method , urlMatcher , requestBody )
93
107
. mockImplementation ( async ( ) => {
94
108
let data
95
109
if ( isFunction ( responseBody ) ) {
@@ -158,7 +172,7 @@ const setMockResponse = {
158
172
{ code = 200 , requestBody } : MockResponseOptions = { } ,
159
173
) => mockRequest ( "patch" , url , requestBody , responseBody , code ) ,
160
174
/**
161
- * Set mock response for a PATCH request; default response status is 204.
175
+ * Set mock response for a DELETE request; default response status is 204.
162
176
*
163
177
* If `responseBody` is a Promise, the request will resolve to the value of
164
178
* `responseBody` when `responseBody` resolves.
0 commit comments