Add attributes tests

This commit is contained in:
Alexandre Bodin 2019-07-23 12:45:54 +02:00
parent 5f36278ec1
commit 1fa399cfd2
3 changed files with 209 additions and 7 deletions

View File

@ -1,9 +1,9 @@
const {
isSortable,
// isEditable,
// hasEditableAttribute,
// hasListableAttribute,
// hasRelationAttribute,
isEditable,
hasEditableAttribute,
hasListableAttribute,
hasRelationAttribute,
} = require('../attributes');
describe('attributesUtils', () => {
@ -11,5 +11,207 @@ describe('attributesUtils', () => {
test('The id attribute is always sortable', () => {
expect(isSortable({}, 'id')).toBe(true);
});
test('Group fields are not sortable', () => {
expect(
isSortable(
{
allAttributes: {
someGroup: {
type: 'group',
},
},
},
'someGroup'
)
).toBe(false);
});
test('Json fields are not sortable', () => {
expect(
isSortable(
{
allAttributes: {
jsonInput: {
type: 'json',
},
},
},
'jsonInput'
)
).toBe(false);
});
test('Relations are not sortable', () => {
expect(
isSortable(
{
allAttributes: {
oneWayRel: {
model: 'someModel',
},
},
},
'oneWayRel'
)
).toBe(false);
expect(
isSortable(
{
allAttributes: {
manyWayRel: {
collection: 'someModel',
},
},
},
'manyWayRel'
)
).toBe(false);
});
});
describe('isEditable', () => {
test('Check if the attribute is in a model attributes', () => {
expect(
isEditable(
{
attributes: {
field: {
type: 'string',
},
},
},
'field'
)
).toBe(true);
expect(
isEditable(
{
attributes: {
field: {
type: 'string',
},
},
},
'createdAt'
)
).toBe(false);
});
});
describe('hasEditableAttribute', () => {
test('Check if the attribute exists and is not a relation', () => {
const model = {
allAttributes: {
rel1: {
model: 'someModel',
},
rel2: {
collection: 'someModel',
},
title: {
type: 'string',
},
},
};
expect(hasEditableAttribute(model, 'rel1')).toBe(false);
expect(hasEditableAttribute(model, 'rel2')).toBe(false);
expect(hasEditableAttribute(model, 'unkown')).toBe(false);
expect(hasEditableAttribute(model, 'title')).toBe(true);
});
});
describe('hasListableAttribute', () => {
test('Ids are listable', () => {
expect(hasListableAttribute({}, 'id')).toBe(true);
});
test('Unknown attributes are not listable', () => {
const model = {
allAttributes: {
rel1: {
model: 'someModel',
},
rel2: {
collection: 'someModel',
},
title: {
type: 'string',
},
},
};
expect(hasListableAttribute(model, 'unkown')).toBe(false);
});
test('Group attributes are not listable', () => {
const model = {
allAttributes: {
someGroup: {
type: 'group',
},
},
};
expect(hasListableAttribute(model, 'someGroup')).toBe(false);
});
test('JSON attributes are not listable', () => {
const model = {
allAttributes: {
someJson: {
type: 'json',
},
},
};
expect(hasListableAttribute(model, 'someJson')).toBe(false);
});
test('Relations are not listable', () => {
const model = {
allAttributes: {
rel1: {
model: 'someModel',
},
rel2: {
collection: 'someModel',
},
title: {
type: 'string',
},
},
};
expect(hasListableAttribute(model, 'rel1')).toBe(false);
expect(hasListableAttribute(model, 'rel2')).toBe(false);
expect(hasListableAttribute(model, 'title')).toBe(true);
});
});
describe('hasRelationAttribute', () => {
test('Only validate relational attributes', () => {
const model = {
allAttributes: {
rel1: {
model: 'someModel',
},
rel2: {
collection: 'someModel',
},
title: {
type: 'string',
},
},
};
expect(hasRelationAttribute(model, 'rel1')).toBe(true);
expect(hasRelationAttribute(model, 'rel2')).toBe(true);
expect(hasRelationAttribute(model, 'unkown')).toBe(false);
expect(hasRelationAttribute(model, 'title')).toBe(false);
});
});
});

View File

@ -2,7 +2,7 @@
const _ = require('lodash');
const NON_SORTABLES = ['group', 'json', 'array'];
const NON_SORTABLES = ['group', 'json'];
const isSortable = (model, name) => {
if (name === 'id') return true;

View File

@ -7,7 +7,7 @@ const {
hasListableAttribute,
} = require('./attributes');
async function createDefaultMetadatas(model) {
function createDefaultMetadatas(model) {
return {
id: {
edit: {},
@ -62,7 +62,7 @@ async function syncMetadatas(configuration, model) {
// add new keys and missing fields
const metasWithDefaults = _.merge(
{},
await createDefaultMetadatas(model),
createDefaultMetadatas(model),
metasWithValidKeys
);