Skip to content

Commit 60d8678

Browse files
authored
test: add tests for autosave creating new versions and losing draft status on reload (#16335)
Add two tests covering autosave bugs specific to the Content API database adapter: - Autosave changes on a published document should be visible after reload (draft found via `latest: true` query) - Repeated autosaves should update the existing draft version in-place instead of creating a new version each time
1 parent e5bc6be commit 60d8678

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

test/versions/int.spec.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,104 @@ describe('Versions', () => {
431431
expect(await getVersionsCount()).toBe(2)
432432
})
433433

434+
it('should show autosave changes after reload on a published document', async () => {
435+
// Bug: When autosave is enabled, editing a published document and then reloading
436+
// should show the draft changes ("Changed" status). Previously, reload showed
437+
// "Published" status with the button disabled, even though draft content persisted.
438+
const published = await payload.create({
439+
collection: autosaveCollectionSlug,
440+
data: { _status: 'published', description: 'original', title: 'Published Post' },
441+
})
442+
443+
// Autosave a change on the published document
444+
await payload.update({
445+
id: published.id,
446+
autosave: true,
447+
collection: autosaveCollectionSlug,
448+
data: { title: 'Autosaved Title' },
449+
draft: true,
450+
})
451+
452+
// Simulate page reload: read the latest draft version (what getLatestCollectionVersion does)
453+
const { docs: latestVersions } = await payload.findVersions({
454+
collection: autosaveCollectionSlug,
455+
limit: 1,
456+
sort: '-updatedAt',
457+
where: {
458+
and: [{ parent: { equals: published.id } }, { latest: { equals: true } }],
459+
},
460+
})
461+
462+
expect(latestVersions).toHaveLength(1)
463+
expect(latestVersions[0].version.title).toBe('Autosaved Title')
464+
// The draft version should exist and be findable, proving the UI would show "Changed"
465+
})
466+
467+
it('should update existing draft version during repeated autosaves instead of creating new ones', async () => {
468+
// Bug: Auto Save when changing content kept adding a new version EVERY time
469+
// instead of updating the existing draft one.
470+
const published = await payload.create({
471+
collection: autosaveCollectionSlug,
472+
data: { _status: 'published', description: 'desc', title: 'Original' },
473+
})
474+
475+
// First autosave creates a draft version
476+
await payload.update({
477+
id: published.id,
478+
autosave: true,
479+
collection: autosaveCollectionSlug,
480+
data: { title: 'Change 1' },
481+
draft: true,
482+
})
483+
484+
const countAfterFirst = await payload.countVersions({
485+
collection: autosaveCollectionSlug,
486+
where: { parent: { equals: published.id } },
487+
})
488+
489+
// Second autosave should update the existing draft, NOT create a new one
490+
await payload.update({
491+
id: published.id,
492+
autosave: true,
493+
collection: autosaveCollectionSlug,
494+
data: { title: 'Change 2' },
495+
draft: true,
496+
})
497+
498+
const countAfterSecond = await payload.countVersions({
499+
collection: autosaveCollectionSlug,
500+
where: { parent: { equals: published.id } },
501+
})
502+
503+
expect(countAfterSecond.totalDocs).toBe(countAfterFirst.totalDocs)
504+
505+
// Third autosave — still same count
506+
await payload.update({
507+
id: published.id,
508+
autosave: true,
509+
collection: autosaveCollectionSlug,
510+
data: { title: 'Change 3' },
511+
draft: true,
512+
})
513+
514+
const countAfterThird = await payload.countVersions({
515+
collection: autosaveCollectionSlug,
516+
where: { parent: { equals: published.id } },
517+
})
518+
519+
expect(countAfterThird.totalDocs).toBe(countAfterFirst.totalDocs)
520+
521+
// Verify the latest version has the most recent content
522+
const { docs } = await payload.findVersions({
523+
collection: autosaveCollectionSlug,
524+
limit: 1,
525+
sort: '-updatedAt',
526+
where: { parent: { equals: published.id } },
527+
})
528+
529+
expect(docs[0].version.title).toBe('Change 3')
530+
})
531+
434532
it('should return null when saving a version with returning:false', async () => {
435533
const collection = autosaveCollectionSlug
436534
const collectionConfig = payload.collections[autosaveCollectionSlug].config

0 commit comments

Comments
 (0)