test: field default values
This commit is contained in:
@@ -1,12 +1,34 @@
|
||||
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 = {
|
||||
slug: 'array-fields',
|
||||
slug: arrayFieldsSlug,
|
||||
fields: [
|
||||
{
|
||||
name: 'items',
|
||||
type: 'array',
|
||||
required: true,
|
||||
defaultValue: arrayDefaultValue,
|
||||
fields: [
|
||||
{
|
||||
name: 'text',
|
||||
type: 'text',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'localized',
|
||||
type: 'array',
|
||||
required: true,
|
||||
localized: true,
|
||||
defaultValue: arrayDefaultValue,
|
||||
fields: [
|
||||
{
|
||||
name: 'text',
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
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 = {
|
||||
slug: 'group-fields',
|
||||
slug: groupFieldsSlug,
|
||||
versions: true,
|
||||
fields: [
|
||||
{
|
||||
label: 'Group Field',
|
||||
name: 'group',
|
||||
type: 'group',
|
||||
defaultValue: {
|
||||
defaultParent: groupDefaultValue,
|
||||
},
|
||||
admin: {
|
||||
description: 'This is a group.',
|
||||
},
|
||||
@@ -16,6 +23,17 @@ const GroupFields: CollectionConfig = {
|
||||
name: 'text',
|
||||
type: 'text',
|
||||
required: true,
|
||||
defaultValue: groupDefaultValue,
|
||||
},
|
||||
{
|
||||
name: 'defaultParent',
|
||||
type: 'text',
|
||||
defaultValue: groupDefaultChild,
|
||||
},
|
||||
{
|
||||
name: 'defaultChild',
|
||||
type: 'text',
|
||||
defaultValue: groupDefaultChild,
|
||||
},
|
||||
{
|
||||
name: 'subGroup',
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import type { CollectionConfig } from '../../../../src/collections/config/types';
|
||||
|
||||
export const defaultText = 'default-text';
|
||||
|
||||
const TextFields: CollectionConfig = {
|
||||
slug: 'text-fields',
|
||||
admin: {
|
||||
@@ -11,6 +13,20 @@ const TextFields: CollectionConfig = {
|
||||
type: 'text',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'defaultFunction',
|
||||
type: 'text',
|
||||
defaultValue: () => (defaultText),
|
||||
},
|
||||
{
|
||||
name: 'defaultAsync',
|
||||
type: 'text',
|
||||
defaultValue: async (): Promise<string> => {
|
||||
return new Promise((resolve) => setTimeout(() => {
|
||||
resolve(defaultText);
|
||||
}, 1));
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
|
||||
@@ -3,6 +3,10 @@ import { RESTClient } from '../helpers/rest';
|
||||
import config from '../uploads/config';
|
||||
import payload from '../../src';
|
||||
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;
|
||||
|
||||
@@ -15,6 +19,23 @@ describe('Fields', () => {
|
||||
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', () => {
|
||||
let doc;
|
||||
|
||||
@@ -26,7 +47,7 @@ describe('Fields', () => {
|
||||
[doc] = findDoc.docs;
|
||||
});
|
||||
|
||||
it('read', async () => {
|
||||
it('should read', async () => {
|
||||
const find = await payload.find({
|
||||
collection: 'point-fields',
|
||||
pagination: false,
|
||||
@@ -39,7 +60,7 @@ describe('Fields', () => {
|
||||
expect(doc.group).toMatchObject(pointDoc.group);
|
||||
});
|
||||
|
||||
it('creates', async () => {
|
||||
it('should create', async () => {
|
||||
const point = [7, -7];
|
||||
const localized = [5, -2];
|
||||
const group = { point: [1, 9] };
|
||||
@@ -57,4 +78,77 @@ describe('Fields', () => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,6 +16,10 @@ export interface ArrayField {
|
||||
text: string;
|
||||
id?: string;
|
||||
}[];
|
||||
localized: {
|
||||
text: string;
|
||||
id?: string;
|
||||
}[];
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
@@ -97,6 +101,8 @@ export interface GroupField {
|
||||
id: string;
|
||||
group?: {
|
||||
text: string;
|
||||
defaultParent?: string;
|
||||
defaultChild?: string;
|
||||
subGroup?: {
|
||||
textWithinGroup?: string;
|
||||
};
|
||||
@@ -212,6 +218,8 @@ export interface TabsField {
|
||||
export interface TextField {
|
||||
id: string;
|
||||
text: string;
|
||||
defaultFunction?: string;
|
||||
defaultAsync?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user