Skip to content

Commit 614b345

Browse files
committed
Ignore url parameters while compare
1 parent 0cfde6b commit 614b345

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { prueUrl } from './util';
12

23
class 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) {

src/util.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}

test/index.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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',

test/util.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
});

0 commit comments

Comments
 (0)