test: query populate

This commit is contained in:
Marc-Roig 2023-05-24 18:03:16 +02:00
parent f3cc11e825
commit e3e3cfe08a
No known key found for this signature in database
GPG Key ID: FB4E2C43A0BEE249

View File

@ -0,0 +1,139 @@
'use strict';
const { getQueryPopulate } = require('../populate');
const getFilterQuery = (conditions) => ({
filters: {
$or: [{
$and: [{
$or: conditions
}]
}]
}
})
const uid = 'model';
describe('Populate', () => {
const fakeModels = {
empty: {
uid: 'empty',
attributes: {},
},
model: {
uid: 'model',
attributes: {
field: {
type: 'string',
},
relation: {
type: 'relation',
relation: 'oneToMany',
target: 'model',
},
component: {
type: 'component',
component: 'component',
},
repeatableComponent: {
type: 'component',
repeatable: true,
component: 'component',
},
media: {
type: 'media',
}
},
},
component: {
uid: 'component',
attributes: {
field: {
type: 'string',
},
compoRelation: {
type: 'relation',
relation: 'oneToMany',
target: 'model',
}
},
},
};
describe('getQueryPopulate', () => {
beforeEach(() => {
global.strapi = {
contentType: jest.fn((uid) => fakeModels[uid]),
getModel: jest.fn((uid) => fakeModels[uid]),
db: {
metadata: {
get: jest.fn((uid) => ({ ...fakeModels[uid], columnToAttribute: {} })),
}
}
};
});
afterEach(() => {
jest.clearAllMocks();
});
test('top level field should not be populated', async () => {
const query = getFilterQuery([{ field: { $exists: true } }]);
const result = await getQueryPopulate(uid, query);
expect(result).toEqual({});
});
test('one relational field should be populated', async () => {
const query = getFilterQuery([{ relation: { field: "value" } }]);
const result = await getQueryPopulate(uid, query);
expect(result).toEqual({
relation: { fields: ['field'] }
});
});
test('relation in component should be populated', async () => {
const query = getFilterQuery([{ component: { compoRelation: { field: "value" } } }]);
const result = await getQueryPopulate(uid, query);
expect(result).toEqual({
component: { populate: { compoRelation: { fields: ['field'] } }, fields: [] }
});
});
test('relation in repeatable component should be populated', async () => {
const query = getFilterQuery([{ repeatableComponent: { compoRelation: { field: "value" } } }]);
const result = await getQueryPopulate(uid, query);
expect(result).toEqual({
repeatableComponent: { populate: { compoRelation: { fields: ['field'] } }, fields: [] }
});
});
test('populate multiple fields at once', async () => {
const query = getFilterQuery([
{ relation: { component: { field: { $eq: "value" } } } },
{ relation: { field: "value" } },
{ repeatableComponent: { $elemMatch: { compoRelation: { field: "value" } } } }
]);
const result = await getQueryPopulate(uid, query);
expect(result).toEqual({
relation: { fields: ['field'], populate: { component: { fields: ['field'] } } },
repeatableComponent: { populate: { compoRelation: { fields: ['field'] } }, fields: [] }
});
});
});
});