2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const { isSortable, isVisible } = require('../attributes');
|
|
|
|
|
|
|
|
const createMockSchema = (attrs, timestamps = true) => {
|
|
|
|
return {
|
|
|
|
options: {
|
|
|
|
timestamps: timestamps ? ['createdAt', 'updatedAt'] : false,
|
|
|
|
},
|
|
|
|
attributes: {
|
|
|
|
id: {
|
|
|
|
type: 'integer',
|
|
|
|
},
|
|
|
|
...attrs,
|
|
|
|
...(timestamps
|
|
|
|
? {
|
|
|
|
createdAt: {
|
|
|
|
type: 'timestamp',
|
|
|
|
},
|
|
|
|
updatedAt: {
|
2019-12-05 11:37:07 +01:00
|
|
|
type: 'timestamp',
|
2019-07-24 11:51:35 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
2019-07-23 10:54:44 +02:00
|
|
|
|
|
|
|
describe('attributesUtils', () => {
|
|
|
|
describe('isSortable', () => {
|
|
|
|
test('The id attribute is always sortable', () => {
|
2019-07-24 11:51:35 +02:00
|
|
|
expect(isSortable(createMockSchema({}), 'id')).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Timestamps are sortable', () => {
|
|
|
|
expect(isSortable(createMockSchema({}, true), 'createdAt')).toBe(true);
|
|
|
|
expect(isSortable(createMockSchema({}, true), 'updatedAt')).toBe(true);
|
|
|
|
expect(isSortable(createMockSchema({}, false), 'createdAt')).toBe(false);
|
2019-07-23 10:54:44 +02:00
|
|
|
});
|
2019-07-23 12:45:54 +02:00
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
test('Component fields are not sortable', () => {
|
2019-07-24 11:51:35 +02:00
|
|
|
const schema = createMockSchema({
|
2019-10-22 18:01:03 +02:00
|
|
|
someComponent: {
|
|
|
|
type: 'component',
|
2019-07-24 11:51:35 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
expect(isSortable(schema, 'someComponent')).toBe(false);
|
2019-07-23 12:45:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Json fields are not sortable', () => {
|
2019-07-24 11:51:35 +02:00
|
|
|
const schema = createMockSchema({
|
|
|
|
jsonInput: {
|
|
|
|
type: 'json',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(isSortable(schema, 'jsonInput')).toBe(false);
|
2019-07-23 12:45:54 +02:00
|
|
|
});
|
|
|
|
|
2021-01-14 12:16:47 +01:00
|
|
|
test('x-to-one relations only are sortable', () => {
|
2019-07-24 11:51:35 +02:00
|
|
|
const schema = createMockSchema({
|
|
|
|
oneWayRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
2021-01-14 12:16:47 +01:00
|
|
|
relationType: 'oneWay',
|
|
|
|
},
|
|
|
|
manyToOneRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
relationType: 'manyToOne',
|
|
|
|
},
|
|
|
|
oneToOneRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
relationType: 'oneToOne',
|
2019-07-24 11:51:35 +02:00
|
|
|
},
|
|
|
|
manyWayRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
2021-01-14 12:16:47 +01:00
|
|
|
relationType: 'manyWay',
|
|
|
|
},
|
|
|
|
oneToManyRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
relationType: 'oneToMany',
|
|
|
|
},
|
|
|
|
manyToManyRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
relationType: 'manyToMany',
|
|
|
|
},
|
|
|
|
manyToManyMorphRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
relationType: 'manyToManyMorph',
|
|
|
|
},
|
|
|
|
manyToOneMorphRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
relationType: 'manyToOneMorph',
|
|
|
|
},
|
|
|
|
oneToManyMorphRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
relationType: 'oneToManyMorph',
|
|
|
|
},
|
|
|
|
oneToOneMorphRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
relationType: 'oneToOneMorph',
|
|
|
|
},
|
|
|
|
oneMorphToOneRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
relationType: 'oneMorphToOne',
|
|
|
|
},
|
|
|
|
manyMorphToOneRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
relationType: 'manyMorphToOne',
|
|
|
|
},
|
|
|
|
manyMorphToManyRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
relationType: 'manyMorphToMany',
|
2019-07-24 11:51:35 +02:00
|
|
|
},
|
|
|
|
});
|
2019-07-23 12:45:54 +02:00
|
|
|
|
2021-01-14 12:16:47 +01:00
|
|
|
expect(isSortable(schema, 'oneWayRel')).toBe(true);
|
|
|
|
expect(isSortable(schema, 'manyToOneRel')).toBe(true);
|
|
|
|
expect(isSortable(schema, 'oneToOneRel')).toBe(true);
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
expect(isSortable(schema, 'manyWayRel')).toBe(false);
|
2021-01-14 12:16:47 +01:00
|
|
|
expect(isSortable(schema, 'oneToManyRel')).toBe(false);
|
|
|
|
expect(isSortable(schema, 'manyToManyRel')).toBe(false);
|
|
|
|
expect(isSortable(schema, 'manyToManyMorphRel')).toBe(false);
|
|
|
|
expect(isSortable(schema, 'manyToOneMorphRel')).toBe(false);
|
|
|
|
expect(isSortable(schema, 'oneToManyMorphRel')).toBe(false);
|
|
|
|
expect(isSortable(schema, 'oneToOneMorphRel')).toBe(false);
|
|
|
|
expect(isSortable(schema, 'oneMorphToOneRel')).toBe(false);
|
|
|
|
expect(isSortable(schema, 'manyMorphToOneRel')).toBe(false);
|
|
|
|
expect(isSortable(schema, 'manyMorphToManyRel')).toBe(false);
|
2019-07-23 12:45:54 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
describe('isVisible', () => {
|
2019-07-23 12:45:54 +02:00
|
|
|
test('Check if the attribute is in a model attributes', () => {
|
|
|
|
expect(
|
2019-07-24 11:51:35 +02:00
|
|
|
isVisible(
|
|
|
|
createMockSchema({
|
|
|
|
field: {
|
|
|
|
type: 'string',
|
2019-07-23 12:45:54 +02:00
|
|
|
},
|
2019-07-24 11:51:35 +02:00
|
|
|
}),
|
2019-07-23 12:45:54 +02:00
|
|
|
'field'
|
|
|
|
)
|
|
|
|
).toBe(true);
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
expect(isVisible(createMockSchema({}), 'createdAt')).toBe(false);
|
2019-07-23 12:45:54 +02:00
|
|
|
});
|
2019-07-23 10:54:44 +02:00
|
|
|
});
|
|
|
|
});
|