Skip to content

Commit

Permalink
Keeping Linter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
prashanth-92 authored and marcbachmann committed Sep 11, 2023
1 parent d7997d6 commit b09456d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ function handleRequest(mockAdapter, resolve, reject, config) {
}
}

function getEffectiveDelay(adapter, handler){
function getEffectiveDelay(adapter, handler) {
var delayPerRequest;
if(handler.length === 8){
if (handler.length === 8) {
delayPerRequest = handler[7];
}
return delayPerRequest ? delayPerRequest : adapter.delayResponse;
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ VERBS.concat("any").forEach(function (method) {
return _this;
}

function withDelayInMs(delay){
return (code, response, headers) => replyWithDelay(delay, code, response, headers);
function withDelayInMs(delay) {
return function (code, response, headers) {
replyWithDelay(delay, code, response, headers);
};
}

function replyOnce(code, response, headers) {
Expand All @@ -119,7 +121,7 @@ VERBS.concat("any").forEach(function (method) {
reply: reply,

replyOnce: replyOnce,

withDelayInMs: withDelayInMs,

passThrough: function passThrough() {
Expand Down
48 changes: 24 additions & 24 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,61 +555,61 @@ describe("MockAdapter basics", function () {
expect(message).to.equal("error");
});
});

it("allows delay in millsecond per request", function () {
mock = new MockAdapter(instance);
const start = new Date().getTime();
const firstDelay = 100;
const secondDelay = 500;
const success = 200;
var start = new Date().getTime();
var firstDelay = 100;
var secondDelay = 500;
var success = 200;

const fooOnDelayResponds = mock.onGet("/foo").withDelayInMs(firstDelay);
var fooOnDelayResponds = mock.onGet("/foo").withDelayInMs(firstDelay);
fooOnDelayResponds(success);
const barOnDelayResponds = mock.onGet("/bar").withDelayInMs(secondDelay);
var barOnDelayResponds = mock.onGet("/bar").withDelayInMs(secondDelay);
barOnDelayResponds(success);

return Promise.all([
instance.get("/foo").then(function (response) {
const end = new Date().getTime();
const totalTime = end - start;
var end = new Date().getTime();
var totalTime = end - start;

expect(response.status).to.equal(success);
expect(totalTime).greaterThanOrEqual(firstDelay);
}),
instance.get("/bar").then(function (response) {
const end = new Date().getTime();
const totalTime = end - start;
var end = new Date().getTime();
var totalTime = end - start;

expect(response.status).to.equal(success);
expect(totalTime).greaterThanOrEqual(secondDelay);
})
]);
});

it("overrides global delay if request per delay is provided and respects global delay if otherwise", function () {
const start = new Date().getTime();
const requestDelay = 100;
const globalDelay = 500;
const success = 200;
var start = new Date().getTime();
var requestDelay = 100;
var globalDelay = 500;
var success = 200;
mock = new MockAdapter(instance, { delayResponse: globalDelay });
const fooOnDelayResponds = mock.onGet("/foo").withDelayInMs(requestDelay);

var fooOnDelayResponds = mock.onGet("/foo").withDelayInMs(requestDelay);
fooOnDelayResponds(success);
mock.onGet("/bar").reply(success);

return Promise.all([
instance.get("/foo").then(function (response) {
const end = new Date().getTime();
const totalTime = end - start;
var end = new Date().getTime();
var totalTime = end - start;

expect(response.status).to.equal(success);
expect(totalTime).greaterThanOrEqual(requestDelay);
//Ensure global delay is not applied
expect(totalTime).lessThan(globalDelay);
}),
instance.get("/bar").then(function (response) {
const end = new Date().getTime();
const totalTime = end - start;
var end = new Date().getTime();
var totalTime = end - start;

expect(response.status).to.equal(success);
expect(totalTime).greaterThanOrEqual(globalDelay);
Expand Down

0 comments on commit b09456d

Please sign in to comment.