@@ -8,16 +8,28 @@ type Method = "get" | "post" | "patch" | "delete"
88
99type PartialAxiosResponse = Pick < AxiosResponse , "data" | "status" >
1010
11- const alwaysError = (
12- method : string ,
11+ type RequestMaker = (
12+ method : Method ,
1313 url : string ,
14- _body ?: unknown ,
15- ) : Promise < PartialAxiosResponse > => {
14+ body ?: unknown ,
15+ ) => Promise < PartialAxiosResponse >
16+
17+ const alwaysError : RequestMaker = ( method , url , _body ) => {
1618 const msg = `No response specified for ${ method } ${ url } `
1719 console . error ( msg )
1820 throw new Error ( msg )
1921}
2022
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+
2133/**
2234 * A jest mock function that makes fake network requests.
2335 *
@@ -29,17 +41,23 @@ const alwaysError = (
2941 * '/some/url/to/thing',
3042 * expect.objectContaining({ some: 'value' }) // request body
3143 * ])
44+ *
45+ * NOTE: URLs called by this function are first
3246 * ```
3347 */
3448const makeRequest = jest . fn ( alwaysError )
49+ const makeSortedRequest : RequestMaker = ( method , url , body ) =>
50+ makeRequest ( method , standardizeUrl ( url ) , body )
3551
3652const 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+ ) ,
3957 patch : jest . fn ( ( url : string , body : unknown ) =>
40- makeRequest ( "patch" , url , body ) ,
58+ makeSortedRequest ( "patch" , url , body ) ,
4159 ) ,
42- delete : jest . fn ( ( url : string ) => makeRequest ( "delete" , url , undefined ) ) ,
60+ delete : jest . fn ( ( url : string ) => makeSortedRequest ( "delete" , url , undefined ) ) ,
4361 request : jest . fn (
4462 (
4563 {
@@ -57,7 +75,11 @@ const mockAxiosInstance = {
5775 // on object shape.
5876 const deserialized =
5977 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+ )
6183 } ,
6284 ) ,
6385 defaults : { } , // OpenAPI Generator accesses this, so it needs to exist
@@ -72,24 +94,16 @@ const expectAnythingOrNil = expect.toBeOneOf([
7294 expect . toBeNil ( ) ,
7395] )
7496
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-
8497const mockRequest = < T , U > (
8598 method : Method ,
8699 url : string ,
87100 requestBody : T = expectAnythingOrNil ,
88101 responseBody : U | ( ( req : T ) => U ) | undefined = undefined ,
89102 code : number ,
90103) => {
104+ const urlMatcher = typeof url === "string" ? standardizeUrl ( url ) : url
91105 when ( makeRequest )
92- . calledWith ( method , standardizeUrl ( url ) , requestBody )
106+ . calledWith ( method , urlMatcher , requestBody )
93107 . mockImplementation ( async ( ) => {
94108 let data
95109 if ( isFunction ( responseBody ) ) {
@@ -158,7 +172,7 @@ const setMockResponse = {
158172 { code = 200 , requestBody } : MockResponseOptions = { } ,
159173 ) => mockRequest ( "patch" , url , requestBody , responseBody , code ) ,
160174 /**
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.
162176 *
163177 * If `responseBody` is a Promise, the request will resolve to the value of
164178 * `responseBody` when `responseBody` resolves.
0 commit comments