2016-09-06 00:12:13 +00:00
|
|
|
import test from 'ava';
|
|
|
|
import { list } from './crud.js';
|
|
|
|
import { stub } from 'sinon';
|
2016-09-08 04:06:34 +00:00
|
|
|
import uniqueId from 'lodash/uniqueId.js';
|
2016-09-06 00:12:13 +00:00
|
|
|
import 'sinon-bluebird';
|
|
|
|
|
|
|
|
const METHODS = {
|
|
|
|
GET: 'GET',
|
|
|
|
};
|
|
|
|
|
|
|
|
test.beforeEach('setup server', (t) => {
|
|
|
|
t.context.server = {
|
|
|
|
route: stub(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-09-08 04:06:34 +00:00
|
|
|
const makeModel = () => {
|
|
|
|
const id = uniqueId();
|
|
|
|
return {
|
2016-09-06 00:12:13 +00:00
|
|
|
findAll: stub(),
|
|
|
|
_plural: 'models',
|
|
|
|
_singular: 'model',
|
2016-09-08 04:06:34 +00:00
|
|
|
toJSON: () => ({ id }),
|
|
|
|
id,
|
2016-09-06 00:12:13 +00:00
|
|
|
};
|
2016-09-08 04:06:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
test.beforeEach('setup model', (t) => {
|
|
|
|
t.context.model = makeModel();
|
|
|
|
});
|
|
|
|
|
|
|
|
test.beforeEach('setup models', (t) => {
|
|
|
|
t.context.models = [t.context.model, makeModel()];
|
2016-09-06 00:12:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test.beforeEach('setup request stub', (t) => {
|
|
|
|
t.context.request = {
|
|
|
|
query: {},
|
|
|
|
payload: {},
|
|
|
|
models: [t.context.model],
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
test.beforeEach('setup reply stub', (t) => {
|
|
|
|
t.context.reply = stub();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('crud#list without prefix', (t) => {
|
|
|
|
const { server, model } = t.context;
|
|
|
|
|
|
|
|
list({ server, model });
|
|
|
|
const { path } = server.route.args[0][0];
|
|
|
|
|
|
|
|
t.falsy(
|
|
|
|
path.includes('undefined'),
|
|
|
|
'correctly sets the path without a prefix defined',
|
|
|
|
);
|
|
|
|
|
|
|
|
t.is(
|
|
|
|
path,
|
|
|
|
`/${model._plural}`,
|
|
|
|
'the path sets to the plural model'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('crud#list with prefix', (t) => {
|
|
|
|
const { server, model } = t.context;
|
|
|
|
const prefix = '/v1';
|
|
|
|
|
|
|
|
list({ server, model, prefix });
|
|
|
|
const { path } = server.route.args[0][0];
|
|
|
|
|
|
|
|
t.is(
|
|
|
|
path,
|
|
|
|
`${prefix}/${model._plural}`,
|
|
|
|
'the path sets to the plural model with the prefix'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('crud#list method', (t) => {
|
|
|
|
const { server, model } = t.context;
|
|
|
|
|
|
|
|
list({ server, model });
|
|
|
|
const { method } = server.route.args[0][0];
|
|
|
|
|
|
|
|
t.is(
|
|
|
|
method,
|
|
|
|
METHODS.GET,
|
|
|
|
`sets the method to ${METHODS.GET}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('crud#list config', (t) => {
|
|
|
|
const { server, model } = t.context;
|
|
|
|
const userConfig = {};
|
|
|
|
|
|
|
|
list({ server, model, config: userConfig });
|
|
|
|
const { config } = server.route.args[0][0];
|
|
|
|
|
|
|
|
t.is(
|
|
|
|
config,
|
|
|
|
userConfig,
|
|
|
|
'sets the user config'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('crud#list handler', async (t) => {
|
2016-09-08 04:06:34 +00:00
|
|
|
const { server, model, request, reply, models } = t.context;
|
2016-09-06 00:12:13 +00:00
|
|
|
|
|
|
|
list({ server, model });
|
|
|
|
const { handler } = server.route.args[0][0];
|
2016-09-08 04:06:34 +00:00
|
|
|
model.findAll.resolves(models);
|
2016-09-06 00:12:13 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await handler(request, reply);
|
|
|
|
} catch (e) {
|
|
|
|
t.ifError(e, 'does not error while handling');
|
|
|
|
} finally {
|
|
|
|
t.pass('does not error while handling');
|
|
|
|
}
|
|
|
|
|
|
|
|
t.truthy(
|
|
|
|
reply.calledOnce
|
|
|
|
, 'calls reply only once'
|
|
|
|
);
|
|
|
|
|
|
|
|
const response = reply.args[0][0];
|
|
|
|
|
2016-09-08 04:06:34 +00:00
|
|
|
t.deepEqual(
|
2016-09-06 00:12:13 +00:00
|
|
|
response,
|
2016-09-08 04:06:34 +00:00
|
|
|
models.map(({ id }) => ({ id })),
|
2016-09-06 00:12:13 +00:00
|
|
|
'responds with the list of models'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('crud#list handler if parseInclude errors', async (t) => {
|
|
|
|
const { server, model, request, reply } = t.context;
|
|
|
|
// we _want_ the error
|
|
|
|
delete request.models;
|
|
|
|
|
|
|
|
list({ server, model });
|
|
|
|
const { handler } = server.route.args[0][0];
|
|
|
|
|
|
|
|
await handler(request, reply);
|
|
|
|
|
|
|
|
t.truthy(
|
|
|
|
reply.calledOnce
|
|
|
|
, 'calls reply only once'
|
|
|
|
);
|
|
|
|
|
|
|
|
const response = reply.args[0][0];
|
|
|
|
|
|
|
|
t.truthy(
|
|
|
|
response.isBoom,
|
|
|
|
'responds with a Boom error'
|
|
|
|
);
|
|
|
|
});
|