Skip to content

Commit 81880ca

Browse files
committed
test (invoke-handler) add result modifying tests for events in after phase
1 parent 5a29272 commit 81880ca

File tree

2 files changed

+70
-31
lines changed

2 files changed

+70
-31
lines changed

lib/server-code/runners/tasks/util/result-wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ exports.exception = function(err) {
3636

3737
/**
3838
* @param {?Error|ExceptionWrapper|String} err
39-
* @param {?*} result
39+
* @param {?*=} result
4040
* @returns {Object}
4141
*/
4242
exports.executionResult = function(err, result) {

test/invoke-handler-task.js

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22

3-
const should = require('should'),
4-
events = require('../lib/server-code/events'),
5-
json = require('../lib/util/json'),
6-
executor = require('../lib/server-code/runners/tasks/executor'),
7-
resultWrapper = require('../lib/server-code/runners/tasks/util/result-wrapper'),
8-
PERSISTENCE = events.providers.PERSISTENCE,
9-
BEFORE_CREATE = PERSISTENCE.events.beforeCreate,
10-
AFTER_CREATE = PERSISTENCE.events.afterCreate;
3+
const should = require('should'),
4+
events = require('../lib/server-code/events'),
5+
json = require('../lib/util/json'),
6+
executor = require('../lib/server-code/runners/tasks/executor'),
7+
executionResult = require('../lib/server-code/runners/tasks/util/result-wrapper').executionResult,
8+
PERSISTENCE = events.providers.PERSISTENCE,
9+
BEFORE_CREATE = PERSISTENCE.events.beforeCreate,
10+
AFTER_CREATE = PERSISTENCE.events.afterCreate;
1111

1212
require('mocha');
1313

@@ -88,7 +88,7 @@ describe('[invoke-handler] task executor', function() {
8888
}
8989

9090
const item = { a: 'a', bar: { ___class: 'Bar', b: 'b' }, ___class: 'Foo' };
91-
const result = resultWrapper.executionResult(null, [{ ___class: 'Baz' }, { ___class: 'Baz' }]);
91+
const result = executionResult(null, [{ ___class: 'Baz' }, { ___class: 'Baz' }]);
9292

9393
return invokeAndParse(createTask(AFTER_CREATE, [{}, item, result]), modelStub(handler, {
9494
Foo,
@@ -202,7 +202,7 @@ describe('[invoke-handler] task executor', function() {
202202
describe('in [after] event phase', function() {
203203
it('should provide succeeded server result in {response} handler argument', function() {
204204
const result = { name: 'John', id: 1 };
205-
const wrappedResult = resultWrapper.executionResult(null, result);
205+
const wrappedResult = executionResult(null, result);
206206
const task = createTask(AFTER_CREATE, [{}, {}, wrappedResult]);
207207

208208
function handler(req, res) {
@@ -218,9 +218,9 @@ describe('[invoke-handler] task executor', function() {
218218
});
219219
});
220220

221-
it('should provide errored server result in {response} handler argument', function() {
221+
it('should provide erred server result in {response} handler argument', function() {
222222
const error = 'error';
223-
const erredResult = resultWrapper.executionResult(error);
223+
const erredResult = executionResult(error);
224224
const task = createTask(AFTER_CREATE, [{}, {}, erredResult]);
225225

226226
const handler = function(req, res) {
@@ -237,30 +237,69 @@ describe('[invoke-handler] task executor', function() {
237237
});
238238
});
239239

240-
it('should allow modifying server result by returning new value', function() {
241-
const task = createTask(AFTER_CREATE, [{}, {}, resultWrapper.executionResult(null, {
242-
name: 'John',
243-
id : 1
244-
})]);
240+
describe('should allow server result modifying', function() {
241+
it('by returning a value from handler', function() {
242+
const task = createTask(AFTER_CREATE, [{}, {}, executionResult(null, {
243+
name: 'John',
244+
id : 1
245+
})]);
245246

246-
function handler() {
247-
return { name: 'Dou', id: 2 };
248-
}
247+
function handler() {
248+
return { name: 'Dou', id: 2 };
249+
}
249250

250-
return invokeAndParse(task, modelStub(handler)).then(res => {
251-
res.arguments[2].result.should.be.eql({ name: 'Dou', id: 2 });
251+
return invokeAndParse(task, modelStub(handler)).then(res => {
252+
res.arguments[2].result.should.be.eql({ name: 'Dou', id: 2 });
253+
});
252254
});
253-
});
254255

255-
it('should allow result modifying via setting {res.result} a new value', function() {
256-
const task = createTask(AFTER_CREATE, [{}, {}, resultWrapper.executionResult({ name: 'John', id: 1 })]);
256+
it('by changing {res.result}', function() {
257+
const task = createTask(AFTER_CREATE, [{}, {}, executionResult(null, { name: 'John', id: 1 })]);
257258

258-
function handler(req, res) {
259-
res.result = { name: 'Dou', id: 2 };
260-
}
259+
function handler(req, res) {
260+
res.result.name = 'John Dou';
261+
}
261262

262-
return invokeAndParse(task, modelStub(handler)).then(res => {
263-
res.arguments[2].result.should.be.eql({ name: 'Dou', id: 2 });
263+
return invokeAndParse(task, modelStub(handler)).then(res => {
264+
res.arguments[2].result.should.be.eql({ name: 'John Dou', id: 1 });
265+
});
266+
});
267+
268+
it('by changing {res.error}', function() {
269+
const task = createTask(AFTER_CREATE, [{}, {}, executionResult('Error')]);
270+
271+
function handler(req, res) {
272+
res.error.code = 1;
273+
}
274+
275+
return invokeAndParse(task, modelStub(handler)).then(res => {
276+
should.exists(res.arguments[2].exception);
277+
278+
res.arguments[2].exception.code.should.equal(1);
279+
res.arguments[2].exception.exceptionMessage.should.equal('Error');
280+
});
281+
});
282+
283+
it('by replacing {res.result} and {res.error}', function() {
284+
const task = createTask(AFTER_CREATE, [{}, {}, executionResult(null, null)]);
285+
286+
function handler(req, res) {
287+
res.result = { name: 'Dou', id: 2 };
288+
res.error = {
289+
code: 4,
290+
exceptionMessage: 'Error',
291+
exceptionClass: 'java.lang.RuntimeException'
292+
};
293+
}
294+
295+
return invokeAndParse(task, modelStub(handler)).then(res => {
296+
should.exists(res.arguments[2].result);
297+
should.exists(res.arguments[2].exception);
298+
299+
res.arguments[2].result.should.be.eql({ name: 'Dou', id: 2 });
300+
res.arguments[2].exception.code.should.equal(4);
301+
res.arguments[2].exception.exceptionMessage.should.equal('Error');
302+
});
264303
});
265304
});
266305
});

0 commit comments

Comments
 (0)