chore: passing tests

This commit is contained in:
James
2023-01-16 19:04:09 -05:00
parent 671adc7e0e
commit 0a4766a61e
6 changed files with 19 additions and 93 deletions

View File

@@ -95,13 +95,13 @@ const Autosave: React.FC<Props> = ({ collection, global, id, publishedDocUpdated
}
if (url) {
const body = {
...reduceFieldsToValues(fieldRef.current, true),
_status: 'draft',
};
setTimeout(async () => {
if (modifiedRef.current) {
const body = {
...reduceFieldsToValues(fieldRef.current, true),
_status: 'draft',
};
const res = await fetch(url, {
method,
credentials: 'include',

View File

@@ -251,6 +251,7 @@ async function update(incomingArgs: Arguments): Promise<Document> {
docWithLocales: result,
id,
autosave,
draft: shouldSaveDraft,
});
}

View File

@@ -3,7 +3,6 @@ import { docHasTimestamps, Where } from '../../types';
import { hasWhereAccessResult } from '../../auth';
import { AccessResult } from '../../config/types';
import { CollectionModel, SanitizedCollectionConfig, TypeWithID } from '../../collections/config/types';
import flattenWhereConstraints from '../../utilities/flattenWhereConstraints';
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
import { appendVersionToQueryKey } from './appendVersionToQueryKey';
import { SanitizedGlobalConfig } from '../../globals/config/types';

View File

@@ -1,80 +0,0 @@
import { Payload } from '..';
import { SanitizedCollectionConfig } from '../collections/config/types';
import { enforceMaxVersions } from './enforceMaxVersions';
import { PayloadRequest } from '../express/types';
import { afterRead } from '../fields/hooks/afterRead';
type Args = {
payload: Payload
config?: SanitizedCollectionConfig
req: PayloadRequest
docWithLocales: any
id: string | number
}
export const ensurePublishedCollectionVersion = async ({
payload,
config,
req,
id,
docWithLocales,
}: Args): Promise<void> => {
// If there are no newer drafts,
// And the current doc is published,
// We need to keep a version of the published document
if (docWithLocales?._status === 'published') {
const VersionModel = payload.versions[config.slug];
const moreRecentDrafts = await VersionModel.find({
parent: {
$eq: docWithLocales.id,
},
updatedAt: {
$gt: docWithLocales.updatedAt,
},
},
{},
{
lean: true,
leanWithId: true,
sort: {
updatedAt: 'desc',
},
});
if (moreRecentDrafts?.length === 0) {
const version = await afterRead({
depth: 0,
doc: docWithLocales,
entityConfig: config,
req,
overrideAccess: true,
showHiddenFields: true,
flattenLocales: false,
});
try {
await VersionModel.create({
parent: id,
version,
autosave: false,
});
} catch (err) {
payload.logger.error(`There was an error while saving a version for the ${config.slug} with ID ${id}.`);
payload.logger.error(err);
}
if (config.versions.maxPerDoc) {
enforceMaxVersions({
id,
payload,
Model: VersionModel,
slug: config.slug,
entityType: 'collection',
max: config.versions.maxPerDoc,
});
}
}
}
};

View File

@@ -11,6 +11,7 @@ type Args = {
docWithLocales: any
id: string | number
autosave?: boolean
draft?: boolean
}
export const saveCollectionVersion = async ({
@@ -19,11 +20,14 @@ export const saveCollectionVersion = async ({
id,
docWithLocales,
autosave,
draft,
}: Args): Promise<Record<string, unknown>> => {
const VersionModel = payload.versions[config.slug];
let result = { ...docWithLocales };
if (draft) result._status = 'draft';
if (result._id) delete result._id;
let existingAutosaveVersion;
@@ -49,7 +53,7 @@ export const saveCollectionVersion = async ({
} else {
result = await VersionModel.create({
parent: id,
version: docWithLocales,
version: result,
autosave: Boolean(autosave),
});
}

View File

@@ -159,7 +159,7 @@ describe('Versions', () => {
locale: 'all',
});
expect(versions.docs[0].version.title.en).toStrictEqual(englishTitle);
expect(versions.docs[0].version.title.en).toStrictEqual(newEnglishTitle);
expect(versions.docs[0].version.title.es).toStrictEqual(spanishTitle);
});
});
@@ -184,7 +184,7 @@ describe('Versions', () => {
const restore = await payload.restoreVersion({
collection,
id: versions.docs[0].id,
id: versions.docs[1].id,
});
expect(restore.title).toBeDefined();
@@ -195,7 +195,7 @@ describe('Versions', () => {
draft: true,
});
expect(restoredPost.title).toBe(restore.title);
expect(restoredPost.title).toBe(versions.docs[1].version.title);
});
});
@@ -226,13 +226,15 @@ describe('Versions', () => {
draft: true,
});
const spanishTitle = 'es title';
// second update to existing draft
await payload.update({
id: collectionLocalPostID,
collection,
locale: 'es',
data: {
title: updatedTitle,
title: spanishTitle,
},
draft: true,
});
@@ -251,7 +253,7 @@ describe('Versions', () => {
expect(publishedPost.title).toBe(originalTitle);
expect(draftPost.title.en).toBe(updatedTitle);
expect(draftPost.title.es).toBe(updatedTitle);
expect(draftPost.title.es).toBe(spanishTitle);
});
});
});
@@ -423,7 +425,7 @@ describe('Versions', () => {
expect(data.id).toBeDefined();
expect(data.parent.id).toStrictEqual(collectionGraphQLPostID);
expect(data.version.title).toStrictEqual(collectionGraphQLOriginalTitle);
expect(data.version.title).toStrictEqual(updatedTitle);
});
it('should allow read of versions by querying version content', async () => {