Skip to content

Commit

Permalink
Merged PR#5 (Removed delay from tests without cancellation.)
Browse files Browse the repository at this point in the history
  • Loading branch information
Loop54 Publisher committed Jan 21, 2019
1 parent 009a0c7 commit 0655c5e
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 37 deletions.
5 changes: 4 additions & 1 deletion src/core.js
Expand Up @@ -84,7 +84,10 @@ let core = {
return Promise.reject(ret);
});

request.cancel = () => cancellationSource.cancel();
request.cancel = () => {
cancellationSource.cancel();
return request;
}

if (callback) {
request.then(callback).catch(function(response){
Expand Down
122 changes: 122 additions & 0 deletions test/cancellation.js
@@ -0,0 +1,122 @@
import searchResponse from "./mocks/search-response-ok";
import nock from "nock";
import { expect } from "chai";
import sinon from "sinon";
import common from "./common";

module.exports = function () {

const client = Loop54.getClient(common.endpoint);
let apiReplyCallback = null;

const checkValidResponse = (response) => {
// check for the `cancelled` response flag to not be set.
expect(response.cancelled).to.be.undefined;
// check for a 200 http response code.
expect(response.status).to.equal(200);
// check for the response body.
expect(response.data).to.eql(searchResponse);
}

const checkCancelledResponse = (response) => {
// check for the `cancelled` response body flag to be set.
expect(response).to.eql({ cancelled: true });
}

const apiReply = (cb) => {
// pass error, http response [code and body] back to nock interceptor.
cb(null, [200, searchResponse]);
};

// mock all calls to the /search endpoint
beforeEach(() => {
// cleanup
nock.cleanAll();

// reset the `apiReplyCallback` handler.
apiReplyCallback = null;

// setup nock with response callback.
nock(common.endpoint).post("/search").reply((uri, requestBody, cb) => {
// trigger request cancel and api response via callback cb.
apiReplyCallback(cb);
});
});

it("Cancellation works when cancel() called on a request with callback", function (done) {
// setup nock reply handler which also calls req.cancel()
apiReplyCallback = (cb) => {
req.cancel();
apiReply(cb);
}

// setup a callback spy
const callback = sinon.spy();

// create a request
const req = client.search("meat", callback);

// check the response when finished.
req.then(() => {
// check if we have a cancelled response.
checkCancelledResponse(callback.firstCall.lastArg);

// signal async test is done.
done();
});
});

it("Cancellation works when cancel() called on a request without a callback", function (done) {
// setup nock reply handler which also calls req.cancel()
apiReplyCallback = (cb) => {
req.cancel();
apiReply(cb);
}

// create a request
const req = client.search("meat");

// check the response when finished.
req.then((response) => {
// check if we have a cancelled response.
checkCancelledResponse(response);

// signal async test is done.
done();
});
});

it("No cancellation when cancel() not called on a request with callback", function (done) {
// setup a nock reply handler which does not call req.cancel().
apiReplyCallback = apiReply;

// setup a callback spy
const callback = sinon.spy();

// create a request
const req = client.search("meat", callback);

// check the response when finished.
req.then(() => {
// also check if the response is valid
checkValidResponse(callback.firstCall.lastArg);

// signal async test is done.
done();
});
});

it("No cancellation when cancel() not called on a request without a callback", function (done) {
// setup a nock reply handler which does not call req.cancel().
apiReplyCallback = apiReply;

// trigger a request
client.search("meat").then((response) => {
// also check if the response is valid
checkValidResponse(response);

// signal async test is done.
done();
});
});
}
3 changes: 3 additions & 0 deletions test/lib-test.js
Expand Up @@ -9,6 +9,7 @@ import getRelatedEntities from "./getRelatedEntities"
import getEntitiesByAttribute from "./getEntitiesByAttribute"
import createEvents from "./createEvents"
import sync from "./sync"
import cancellation from "./cancellation";

describe("Loop54", function () {

Expand All @@ -22,4 +23,6 @@ describe("Loop54", function () {
describe("client.getEntitiesByAttribute", getEntitiesByAttribute);
describe("client.createEvents", createEvents);
describe("client.sync", sync);

describe("request.cancel", cancellation);
});
37 changes: 1 addition & 36 deletions test/search.js
Expand Up @@ -12,7 +12,7 @@ module.exports = function () {

//mock all calls to the /search endpoint
beforeEach(() => {
nock(common.endpoint).post("/search").delay(100).reply(200, searchResponse);
nock(common.endpoint).post("/search").reply(200, searchResponse);
});

var searchOKFunc = function(response) {
Expand Down Expand Up @@ -51,39 +51,4 @@ module.exports = function () {
it("Returns error if invalid search query, with callback", function (done) {
client.search("",response => common.testCallBack(response,common.includesError,done));
});

[true,false].forEach(function(doCancel){
it("Cancellation (doCancel=" + doCancel + ") works, without callback", function () {

var wasRun = false;

var req = client.search("meat");

var promiseToReturn = req
.then(function(response){ if(!response.cancelled) wasRun=true; })
.then(function() { expect(wasRun).to.equal(!doCancel); })

if(doCancel)
req.cancel();

return promiseToReturn;
});


it("Cancellation (doCancel=" + doCancel + ") works, with callback", function (done) {

var wasRun = false;

var req = client.search("meat",function(response){ if(!response.cancelled) wasRun = true; });

if(doCancel)
req.cancel();

setTimeout(function(){
expect(wasRun).to.equal(!doCancel);
done();
},1000);
});

});
}

0 comments on commit 0655c5e

Please sign in to comment.