Skip to content

Commit 2ea38d6

Browse files
author
Hovhannes Babayan
committed
added plugin tests
1 parent 191a029 commit 2ea38d6

9 files changed

Lines changed: 247 additions & 15 deletions

File tree

examples/node/get-metadata.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ util.log('Logging in ...');
1515
instance.login('new@user.attask', 'user').then(
1616
function(data) {
1717
util.log('Getting metadata ...');
18-
instance.objects().then(
18+
instance.metadata().then(
1919
function(data) {
2020
util.log('Get success. Received data:');
2121
console.log(util.inspect(data, {colors:true}));

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"gulp-uglify": "^1.0.2",
2020
"gulp-webserver": "^0.9.0",
2121
"nock": "^0.52.4",
22+
"sinon": "^1.12.2",
23+
"sinon-as-promised": "^2.0.3",
24+
"sinon-chai": "^2.6.0",
2225
"vinyl-buffer": "^1.0.0",
2326
"vinyl-source-stream": "^1.0.0"
2427
},

src/plugins/metadata.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = function(Api) {
22
/**
33
* Retrieves API metadata for an object.
4-
* @param {String} objCode One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer}
4+
* @param {String} [objCode] One of object codes from {@link https://developers.attask.com/api-docs/api-explorer/|AtTask API Explorer}. If omitted will return list of objects available in API.
55
* @return {Promise} A promise which will resolved with object metadata if everything went ok and rejected otherwise
66
*/
77
Api.prototype.metadata = function (objCode) {
@@ -11,12 +11,4 @@ module.exports = function(Api) {
1111
}
1212
return this.request(path, null, null, 'GET');
1313
};
14-
15-
/**
16-
* Retrieves list of objects available in API
17-
* @return {Promise} A promise which will resolved with object list if everything went ok and rejected otherwise
18-
*/
19-
Api.prototype.objects = function () {
20-
return this.request('/metadata', null, null, 'GET');
21-
};
2214
};

test/common.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
var chai = require('chai');
2-
var chaiAsPromised = require("chai-as-promised");
1+
// this files defines globals which can be used in tests
32

4-
chai.use(chaiAsPromised);
3+
var chai = require('chai');
4+
chai.use(require('chai-as-promised'));
55

6-
var expect = chai.expect;
6+
chai.use(require('sinon-chai'));
77

88
global.chai = chai;
9-
global.expect = expect;
9+
10+
global.expect = chai.expect;
11+
12+
global.sinon = require('sinon');
13+
14+
require('promise/polyfill');
15+
require('sinon-as-promised')(Promise);

test/plugins/countSpec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require('./../common');
2+
3+
var Api = require('./../../').Api;
4+
var ApiConstants = require('./../../').ApiConstants;
5+
6+
7+
describe('Api.count() method', function() {
8+
9+
var api;
10+
var url = 'http://foobar:8080';
11+
12+
beforeEach(function () {
13+
api = new Api({url: url});
14+
sinon.stub(api, "request");
15+
});
16+
17+
afterEach(function () {
18+
api.request.restore(); // Unwraps the spy
19+
});
20+
21+
it('should call request() with proper params', function() {
22+
var query = {
23+
'foo': 'bar'
24+
};
25+
26+
var objCode = 'baz';
27+
28+
api.count(objCode, query);
29+
expect(api.request).to.have.callCount(1);
30+
expect(api.request).to.have.been.calledWith(objCode + "/count", query, null, "GET");
31+
});
32+
33+
it('should resolve returned promise with count if everything was ok', function() {
34+
var c = 7;
35+
api.request.resolves({count: c});
36+
expect(api.count('foo', {})).to.eventually.equal(c);
37+
});
38+
});

test/plugins/getSpec.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
require('./../common');
2+
3+
var Api = require('./../../').Api;
4+
5+
6+
describe('Api.get() method', function() {
7+
8+
var api;
9+
var url = 'http://foobar:8080';
10+
11+
beforeEach(function () {
12+
api = new Api({url: url});
13+
sinon.stub(api, "request");
14+
});
15+
16+
afterEach(function () {
17+
api.request.restore(); // Unwraps the spy
18+
});
19+
20+
it('should call request() with proper params and return value received from request() if called with single objId (String)', function() {
21+
var objId = 'myid';
22+
23+
var fields = [
24+
'*', "zzz:*"
25+
];
26+
27+
var objCode = 'baz';
28+
29+
var expectedReturnValue = 123;
30+
api.request.returns(expectedReturnValue);
31+
32+
var actualReturnedValue = api.get(objCode, objId, fields);
33+
expect(api.request).to.have.callCount(1);
34+
expect(api.request).to.have.been.calledWith(objCode + '/' + objId, null, fields, "GET");
35+
expect(actualReturnedValue).to.equal(expectedReturnValue);
36+
});
37+
38+
it('should call request() with proper params and return value received from request() if called with single objId (Array)', function() {
39+
var objId = ['myid'];
40+
41+
var fields = [
42+
'*', "zzz:*"
43+
];
44+
45+
var objCode = 'baz';
46+
47+
var expectedReturnValue = 123;
48+
api.request.returns(expectedReturnValue);
49+
50+
var actualReturnedValue = api.get(objCode, objId, fields);
51+
expect(api.request).to.have.callCount(1);
52+
expect(api.request).to.have.been.calledWith(objCode + '/' + objId, null, fields, "GET");
53+
expect(actualReturnedValue).to.equal(expectedReturnValue);
54+
});
55+
56+
it('should call request() with proper params and return value received from request() if called with multiple objIds', function() {
57+
var objIds = ['myid1', 'myid2'];
58+
59+
var fields = [
60+
'*', "zzz:*"
61+
];
62+
63+
var objCode = 'baz';
64+
65+
var expectedReturnValue = 123;
66+
api.request.returns(expectedReturnValue);
67+
68+
var actualReturnedValue = api.get(objCode, objIds, fields);
69+
expect(api.request).to.have.callCount(1);
70+
expect(api.request).to.have.been.calledWith(objCode, { id: objIds }, fields, "GET");
71+
expect(actualReturnedValue).to.equal(expectedReturnValue);
72+
});
73+
});

test/plugins/metadataSpec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require('./../common');
2+
3+
var Api = require('./../../').Api;
4+
5+
6+
describe('Api.metadata() method', function() {
7+
8+
var api;
9+
var url = 'http://foobar:8080';
10+
11+
beforeEach(function () {
12+
api = new Api({url: url});
13+
sinon.stub(api, "request");
14+
});
15+
16+
afterEach(function () {
17+
api.request.restore(); // Unwraps the spy
18+
});
19+
20+
it('should call request() with proper params and return value received from request() when called with 1 argument', function() {
21+
var objCode = 'baz';
22+
23+
var expectedReturnValue = 123;
24+
api.request.returns(expectedReturnValue);
25+
26+
var actualReturnedValue = api.metadata(objCode);
27+
expect(api.request).to.have.callCount(1);
28+
expect(api.request).to.have.been.calledWith(objCode + "/metadata", null, null, "GET");
29+
expect(actualReturnedValue).to.equal(expectedReturnValue);
30+
});
31+
32+
it('should call request() with proper params and return value received from request() when called without arguments', function() {
33+
var expectedReturnValue = 123;
34+
api.request.returns(expectedReturnValue);
35+
36+
var actualReturnedValue = api.metadata();
37+
expect(api.request).to.have.callCount(1);
38+
expect(api.request).to.have.been.calledWith("/metadata", null, null, "GET");
39+
expect(actualReturnedValue).to.equal(expectedReturnValue);
40+
});
41+
});

test/plugins/postSpec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require('./../common');
2+
3+
var Api = require('./../../').Api;
4+
5+
6+
describe('Api.post() method', function() {
7+
8+
var api;
9+
var url = 'http://foobar:8080';
10+
11+
beforeEach(function () {
12+
api = new Api({url: url});
13+
sinon.stub(api, "request");
14+
});
15+
16+
afterEach(function () {
17+
api.request.restore(); // Unwraps the spy
18+
});
19+
20+
it('should call request() with proper params and return value received from request()', function() {
21+
var params = {
22+
'foo': 'bar'
23+
};
24+
25+
var fields = [
26+
'*', "zzz:*"
27+
];
28+
29+
var objCode = 'baz';
30+
31+
var expectedReturnValue = 123;
32+
api.request.returns(expectedReturnValue);
33+
34+
var actualReturnedValue = api.post(objCode, params, fields);
35+
expect(api.request).to.have.callCount(1);
36+
expect(api.request).to.have.been.calledWith(objCode, params, fields, "POST");
37+
expect(actualReturnedValue).to.equal(expectedReturnValue);
38+
});
39+
});

test/plugins/searchSpec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require('./../common');
2+
3+
var Api = require('./../../').Api;
4+
var ApiConstants = require('./../../').ApiConstants;
5+
6+
7+
describe('Api.search() method', function() {
8+
9+
var api;
10+
var url = 'http://foobar:8080';
11+
12+
beforeEach(function () {
13+
api = new Api({url: url});
14+
sinon.stub(api, "request");
15+
});
16+
17+
afterEach(function () {
18+
api.request.restore(); // Unwraps the spy
19+
});
20+
21+
it('should call request() with proper params and return value received from request()', function() {
22+
var query = {
23+
'foo': 'bar'
24+
};
25+
26+
var fields = [
27+
'*', "zzz:*"
28+
];
29+
30+
var objCode = 'baz';
31+
32+
var expectedReturnValue = 123;
33+
api.request.returns(expectedReturnValue);
34+
35+
var actualReturnedValue = api.search(objCode, query, fields);
36+
expect(api.request).to.have.callCount(1);
37+
expect(api.request).to.have.been.calledWith(objCode + "/search", query, fields, "GET");
38+
expect(actualReturnedValue).to.equal(expectedReturnValue);
39+
});
40+
});

0 commit comments

Comments
 (0)