Skip to content

Commit 6fb9e2f

Browse files
committed
Support delay mock
1 parent 8b54923 commit 6fb9e2f

File tree

8 files changed

+83
-21
lines changed

8 files changed

+83
-21
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ So, I create one by myself.
1414
- [x] Combined with Mock.js
1515
- [x] Support exclude for some other path
1616
- [x] Proxy for other api server
17+
- [x] Delay for global and specific path
1718
- [ ] Support RAP system
1819

1920
## Usage
@@ -69,6 +70,7 @@ import FetchMock from 'react-fetch-mock';
6970
if (__dev__) {
7071
// attention: mocks file should be under `src/`
7172
window.fetch = new FetchMock(require('path/to/mocks/directory'), {
73+
delay: 200, // 200ms
7274
fetch: window.fetch,
7375
exclude: [
7476
'http://www.google.com',
@@ -87,6 +89,7 @@ if (__dev__) {
8789
// if __dev__ is true, it will back the data you defined in mock directory
8890
fetch('/api/path', options);
8991
fetch('/api/path', {
92+
delay: 2000, // /api/path will delayed after 2000ms. Most of suitation, this won't be used usually.
9093
method: 'POST',
9194
headers: {
9295
'Content-Type': 'application/json',

dist/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ var FetchMock = function () {
3737
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
3838
fetch: function fetch() {},
3939
exclude: [],
40-
proxy: []
40+
proxy: [],
41+
delay: 2000 // ms
4142
};
4243

4344
_classCallCheck(this, FetchMock);
@@ -50,6 +51,7 @@ var FetchMock = function () {
5051
this.raw = options.fetch;
5152
this.exclude = options.exclude || [];
5253
this.proxy = options.proxy || [];
54+
this.delayTime = options.delay;
5355

5456
this.loadMocks = this.loadMocks.bind(this);
5557
this.loadMock = this.loadMock.bind(this);
@@ -189,7 +191,10 @@ var FetchMock = function () {
189191
}
190192

191193
var response = new _response2.default(obj);
192-
return Promise.resolve(response);
194+
var delayTime = options.delay || this.delayTime || 0;
195+
return (0, _util.delay)(delayTime).then(function () {
196+
return Promise.resolve(response);
197+
});
193198
}
194199
}]);
195200

dist/util.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,17 @@ var matchUrl = function matchUrl(sourceUrl, targetUrl) {
132132
};
133133
};
134134

135+
var delay = function delay(duration) {
136+
return new Promise(function (resolve) {
137+
setTimeout(function () {
138+
resolve();
139+
}, duration);
140+
});
141+
};
142+
135143
exports.isNull = isNull;
136144
exports.prueUrl = prueUrl;
137145
exports.parseUrl = parseUrl;
138146
exports.parseRequest = parseRequest;
139-
exports.matchUrl = matchUrl;
147+
exports.matchUrl = matchUrl;
148+
exports.delay = delay;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-fetch-mock",
3-
"version": "0.5.4",
3+
"version": "0.6.0",
44
"description": "fetch mock for reactjs",
55
"main": "index.js",
66
"scripts": {

src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import pathToRegexp from 'path-to-regexp';
2-
import { parseRequest, matchUrl, isNull } from './util';
2+
import { parseRequest, matchUrl, isNull, delay } from './util';
33
import Response from './response';
44

55
class FetchMock {
66
constructor(required, options = {
77
fetch: () => {},
88
exclude: [],
99
proxy: [],
10+
delay: 2000, // ms
1011
}) {
1112
if ('object' !== typeof required) {
1213
throw new Error('There is no required defined.');
@@ -16,6 +17,7 @@ class FetchMock {
1617
this.raw = options.fetch;
1718
this.exclude = options.exclude || [];
1819
this.proxy = options.proxy || [];
20+
this.delayTime = options.delay;
1921

2022
this.loadMocks = this.loadMocks.bind(this);
2123
this.loadMock = this.loadMock.bind(this);
@@ -106,7 +108,7 @@ class FetchMock {
106108
return proxied.process ? proxied.process(proxied, matches) : `${proxied.target}/${matches[1]}`;
107109
}
108110

109-
fetch(url, options) {
111+
fetch(url, options = {}) {
110112
// using proxy
111113
if (this.isProxied(url)) {
112114
url = this.proxied(url);
@@ -137,7 +139,8 @@ class FetchMock {
137139
}
138140

139141
const response = new Response(obj);
140-
return Promise.resolve(response);
142+
const delayTime = options.delay || this.delayTime || 0;
143+
return delay(delayTime).then(() => Promise.resolve(response));
141144
}
142145
}
143146

src/util.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const matchUrl = (sourceUrl, targetUrl) => {
8888
const targetUrlWithoutProctol = removeProctol(targetUrl);
8989
const sourceUrlSplits = sourceUrlWithoutProctol.split('/');
9090
const targetUrlSplits = targetUrlWithoutProctol.split('/');
91-
91+
9292
if (sourceUrlSplits.length !== targetUrlSplits.length) {
9393
return {
9494
result: false,
@@ -104,7 +104,7 @@ const matchUrl = (sourceUrl, targetUrl) => {
104104
}
105105

106106
if (sourceUrlSplit.startsWith('{') && sourceUrlSplit.endsWith('}')) {
107-
if (sourceUrlSplit.replace(/[^{]/g,'').length > 1 || sourceUrlSplit.replace(/[^}]/g,'').length > 1) {
107+
if (sourceUrlSplit.replace(/[^{]/g, '').length > 1 || sourceUrlSplit.replace(/[^}]/g, '').length > 1) {
108108
return {
109109
result: false,
110110
};
@@ -124,10 +124,19 @@ const matchUrl = (sourceUrl, targetUrl) => {
124124
};
125125
};
126126

127+
const delay = (duration) => {
128+
return new Promise(resolve => {
129+
setTimeout(() => {
130+
resolve();
131+
}, duration);
132+
});
133+
};
134+
127135
export {
128136
isNull,
129137
prueUrl,
130138
parseUrl,
131139
parseRequest,
132140
matchUrl,
141+
delay,
133142
};

test/index.test.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import 'babel-polyfill';
22
import expect from 'expect.js';
3-
import FetchMock, { Mock } from '../';
3+
import FetchMock, { Mock } from '../src';
44

55
const fetch = new FetchMock(require('../__mocks__'), {
6+
delay: 2000, // delay 5s
67
fetch: require('isomorphic-fetch'),
78
exclude: [
89
'https://www.amazon.com',
@@ -28,7 +29,7 @@ describe('test fetch mock', () => {
2829
expect(data).not.to.be.empty();
2930
expect(data).to.be.an('array');
3031
expect(data).to.have.length(2);
31-
});
32+
}).timeout(20000);
3233

3334
it('fetch /api/users?a=b', async () => {
3435
const response = await fetch('/api/users');
@@ -39,7 +40,7 @@ describe('test fetch mock', () => {
3940
expect(data).not.to.be.empty();
4041
expect(data).to.be.an('array');
4142
expect(data).to.have.length(2);
42-
});
43+
}).timeout(20000);
4344

4445
it('fetch /api/users with url parameters', async () => {
4546
const response = await fetch('/api/users?name=John');
@@ -50,7 +51,7 @@ describe('test fetch mock', () => {
5051
expect(data).not.to.be.empty();
5152
expect(data).to.be.an('array');
5253
expect(data).to.have.length(1);
53-
});
54+
}).timeout(20000);
5455

5556
it('fetch /api/users with post parameters', async () => {
5657
const response = await fetch('/api/users', {
@@ -69,7 +70,7 @@ describe('test fetch mock', () => {
6970
expect(data).not.to.be.empty();
7071
expect(data).to.be.an('array');
7172
expect(data).to.have.length(1);
72-
});
73+
}).timeout(20000);
7374

7475
it('fetch /api/users/{userId}', async () => {
7576
const response = await fetch('/api/users/123');
@@ -79,7 +80,7 @@ describe('test fetch mock', () => {
7980
expect(data).not.to.be(undefined);
8081
expect(data).not.to.be.empty();
8182
expect(data).to.be.property('userId', '123');
82-
});
83+
}).timeout(20000);
8384

8485
it('fetch /api/users/mockjs with mockjs', async () => {
8586
const response = await fetch('/api/users/mockjs');
@@ -90,7 +91,7 @@ describe('test fetch mock', () => {
9091
expect(data).not.to.be.empty();
9192
expect(data).to.be.an('array');
9293
expect(data).to.have.length(2);
93-
});
94+
}).timeout(20000);
9495

9596
it('fetch /api/users/mockjs with mockjs', async () => {
9697
const response = await fetch('/api/users/mockjs');
@@ -101,7 +102,7 @@ describe('test fetch mock', () => {
101102
expect(data).not.to.be.empty();
102103
expect(data).to.be.an('array');
103104
expect(data).to.have.length(2);
104-
});
105+
}).timeout(20000);
105106

106107
it('fetch /api/users/{userid} with prue data response', async () => {
107108
const response = await fetch('/api/users/pru/121');
@@ -112,7 +113,7 @@ describe('test fetch mock', () => {
112113
expect(data).not.to.be.empty();
113114
expect(data).to.be.an('object');
114115
expect(data).to.be.property('userId', '121');
115-
});
116+
}).timeout(20000);
116117

117118
it('fetch exclude path', async () => {
118119
const response = await fetch('https://www.amazon.com');
@@ -135,7 +136,7 @@ describe('test fetch mock', () => {
135136
}),
136137
});
137138
expect(status).to.be.eql(201);
138-
});
139+
}).timeout(20000);
139140

140141
it('put /api/users/123', async () => {
141142
const response = await fetch('/api/users/123', {
@@ -154,7 +155,7 @@ describe('test fetch mock', () => {
154155
expect(data).not.to.be.empty();
155156
expect(data).to.be.an('object');
156157
expect(data.userId).to.be.eql(123);
157-
});
158+
}).timeout(20000);
158159

159160
it('proxy other api server', async () => {
160161
const response = await fetch('/ip/?format=json');
@@ -165,5 +166,31 @@ describe('test fetch mock', () => {
165166
expect(data).not.to.be.empty();
166167
expect(data).to.be.an('object');
167168
expect(data.ip).to.be.an('string');
169+
}).timeout(20000);
170+
171+
describe('test delay mock', () => {
172+
173+
it('global delay', async () => {
174+
const start = new Date().getTime();
175+
await fetch('/api/users');
176+
expect(new Date().getTime() - start).to.greaterThan(2000);
177+
}).timeout(20000);
178+
179+
it('method delay 30ms', async () => {
180+
const start = new Date().getTime();
181+
await fetch('/api/users', {
182+
delay: 30,
183+
});
184+
expect(new Date().getTime() - start).to.greaterThan(30);
185+
}).timeout(20000);
186+
187+
it('method delay 3000ms', async () => {
188+
const start = new Date().getTime();
189+
await fetch('/api/users', {
190+
delay: 3000,
191+
});
192+
expect(new Date().getTime() - start).to.greaterThan(3000);
193+
}).timeout(20000);
194+
168195
});
169196
});

test/util.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'babel-polyfill';
22
import expect from 'expect.js';
3-
import { prueUrl, parseUrl, parseRequest, matchUrl } from '../src/util';
3+
import { prueUrl, parseUrl, parseRequest, matchUrl, delay } from '../src/util';
44

55
describe('test util methods', () => {
66
it('get prue url', async () => {
@@ -104,4 +104,10 @@ describe('test util methods', () => {
104104
result: false,
105105
});
106106
});
107+
108+
it('delay 5000ms', async () => {
109+
const start = new Date().getTime();
110+
await delay(5000);
111+
expect(new Date().getTime() - start).to.greaterThan(5000);
112+
}).timeout(20000);
107113
});

0 commit comments

Comments
 (0)