Fix toJSON responses #24

Merged
joeybaker merged 1 commits from use-json into master 2016-09-08 18:33:48 +00:00
2 changed files with 29 additions and 15 deletions
Showing only changes of commit 4e078f5ba5 - Show all commits

View File

@ -143,7 +143,7 @@ export const list = ({ server, model, prefix = '/', config }) => {
where, include,
});
reply(list);
reply(list.map((item) => item.toJSON()));
},
config,
@ -168,7 +168,7 @@ export const get = ({ server, model, prefix = '/', config }) => {
if (!instance) return void reply(notFound(`${id} not found.`));
reply(instance);
reply(instance.toJSON());
},
config: _.defaultsDeep(config, {
validate: {
@ -196,7 +196,7 @@ export const scope = ({ server, model, prefix = '/', config }) => {
const list = await model.scope(request.params.scope).findAll({ include, where });
reply(list);
reply(list.map((item) => item.toJSON()));
},
config: _.defaultsDeep(config, {
validate: {
@ -217,7 +217,7 @@ export const create = ({ server, model, prefix = '/', config }) => {
async handler(request, reply) {
const instance = await model.create(request.payload);
reply(instance);
reply(instance.toJSON());
},
config,
@ -238,7 +238,8 @@ export const destroy = ({ server, model, prefix = '/', config }) => {
await Promise.all(list.map(instance => instance.destroy()));
reply(list.length === 1 ? list[0] : list);
const listAsJSON = list.map((item) => item.toJSON());
reply(listAsJSON.length === 1 ? listAsJSON[0] : listAsJSON);
},
config,
@ -258,7 +259,8 @@ export const destroyAll = ({ server, model, prefix = '/', config }) => {
await Promise.all(list.map(instance => instance.destroy()));
reply(list.length === 1 ? list[0] : list);
const listAsJSON = list.map((item) => item.toJSON());
reply(listAsJSON.length === 1 ? listAsJSON[0] : listAsJSON);
},
config,
@ -283,7 +285,8 @@ export const destroyScope = ({ server, model, prefix = '/', config }) => {
await Promise.all(list.map(instance => instance.destroy()));
reply(list);
const listAsJSON = list.map((item) => item.toJSON());
reply(listAsJSON.length === 1 ? listAsJSON[0] : listAsJSON);
},
config: _.defaultsDeep(config, {
validate: {
@ -309,7 +312,7 @@ export const update = ({ server, model, prefix = '/', config }) => {
await instance.update(request.payload);
reply(instance);
reply(instance.toJSON());
},
config: _.defaultsDeep(config, {

View File

@ -1,6 +1,7 @@
import test from 'ava';
import { list } from './crud.js';
import { stub } from 'sinon';
import uniqueId from 'lodash/uniqueId.js';
import 'sinon-bluebird';
const METHODS = {
@ -13,12 +14,23 @@ test.beforeEach('setup server', (t) => {
};
});
test.beforeEach('setup model', (t) => {
t.context.model = {
const makeModel = () => {
const id = uniqueId();
return {
findAll: stub(),
_plural: 'models',
_singular: 'model',
toJSON: () => ({ id }),
id,
};
};
test.beforeEach('setup model', (t) => {
t.context.model = makeModel();
});
test.beforeEach('setup models', (t) => {
t.context.models = [t.context.model, makeModel()];
});
test.beforeEach('setup request stub', (t) => {
@ -93,12 +105,11 @@ test('crud#list config', (t) => {
});
test('crud#list handler', async (t) => {
const { server, model, request, reply } = t.context;
const allModels = [{ id: 1 }, { id: 2 }];
const { server, model, request, reply, models } = t.context;
list({ server, model });
const { handler } = server.route.args[0][0];
model.findAll.resolves(allModels);
model.findAll.resolves(models);
try {
await handler(request, reply);
@ -115,9 +126,9 @@ test('crud#list handler', async (t) => {
const response = reply.args[0][0];
t.is(
t.deepEqual(
response,
allModels,
models.map(({ id }) => ({ id })),
'responds with the list of models'
);
});