test: field default values

This commit is contained in:
Dan Ribbens
2022-07-18 14:25:18 -04:00
parent cc8b636248
commit 6826e44072
5 changed files with 162 additions and 4 deletions

View File

@@ -1,12 +1,34 @@
import type { CollectionConfig } from '../../../../src/collections/config/types'; import type { CollectionConfig } from '../../../../src/collections/config/types';
export const arrayDefaultValue = [
{ text: 'row one' },
{ text: 'row two' },
];
export const arrayFieldsSlug = 'array-fields';
const ArrayFields: CollectionConfig = { const ArrayFields: CollectionConfig = {
slug: 'array-fields', slug: arrayFieldsSlug,
fields: [ fields: [
{ {
name: 'items', name: 'items',
type: 'array', type: 'array',
required: true, required: true,
defaultValue: arrayDefaultValue,
fields: [
{
name: 'text',
type: 'text',
required: true,
},
],
},
{
name: 'localized',
type: 'array',
required: true,
localized: true,
defaultValue: arrayDefaultValue,
fields: [ fields: [
{ {
name: 'text', name: 'text',

View File

@@ -1,13 +1,20 @@
import type { CollectionConfig } from '../../../../src/collections/config/types'; import type { CollectionConfig } from '../../../../src/collections/config/types';
export const groupDefaultValue = 'set from parent';
export const groupDefaultChild = 'child takes priority';
export const groupFieldsSlug = 'group-fields';
const GroupFields: CollectionConfig = { const GroupFields: CollectionConfig = {
slug: 'group-fields', slug: groupFieldsSlug,
versions: true, versions: true,
fields: [ fields: [
{ {
label: 'Group Field', label: 'Group Field',
name: 'group', name: 'group',
type: 'group', type: 'group',
defaultValue: {
defaultParent: groupDefaultValue,
},
admin: { admin: {
description: 'This is a group.', description: 'This is a group.',
}, },
@@ -16,6 +23,17 @@ const GroupFields: CollectionConfig = {
name: 'text', name: 'text',
type: 'text', type: 'text',
required: true, required: true,
defaultValue: groupDefaultValue,
},
{
name: 'defaultParent',
type: 'text',
defaultValue: groupDefaultChild,
},
{
name: 'defaultChild',
type: 'text',
defaultValue: groupDefaultChild,
}, },
{ {
name: 'subGroup', name: 'subGroup',

View File

@@ -1,5 +1,7 @@
import type { CollectionConfig } from '../../../../src/collections/config/types'; import type { CollectionConfig } from '../../../../src/collections/config/types';
export const defaultText = 'default-text';
const TextFields: CollectionConfig = { const TextFields: CollectionConfig = {
slug: 'text-fields', slug: 'text-fields',
admin: { admin: {
@@ -11,6 +13,20 @@ const TextFields: CollectionConfig = {
type: 'text', type: 'text',
required: true, required: true,
}, },
{
name: 'defaultFunction',
type: 'text',
defaultValue: () => (defaultText),
},
{
name: 'defaultAsync',
type: 'text',
defaultValue: async (): Promise<string> => {
return new Promise((resolve) => setTimeout(() => {
resolve(defaultText);
}, 1));
},
},
], ],
}; };

View File

@@ -3,6 +3,10 @@ import { RESTClient } from '../helpers/rest';
import config from '../uploads/config'; import config from '../uploads/config';
import payload from '../../src'; import payload from '../../src';
import { pointDoc } from './collections/Point'; import { pointDoc } from './collections/Point';
import type { ArrayField, GroupField } from './payload-types';
import { arrayFieldsSlug, arrayDefaultValue } from './collections/Array';
import { groupFieldsSlug, groupDefaultChild, groupDefaultValue } from './collections/Group';
import { defaultText } from './collections/Text';
let client; let client;
@@ -15,6 +19,23 @@ describe('Fields', () => {
done(); done();
}); });
describe('text', () => {
let doc;
const text = 'text field';
beforeAll(async () => {
doc = await payload.create({
collection: 'text-fields',
data: { text },
});
});
it('creates with default values', () => {
expect(doc.text).toEqual(text);
expect(doc.defaultFunction).toEqual(defaultText);
expect(doc.defaultAsync).toEqual(defaultText);
});
});
describe('point', () => { describe('point', () => {
let doc; let doc;
@@ -26,7 +47,7 @@ describe('Fields', () => {
[doc] = findDoc.docs; [doc] = findDoc.docs;
}); });
it('read', async () => { it('should read', async () => {
const find = await payload.find({ const find = await payload.find({
collection: 'point-fields', collection: 'point-fields',
pagination: false, pagination: false,
@@ -39,7 +60,7 @@ describe('Fields', () => {
expect(doc.group).toMatchObject(pointDoc.group); expect(doc.group).toMatchObject(pointDoc.group);
}); });
it('creates', async () => { it('should create', async () => {
const point = [7, -7]; const point = [7, -7];
const localized = [5, -2]; const localized = [5, -2];
const group = { point: [1, 9] }; const group = { point: [1, 9] };
@@ -57,4 +78,77 @@ describe('Fields', () => {
expect(doc.group).toMatchObject(group); expect(doc.group).toMatchObject(group);
}); });
}); });
describe('array', () => {
let doc;
const collection = arrayFieldsSlug;
beforeAll(async () => {
doc = await payload.create<ArrayField>({
collection,
data: {},
});
});
it('should create with defaultValue', async () => {
expect(doc.items).toMatchObject(arrayDefaultValue);
expect(doc.localized).toMatchObject(arrayDefaultValue);
});
it('should update without overwriting other locales with defaultValue', async () => {
const localized = [{ text: 'unique' }];
const enText = 'english';
const esText = 'spanish';
const { id } = await payload.create<ArrayField>({
collection,
data: {
localized,
},
});
const enDoc = await payload.update<ArrayField>({
collection,
id,
locale: 'en',
data: {
localized: [{ text: enText }],
},
});
const esDoc = await payload.update<ArrayField>({
collection,
id,
locale: 'es',
data: {
localized: [{ text: esText }],
},
});
const allLocales = await payload.findByID({
collection,
id,
locale: 'all',
}) as unknown as {localized: {en: unknown, es: unknown}};
expect(enDoc.localized[0].text).toStrictEqual(enText);
expect(esDoc.localized[0].text).toStrictEqual(esText);
expect(allLocales.localized.en[0].text).toStrictEqual(enText);
expect(allLocales.localized.es[0].text).toStrictEqual(esText);
});
});
describe('group', () => {
let groupDoc;
beforeAll(async () => {
groupDoc = await payload.create<GroupField>({
collection: groupFieldsSlug,
data: {},
});
});
it('should create with defaultValue', async () => {
expect(groupDoc.group.defaultParent).toStrictEqual(groupDefaultValue);
expect(groupDoc.group.defaultChild).toStrictEqual(groupDefaultChild);
});
});
}); });

View File

@@ -16,6 +16,10 @@ export interface ArrayField {
text: string; text: string;
id?: string; id?: string;
}[]; }[];
localized: {
text: string;
id?: string;
}[];
createdAt: string; createdAt: string;
updatedAt: string; updatedAt: string;
} }
@@ -97,6 +101,8 @@ export interface GroupField {
id: string; id: string;
group?: { group?: {
text: string; text: string;
defaultParent?: string;
defaultChild?: string;
subGroup?: { subGroup?: {
textWithinGroup?: string; textWithinGroup?: string;
}; };
@@ -212,6 +218,8 @@ export interface TabsField {
export interface TextField { export interface TextField {
id: string; id: string;
text: string; text: string;
defaultFunction?: string;
defaultAsync?: string;
createdAt: string; createdAt: string;
updatedAt: string; updatedAt: string;
} }