Skip to content

Commit 611498e

Browse files
authored
allow to pass delay option as a promise (#153)
close #152
1 parent d594dce commit 611498e

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed

api.md

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
### Table of Contents
44

5-
- [MockServer][1]
6-
- [init][2]
7-
- [Parameters][3]
8-
- [Examples][4]
9-
- [InitOptions][5]
10-
- [Properties][6]
11-
- [MockRequest][7]
12-
- [on][8]
13-
- [Parameters][9]
14-
- [Examples][10]
15-
- [ResponseOptions][11]
16-
- [Properties][12]
5+
* [MockServer][1]
6+
* [init][2]
7+
* [Parameters][3]
8+
* [Examples][4]
9+
* [InitOptions][5]
10+
* [Properties][6]
11+
* [MockRequest][7]
12+
* [on][8]
13+
* [Parameters][9]
14+
* [Examples][10]
15+
* [ResponseOptions][11]
16+
* [Properties][12]
1717

1818
## MockServer
1919

@@ -23,8 +23,8 @@ Init mock server and set request interception on the page
2323

2424
#### Parameters
2525

26-
- `page` **[Object][13]** Puppeteer's page
27-
- `options` **[InitOptions][14]** init options (optional, default `{}`)
26+
* `page` **[Object][13]** Puppeteer's page
27+
* `options` **[InitOptions][14]** init options (optional, default `{}`)
2828

2929
#### Examples
3030

@@ -43,27 +43,27 @@ const mockRequest = await mockServer.init(page, {
4343
// now you can use `mockRequest` in your tests
4444
```
4545

46-
Returns **[Promise][15]<[MockRequest][16]>**
46+
Returns **[Promise][15]<[MockRequest][16]>**
4747

4848
## InitOptions
4949

5050
Type: [Object][13]
5151

5252
### Properties
5353

54-
- `baseAppUrl` **[string][17]** Base app url. By default all requests matching
54+
* `baseAppUrl` **[string][17]** Base app url. By default all requests matching
5555
base app url are continued.
56-
- `baseApiUrl` **[string][17]** Base api url. By default all requests matching
56+
* `baseApiUrl` **[string][17]** Base api url. By default all requests matching
5757
base api url are responded with 200 status and empty body, but you will see a
5858
warning in output.
59-
- `onRequest` **function (PuppeteerRequest)** Optional callback to be
59+
* `onRequest` **function (PuppeteerRequest)** Optional callback to be
6060
executed for any unhandled request. By default requests are aborted if this
6161
callback is not provided or returns falsy.
62-
- `onAppRequest` **function (PuppeteerRequest)** Optional callback to be
62+
* `onAppRequest` **function (PuppeteerRequest)** Optional callback to be
6363
executed for any unhandled app request, i.e. request matching `baseAppUrl`
6464
option. By default requests are continued if this callback is not provided or
6565
returns falsy.
66-
- `onApiRequest` **function (PuppeteerRequest)** Optional callback to be
66+
* `onApiRequest` **function (PuppeteerRequest)** Optional callback to be
6767
executed for any unhandled api request, i.e. request matching `baseApiUrl`
6868
option. By default requests are responded with `200 OK {}` for convenience if
6969
this callback is not provided or returns falsy.
@@ -80,33 +80,30 @@ Set expected mock response for request. There are also shortcuts `.get()`,
8080

8181
#### Parameters
8282

83-
- `method` **[string][17]** request HTTP method
84-
- `endpoint` **[string][17]** request endpoint URL. If relative URL is passed,
83+
* `method` **[string][17]** request HTTP method
84+
* `endpoint` **[string][17]** request endpoint URL. If relative URL is passed,
8585
it's considered as a request to api **relative** to baseApiUrl.
86-
- `status` **[number][18]** response status code
87-
- `response` **[ResponseOptions][19]** additional response options
86+
* `status` **[number][18]** response status code
87+
* `response` **[ResponseOptions][19]** additional response options
8888

8989
#### Examples
9090

9191
Handle request to relative endpoint
9292

93-
9493
```javascript
9594
const responseConfig = {body: {result: 'ok'}};
9695
mockRequest.on('get', 'account', 200, responseConfig);
9796
```
9897

9998
Using shortcut method and absolute url
10099

101-
102100
```javascript
103101
const responseConfig = {body: {result: 'not found'}};
104102
mockRequest.get('https://example.com/test', 404, responseConfig);
105103
```
106104

107105
Simulate request timeout
108106

109-
110107
```javascript
111108
mockRequest.post('search', null, {abort: 'timedout', delay: 10000});
112109
```
@@ -119,10 +116,10 @@ Type: [Object][13]
119116

120117
### Properties
121118

122-
- `body` **[Object][13]** response body
123-
- `delay` **[number][18]?** delay response for N milliseconds
124-
- `abort` **[string][17]?** abort request with supplied error code
125-
- `contentType` **[string][17]?** content type. Defaults to
119+
* `body` **[Object][13]** response body
120+
* `delay` **([Promise][15] | [number][18])?** delay response for N milliseconds or until promise is resolved
121+
* `abort` **[string][17]?** abort request with supplied error code
122+
* `contentType` **[string][17]?** content type. Defaults to
126123
`application/json`.
127124

128125
[1]: #mockserver

src/handle-request.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ export default async function handleRequest(
4545
let {body} = options;
4646
body = isFunction(body) ? body(request) : body;
4747

48-
if (options.delay) {
49-
await sleep(options.delay);
48+
const delay = options.delay;
49+
if (delay) {
50+
await delay.then ? delay : sleep(delay);
5051
}
5152

5253
if (options.abort) {

src/handle-request.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ describe('handle request', () => {
102102
expect(request.respond).toHaveBeenCalled();
103103
});
104104

105+
test('with delay if delay promise option provided', async () => {
106+
let delayResolve;
107+
const delay = new Promise((resolve)=> {
108+
delayResolve = resolve;
109+
});
110+
const handlers = [
111+
{
112+
method: 'get',
113+
endpoint,
114+
options: {delay},
115+
},
116+
];
117+
118+
request.url.mockReturnValue(endpoint);
119+
request.method.mockReturnValue('GET');
120+
121+
delayResolve();
122+
await handleRequest(request, config, handlers);
123+
124+
expect(request.respond).toHaveBeenCalled();
125+
});
126+
105127
test('if there is no match, but url starts with base api url', () => {
106128
request.url.mockReturnValue(endpoint);
107129
request.method.mockReturnValue('GET');

src/mock-request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default function MockRequest(
5656
/**
5757
* @typedef {Object} ResponseOptions
5858
* @property {Object} body response body
59-
* @property {number=} delay delay response for N milliseconds
59+
* @property {Promise|number=} delay delay response for N milliseconds or until promise is resolved
6060
* @property {string=} abort abort request with supplied error code
6161
* @property {string} [contentType] content type. Defaults to
6262
* `application/json`.

0 commit comments

Comments
 (0)