11import { parseRequest , matchUrl } from './util' ;
2+ import Response from './response' ;
23
34class FetchMock {
45 constructor ( required ) {
@@ -26,8 +27,12 @@ class FetchMock {
2627 loadMock ( key , mock ) {
2728 if ( 'object' !== typeof mock ) {
2829 if ( 'function' === typeof mock ) {
30+ var items = key . split ( ' ' ) ;
31+ var method = items . length === 2 ? items [ 0 ] : 'GET' ;
32+ var url = items . length === 2 ? items [ 1 ] : key ;
2933 this . urls . push ( {
30- url : key ,
34+ method,
35+ url,
3136 func : mock ,
3237 } ) ;
3338 }
@@ -43,7 +48,7 @@ class FetchMock {
4348 let insideParams ;
4449 const filters = this . urls . filter ( uri => {
4550 const obj = matchUrl ( uri . url , request . url ) ;
46- if ( obj . result ) {
51+ if ( obj . result && uri . method . toUpperCase ( ) === request . method . toUpperCase ( ) ) {
4752 insideParams = obj . params ;
4853 return true ;
4954 }
@@ -62,11 +67,13 @@ class FetchMock {
6267 if ( 'function' !== typeof mock . func ) {
6368 throw new Error ( 'There is no url defined in __mocks__' ) ;
6469 }
65- const promise = mock . func ( request ) ;
66- if ( ! promise || 'function ' !== typeof promise . then ) {
67- throw new Error ( 'The result of mock function should be a promise .' ) ;
70+ const obj = mock . func ( request ) ;
71+ if ( ! obj || 'object ' !== typeof obj ) {
72+ throw new Error ( 'The result of mock function should be an object .' ) ;
6873 }
69- return promise ;
74+
75+ const response = new Response ( obj ) ;
76+ return Promise . resolve ( response ) ;
7077 }
7178}
7279
0 commit comments