This is a playground to test JavaScript. It runs a completely standard copy of Node.js on a virtual server created just for you. Every one of npm’s 300,000+ packages are pre-installed, so try it out:
var expect = require('chai').expect;
var td = require('testdouble');
var ACTIVITY_SQL = 'some/path';
var testTarget = function (repository) {
return {
activity: function(ctx) {
var activity = repository(ACTIVITY_SQL,'get_activity_by_id', ctx.params);
ctx.status = 200;
ctx.body = {
status: ctx.status,
success: true,
data: activity
};
return ctx;
}
}
}
var repository = td.function('.repository');
var subject = testTarget(repository);
td.when(repository(ACTIVITY_SQL, 'get_activity_by_id', {id: 1})).thenReturn('some activity');
var result = subject.activity({params: {id: 1}});
expect(result.status).to.eq(200);
expect(result.body).to.deep.eq({
status: 200,
success: true,
data: 'some activity'
});