mirror of
https://github.com/strapi/strapi.git
synced 2025-12-25 06:04:29 +00:00
Update tests & add type util to convert query params
This commit is contained in:
parent
b784135413
commit
c49df55a67
@ -108,6 +108,11 @@ const fixtures = {
|
||||
number: 2,
|
||||
field: 'short string',
|
||||
},
|
||||
{
|
||||
__component: 'default.foo',
|
||||
number: 3,
|
||||
field: 'long string',
|
||||
},
|
||||
{
|
||||
__component: 'default.bar',
|
||||
title: 'this is a title',
|
||||
@ -328,8 +333,19 @@ describe('Populate filters', () => {
|
||||
|
||||
expect(status).toBe(200);
|
||||
expect(body.data).toHaveLength(2);
|
||||
expect(body.data[0].attributes.dz).toHaveLength(3);
|
||||
expect(body.data[1].attributes.dz).toHaveLength(1);
|
||||
|
||||
fixtures.b.forEach((fixture, i) => {
|
||||
const res = body.data[i];
|
||||
const { dz } = res.attributes;
|
||||
|
||||
expect(dz).toHaveLength(fixture.dz.length);
|
||||
expect(dz).toMatchObject(
|
||||
fixture.dz.map((component) => ({
|
||||
...omit('field', component),
|
||||
id: expect.any(Number),
|
||||
}))
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test('Populate only one component type using fragment', async () => {
|
||||
@ -347,8 +363,18 @@ describe('Populate filters', () => {
|
||||
|
||||
expect(status).toBe(200);
|
||||
expect(body.data).toHaveLength(2);
|
||||
expect(body.data[0].attributes.dz).toHaveLength(2);
|
||||
|
||||
expect(body.data[0].attributes.dz).toHaveLength(3);
|
||||
expect(body.data[1].attributes.dz).toHaveLength(0);
|
||||
|
||||
const expected = fixtures.b[0].dz
|
||||
.filter(({ __component }) => __component === 'default.foo')
|
||||
.map((component) => ({
|
||||
...component,
|
||||
id: expect.any(Number),
|
||||
}));
|
||||
|
||||
expect(body.data[0].attributes.dz).toMatchObject(expected);
|
||||
});
|
||||
|
||||
test('Populate the dynamic zone with filters in fragments', async () => {
|
||||
@ -357,7 +383,7 @@ describe('Populate filters', () => {
|
||||
dz: {
|
||||
on: {
|
||||
'default.foo': {
|
||||
filters: { number: { $lt: 2 } },
|
||||
filters: { number: { $lt: 3 } },
|
||||
},
|
||||
'default.bar': {
|
||||
filters: { title: { $contains: 'another' } },
|
||||
@ -371,8 +397,23 @@ describe('Populate filters', () => {
|
||||
|
||||
expect(status).toBe(200);
|
||||
expect(body.data).toHaveLength(2);
|
||||
expect(body.data[0].attributes.dz).toHaveLength(1);
|
||||
expect(body.data[0].attributes.dz).toHaveLength(2);
|
||||
expect(body.data[1].attributes.dz).toHaveLength(1);
|
||||
|
||||
const filter = (data = []) =>
|
||||
data
|
||||
.filter(({ __component, number, title }) => {
|
||||
if (__component === 'default.foo') return number < 3;
|
||||
if (__component === 'default.bar') return title.includes('another');
|
||||
return false;
|
||||
})
|
||||
.map((component) => ({
|
||||
...(component.__component === 'default.foo' ? component : omit('field', component)),
|
||||
id: expect.any(Number),
|
||||
}));
|
||||
|
||||
expect(body.data[0].attributes.dz).toMatchObject(filter(fixtures.b[0].dz));
|
||||
expect(body.data[1].attributes.dz).toMatchObject(filter(fixtures.b[1].dz));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -104,12 +104,16 @@ const isPrivateAttribute = (model = {}, attributeName) => {
|
||||
};
|
||||
|
||||
const isScalarAttribute = (attribute) => {
|
||||
return !['media', 'component', 'relation', 'dynamiczone'].includes(attribute.type);
|
||||
return !['media', 'component', 'relation', 'dynamiczone'].includes(attribute?.type);
|
||||
};
|
||||
const isMediaAttribute = (attribute) => attribute?.type === 'media';
|
||||
const isRelationalAttribute = (attribute) => attribute?.type === 'relation';
|
||||
const isComponentAttribute = (attribute) => ['component', 'dynamiczone'].includes(attribute?.type);
|
||||
|
||||
const isDynamicZoneAttribute = (attribute) => attribute?.type === 'dynamiczone';
|
||||
const isMorphToRelationalAttribute = (attribute) => {
|
||||
return isRelationalAttribute(attribute) && attribute?.relation?.startsWith?.('morphTo');
|
||||
};
|
||||
const isMediaAttribute = (attribute) => attribute && attribute.type === 'media';
|
||||
const isRelationalAttribute = (attribute) => attribute && attribute.type === 'relation';
|
||||
const isComponentAttribute = (attribute) =>
|
||||
attribute && ['component', 'dynamiczone'].includes(attribute.type);
|
||||
|
||||
const getComponentAttributes = (schema) => {
|
||||
return _.reduce(
|
||||
@ -158,6 +162,8 @@ module.exports = {
|
||||
isMediaAttribute,
|
||||
isRelationalAttribute,
|
||||
isComponentAttribute,
|
||||
isDynamicZoneAttribute,
|
||||
isMorphToRelationalAttribute,
|
||||
isTypedAttribute,
|
||||
getPrivateAttributes,
|
||||
isPrivateAttribute,
|
||||
|
||||
@ -23,6 +23,11 @@ const _ = require('lodash');
|
||||
const parseType = require('./parse-type');
|
||||
const contentTypesUtils = require('./content-types');
|
||||
const { PaginationError } = require('./errors');
|
||||
const {
|
||||
isMediaAttribute,
|
||||
isDynamicZoneAttribute,
|
||||
isMorphToRelationalAttribute,
|
||||
} = require('./content-types');
|
||||
|
||||
const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
|
||||
|
||||
@ -188,9 +193,9 @@ const convertPopulateObject = (populate, schema) => {
|
||||
|
||||
// Allow adding an 'on' strategy to populate queries for polymorphic relations, media and dynamic zones
|
||||
const isAllowedAttributeForFragmentPopulate =
|
||||
attribute.type === 'dynamiczone' ||
|
||||
attribute.type === 'media' ||
|
||||
(attribute.relation && attribute.relation.startsWith('morphTo'));
|
||||
isDynamicZoneAttribute(attribute) ||
|
||||
isMediaAttribute(attribute) ||
|
||||
isMorphToRelationalAttribute(attribute);
|
||||
|
||||
const hasFragmentPopulateDefined = typeof subPopulate === 'object' && 'on' in subPopulate;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user