|
1 |
| -import type { Payload } from 'payload'; |
| 1 | +import type { Payload } from 'payload' |
2 | 2 |
|
3 | 3 | import { schedulePublishHandler } from '@payloadcms/ui/utilities/schedulePublishHandler'
|
4 | 4 | import path from 'path'
|
5 |
| -import { createLocalReq , ValidationError } from 'payload' |
| 5 | +import { createLocalReq, ValidationError } from 'payload' |
6 | 6 | import { wait } from 'payload/shared'
|
7 | 7 | import { fileURLToPath } from 'url'
|
8 | 8 |
|
@@ -689,6 +689,62 @@ describe('Versions', () => {
|
689 | 689 | { id: doc.id, message: 'The following field is invalid: Title' },
|
690 | 690 | ])
|
691 | 691 | })
|
| 692 | + |
| 693 | + it('should update with autosave: true', async () => { |
| 694 | + // Save a draft |
| 695 | + const { id } = await payload.create({ |
| 696 | + collection: autosaveCollectionSlug, |
| 697 | + draft: true, |
| 698 | + data: { title: 'my-title', description: 'some-description', _status: 'draft' }, |
| 699 | + }) |
| 700 | + |
| 701 | + // Autosave the same draft, calls db.updateVersion |
| 702 | + const updated1 = await payload.update({ |
| 703 | + collection: autosaveCollectionSlug, |
| 704 | + id, |
| 705 | + data: { |
| 706 | + title: 'new-title', |
| 707 | + }, |
| 708 | + autosave: true, |
| 709 | + draft: true, |
| 710 | + }) |
| 711 | + |
| 712 | + const versionsCount = await payload.countVersions({ |
| 713 | + collection: autosaveCollectionSlug, |
| 714 | + where: { |
| 715 | + parent: { |
| 716 | + equals: id, |
| 717 | + }, |
| 718 | + }, |
| 719 | + }) |
| 720 | + |
| 721 | + // This should not create a new version |
| 722 | + const updated2 = await payload.update({ |
| 723 | + collection: autosaveCollectionSlug, |
| 724 | + id, |
| 725 | + data: { |
| 726 | + title: 'new-title-2', |
| 727 | + }, |
| 728 | + autosave: true, |
| 729 | + draft: true, |
| 730 | + }) |
| 731 | + |
| 732 | + const versionsCountAfter = await payload.countVersions({ |
| 733 | + collection: autosaveCollectionSlug, |
| 734 | + where: { |
| 735 | + parent: { |
| 736 | + equals: id, |
| 737 | + }, |
| 738 | + }, |
| 739 | + }) |
| 740 | + |
| 741 | + expect(versionsCount.totalDocs).toBe(versionsCountAfter.totalDocs) |
| 742 | + expect(updated1.id).toBe(id) |
| 743 | + expect(updated1.title).toBe('new-title') |
| 744 | + |
| 745 | + expect(updated2.id).toBe(id) |
| 746 | + expect(updated2.title).toBe('new-title-2') |
| 747 | + }) |
692 | 748 | })
|
693 | 749 |
|
694 | 750 | describe('Update Many', () => {
|
|
0 commit comments