feat: improve field ops (#3172)
Co-authored-by: PatrikKozak <patrik@trbl.design>
This commit is contained in:
@@ -57,6 +57,26 @@ const TextFields: CollectionConfig = {
|
||||
type: 'text',
|
||||
maxLength: 50000,
|
||||
},
|
||||
{
|
||||
name: 'fieldWithDefaultValue',
|
||||
type: 'text',
|
||||
defaultValue: async () => {
|
||||
const defaultValue = new Promise((resolve) => setTimeout(() => resolve('some-value'), 1000));
|
||||
|
||||
return defaultValue;
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'dependentOnFieldWithDefaultValue',
|
||||
type: 'text',
|
||||
hooks: {
|
||||
beforeChange: [
|
||||
({ data }) => {
|
||||
return data?.fieldWithDefaultValue || '';
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
|
||||
@@ -47,6 +47,15 @@ describe('Fields', () => {
|
||||
expect(doc.defaultFunction).toEqual(defaultText);
|
||||
expect(doc.defaultAsync).toEqual(defaultText);
|
||||
});
|
||||
|
||||
it('should populate default values in beforeValidate hook', async () => {
|
||||
const { fieldWithDefaultValue, dependentOnFieldWithDefaultValue } = await payload.create({
|
||||
collection: 'text-fields',
|
||||
data: { text },
|
||||
});
|
||||
|
||||
await expect(fieldWithDefaultValue).toEqual(dependentOnFieldWithDefaultValue);
|
||||
});
|
||||
});
|
||||
|
||||
describe('timestamps', () => {
|
||||
|
||||
Reference in New Issue
Block a user