Permalink
Browse files

Improvements to responder strategies, tests for index/collections

  • Loading branch information...
1 parent 975141c commit 1c51f31f1c6c62d23cba399f3bafb4e4060c16e4 @mde mde committed Jan 25, 2014
@@ -49,10 +49,7 @@ var utils = require('utilities')
// Determine if `content` is a collection of models
else if (content instanceof Array) {
response = content.map(function (item) {
- var contentItem = item.toObj();
- contentItem.id = item.id;
- contentItem.type = item.type;
- return contentItem;
+ return item.toObj();
});
}
else if (content instanceof Error || (content.message && content.stack)) {
@@ -3,6 +3,7 @@
* because
*/
var utils = require('utilities')
+ , model = require('model')
, strategy = function (content, options) {
// Where to redirect to on a success event
@@ -93,7 +94,7 @@ var utils = require('utilities')
};
// Determine if `content` is a model object
- if (typeof content === 'object' && content.type && content.toObj) {
+ if (content instanceof model.ModelBase) {
type = utils.string.getInflection(content.type, 'property', 'singular');
@@ -143,10 +144,9 @@ var utils = require('utilities')
response = {params: params};
response[type] = [];
- for(var i=0, ii=content.length; i<ii; i++) {
- response[type][i] = content[i].toObj();
- response[type][i].id = content[i].id;
- }
+ content.forEach(function (item) {
+ response[type].push(item.toObj());
+ });
this.respond(response, options);
}
@@ -446,6 +446,67 @@ but not explicitly supported on controller': function (next) {
c.respondWith(createModelInstance());
}
+, 'respondWith html index action (array data), format in params': function (next) {
+ var c = createController()
+ , items = [];
+ c.params.format = 'html';
+ c.params.action = 'index';
+ c.output = function (statusCode, headers, content) {
+ var parseable
+ , data
+ , items;
+ assert.equal(200, statusCode);
+ assert.equal('text/html', headers['Content-Type']);
+ // Should be whatever renderTemplate spits out
+ assert.ok(content.indexOf('<div>') > -1);
+ assert.ok(content.indexOf('</div>') > -1);
+ // Strip the token HTML tags, see what content got
+ // passed to renderTemplate
+ parseable = content.replace('<div>', '').replace('</div>', '');
+ data = JSON.parse(parseable);
+ // Should have a params obj
+ assert.ok(data.params);
+ // Should have data items
+ items = data.zoobies;
+ assert.equal(3, items.length);
+ items.forEach(function (item) {
+ assert.ok(item.id);
+ assert.ok(item.createdAt);
+ assert.ok(item.title);
+ assert.ok(item.description);
+ });
+ next();
+ };
+ items.push(createModelInstance());
+ items.push(createModelInstance());
+ items.push(createModelInstance());
+ c.respondWith(items);
+ }
+
+, 'respondWith json index action (array data), format in params': function (next) {
+ var c = createController()
+ , items = [];
+ c.params.format = 'json';
+ c.params.action = 'index';
+ c.output = function (statusCode, headers, content) {
+ var items = JSON.parse(content);
+ assert.equal(200, statusCode);
+ assert.equal('application/json', headers['Content-Type']);
+ assert.equal(3, items.length);
+ items.forEach(function (item) {
+ assert.ok(item.id);
+ assert.ok(item.createdAt);
+ assert.ok(item.title);
+ assert.ok(item.description);
+ });
+ next();
+ };
+ items.push(createModelInstance());
+ items.push(createModelInstance());
+ items.push(createModelInstance());
+ c.respondWith(items);
+ }
+
};
module.exports = tests;

0 comments on commit 1c51f31

Please sign in to comment.