feat: improve field ops (#3172)

Co-authored-by: PatrikKozak <patrik@trbl.design>
This commit is contained in:
James Mikrut
2023-08-16 11:00:52 -04:00
committed by GitHub
parent e03a8e6b03
commit d91b44cbb3
4 changed files with 57 additions and 28 deletions

View File

@@ -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 || '';
},
],
},
},
],
};

View File

@@ -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', () => {