Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18966 from strapi/fix/deep-populate-dynamiczone
fix deep query populate issue on dynamic zone
- Loading branch information
Showing
5 changed files
with
295 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
265 changes: 265 additions & 0 deletions
265
packages/core/utils/src/__tests__/query-populate.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,265 @@ | ||
import { traverseQueryPopulate } from '../traverse'; | ||
import { setGlobalStrapi, getStrapiFactory } from './test-utils'; | ||
|
||
describe('traverseQueryPopulate', () => { | ||
test('should return an empty object incase no populatable field exists', async () => { | ||
const query = await traverseQueryPopulate(jest.fn(), { | ||
schema: { | ||
kind: 'collectionType', | ||
attributes: { | ||
title: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
})('*'); | ||
|
||
expect(query).toEqual({}); | ||
}); | ||
|
||
test('should return all populatable fields', async () => { | ||
const strapi = getStrapiFactory({ | ||
getModel: jest.fn((uid) => { | ||
return { | ||
uid, | ||
attributes: { | ||
street: { | ||
type: 'string', | ||
}, | ||
}, | ||
}; | ||
}), | ||
db: { | ||
metadata: { | ||
get: jest.fn(() => ({ | ||
columnToAttribute: { | ||
address: 'address', | ||
some: 'some', | ||
}, | ||
})), | ||
}, | ||
}, | ||
})(); | ||
|
||
setGlobalStrapi(strapi); | ||
|
||
const query = await traverseQueryPopulate(jest.fn(), { | ||
schema: { | ||
kind: 'collectionType', | ||
attributes: { | ||
title: { | ||
type: 'string', | ||
}, | ||
address: { | ||
type: 'relation', | ||
relation: 'oneToOne', | ||
target: 'api::address.address', | ||
}, | ||
some: { | ||
type: 'relation', | ||
relation: 'ManyToMany', | ||
target: 'api::some.some', | ||
}, | ||
}, | ||
}, | ||
})('*'); | ||
|
||
expect(query).toEqual({ address: true, some: true }); | ||
}); | ||
|
||
test('should return only selected populatable field', async () => { | ||
const strapi = getStrapiFactory({ | ||
getModel: jest.fn((uid) => { | ||
return { | ||
uid, | ||
attributes: { | ||
street: { | ||
type: 'string', | ||
}, | ||
}, | ||
}; | ||
}), | ||
db: { | ||
metadata: { | ||
get: jest.fn(() => ({ | ||
columnToAttribute: { | ||
address: 'address', | ||
}, | ||
})), | ||
}, | ||
}, | ||
})(); | ||
|
||
setGlobalStrapi(strapi); | ||
|
||
const query = await traverseQueryPopulate(jest.fn(), { | ||
schema: { | ||
kind: 'collectionType', | ||
attributes: { | ||
title: { | ||
type: 'string', | ||
}, | ||
address: { | ||
type: 'relation', | ||
relation: 'oneToOne', | ||
target: 'api::address.address', | ||
}, | ||
some: { | ||
type: 'relation', | ||
relation: 'ManyToMany', | ||
target: 'api::some.some', | ||
}, | ||
}, | ||
}, | ||
})('address'); | ||
|
||
expect(query).toEqual('address'); | ||
}); | ||
|
||
test('should populate dynamiczone', async () => { | ||
const strapi = getStrapiFactory({ | ||
getModel: jest.fn((uid) => { | ||
return { | ||
uid, | ||
attributes: { | ||
street: { | ||
type: 'string', | ||
}, | ||
}, | ||
}; | ||
}), | ||
db: { | ||
metadata: { | ||
get: jest.fn(() => ({ | ||
columnToAttribute: { | ||
address: 'address', | ||
}, | ||
})), | ||
}, | ||
}, | ||
})(); | ||
|
||
setGlobalStrapi(strapi); | ||
|
||
const query = await traverseQueryPopulate(jest.fn(), { | ||
schema: { | ||
kind: 'collectionType', | ||
attributes: { | ||
title: { | ||
type: 'string', | ||
}, | ||
address: { | ||
type: 'relation', | ||
relation: 'oneToOne', | ||
target: 'api::address.address', | ||
}, | ||
some: { | ||
type: 'relation', | ||
relation: 'ManyToMany', | ||
target: 'api::some.some', | ||
}, | ||
zone: { | ||
type: 'dynamiczone', | ||
components: ['blog.test-como', 'some.another-como'], | ||
}, | ||
}, | ||
}, | ||
})('*'); | ||
|
||
expect(query).toEqual({ | ||
address: true, | ||
some: true, | ||
zone: true, | ||
}); | ||
}); | ||
|
||
test('should deep populate dynamiczone components', async () => { | ||
const strapi = getStrapiFactory({ | ||
getModel: jest.fn((uid) => { | ||
if (uid === 'blog.test-como') { | ||
return { | ||
uid, | ||
attributes: { | ||
street: { | ||
type: 'string', | ||
}, | ||
address: { | ||
type: 'relation', | ||
relation: 'oneToOne', | ||
target: 'api::address.address', | ||
}, | ||
}, | ||
}; | ||
} | ||
if (uid === 'some.another-como') { | ||
return { | ||
uid, | ||
attributes: { | ||
street: { | ||
type: 'string', | ||
}, | ||
some: { | ||
type: 'relation', | ||
relation: 'ManyToMany', | ||
target: 'api::some.some', | ||
}, | ||
}, | ||
}; | ||
} | ||
return { | ||
uid, | ||
attributes: { | ||
street: { | ||
type: 'string', | ||
}, | ||
}, | ||
}; | ||
}), | ||
db: { | ||
metadata: { | ||
get: jest.fn(() => ({ | ||
columnToAttribute: { | ||
address: 'address', | ||
}, | ||
})), | ||
}, | ||
}, | ||
})(); | ||
|
||
setGlobalStrapi(strapi); | ||
|
||
const query = await traverseQueryPopulate(jest.fn(), { | ||
schema: { | ||
kind: 'collectionType', | ||
attributes: { | ||
title: { | ||
type: 'string', | ||
}, | ||
address: { | ||
type: 'relation', | ||
relation: 'oneToOne', | ||
target: 'api::address.address', | ||
}, | ||
some: { | ||
type: 'relation', | ||
relation: 'ManyToMany', | ||
target: 'api::some.some', | ||
}, | ||
zone: { | ||
type: 'dynamiczone', | ||
components: ['blog.test-como', 'some.another-como'], | ||
}, | ||
}, | ||
}, | ||
})({ zone: { populate: '*' } }); | ||
|
||
expect(query).toEqual({ | ||
zone: { | ||
populate: { | ||
address: true, | ||
some: true, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { LoadedStrapi } from '@strapi/types'; | ||
|
||
/** | ||
* Update the global store with the given strapi value | ||
*/ | ||
export const setGlobalStrapi = (strapi: LoadedStrapi): void => { | ||
(global as unknown as Global).strapi = strapi; | ||
}; | ||
|
||
/** | ||
* Create a "Strapi" like object factory based on the | ||
* given params and cast it to the correct type | ||
*/ | ||
export const getStrapiFactory = | ||
< | ||
T extends { | ||
[key in keyof Partial<LoadedStrapi>]: unknown; | ||
} | ||
>( | ||
properties?: T | ||
) => | ||
(additionalProperties?: Partial<T>) => { | ||
return { ...properties, ...additionalProperties } as LoadedStrapi; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters