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' ;
1
2
2
3
class FetchMock {
3
4
constructor ( required ) {
@@ -38,7 +39,7 @@ class FetchMock {
38
39
}
39
40
40
41
fetch ( url , options ) {
41
- const filters = this . urls . filter ( uri => uri . url === url ) ;
42
+ const filters = this . urls . filter ( uri => uri . url === prueUrl ( url ) ) ;
42
43
if ( ! filters || filters . length == 0 ) throw new Error ( `No url ${ url } is defined.` ) ;
43
44
const mock = filters [ 0 ] ;
44
45
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', () => {
12
12
expect ( data ) . to . have . length ( 2 ) ;
13
13
} ) ;
14
14
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
+
15
23
it ( 'fetch /api/users with parameters' , async ( ) => {
16
24
const { data } = await fetch ( '/api/users' , {
17
25
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