-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* basic SafeRelationship field * 🚧 refactor * cleanup * 🚧 wip tests * rm collection spread * fix tests * thread req through to api call; catch api errors * fix type error * better error message * add dev cmd; specify separate port for pg tests * update docs
- Loading branch information
Showing
11 changed files
with
476 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { type CollectionConfig } from 'payload/types' | ||
|
||
const Basics: CollectionConfig = { | ||
slug: 'basics', | ||
admin: { | ||
useAsTitle: 'title', | ||
}, | ||
fields: [ | ||
{ | ||
name: 'title', | ||
type: 'text', | ||
}, | ||
], | ||
} | ||
|
||
export default Basics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
import { type CollectionConfig } from 'payload/types' | ||
import Pages from './Pages' | ||
|
||
const Posts: CollectionConfig = { | ||
...Pages, | ||
slug: 'posts', | ||
admin: { | ||
useAsTitle: 'title', | ||
}, | ||
versions: { drafts: true }, | ||
fields: [ | ||
{ | ||
name: 'title', | ||
type: 'text', | ||
}, | ||
], | ||
} | ||
|
||
export default Posts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
import { addMinutes, subMinutes } from "date-fns" | ||
import type { Payload } from "payload" | ||
|
||
describe('SafeRelationshipField', () => { | ||
const payload = globalThis.payloadClient as Payload | ||
|
||
let post | ||
|
||
beforeAll(async () => { | ||
post = await payload.create({ | ||
collection: 'posts', | ||
data: { | ||
title: 'published', | ||
_status: 'published' | ||
} | ||
}) | ||
}) | ||
|
||
describe('false positives', () => { | ||
test('published docs', async () => { | ||
const publishedPost = await payload.create({ | ||
collection: 'posts', | ||
data: { | ||
title: 'published', | ||
publish_date: subMinutes(new Date(), 10).toISOString(), | ||
_status: 'published', | ||
} | ||
}) | ||
|
||
const published = await payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'published', | ||
featured_post: publishedPost.id, | ||
_status: 'published', | ||
} | ||
}) | ||
|
||
expect(published._status).toBe('published') | ||
// @ts-expect-error | ||
expect(published.featured_post.id).toEqual(publishedPost.id) | ||
}) | ||
|
||
test('draft docs', async () => { | ||
const scheduledPost = await payload.create({ | ||
collection: 'posts', | ||
data: { | ||
title: 'scheduled', | ||
publish_date: addMinutes(new Date(), 10).toISOString(), | ||
_status: 'draft', | ||
} | ||
}) | ||
|
||
const draftPage = await payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'second page', | ||
featured_post: scheduledPost.id, | ||
_status: 'draft', | ||
} | ||
}) | ||
|
||
expect(draftPage._status).toBe('draft') | ||
// @ts-expect-error | ||
expect(draftPage.featured_post.id).toBe(scheduledPost.id) | ||
}) | ||
|
||
test('polymorphic field', async () => { | ||
const page = await payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'page', | ||
_status: 'published', | ||
} | ||
}) | ||
|
||
await expect(payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'polymorphic', | ||
polymorphic: [ | ||
{ relationTo: 'pages', value: page.id}, | ||
{ relationTo: 'posts', value: post.id}, | ||
], | ||
_status: 'published', | ||
} | ||
})).resolves.not.toThrow() | ||
}) | ||
|
||
test('mixed field', async () => { | ||
const page = await payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'page', | ||
_status: 'published', | ||
} | ||
}) | ||
|
||
const basic = await payload.create({ | ||
collection: 'basics', | ||
data: { | ||
title: 'published', | ||
_status: 'published' | ||
} | ||
}) | ||
|
||
await expect(payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'mixed', | ||
mixed_relationship: [ | ||
{ relationTo: 'basics', value: basic.id }, | ||
{ relationTo: 'pages', value: page.id } | ||
], | ||
_status: 'published', | ||
} | ||
})).resolves.not.toThrow() | ||
}) | ||
}) | ||
|
||
describe('errors', () => { | ||
test('related document is scheduled after current document', async () => { | ||
const scheduledPost = await payload.create({ | ||
collection: 'posts', | ||
data: { | ||
title: 'scheduled', | ||
publish_date: addMinutes(new Date(), 10).toISOString(), | ||
_status: 'draft', | ||
} | ||
}) | ||
|
||
await expect(payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'second page', | ||
featured_post: scheduledPost.id, | ||
_status: 'published', | ||
} | ||
})).rejects.toThrow('The following field is invalid: featured_post') | ||
}) | ||
|
||
test('one invalid document out of multiple', async () => { | ||
const scheduledPage = await payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'scheduled', | ||
publish_date: addMinutes(new Date(), 10).toISOString(), | ||
_status: 'draft', | ||
} | ||
}) | ||
|
||
const publishedPage = await payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'published', | ||
_status: 'published', | ||
} | ||
}) | ||
|
||
await expect(payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'multiple', | ||
related_pages: [ | ||
{ relationTo: 'pages', value: scheduledPage.id }, | ||
{ relationTo: 'pages', value: publishedPage.id } | ||
], | ||
_status: 'published', | ||
} | ||
})).rejects.toThrow('The following field is invalid: related_pages') | ||
}) | ||
|
||
test('one invalid document out of polymorphic', async () => { | ||
const scheduledPage = await payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'scheduled', | ||
publish_date: addMinutes(new Date(), 10).toISOString(), | ||
_status: 'draft', | ||
} | ||
}) | ||
|
||
const publishedPost = await payload.create({ | ||
collection: 'posts', | ||
data: { | ||
title: 'published', | ||
_status: 'published', | ||
} | ||
}) | ||
|
||
await expect(payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'multiple', | ||
polymorphic: [ | ||
{ relationTo: 'pages', value: scheduledPage.id }, | ||
{ relationTo: 'posts', value: publishedPost.id } | ||
], | ||
_status: 'published', | ||
} | ||
})).rejects.toThrow('The following field is invalid: polymorphic') | ||
}) | ||
|
||
test('one invalid document out of mixed', async () => { | ||
const scheduledPage = await payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'scheduled', | ||
publish_date: addMinutes(new Date(), 10).toISOString(), | ||
_status: 'draft', | ||
} | ||
}) | ||
|
||
const basic = await payload.create({ | ||
collection: 'basics', | ||
data: { | ||
title: 'basic', | ||
} | ||
}) | ||
|
||
await expect(payload.create({ | ||
collection: 'pages', | ||
data: { | ||
title: 'multiple', | ||
mixed_relationship: [ | ||
{ relationTo: 'pages', value: scheduledPage.id }, | ||
{ relationTo: 'basics', value: basic.id } | ||
], | ||
_status: 'published', | ||
} | ||
})).rejects.toThrow('The following field is invalid: mixed_relationship') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.