Skip to content

Commit

Permalink
Restructure testfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Fröhlich A committed Oct 3, 2017
1 parent c1c7141 commit df49eb1
Showing 1 changed file with 75 additions and 74 deletions.
149 changes: 75 additions & 74 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,15 @@ describe('Main Entry point', function () {
runTestsWithApi(testApi, 'search?q=javascript', 'test');
});

describe('with no auth - JSON return-capable', function() {
describe('with no auth - JSON return-capable', function () {
const testApi = api('openlibrary.org');
runTestsWithApi(testApi, 'api/books?bibkeys=ISBN:0201558025,LCCN:93005405&format=json', 'unavailable', true);
});
});

/**
* Dynamic create a fetch callback.
*/
function fetchFn(testApi, availableUrl, unavailableUrl, options, callbacks) {
it("should callback when fetching (website available)", function (done) {
testApi.fetch(availableUrl, function (err, result) {
callbacks.cb(err, result);
done();
}, options);
})
it("should callback with error when fetching (website not available)", function (done) {
testApi.fetch(unavailableUrl, function (err, result) {
callbacks.noWebCb(err, result);
done();
}, options)
})
it("should callback when fetching cached result (website available)", function (done) {
testApi.__addToCache(availableUrl, true);
testApi.fetch(availableUrl, function (err, result) {
callbacks.cacheCb(err, result);
done();
}, options);
})
it("should callback when fetching cached result (website not available)", function (done) {
testApi.__addToCache(unavailableUrl, true);
testApi.fetch(unavailableUrl, function (err, result) {
callbacks.cacheNoWebCb(err, result);
done();
}, options);
})
}

// Run all tests
function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonReturnCapable=false) {
function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonReturnCapable = false) {

beforeEach(function () {
testApi.__clearCache();
Expand Down Expand Up @@ -164,17 +133,17 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
});
})
}

describe("With API method", () => {
it('should define an endpoint', (done) => {
let endpoint = testApi.defineEndpoint("search", "GET");
demand(endpoint).must.be.a.function();
endpoint({q: "javascript"}).then((resp) => {
endpoint({ q: "javascript" }).then((resp) => {
demand(resp).must.be.a.object();
demand(resp.cache).must.be.false();
});

endpoint({q: "html"}).then((resp) => {
endpoint({ q: "html" }).then((resp) => {
demand(resp).must.be.a.object();
demand(resp.cache).must.be.false();
done();
Expand All @@ -187,7 +156,7 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
testApi.get("search", "search");

demand(testApi.search).to.be.a.function();
testApi.search({q: "javascript"}).then((resp) => {
testApi.search({ q: "javascript" }).then((resp) => {
demand(resp).must.be.a.object();
demand(resp.cache).must.be.false();
done();
Expand All @@ -199,11 +168,11 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
testApi.fetch = (endpoint, cb, config) => {
demand(endpoint).to.equal("search");
demand(config.method).to.equal("POST");
cb(undefined, {test: true});
cb(undefined, { test: true });
};

testApi.post("create_search", "search")
testApi.create_search({id: 1234}).then(response => {
testApi.create_search({ id: 1234 }).then(response => {
demand(response.test).to.be.true();
done();
});
Expand All @@ -216,11 +185,11 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
testApi.fetch = (endpoint, cb, config) => {
demand(endpoint).to.equal("search/1234");
demand(config.method).to.equal("DELETE");
cb(undefined, {test: true});
cb(undefined, { test: true });
};

testApi.delete("create_search", "search/:id")
testApi.create_search({id: 1234}).then(response => {
testApi.create_search({ id: 1234 }).then(response => {
demand(response.test).to.be.true();
done();
});
Expand All @@ -233,34 +202,34 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
testApi.fetch = (endpoint, cb, config) => {
demand(endpoint).to.equal("search/1234/bogus/444");
demand(config.method).to.equal("PUT");
cb(undefined, {test: true});
cb(undefined, { test: true });
};

testApi.put("update_search", "search/:id/bogus/:test")
testApi.update_search({id: 1234, test: 444}).then(response => {
testApi.update_search({ id: 1234, test: 444 }).then(response => {
demand(response.test).to.be.true();
done();
});

testApi.fetch = original;
});

it("should 'PATCH'", (done) => {
it("should 'PATCH'", (done) => {
let original = testApi.fetch;
testApi.fetch = (endpoint, cb, config) => {
demand(endpoint).to.equal("search/1234/bogus/444");
demand(config.method).to.equal("PATCH");
cb(undefined, {test: true});
cb(undefined, { test: true });
};

testApi.patch("update_search", "search/:id/bogus/:test")
testApi.update_search({id: 1234, test: 444}).then(response => {
testApi.update_search({ id: 1234, test: 444 }).then(response => {
demand(response.test).to.be.true();
done();
});
testApi.fetch = original;
});

it("should create a resource", () => {
testApi.resource("users");
demand(testApi.users).to.not.be.undefined();
Expand All @@ -269,7 +238,7 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
demand(testApi.users.create).to.be.a.function();
demand(testApi.users.delete).to.be.a.function();
demand(testApi.users.update).to.be.a.function();

demand(testApi.users.find.url).to.be.equal("/users/:id");
demand(testApi.users.all.url).to.be.equal("/users");
demand(testApi.users.create.url).to.be.equal("/users");
Expand All @@ -282,10 +251,10 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
testApi.fetch = (endpoint, cb, config) => {
demand(endpoint).to.equal("/users/1234");
demand(config.method).to.equal("GET");
cb(undefined, {test: true});
cb(undefined, { test: true });
};
testApi.users.find({id: 1234}).then((resp) => {

testApi.users.find({ id: 1234 }).then((resp) => {
demand(resp.test).to.be.true();
done();
});
Expand All @@ -298,9 +267,9 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
testApi.fetch = (endpoint, cb, config) => {
demand(endpoint).to.equal("/users");
demand(config.method).to.equal("GET");
cb(undefined, {test: true});
cb(undefined, { test: true });
};

testApi.users.all().then((resp) => {
demand(resp.test).to.be.true();
done();
Expand All @@ -315,9 +284,9 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
demand(endpoint).to.equal("/users");
demand(config.method).to.equal("POST");
demand(config.body).to.equal("yessss");
cb(undefined, {test: true});
cb(undefined, { test: true });
};

testApi.users.create("yessss").then((resp) => {
demand(resp.test).to.be.true();
done();
Expand All @@ -331,10 +300,10 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
testApi.fetch = (endpoint, cb, config) => {
demand(endpoint).to.equal("/users/1234");
demand(config.method).to.equal("DELETE");
cb(undefined, {test: true});
cb(undefined, { test: true });
};
testApi.users.delete({id: 1234}).then((resp) => {

testApi.users.delete({ id: 1234 }).then((resp) => {
demand(resp.test).to.be.true();
done();
});
Expand All @@ -347,10 +316,10 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
testApi.fetch = (endpoint, cb, config) => {
demand(endpoint).to.equal("/users/1234");
demand(config.method).to.equal("PUT");
cb(undefined, {test: true});
cb(undefined, { test: true });
};
testApi.users.update({id: 1234}).then((resp) => {

testApi.users.update({ id: 1234 }).then((resp) => {
demand(resp.test).to.be.true();
done();
});
Expand All @@ -365,8 +334,8 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
demand(config.method).to.equal("PUT");
cb(new Error("test"), undefined);
};
testApi.users.update({id: 1234}).then((resp) => {

testApi.users.update({ id: 1234 }).then((resp) => {
done(new Error("This shouldn't have been called"));
}).catch(e => {
demand(e.message).to.equal("test");
Expand All @@ -378,7 +347,7 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu

it("should create base- and buildUrl", () => {
demand(testApi.users.all.buildUrl()).to.equal("/users");
demand(testApi.users.find.buildUrl({id: 123})).to.equal("/users/123");
demand(testApi.users.find.buildUrl({ id: 123 })).to.equal("/users/123");
demand(testApi.users.create.buildUrl()).to.equal("/users");
})
});
Expand Down Expand Up @@ -412,24 +381,56 @@ function runTestsWithApi(testApi, availableUrl, unavailableUrl, endPointJsonRetu
}, options);
})
}

/**
* Dynamic create a fetch callback.
*/
function fetchFn(testApi, availableUrl, unavailableUrl, options, callbacks) {
it("should callback when fetching (website available)", function (done) {
testApi.fetch(availableUrl, function (err, result) {
callbacks.cb(err, result);
done();
}, options);
})
it("should callback with error when fetching (website not available)", function (done) {
testApi.fetch(unavailableUrl, function (err, result) {
callbacks.noWebCb(err, result);
done();
}, options)
})
it("should callback when fetching cached result (website available)", function (done) {
testApi.__addToCache(availableUrl, true);
testApi.fetch(availableUrl, function (err, result) {
callbacks.cacheCb(err, result);
done();
}, options);
})
it("should callback when fetching cached result (website not available)", function (done) {
testApi.__addToCache(unavailableUrl, true);
testApi.fetch(unavailableUrl, function (err, result) {
callbacks.cacheNoWebCb(err, result);
done();
}, options);
})
}
}

describe("Test Api utils", () => {
const api = require('./../lib/index.js');

it("should create buildurl", () => {
demand(api.buildUrl).must.be.a.function();
let [url, payload] = api.buildUrl("/test/:id", {id: 1234});
let [url, payload] = api.buildUrl("/test/:id", { id: 1234 });
demand(url).to.equal("/test/1234");
demand(payload).to.be.empty();
demand(() => {
api.buildUrl("/test/:id/:fail", {id: 1234});
api.buildUrl("/test/:id/:fail", { id: 1234 });
}).to.throw();

[url, payload] = api.buildUrl("/test/:id/:test", {id: 1234, test: "yes", more: true, idk: false});
[url, payload] = api.buildUrl("/test/:id/:test", { id: 1234, test: "yes", more: true, idk: false });

demand(url).to.equal("/test/1234/yes");
demand(payload).to.eql({more: true, idk: false});
demand(payload).to.eql({ more: true, idk: false });

});
})

0 comments on commit df49eb1

Please sign in to comment.