Skip to content

Commit

Permalink
fix(pluginfields): ensure syncAllTranslations is virtual (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
thompsonsj authored Dec 19, 2023
1 parent f70e1bd commit 13c3f3e
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 12 deletions.
10 changes: 10 additions & 0 deletions dev/src/lib/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export interface MultiRichText {
[k: string]: unknown;
}[]
| null;
syncTranslations?: boolean | null;
syncAllTranslations?: boolean | null;
crowdinArticleDirectory?: (string | null) | CrowdinArticleDirectory;
updatedAt: string;
createdAt: string;
Expand Down Expand Up @@ -155,6 +157,8 @@ export interface LocalizedPost {
}[]
| null;
status?: ('draft' | 'published') | null;
syncTranslations?: boolean | null;
syncAllTranslations?: boolean | null;
crowdinArticleDirectory?: (string | null) | CrowdinArticleDirectory;
updatedAt: string;
createdAt: string;
Expand Down Expand Up @@ -290,6 +294,8 @@ export interface NestedFieldCollection {
}[]
| null;
};
syncTranslations?: boolean | null;
syncAllTranslations?: boolean | null;
crowdinArticleDirectory?: (string | null) | CrowdinArticleDirectory;
updatedAt: string;
createdAt: string;
Expand Down Expand Up @@ -342,6 +348,8 @@ export interface LocalizedNav {
label?: string | null;
id?: string | null;
}[];
syncTranslations?: boolean | null;
syncAllTranslations?: boolean | null;
crowdinArticleDirectory?: (string | null) | CrowdinArticleDirectory;
updatedAt?: string | null;
createdAt?: string | null;
Expand All @@ -365,6 +373,8 @@ export interface Statistic {
text?: string | null;
number?: number | null;
};
syncTranslations?: boolean | null;
syncAllTranslations?: boolean | null;
crowdinArticleDirectory?: (string | null) | CrowdinArticleDirectory;
_status?: ('draft' | 'published') | null;
updatedAt?: string | null;
Expand Down
98 changes: 98 additions & 0 deletions dev/src/lib/tests/translations/receive/virtual-fields.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import payload from "payload";
import { initPayloadTest } from "./../../helpers/config";
import {
getFilesByDocumentID,
} from "payload-crowdin-sync";
import { connectionTimeout } from "./../../config";
import { CrowdinArticleDirectory, CrowdinCollectionDirectory, CrowdinFile } from "../../../payload-types";
import nock from "nock";

/**
* Test virtual fields
*
* Virtual fields are added to localized documents
* and are used to indicate that translations should
* be retrieved on:
*
* save; or
* save draft/publish.
*
*/

const pluginOptions = {
projectId: 323731,
directoryId: 1169,
token: process.env['CROWDIN_TOKEN'] as string,
localeMap: {
de_DE: {
crowdinId: "de",
},
fr_FR: {
crowdinId: "fr",
},
},
sourceLocale: "en",
};

describe("Virtual fields", () => {
beforeAll(async () => {
await initPayloadTest({
__dirname,
payloadConfigFile: "./../../payload.config.default.ts"
});
await new Promise(resolve => setTimeout(resolve, connectionTimeout));
});

afterAll(async () => {
if (typeof payload?.db?.destroy === 'function') {
await payload.db.destroy(payload)
// setTimeout(async () => {await payload.db.destroy(payload)}, connectionTimeout)
}
});

describe("No database storage", () => {
it("does not store syncTranslations", async () => {
nock('https://api.crowdin.com')
.persist()
.get(/.*/)
.reply(200)
.post(/.*/)
.reply(200)
const post = await payload.create({
collection: "localized-posts",
data: {
title: "Test post",
syncTranslations: true,
},
});
// retrieve post to get populated fields
const result = await payload.findByID({
collection: "localized-posts",
id: post.id,
});
expect(Object.prototype.hasOwnProperty.call(result, 'syncTranslations')).toBeFalsy();
});

it("does not store syncAllTranslations", async () => {
nock('https://api.crowdin.com')
.persist()
.get(/.*/)
.reply(200)
.post(/.*/)
.reply(200)
const post = await payload.create({
collection: "localized-posts",
data: {
title: "Test post",
syncAllTranslations: true,
},
});
// retrieve post to get populated fields
const result = await payload.findByID({
collection: "localized-posts",
id: post.id,
});
expect(Object.prototype.hasOwnProperty.call(result, 'syncAllTranslations')).toBeFalsy();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import payload from "payload";
import { initPayloadTest } from "./helpers/config";
import { connectionTimeout } from "./config";
import { initPayloadTest } from "../../helpers/config";
import { connectionTimeout } from "../../config";
import { payloadHtmlToSlateConfig, payloadCrowdinSyncTranslationsApi } from "payload-crowdin-sync";
import nock from "nock";

Expand Down Expand Up @@ -46,7 +46,9 @@ const pluginOptions = {
describe("Translations", () => {
beforeAll(async () => {
await initPayloadTest({
__dirname });
__dirname,
payloadConfigFile: "./../../payload.config.default.ts"
});
await new Promise((resolve) => setTimeout(resolve, connectionTimeout));
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import payload from "payload";
import { initPayloadTest } from "./helpers/config";
import { initPayloadTest } from "../../helpers/config";
import { payloadCrowdinSyncTranslationsApi } from "payload-crowdin-sync";
import nock from "nock";
import { payloadCreateData } from "./fixtures/nested-field-collection/simple-blocks.fixture";
import { payloadCreateBlocksRichTextData } from "./fixtures/nested-field-collection/rich-text-blocks.fixture";
import { payloadCreateIncludesNonLocalizedBlocksData } from "./fixtures/nested-field-collection/simple-blocks-with-non-localized-blocks.fixture";
import { multiRichTextFields } from "../collections/fields/multiRichTextFields";
import { connectionTimeout } from "./config";
import { payloadCreateData } from "../../fixtures/nested-field-collection/simple-blocks.fixture";
import { payloadCreateBlocksRichTextData } from "../../fixtures/nested-field-collection/rich-text-blocks.fixture";
import { payloadCreateIncludesNonLocalizedBlocksData } from "../../fixtures/nested-field-collection/simple-blocks-with-non-localized-blocks.fixture";
import { multiRichTextFields } from "../../../collections/fields/multiRichTextFields";
import { connectionTimeout } from "../../config";

/**
* Test translations
Expand All @@ -32,7 +32,10 @@ const pluginOptions = {

describe("Translations", () => {
beforeAll(async () => {
await initPayloadTest({ __dirname });
await initPayloadTest({
__dirname,
payloadConfigFile: "./../../payload.config.default.ts"
});
await new Promise(resolve => setTimeout(resolve, connectionTimeout));
});

Expand Down Expand Up @@ -628,6 +631,12 @@ describe("Translations", () => {
collection: "nested-field-collection",
dryRun: false,
});
// run again - hacky way to wait for all files.
await payload.findByID({
collection: "nested-field-collection",
id: `${post.id}`,
locale: "de_DE",
});
// retrieve translated post from Payload
const resultDe = await payload.findByID({
collection: "nested-field-collection",
Expand Down Expand Up @@ -664,6 +673,12 @@ describe("Translations", () => {
blockType: "basicBlockRichText",
},
]);
// run again - hacky way to wait for all files.
await payload.findByID({
collection: "nested-field-collection",
id: `${post.id}`,
locale: "fr_FR",
});
// retrieve translated post from Payload
const resultFr = await payload.findByID({
collection: "nested-field-collection",
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/lib/fields/pluginFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const pluginCollectionOrGlobalFields = ({
},
hooks: {
beforeChange: [async ({ siblingData, req }) => {
if (siblingData["syncTranslations"] && siblingData["crowdinArticleDirectory"]) {
if (siblingData["syncAllTranslations"] && siblingData["crowdinArticleDirectory"]) {
// is this a draft?
const draft = Boolean(siblingData["_status"] && siblingData["_status"] !== 'published')

Expand All @@ -93,7 +93,7 @@ export const pluginCollectionOrGlobalFields = ({
}
// Mutate the sibling data to prevent DB storage
// eslint-disable-next-line no-param-reassign
siblingData["syncTranslations"] = undefined;
siblingData["syncAllTranslations"] = undefined;
}],
},
},
Expand Down

0 comments on commit 13c3f3e

Please sign in to comment.