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: {
|
|
|
|
type: 'timestampUpdate',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
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
|
|
|
|
|
|
|
test('Group fields are not sortable', () => {
|
2019-07-24 11:51:35 +02:00
|
|
|
const schema = createMockSchema({
|
|
|
|
someGroup: {
|
|
|
|
type: 'group',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(isSortable(schema, 'someGroup')).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
|
|
|
});
|
|
|
|
|
|
|
|
test('Relations are not sortable', () => {
|
2019-07-24 11:51:35 +02:00
|
|
|
const schema = createMockSchema({
|
|
|
|
oneWayRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
},
|
|
|
|
manyWayRel: {
|
|
|
|
type: 'relation',
|
|
|
|
targetModel: 'someModel',
|
|
|
|
},
|
|
|
|
});
|
2019-07-23 12:45:54 +02:00
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
expect(isSortable(schema, 'oneWayRel')).toBe(false);
|
|
|
|
expect(isSortable(schema, 'manyWayRel')).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
|
|
|
});
|
|
|
|
});
|