Skip to content

Commit

Permalink
fix(files): correct parameter for file update/delete (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
thompsonsj authored Jul 3, 2023
1 parent c784ef6 commit 78eed77
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 141 deletions.
17 changes: 0 additions & 17 deletions dev/src/tests/collections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,4 @@ describe('Collections', () => {
expect(postOneRefreshed.crowdinArticleDirectory.crowdinCollectionDirectory).toEqual(postTwoRefreshed.crowdinArticleDirectory.crowdinCollectionDirectory)
})
})

describe('crowdin-files: on update', () => {
it('updates the `fields` file if a text field has changed', async () => {
const post = await payload.create({
collection: collections.localized,
data: { title: 'Test post' },
});
const file = await getFileByDocumentID('fields', post.id, payload)
const updatedPost = await payload.update({
id: post.id,
collection: collections.localized,
data: { title: 'Test post updated' },
});
const updatedFile = await getFileByDocumentID('fields', post.id, payload)
expect(file.updatedAt).not.toEqual(updatedFile.updatedAt)
})
})
});
261 changes: 140 additions & 121 deletions dev/src/tests/files.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import payload from 'payload';
import { initPayloadTest } from './helpers/config';
import { getFilesByDocumentID } from '../../../dist/api/helpers';
import { getFileByDocumentID, getFilesByDocumentID } from '../../../dist/api/helpers';

/**
* Test files
Expand All @@ -18,133 +18,152 @@ const collections = {
nestedFields: 'nested-field-collection'
}

describe(`Collection: ${collections.nestedFields}`, () => {
describe(`CrowdIn file create, update and delete`, () => {
beforeEach(async () => {
await initPayloadTest({ __dirname });
});

it('does not create files for empty localized fields', async () => {
const article = await payload.create({
collection: collections.nestedFields,
data: {},
});

const crowdInFiles = await getFilesByDocumentID(article.id, payload)
expect(crowdInFiles.length).toEqual(0)
describe(`Collection: ${collections.localized}`, () => {
it('updates the `fields` file if a text field has changed', async () => {
const post = await payload.create({
collection: collections.localized,
data: { title: 'Test post' },
});
const file = await getFileByDocumentID('fields', post.id, payload)
const updatedPost = await payload.update({
id: post.id,
collection: collections.localized,
data: { title: 'Test post updated' },
});
const updatedFile = await getFileByDocumentID('fields', post.id, payload)
expect(file.updatedAt).not.toEqual(updatedFile.updatedAt)
})
})

/**
* TODO: Fix this test
*
* ValidationError: nested-field-collection validation failed: title.en: Cast to string failed for value "{ en: 'Test title' }" (type Object) at path "en"
* model.Object.<anonymous>.Document.invalidate (dev/node_modules/mongoose/lib/document.js:3050:32)
* /
it('creates files containing fieldType content', async () => {
const article = await payload.create({
collection: collections.nestedFields,
locale: "en",
data: {
title: 'Test title',
content: [
{
children: [
{
text: "Test content"
}
]
}
],
metaDescription: 'Test meta description',
},
});
const crowdInFiles = await getFilesByDocumentID(article.id, payload)
expect(crowdInFiles.length).toEqual(2)
expect(crowdInFiles.find((file) => file.name === 'content.html')).toBeDefined()
expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined()
})
*/
describe(`Collection: ${collections.nestedFields}`, () => {
it('does not create files for empty localized fields', async () => {
const article = await payload.create({
collection: collections.nestedFields,
data: {},
});

it('creates files containing `array` fieldType content', async () => {
const article = await payload.create({
collection: collections.nestedFields,
data: {
arrayField: [
{
title: 'Test title 1',
content: [
{
children: [
{
text: "Test content 1"
}
]
}
],
metaDescription: 'Test meta description 1',
},
{
title: 'Test title 2',
content: [
{
children: [
{
text: "Test content 2"
}
]
}
],
metaDescription: 'Test meta description 2',
},
],
},
});
const crowdInFiles = await getFilesByDocumentID(article.id, payload)
expect(crowdInFiles.length).toEqual(3)
expect(crowdInFiles.find((file) => file.name === 'arrayField[0].content.html')).toBeDefined()
expect(crowdInFiles.find((file) => file.name === 'arrayField[1].content.html')).toBeDefined()
expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined()
})
const crowdInFiles = await getFilesByDocumentID(article.id, payload)
expect(crowdInFiles.length).toEqual(0)
})

/**
* TODO: Fix this test
*
* ValidationError: nested-field-collection validation failed: title.en: Cast to string failed for value "{ en: 'Test title' }" (type Object) at path "en"
* model.Object.<anonymous>.Document.invalidate (dev/node_modules/mongoose/lib/document.js:3050:32)
* /
it('creates files containing fieldType content', async () => {
const article = await payload.create({
collection: collections.nestedFields,
locale: "en",
data: {
title: 'Test title',
content: [
{
children: [
{
text: "Test content"
}
]
}
],
metaDescription: 'Test meta description',
},
});
const crowdInFiles = await getFilesByDocumentID(article.id, payload)
expect(crowdInFiles.length).toEqual(2)
expect(crowdInFiles.find((file) => file.name === 'content.html')).toBeDefined()
expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined()
})
*/

it('creates files containing `array` fieldType content', async () => {
const article = await payload.create({
collection: collections.nestedFields,
data: {
arrayField: [
{
title: 'Test title 1',
content: [
{
children: [
{
text: "Test content 1"
}
]
}
],
metaDescription: 'Test meta description 1',
},
{
title: 'Test title 2',
content: [
{
children: [
{
text: "Test content 2"
}
]
}
],
metaDescription: 'Test meta description 2',
},
],
},
});
const crowdInFiles = await getFilesByDocumentID(article.id, payload)
expect(crowdInFiles.length).toEqual(3)
expect(crowdInFiles.find((file) => file.name === 'arrayField[0].content.html')).toBeDefined()
expect(crowdInFiles.find((file) => file.name === 'arrayField[1].content.html')).toBeDefined()
expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined()
})

it('creates files containing `blocks` fieldType content', async () => {
const article = await payload.create({
collection: collections.nestedFields,
data: {
layout: [
{
title: 'Test title 1',
content: [
{
children: [
{
text: "Test content 1"
}
]
}
],
metaDescription: 'Test meta description 1',
blockType: 'basicBlock'
},
{
title: 'Test title 2',
content: [
{
children: [
{
text: "Test content 2"
}
]
}
],
metaDescription: 'Test meta description 2',
blockType: 'basicBlock'
},
],
},
});
const crowdInFiles = await getFilesByDocumentID(article.id, payload)
expect(crowdInFiles.length).toEqual(3)
expect(crowdInFiles.find((file) => file.name === 'layout[0].content.html')).toBeDefined()
expect(crowdInFiles.find((file) => file.name === 'layout[1].content.html')).toBeDefined()
expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined()
it('creates files containing `blocks` fieldType content', async () => {
const article = await payload.create({
collection: collections.nestedFields,
data: {
layout: [
{
title: 'Test title 1',
content: [
{
children: [
{
text: "Test content 1"
}
]
}
],
metaDescription: 'Test meta description 1',
blockType: 'basicBlock'
},
{
title: 'Test title 2',
content: [
{
children: [
{
text: "Test content 2"
}
]
}
],
metaDescription: 'Test meta description 2',
blockType: 'basicBlock'
},
],
},
});
const crowdInFiles = await getFilesByDocumentID(article.id, payload)
expect(crowdInFiles.length).toEqual(3)
expect(crowdInFiles.find((file) => file.name === 'layout[0].content.html')).toBeDefined()
expect(crowdInFiles.find((file) => file.name === 'layout[1].content.html')).toBeDefined()
expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined()
})
})
});
6 changes: 3 additions & 3 deletions src/api/payload-crowdin-sync/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isEmpty } from 'lodash';

interface IcrowdinFile {
id: string
fileId: number
originalId: number
}

interface IfindOrCreateCollectionDirectory {
Expand Down Expand Up @@ -242,7 +242,7 @@ export class payloadCrowdInSyncFilesApi {
}: IupdateFile) {
// Update file on CrowdIn
const updatedCrowdInFile = await this.crowdInUpdateFile({
fileId: crowdInFile.fileId,
fileId: crowdInFile.originalId,
name,
fileData,
fileType,
Expand Down Expand Up @@ -320,7 +320,7 @@ export class payloadCrowdInSyncFilesApi {
}

private async deleteFile(crowdInFile: IcrowdinFile) {
const file = await this.sourceFilesApi.deleteFile(this.projectId, crowdInFile.fileId)
const file = await this.sourceFilesApi.deleteFile(this.projectId, crowdInFile.originalId)
const payloadFile = await this.payload.delete({
collection: 'crowdin-files', // required
id: crowdInFile.id, // required
Expand Down

0 comments on commit 78eed77

Please sign in to comment.