The `fields-relationship` test suite is disorganized to the point of being unusable. This makes it very difficult to digest at a high level and add new tests. This PR cleans it up in the following ways: - Moves collection configs to their own standalone files - Moves the seed function to its own file - Consolidates collection slugs in their own file - Uses generated types instead of defining them statically - Wraps the `filterOptions` e2e tests within a describe block Related, there are three distinct test suites where we manage relationships: `relationships`, `fields-relationship`, and `fields > relationships`. In the future we ought to consolidate at least two of these. IMO the `fields > relationship` suite should remain in place for general _component level_ UI tests for the field itself, whereas the other suite could run the integration tests and test the more complex UI patterns that exist outside of the field component.
131 lines
3.0 KiB
TypeScript
131 lines
3.0 KiB
TypeScript
import type { CollectionConfig, FilterOptionsProps } from 'payload'
|
|
|
|
import type { FieldsRelationship } from '../../payload-types.js'
|
|
|
|
import {
|
|
relationFalseFilterOptionSlug,
|
|
relationOneSlug,
|
|
relationRestrictedSlug,
|
|
relationTrueFilterOptionSlug,
|
|
relationTwoSlug,
|
|
relationWithTitleSlug,
|
|
slug,
|
|
} from '../../slugs.js'
|
|
|
|
export const Relationship: CollectionConfig = {
|
|
admin: {
|
|
defaultColumns: [
|
|
'id',
|
|
'relationship',
|
|
'relationshipRestricted',
|
|
'relationshipHasManyMultiple',
|
|
'relationshipWithTitle',
|
|
],
|
|
},
|
|
fields: [
|
|
{
|
|
name: 'relationship',
|
|
relationTo: relationOneSlug,
|
|
type: 'relationship',
|
|
},
|
|
{
|
|
name: 'relationshipHasMany',
|
|
hasMany: true,
|
|
relationTo: relationOneSlug,
|
|
type: 'relationship',
|
|
},
|
|
{
|
|
name: 'relationshipMultiple',
|
|
relationTo: [relationOneSlug, relationTwoSlug],
|
|
type: 'relationship',
|
|
},
|
|
{
|
|
name: 'relationshipHasManyMultiple',
|
|
hasMany: true,
|
|
relationTo: [relationOneSlug, relationTwoSlug],
|
|
type: 'relationship',
|
|
},
|
|
{
|
|
name: 'relationshipRestricted',
|
|
relationTo: relationRestrictedSlug,
|
|
type: 'relationship',
|
|
},
|
|
{
|
|
name: 'relationshipWithTitle',
|
|
relationTo: relationWithTitleSlug,
|
|
type: 'relationship',
|
|
},
|
|
{
|
|
name: 'relationshipFilteredByID',
|
|
filterOptions: (args: FilterOptionsProps<FieldsRelationship>) => {
|
|
return {
|
|
id: {
|
|
equals: args.data.relationship,
|
|
},
|
|
}
|
|
},
|
|
relationTo: relationOneSlug,
|
|
type: 'relationship',
|
|
admin: {
|
|
description:
|
|
'This will filter the relationship options based on id, which is the same as the relationship field in this document',
|
|
},
|
|
},
|
|
{
|
|
name: 'relationshipFilteredAsync',
|
|
filterOptions: (args: FilterOptionsProps<FieldsRelationship>) => {
|
|
return {
|
|
id: {
|
|
equals: args.data.relationship,
|
|
},
|
|
}
|
|
},
|
|
relationTo: relationOneSlug,
|
|
type: 'relationship',
|
|
},
|
|
{
|
|
name: 'relationshipManyFiltered',
|
|
filterOptions: ({ relationTo, siblingData }) => {
|
|
if (relationTo === relationOneSlug) {
|
|
return { name: { equals: 'include' } }
|
|
}
|
|
|
|
if (relationTo === relationTrueFilterOptionSlug) {
|
|
return true
|
|
}
|
|
|
|
if (relationTo === relationFalseFilterOptionSlug) {
|
|
return false
|
|
}
|
|
|
|
if (siblingData.filter) {
|
|
return { name: { contains: siblingData.filter } }
|
|
}
|
|
|
|
return { and: [] }
|
|
},
|
|
hasMany: true,
|
|
relationTo: [
|
|
relationWithTitleSlug,
|
|
relationFalseFilterOptionSlug,
|
|
relationTrueFilterOptionSlug,
|
|
relationOneSlug,
|
|
],
|
|
type: 'relationship',
|
|
},
|
|
{
|
|
name: 'filter',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'relationshipReadOnly',
|
|
admin: {
|
|
readOnly: true,
|
|
},
|
|
relationTo: relationOneSlug,
|
|
type: 'relationship',
|
|
},
|
|
],
|
|
slug,
|
|
}
|