Skip to content

Commit

Permalink
refactor(collections): simplify tests to use helpers (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
thompsonsj authored Jul 3, 2023
1 parent ee93725 commit c784ef6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 67 deletions.
74 changes: 12 additions & 62 deletions dev/src/tests/collections.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import payload from 'payload';
import { initPayloadTest } from './helpers/config';
import { getFilesByDocumentID, getFileByDocumentID } from '../../../dist/api/helpers';

/**
* Test the collections
Expand Down Expand Up @@ -113,20 +114,9 @@ describe('Collections', () => {
collection: collections.localized,
data: { title: 'Test post' },
});
// retrieve post to get populated fields
const result = await payload.findByID({
collection: collections.localized,
id: post.id,
});
const crowdinArticleDirectoryId = result.crowdinArticleDirectory?.id
const crowdInFiles = await payload.find({
collection: 'crowdin-files',
where: {
crowdinArticleDirectory: { equals: crowdinArticleDirectoryId },
},
});
expect(crowdInFiles.docs.length).toEqual(1)
const file = crowdInFiles.docs.find(doc => doc.field === 'fields')
const crowdInFiles = await getFilesByDocumentID(post.id, payload)
expect(crowdInFiles.length).toEqual(1)
const file = crowdInFiles.find(doc => doc.field === 'fields')
expect(file).not.toEqual(undefined)
expect(file.type).toEqual('json')
})
Expand All @@ -136,19 +126,8 @@ describe('Collections', () => {
collection: collections.localized,
data: { title: '' },
});
// retrieve post to get populated fields
const result = await payload.findByID({
collection: collections.localized,
id: post.id,
});
const crowdinArticleDirectoryId = result.crowdinArticleDirectory?.id
const crowdInFiles = await payload.find({
collection: 'crowdin-files',
where: {
crowdinArticleDirectory: { equals: crowdinArticleDirectoryId },
},
});
expect(crowdInFiles.docs.length).toEqual(0)
const crowdInFiles = await getFilesByDocumentID(post.id, payload)
expect(crowdInFiles.length).toEqual(0)
})

const fieldsAndContentTestName = 'creates a `fields` file to include the title field and a `content` file for the content richText field'
Expand All @@ -166,21 +145,10 @@ describe('Collections', () => {
}],
},
});
// retrieve post to get populated fields
const result = await payload.findByID({
collection: collections.localized,
id: post.id,
});
const crowdinArticleDirectoryId = result.crowdinArticleDirectory?.id
const crowdInFiles = await payload.find({
collection: 'crowdin-files',
where: {
crowdinArticleDirectory: { equals: crowdinArticleDirectoryId },
},
});
expect(crowdInFiles.docs.length).toEqual(2)
const fields = crowdInFiles.docs.find(doc => doc.field === 'fields')
const content = crowdInFiles.docs.find(doc => doc.field === 'content')
const crowdInFiles = await getFilesByDocumentID(post.id, payload)
expect(crowdInFiles.length).toEqual(2)
const fields = crowdInFiles.find(doc => doc.field === 'fields')
const content = crowdInFiles.find(doc => doc.field === 'content')
expect(fields).not.toEqual(undefined)
expect(fields.type).toEqual('json')
expect(content).not.toEqual(undefined)
Expand Down Expand Up @@ -233,31 +201,13 @@ describe('Collections', () => {
collection: collections.localized,
data: { title: 'Test post' },
});
// retrieve post to get populated fields
const result = await payload.findByID({
collection: collections.localized,
id: post.id,
});
const crowdinArticleDirectoryId = result.crowdinArticleDirectory?.id
const crowdInFiles = await payload.find({
collection: 'crowdin-files',
where: {
crowdinArticleDirectory: { equals: crowdinArticleDirectoryId },
},
});
const file = crowdInFiles.docs.find(doc => doc.field === 'fields')
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 updatedCrowdInFiles = await payload.find({
collection: 'crowdin-files',
where: {
crowdinArticleDirectory: { equals: crowdinArticleDirectoryId },
},
});
const updatedFile = updatedCrowdInFiles.docs.find(doc => doc.field === 'fields')
const updatedFile = await getFileByDocumentID('fields', post.id, payload)
expect(file.updatedAt).not.toEqual(updatedFile.updatedAt)
})
})
Expand Down
19 changes: 16 additions & 3 deletions src/api/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Payload } from "payload";

/**
* get CrowdIn Article Directory for a given documentId
*
* The CrowdIn Article Directory is associated with a document,
* so is easy to retrieve. Use this function when you only have
* a document id.
*/
export async function getArticleDirectory(documentId: string, payload: Payload) {
// Get directory
const crowdInPayloadArticleDirectory = await payload.find({
Expand All @@ -18,7 +25,7 @@ export async function getArticleDirectory(documentId: string, payload: Payload)
return crowdInPayloadArticleDirectory.docs[0]
}

export async function getFile(name: string, crowdinArticleDirectoryId: number, payload: Payload): Promise<any> {
export async function getFile(name: string, crowdinArticleDirectoryId: string, payload: Payload): Promise<any> {
const result = await payload.find({
collection: "crowdin-files",
where: {
Expand All @@ -31,7 +38,7 @@ export async function getFile(name: string, crowdinArticleDirectoryId: number, p
return result.docs[0]
}

export async function getFiles(crowdinArticleDirectoryId: number, payload: Payload): Promise<any> {
export async function getFiles(crowdinArticleDirectoryId: string, payload: Payload): Promise<any> {
const result = await payload.find({
collection: "crowdin-files",
where: {
Expand All @@ -43,7 +50,13 @@ export async function getFiles(crowdinArticleDirectoryId: number, payload: Paylo
return result.docs
}

export async function getFileByDocumentID(name: string, documentId: string, payload: Payload): Promise<any> {
const articleDirectory = await getArticleDirectory(documentId, payload)
return getFile(name, articleDirectory.id, payload)
}

export async function getFilesByDocumentID(documentId: string, payload: Payload): Promise<any> {
const articleDirectory = await getArticleDirectory(documentId, payload)
return getFiles(articleDirectory.id, payload)
const files = await getFiles(articleDirectory.id, payload)
return files
}
4 changes: 2 additions & 2 deletions src/api/payload-crowdin-sync/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ export class payloadCrowdInSyncFilesApi {
return crowdInPayloadCollectionDirectory
}

async getFile(name: string, crowdinArticleDirectoryId: number): Promise<any> {
async getFile(name: string, crowdinArticleDirectoryId: string): Promise<any> {
return getFile(name, crowdinArticleDirectoryId, this.payload)
}

async getFiles(crowdinArticleDirectoryId: number): Promise<any> {
async getFiles(crowdinArticleDirectoryId: string): Promise<any> {
return getFiles(crowdinArticleDirectoryId, this.payload)
}

Expand Down

0 comments on commit c784ef6

Please sign in to comment.