File tree Expand file tree Collapse file tree 4 files changed +30
-1
lines changed Expand file tree Collapse file tree 4 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 1+ import { prueUrl } from './util' ;
12
23class FetchMock {
34 constructor ( required ) {
@@ -38,7 +39,7 @@ class FetchMock {
3839 }
3940
4041 fetch ( url , options ) {
41- const filters = this . urls . filter ( uri => uri . url === url ) ;
42+ const filters = this . urls . filter ( uri => uri . url === prueUrl ( url ) ) ;
4243 if ( ! filters || filters . length == 0 ) throw new Error ( `No url ${ url } is defined.` ) ;
4344 const mock = filters [ 0 ] ;
4445 if ( 'function' !== typeof mock . func ) {
Original file line number Diff line number Diff line change 1+
2+ const prueUrl = ( url ) => {
3+ const index = url . indexOf ( '?' ) ;
4+ const result = index > - 1 ? url . substring ( 0 , index ) : url ;
5+ return result ;
6+ }
7+
8+ export {
9+ prueUrl ,
10+ }
Original file line number Diff line number Diff line change @@ -12,6 +12,14 @@ describe('test fetch mock', () => {
1212 expect ( data ) . to . have . length ( 2 ) ;
1313 } ) ;
1414
15+ it ( 'fetch /api/users?a=b' , async ( ) => {
16+ const { data } = await fetch ( '/api/users' ) ;
17+ expect ( data ) . not . to . be ( undefined ) ;
18+ expect ( data ) . not . to . be . empty ( ) ;
19+ expect ( data ) . to . be . an ( 'array' ) ;
20+ expect ( data ) . to . have . length ( 2 ) ;
21+ } ) ;
22+
1523 it ( 'fetch /api/users with parameters' , async ( ) => {
1624 const { data } = await fetch ( '/api/users' , {
1725 name : 'John' ,
Original file line number Diff line number Diff line change 1+ import 'babel-polyfill' ;
2+ import expect from 'expect.js' ;
3+ import { prueUrl } from '../src/util' ;
4+
5+ describe ( 'test util methods' , ( ) => {
6+ it ( 'get prue url' , async ( ) => {
7+ expect ( prueUrl ( 'http://www.baidu.com?' ) ) . to . eql ( 'http://www.baidu.com' ) ;
8+ expect ( prueUrl ( 'http://www.baidu.com?ab=23' ) ) . to . eql ( 'http://www.baidu.com' ) ;
9+ } ) ;
10+ } ) ;
You can’t perform that action at this time.
0 commit comments