chore: merge tests

This commit is contained in:
Jarrod Flesch
2022-11-23 14:05:56 -05:00
parent 99c1f41e30
commit 56144c07f0
7 changed files with 159 additions and 108 deletions

View File

@@ -1,19 +1,19 @@
import * as React from 'react';
import useField from '../../../../../src/admin/components/forms/useField';
import { textFieldsSlug } from '../../Text';
import useField from '../../../src/admin/components/forms/useField';
import { collection1Slug } from '../collectionSlugs';
export const PrePopulateFieldUI: React.FC<{ path: string, hasMany?: boolean, hasMultipleRelations?: boolean }> = ({ path, hasMany = true, hasMultipleRelations = false }) => {
const { setValue } = useField({ path });
const addDefaults = React.useCallback(() => {
const fetchRelationDocs = async () => {
const res = await fetch(`/api/${textFieldsSlug}?limit=20&where[text][contains]=relationship-test`);
const res = await fetch(`/api/${collection1Slug}?limit=20&where[name][contains]=relationship-test`);
const json = await res.json();
if (hasMany) {
const docIds = json.docs.map((doc) => {
if (hasMultipleRelations) {
return {
relationTo: textFieldsSlug,
relationTo: collection1Slug,
value: doc.id,
};
}
@@ -28,7 +28,7 @@ export const PrePopulateFieldUI: React.FC<{ path: string, hasMany?: boolean, has
};
fetchRelationDocs();
}, [setValue, hasMultipleRelations]);
}, [setValue, hasMultipleRelations, hasMany]);
return (
<div

View File

@@ -0,0 +1,10 @@
export const slug = 'fields-relationship';
export const relationOneSlug = 'relation-one';
export const relationTwoSlug = 'relation-two';
export const relationRestrictedSlug = 'relation-restricted';
export const relationWithTitleSlug = 'relation-with-title';
export const relationUpdatedExternallySlug = 'relation-updated-externally';
export const collection1Slug = 'collection-1';
export const collection2Slug = 'collection-2';

View File

@@ -3,13 +3,8 @@ import { buildConfig } from '../buildConfig';
import { devUser } from '../credentials';
import { mapAsync } from '../../src/utilities/mapAsync';
import { FilterOptionsProps } from '../../src/fields/config/types';
export const slug = 'fields-relationship';
export const relationOneSlug = 'relation-one';
export const relationTwoSlug = 'relation-two';
export const relationRestrictedSlug = 'relation-restricted';
export const relationWithTitleSlug = 'relation-with-title';
import { PrePopulateFieldUI } from './PrePopulateFieldUI';
import { relationOneSlug, relationTwoSlug, relationRestrictedSlug, relationWithTitleSlug, relationUpdatedExternallySlug, collection1Slug, collection2Slug, slug } from './collectionSlugs';
export interface FieldsRelationship {
id: string;
@@ -93,8 +88,8 @@ export default buildConfig({
id: {
equals: args.data.relationship,
},
})
}
});
},
},
],
},
@@ -123,6 +118,103 @@ export default buildConfig({
},
fields: baseRelationshipFields,
},
{
slug: relationUpdatedExternallySlug,
admin: {
useAsTitle: 'name',
},
fields: [
{
type: 'row',
fields: [
{
name: 'relationPrePopulate',
type: 'relationship',
relationTo: collection1Slug,
admin: {
width: '75%',
},
},
{
type: 'ui',
name: 'prePopulate',
admin: {
width: '25%',
components: {
Field: () => PrePopulateFieldUI({ path: 'relationPrePopulate', hasMany: false }),
},
},
},
],
},
{
type: 'row',
fields: [
{
name: 'relationHasMany',
type: 'relationship',
relationTo: collection1Slug,
hasMany: true,
admin: {
width: '75%',
},
},
{
type: 'ui',
name: 'prePopulateRelationHasMany',
admin: {
width: '25%',
components: {
Field: () => PrePopulateFieldUI({ path: 'relationHasMany', hasMultipleRelations: false }),
},
},
},
],
},
{
type: 'row',
fields: [
{
name: 'relationToManyHasMany',
type: 'relationship',
relationTo: [collection1Slug, collection2Slug],
hasMany: true,
admin: {
width: '75%',
},
},
{
type: 'ui',
name: 'prePopulateToMany',
admin: {
width: '25%',
components: {
Field: () => PrePopulateFieldUI({ path: 'relationToManyHasMany', hasMultipleRelations: true }),
},
},
},
],
},
],
},
{
slug: collection1Slug,
fields: [
{
type: 'text',
name: 'name',
},
],
},
{
slug: collection2Slug,
fields: [
{
type: 'text',
name: 'name',
},
],
},
],
onInit: async (payload) => {
await payload.create({
@@ -213,5 +305,20 @@ export default buildConfig({
},
});
});
[...Array(15)].forEach((_, i) => {
payload.create({
collection: collection1Slug,
data: {
name: `relationship-test ${i}`,
},
});
payload.create({
collection: collection2Slug,
data: {
name: `relationship-test ${i}`,
},
});
});
},
});

View File

@@ -12,14 +12,14 @@ import type {
RelationTwo,
RelationWithTitle,
} from './config';
import { relationOneSlug, relationRestrictedSlug, relationTwoSlug, relationWithTitleSlug, slug } from './config';
import { relationOneSlug, relationRestrictedSlug, relationTwoSlug, relationUpdatedExternallySlug, relationWithTitleSlug, slug } from './collectionSlugs';
import wait from '../../src/utilities/wait';
const { beforeAll, beforeEach, describe } = test;
let url: AdminUrlUtil;
describe('fields - relationship', () => {
let url: AdminUrlUtil;
let page: Page;
let relationOneDoc: RelationOne;
let anotherRelationOneDoc: RelationOne;
@@ -28,9 +28,11 @@ describe('fields - relationship', () => {
let docWithExistingRelations: CollectionWithRelationships;
let restrictedRelation: RelationRestricted;
let relationWithTitle: RelationWithTitle;
let serverURL: string;
beforeAll(async ({ browser }) => {
const { serverURL } = await initPayloadE2E(__dirname);
const { serverURL: serverURLFromConfig } = await initPayloadE2E(__dirname);
serverURL = serverURLFromConfig;
url = new AdminUrlUtil(serverURL, slug);
@@ -263,6 +265,29 @@ describe('fields - relationship', () => {
await expect(relationship).toHaveText(relationWithTitle.name);
});
});
describe('externally update field', () => {
beforeAll(async () => {
url = new AdminUrlUtil(serverURL, relationUpdatedExternallySlug);
await page.goto(url.create);
});
test('has many, one collection', async () => {
await page.goto(url.create);
await page.locator('#field-relationHasMany + .pre-populate-field-ui button').click();
await wait(300);
expect(await page.locator('#field-relationHasMany .rs__value-container > .rs__multi-value').count()).toEqual(15);
});
test('has many, many collections', async () => {
await page.locator('#field-relationToManyHasMany + .pre-populate-field-ui button').click();
await wait(300);
expect(await page.locator('#field-relationToManyHasMany .rs__value-container > .rs__multi-value').count()).toEqual(15);
});
});
});
async function clearAllDocs(): Promise<void> {

View File

@@ -1,5 +1,4 @@
import type { CollectionConfig } from '../../../../src/collections/config/types';
import { PrePopulateFieldUI } from './PrePopulateFieldUI';
export const relationshipFieldsSlug = 'relationship-fields';
@@ -17,77 +16,6 @@ const RelationshipFields: CollectionConfig = {
type: 'relationship',
relationTo: relationshipFieldsSlug,
},
{
type: 'row',
fields: [
{
name: 'relationPrePopulate',
type: 'relationship',
relationTo: 'text-fields',
admin: {
width: '75%',
},
},
{
type: 'ui',
name: 'prePopulate',
admin: {
width: '25%',
components: {
Field: () => PrePopulateFieldUI({ path: 'relationPrePopulate', hasMany: false }),
},
},
},
],
},
{
type: 'row',
fields: [
{
name: 'relationHasMany',
type: 'relationship',
relationTo: 'text-fields',
hasMany: true,
admin: {
width: '75%',
},
},
{
type: 'ui',
name: 'prePopulateRelationHasMany',
admin: {
width: '25%',
components: {
Field: () => PrePopulateFieldUI({ path: 'relationHasMany', hasMultipleRelations: false }),
},
},
},
],
},
{
type: 'row',
fields: [
{
name: 'relationToManyHasMany',
type: 'relationship',
relationTo: ['text-fields', 'array-fields'],
hasMany: true,
admin: {
width: '75%',
},
},
{
type: 'ui',
name: 'prePopulateToMany',
admin: {
width: '25%',
components: {
Field: () => PrePopulateFieldUI({ path: 'relationToManyHasMany', hasMultipleRelations: true }),
},
},
},
],
},
],
};

View File

@@ -78,16 +78,6 @@ export default buildConfig({
await payload.create({ collection: 'date-fields', data: dateDoc });
await payload.create({ collection: 'code-fields', data: codeDoc });
[...Array(15)].forEach((_, i) => {
payload.create({
collection: textFieldsSlug,
data: {
text: `relationship-test ${i}`,
localizedText: `relationship-test ${i}`,
},
});
});
const createdTextDoc = await payload.create({ collection: textFieldsSlug, data: textDoc });
const uploadsDir = path.resolve(__dirname, './collections/Upload/uploads');

View File

@@ -415,14 +415,5 @@ describe('fields', () => {
await expect(page.locator('.Toastify')).toContainText('successfully');
});
test('should resolve relationships when added by form context', async () => {
await page.goto(url.create);
await page.locator('#field-relationHasMany + .pre-populate-field-ui button').click();
await wait(300);
expect(await page.locator('#field-relationHasMany .rs__value-container > .rs__multi-value').count()).toEqual(15);
});
});
});