Skip to content

Commit a58e365

Browse files
committed
test (acceptance) refactor tests and add few more test cases
1 parent e8b90bf commit a58e365

File tree

5 files changed

+66
-24
lines changed

5 files changed

+66
-24
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Backendless CodeRunner for Node.js",
55
"scripts": {
66
"test": "mocha",
7+
"test-acceptance": "mocha test/acceptance",
78
"lint": "eslint lib test examples bin"
89
},
910
"author": {

test/.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"env": {
33
"mocha": true
4+
},
5+
"rules": {
6+
"no-throw-literal": 0
47
}
58
}

test/acceptance/cloud.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict';
1+
/* global Backendless */
22

3-
const serverCode = require('../support/server-code'),
4-
events = require('../../lib/server-code/events'),
5-
PERSISTENCE = events.providers.PERSISTENCE;
3+
'use strict';
64

75
require('mocha');
86

@@ -14,11 +12,10 @@ const app = {
1412
version: 'v1'
1513
};
1614

17-
function request(method, path) {
18-
return require('supertest')(app.server + '/' + app.version)[method](path)
19-
.set('application-id', app.id)
20-
.set('secret-key', app.restKey);
21-
}
15+
const serverCode = require('../support/server-code'),
16+
request = require('../support/request')(app),
17+
events = require('../../lib/server-code/events'),
18+
PERSISTENCE = events.providers.PERSISTENCE;
2219

2320
describe('In CLOUD', function() {
2421
describe('[before] event handler', function() {
@@ -33,11 +30,9 @@ describe('In CLOUD', function() {
3330
.addHandler(PERSISTENCE.events.beforeCreate, handler)
3431
.deploy()
3532
.then(() => {
36-
request('post', '/data/Person')
37-
.send({ name: 'Foo' })
33+
request('post', '/data/Person', { name: 'Foo' })
3834
.expect(200, /"name":"Foo Bar"/, done);
39-
})
40-
.catch(done);
35+
}, done);
4136
});
4237

4338
it('should be able to replace request', function(done) {
@@ -49,27 +44,42 @@ describe('In CLOUD', function() {
4944
.addHandler(PERSISTENCE.events.beforeCreate, handler)
5045
.deploy()
5146
.then(() => {
52-
request('post', '/data/Person')
53-
.send({ name: 'Foo' })
47+
request('post', '/data/Person', { name: 'Foo' })
5448
.expect(200, /"Foo":"Bar"/, done);
55-
})
56-
.catch(done);
49+
}, done);
50+
});
51+
52+
it('should be able to prevent [default] and [after] behaviours by returning specific result', function(done) {
53+
function beforeHandler() {
54+
return { foo: 'bar' };
55+
}
56+
57+
function afterHandler() {
58+
throw 'Should not be called';
59+
}
60+
61+
serverCode(app)
62+
.addHandler(PERSISTENCE.events.beforeCreate, beforeHandler)
63+
.addHandler(PERSISTENCE.events.afterCreate, afterHandler)
64+
.deploy()
65+
.then(() => {
66+
request('post', '/data/Person', { name: 'Foo' })
67+
.expect(200, { foo: 'bar' }, done);
68+
}, done);
5769
});
5870

5971
it('should be able to prevent default behavior by throwing simple Error', function(done) {
6072
function handler() {
61-
throw new Error('You shall not pass');
73+
throw 'You shall not pass';
6274
}
63-
75+
6476
serverCode(app)
6577
.addHandler(PERSISTENCE.events.beforeCreate, handler)
6678
.deploy()
6779
.then(() => {
68-
request('post', '/data/Person')
69-
.send({ name: 'Foo' })
80+
request('post', '/data/Person', { name: 'Foo' })
7081
.expect(400, { code: 0, message: 'You shall not pass' }, done);
71-
})
72-
.catch(done);
82+
}, done);
7383
});
7484

7585
it('should be able to prevent default behavior by throwing custom Error', function(done) {

test/support/request.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const supertest = require('supertest');
4+
5+
/**
6+
* @param {Object} app
7+
* @returns {Function.<Test>}
8+
*/
9+
module.exports = function(app) {
10+
11+
/**
12+
* @param {String} method
13+
* @param {String} path
14+
* @param {Object=} body
15+
* @returns {Test}
16+
*/
17+
return function(method, path, body) {
18+
const result = supertest(`${app.server}/${app.version}`)[method](path)
19+
.set('application-id', app.id)
20+
.set('secret-key', app.restKey);
21+
22+
if (body) {
23+
result.send(body);
24+
}
25+
26+
return result;
27+
};
28+
};

test/support/server-code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ServerCode {
3333
addHandler(event, handler, context) {
3434
const p = event.provider;
3535
const ctx = p.targeted ? `'${context || '*'}'` : null;
36-
const handlerBody = `'use strict';` +
36+
const handlerBody = `'use strict';\n` +
3737
`Backendless.ServerCode.${providerApi(p)}.${event.name}(${ctx ? ctx + ', ' : ''}${handler.toString()});`;
3838

3939
this.items.push(handlerBody);

0 commit comments

Comments
 (0)