Skip to content

Commit

Permalink
Add tests for sanitize-query dz populate
Browse files Browse the repository at this point in the history
  • Loading branch information
Convly committed Mar 16, 2023
1 parent ae54810 commit 26ee279
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 5 deletions.
Expand Up @@ -16,6 +16,26 @@ module.exports = (fixtures) => {
name_private: 'Private Component A Name A',
password: 'compPass9101',
},
dz: [
{
__component: 'default.component-a',
name: 'Name A',
name_private: 'Private Name A',
password: 'Password A',
},
{
__component: 'default.component-b',
name: 'Name B',
name_private: 'Private Name B',
password: 'Password B',
},
{
__component: 'default.component-a',
name: 'Name C',
name_private: 'Private Name C',
password: 'Password C',
},
],
},
{
name: '1 Document B OO',
Expand All @@ -29,6 +49,26 @@ module.exports = (fixtures) => {
name_private: 'Private Component A Name B',
password: 'compPass5678',
},
dz: [
{
__component: 'default.component-a',
name: 'Name A',
name_private: 'Private Name A',
password: 'Password A',
},
{
__component: 'default.component-b',
name: 'Name B',
name_private: 'Private Name B',
password: 'Password B',
},
{
__component: 'default.component-a',
name: 'Name C',
name_private: 'Private Name C',
password: 'Password C',
},
],
},
{
name: '2 Document C OO',
Expand All @@ -42,6 +82,26 @@ module.exports = (fixtures) => {
name_private: 'Private Component A Name C',
password: 'compPass1234',
},
dz: [
{
__component: 'default.component-a',
name: 'Name A',
name_private: 'Private Name A',
password: 'Password A',
},
{
__component: 'default.component-b',
name: 'Name B',
name_private: 'Private Name B',
password: 'Password B',
},
{
__component: 'default.component-a',
name: 'Name C',
name_private: 'Private Name C',
password: 'Password C',
},
],
},
];
};
@@ -0,0 +1,18 @@
'use strict';

module.exports = {
displayName: 'component-b',
category: 'default',
attributes: {
name: {
type: 'string',
},
name_private: {
type: 'string',
private: true,
},
password: {
type: 'password',
},
},
};
Expand Up @@ -36,5 +36,9 @@ module.exports = {
component: 'default.component-a',
repeatable: false,
},
dz: {
type: 'dynamiczone',
components: ['default.component-a', 'default.component-b'],
},
},
};
Expand Up @@ -7,5 +7,6 @@ module.exports = {
},
components: {
'default.component-a': require('./component-a'),
'default.component-b': require('./component-b'),
},
};
83 changes: 78 additions & 5 deletions packages/core/strapi/tests/api/sanitize/sanitize-query.test.api.js
Expand Up @@ -131,7 +131,7 @@ describe('Core API - Sanitize', () => {
});
});

describe.only('Private modifier', () => {
describe('Private modifier', () => {
it.each([
['$endsWith', { name_private: { $endsWith: 'None' } }],
['$not > $endsWith', { name_private: { $not: { $endsWith: 'None' } } }],
Expand Down Expand Up @@ -805,10 +805,59 @@ describe('Core API - Sanitize', () => {
it.todo('Populates a media');

describe('Dynamic Zone', () => {
it.todo('Populates a dynamic-zone');
it.todo('Populates a dynamic-use using populate fragments');
it.each([{ dz: { populate: '*' } }, { dz: { populate: true } }, { dz: '*' }, { dz: true }])(
'Populates a dynamic-zone (%s)',
async (populate) => {
const res = await rq.get('/api/documents', { qs: { populate } });

checkAPIResultValidity(res);

res.body.data.forEach((document) => {
expect(document.attributes).toHaveProperty(
'dz',
expect.arrayContaining([
expect.objectContaining({ __component: expect.any(String) }),
])
);
expect(document.attributes.dz).toHaveLength(3);
});
}
);

it.each([
[{ dz: { on: { 'default.component-a': true } } }, 'default.component-a', 2],
[{ dz: { on: { 'default.component-b': true } } }, 'default.component-b', 1],
])(
'Populates a dynamic-use using populate fragments (%s)',
async (populate, componentUID, expectedLength) => {
const res = await rq.get('/api/documents', { qs: { populate } });

checkAPIResultValidity(res);

res.body.data.forEach((document) => {
expect(document.attributes).toHaveProperty(
'dz',
expect.arrayContaining([expect.objectContaining({ __component: componentUID })])
);
expect(document.attributes.dz).toHaveLength(expectedLength);
});
}
);

it(`Doesn't populate the dynamic zone on empty populate fragment definition`, async () => {
const res = await rq.get('/api/documents', { qs: { populate: { dz: { on: {} } } } });

checkAPIResultValidity(res);

res.body.data.forEach((document) => {
expect(document.attributes).not.toHaveProperty('dz');
});
});

it.todo('Nested filtering on dynamic zone populate');

it.todo('Nested field selection on dynamic zone populate');

it.todo(`Sort populated dynamic zone's components`);
});
});
Expand All @@ -832,7 +881,19 @@ describe('Core API - Sanitize', () => {

it.todo('Populates a media');

it.todo('Populates a dynamic-zone');
it('Populates a dynamic-zone', async () => {
const populate = 'dz';
const res = await rq.get('/api/documents', { qs: { populate } });

checkAPIResultValidity(res);

res.body.data.forEach((document) => {
expect(document.attributes).toHaveProperty(
'dz',
expect.arrayContaining([expect.objectContaining({ __component: expect.any(String) })])
);
});
});
});

describe('string[] notation', () => {
Expand All @@ -854,7 +915,19 @@ describe('Core API - Sanitize', () => {

it.todo('Populates a media');

it.todo('Populates a dynamic-zone');
it('Populates a dynamic-zone', async () => {
const populate = ['dz'];
const res = await rq.get('/api/documents', { qs: { populate } });

checkAPIResultValidity(res);

res.body.data.forEach((document) => {
expect(document.attributes).toHaveProperty(
'dz',
expect.arrayContaining([expect.objectContaining({ __component: expect.any(String) })])
);
});
});
});
});
});

0 comments on commit 26ee279

Please sign in to comment.