Init group tests and fix some groups issues

This commit is contained in:
Alexandre Bodin 2019-08-08 11:59:45 +02:00
parent 93c7f63cd1
commit 60d04d26eb
5 changed files with 204 additions and 6 deletions

View File

@ -279,6 +279,8 @@ module.exports = function createQueryBuilder({ model, modelKey, strapi }) {
);
} else {
validateNonRepeatableInput(groupValue, { key, ...attr });
if (groupValue === null) return;
await createGroupAndLink({ value: groupValue, order: 1 });
}
}
@ -375,6 +377,8 @@ module.exports = function createQueryBuilder({ model, modelKey, strapi }) {
transacting,
});
if (groupValue === null) return;
await updateOrCreateGroupAndLink({ value: groupValue, order: 1 });
}
}
@ -560,6 +564,16 @@ function validateRepeatableInput(value, { key, min, max }) {
throw err;
}
value.forEach(val => {
if (typeof val !== 'object' || Array.isArray(val) || val === null) {
const err = new Error(
`Group ${key} as invalid items. Expected each items to be objects`
);
err.status = 400;
throw err;
}
});
if (min && value.length < min) {
const err = new Error(`Group ${key} must contain at least ${min} items`);
err.status = 400;
@ -573,7 +587,7 @@ function validateRepeatableInput(value, { key, min, max }) {
}
function validateNonRepeatableInput(value, { key, required }) {
if (typeof value !== 'object') {
if (typeof value !== 'object' || Array.isArray(value)) {
const err = new Error(`Group ${key} should be an object`);
err.status = 400;
throw err;

View File

@ -69,6 +69,8 @@ module.exports = ({ model, modelKey, strapi }) => {
await entry.save();
} else {
validateNonRepeatableInput(groupValue, { key, ...attr });
if (groupValue === null) return;
const groupEntry = await strapi.query(group).create(groupValue);
entry[key] = [
{
@ -125,6 +127,8 @@ module.exports = ({ model, modelKey, strapi }) => {
await deleteOldGroups(entry, groupValue, { key, groupModel });
if (groupValue === null) return;
const group = await updateOrCreateGroup(groupValue);
entry[key] = [
{
@ -389,6 +393,16 @@ function validateRepeatableInput(value, { key, min, max }) {
throw err;
}
value.forEach(val => {
if (typeof val !== 'object' || Array.isArray(val) || val === null) {
const err = new Error(
`Group ${key} as invalid items. Expected each items to be objects`
);
err.status = 400;
throw err;
}
});
if (min && value.length < min) {
const err = new Error(`Group ${key} must contain at least ${min} items`);
err.status = 400;
@ -402,7 +416,7 @@ function validateRepeatableInput(value, { key, min, max }) {
}
function validateNonRepeatableInput(value, { key, required }) {
if (typeof value !== 'object') {
if (typeof value !== 'object' || Array.isArray(value)) {
const err = new Error(`Group ${key} should be an object`);
err.status = 400;
throw err;

View File

@ -13,7 +13,7 @@ module.exports = {
},
fetchAll(params, query) {
const { query: request, source, populate = [], ...filters } = query;
const { query: request, source, populate, ...filters } = query;
const queryFilter = !_.isEmpty(request)
? {

View File

@ -1,3 +1,145 @@
'use strict';
const { registerAndLogin } = require('../../../test/helpers/auth');
const createModelsUtils = require('../../../test/helpers/models');
const { createAuthRequest } = require('../../../test/helpers/request');
describe('Testing groups CRUD', () => {});
let modelsUtils;
let rq;
describe('Test type groups', () => {
beforeAll(async () => {
const token = await registerAndLogin();
rq = createAuthRequest(token);
modelsUtils = createModelsUtils({ rq });
await modelsUtils.createGroup({
name: 'somegroup',
attributes: {
name: {
type: 'string',
},
},
});
}, 60000);
// afterAll(async () => {
// await modelsUtils.deleteGroup('somegroup');
// });
describe('Non repeatable and Non required group', () => {
beforeAll(async () => {
await modelsUtils.createModelWithType('withgroup', 'group', {
group: 'somegroup',
repeatable: false,
required: false,
});
}, 60000);
// afterAll(async () => {
// await modelsUtils.deleteModel('withgroup');
// }, 60000);
describe('POST new entry', () => {
test('Creating entry with JSON works', async () => {
const res = await rq.post('/content-manager/explorer/withgroup', {
body: {
field: {
name: 'someString',
},
},
});
expect(res.statusCode).toBe(200);
expect(res.body.field).toEqual(
expect.objectContaining({
id: expect.anything(),
name: 'someString',
})
);
});
test('Creating entry with formdata works', async () => {
const res = await rq.post('/content-manager/explorer/withgroup', {
formData: {
data: JSON.stringify({
field: {
name: 'someValue',
},
}),
},
});
expect(res.statusCode).toBe(200);
expect(res.body.field).toEqual(
expect.objectContaining({
id: expect.anything(),
name: 'someValue',
})
);
});
test.each([[], 'someString', 128219, false])(
'Throws if the field is not an object %p',
async value => {
const res = await rq.post('/content-manager/explorer/withgroup', {
body: {
field: value,
},
});
expect(res.statusCode).toBe(400);
}
);
test('Can send a null value', async () => {
const res = await rq.post('/content-manager/explorer/withgroup', {
body: {
field: null,
},
});
expect(res.statusCode).toBe(200);
expect(res.body.field).toBe(null);
});
test('Can send input without the group field', async () => {
const res = await rq.post('/content-manager/explorer/withgroup', {
body: {},
});
expect(res.statusCode).toBe(200);
expect(res.body.field).toBe(null);
});
});
describe('GET entries', () => {
test('Should return entries with their nested groups', async () => {
const res = await rq.get('/content-manager/explorer/withgroup');
expect(res.statusCode).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
res.body.forEach(entry => {
if (entry.field === null) return;
expect(entry.field).toMatchObject({
name: expect.any(String),
});
});
});
});
describe('PUT entry', () => {
test.todo('Keeps the previous value if group not sent');
test.todo('Removes previous group if null sent');
test.todo('Replaces the previous group if sent without id');
test.todo('Throws on invalid id in sent group');
test.todo('Updates group if previsous group id is sent');
});
});
describe('Non repeatable required group', () => {});
describe('Repeatable non required group', () => {});
describe('Repeatable non required group with min and max', () => {});
describe('Repeatable required group', () => {});
describe('Repeatable required group with min and max', () => {});
});

View File

@ -1,6 +1,28 @@
const waitRestart = require('./waitRestart');
module.exports = ({ rq }) => {
async function createGroup(data) {
await rq({
url: '/content-type-builder/groups',
method: 'POST',
body: {
connection: 'default',
...data,
},
});
await waitRestart();
}
async function deleteGroup(name) {
await rq({
url: `/content-type-builder/groups/${name}`,
method: 'DELETE',
});
await waitRestart();
}
function createModelWithType(name, type, opts = {}) {
return createModel({
connection: 'default',
@ -21,7 +43,10 @@ module.exports = ({ rq }) => {
await rq({
url: '/content-type-builder/models',
method: 'POST',
body: data,
body: {
connection: 'default',
...data,
},
});
await waitRestart();
@ -49,6 +74,9 @@ module.exports = ({ rq }) => {
}
return {
createGroup,
deleteGroup,
createModels,
createModel,
createModelWithType,