test: refactor all test directories into one
This commit is contained in:
24
test/array-update/config.ts
Normal file
24
test/array-update/config.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { buildConfig } from '../buildConfig';
|
||||
|
||||
export default buildConfig({
|
||||
collections: [{
|
||||
slug: 'arrays',
|
||||
fields: [
|
||||
{
|
||||
name: 'array',
|
||||
type: 'array',
|
||||
fields: [
|
||||
{
|
||||
type: 'text',
|
||||
name: 'required',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
type: 'text',
|
||||
name: 'optional',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}],
|
||||
});
|
||||
109
test/array-update/int.spec.ts
Normal file
109
test/array-update/int.spec.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { initPayloadTest } from '../helpers/configHelpers';
|
||||
import payload from '../../src';
|
||||
import config from './config';
|
||||
import type { Array as ArrayCollection } from './payload-types';
|
||||
|
||||
const collection = config.collections[0]?.slug;
|
||||
|
||||
describe('array-update', () => {
|
||||
beforeAll(async () => {
|
||||
await initPayloadTest({ __dirname });
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await mongoose.connection.dropDatabase();
|
||||
await mongoose.connection.close();
|
||||
await payload.mongoMemoryServer.stop();
|
||||
});
|
||||
|
||||
it('should persist existing array-based data while updating and passing row ID', async () => {
|
||||
const originalText = 'some optional text';
|
||||
|
||||
const doc = await payload.create({
|
||||
collection,
|
||||
data: {
|
||||
array: [
|
||||
{
|
||||
required: 'a required field here',
|
||||
optional: originalText,
|
||||
},
|
||||
{
|
||||
required: 'another required field here',
|
||||
optional: 'this is cool',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const arrayWithExistingValues = [
|
||||
...doc.array,
|
||||
];
|
||||
|
||||
const updatedText = 'this is some new text for the first item in array';
|
||||
|
||||
arrayWithExistingValues[0] = {
|
||||
id: arrayWithExistingValues[0].id,
|
||||
required: updatedText,
|
||||
};
|
||||
|
||||
|
||||
const updatedDoc = await payload.update<ArrayCollection>({
|
||||
id: doc.id,
|
||||
collection,
|
||||
data: {
|
||||
array: arrayWithExistingValues,
|
||||
},
|
||||
});
|
||||
|
||||
expect(updatedDoc.array?.[0]).toMatchObject({
|
||||
required: updatedText,
|
||||
optional: originalText,
|
||||
});
|
||||
});
|
||||
|
||||
it('should disregard existing array-based data while updating and NOT passing row ID', async () => {
|
||||
const updatedText = 'here is some new text';
|
||||
|
||||
const secondArrayItem = {
|
||||
required: 'test',
|
||||
optional: 'optional test',
|
||||
};
|
||||
|
||||
const doc = await payload.create<ArrayCollection>({
|
||||
collection,
|
||||
data: {
|
||||
array: [
|
||||
{
|
||||
required: 'a required field here',
|
||||
optional: 'some optional text',
|
||||
},
|
||||
secondArrayItem,
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const updatedDoc = await payload.update<ArrayCollection>({
|
||||
id: doc.id,
|
||||
collection,
|
||||
data: {
|
||||
array: [
|
||||
{
|
||||
required: updatedText,
|
||||
},
|
||||
{
|
||||
id: doc.array?.[1].id,
|
||||
required: doc.array?.[1].required as string,
|
||||
// NOTE - not passing optional field. It should persist
|
||||
// because we're passing ID
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
expect(updatedDoc.array?.[0].required).toStrictEqual(updatedText);
|
||||
expect(updatedDoc.array?.[0].optional).toBeUndefined();
|
||||
|
||||
expect(updatedDoc.array?.[1]).toMatchObject(secondArrayItem);
|
||||
});
|
||||
});
|
||||
36
test/array-update/payload-types.ts
Normal file
36
test/array-update/payload-types.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/* tslint:disable */
|
||||
/**
|
||||
* This file was automatically generated by Payload CMS.
|
||||
* DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,
|
||||
* and re-run `payload generate:types` to regenerate this file.
|
||||
*/
|
||||
|
||||
export interface Config {}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "arrays".
|
||||
*/
|
||||
export interface Array {
|
||||
id: string;
|
||||
array?: {
|
||||
required: string;
|
||||
optional?: string;
|
||||
id?: string;
|
||||
}[];
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "users".
|
||||
*/
|
||||
export interface User {
|
||||
id: string;
|
||||
email?: string;
|
||||
resetPasswordToken?: string;
|
||||
resetPasswordExpiration?: string;
|
||||
loginAttempts?: number;
|
||||
lockUntil?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
Reference in New Issue
Block a user