Files
payload/test/joins/seed.ts
Said Akhrarov 077fb3ab30 fix(ui): respect locale in buildTableState (#11147)
### What?
This PR fixes an issue where the `join` field table was not respecting
the locale selected in the admin ui localizer.

This also introduces an e2e test to the existing suite to catch this
issue.

### Why?
To properly render `join` field table data according to chosen and
configured locales.

### How?
Threading `req.locale` through to the `payload.find` call in
`buildTableState`.

Fixes #11134

Before:

[Editing---Category--join-locales-before-Payload.webm](https://github.com/user-attachments/assets/d77b71bb-f849-4be2-aa96-26dbfedb52d4)

After:

[Editing---Category--join-locales-after-Payload.webm](https://github.com/user-attachments/assets/0d1f7351-adf4-4bad-ac82-0fee67f8b66a)
2025-02-14 12:23:49 -05:00

159 lines
3.2 KiB
TypeScript

import type { Payload } from 'payload'
import path from 'path'
import { getFileByPath } from 'payload'
import { fileURLToPath } from 'url'
import { devUser } from '../credentials.js'
import { seedDB } from '../helpers/seed.js'
import {
categoriesJoinRestrictedSlug,
categoriesSlug,
collectionRestrictedSlug,
collectionSlugs,
hiddenPostsSlug,
postsSlug,
uploadsSlug,
} from './shared.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
export const seed = async (_payload: Payload) => {
await _payload.create({
collection: 'users',
data: {
email: devUser.email,
password: devUser.password,
},
})
const category = await _payload.create({
collection: categoriesSlug,
data: {
name: 'example',
group: {},
},
})
await _payload.create({
collection: hiddenPostsSlug,
data: {
category: category.id,
title: 'Test Post 1',
},
})
const post1 = await _payload.create({
collection: postsSlug,
data: {
category: category.id,
group: {
category: category.id,
},
title: 'Test Post 1',
localizedText: 'Text in en',
},
})
const post2 = await _payload.create({
collection: postsSlug,
data: {
category: category.id,
group: {
category: category.id,
},
title: 'Test Post 2',
localizedText: 'Text in en',
},
})
const post3 = await _payload.create({
collection: postsSlug,
data: {
category: category.id,
group: {
category: category.id,
},
title: 'Test Post 3',
localizedText: 'Text in en',
},
})
await _payload.update({
collection: postsSlug,
id: post1.id,
data: {
localizedText: 'Text in es',
},
locale: 'es',
})
await _payload.update({
collection: postsSlug,
id: post2.id,
data: {
localizedText: 'Text in es',
},
locale: 'es',
})
await _payload.update({
collection: postsSlug,
id: post3.id,
data: {
localizedText: 'Text in es',
},
locale: 'es',
})
// create an upload with image.png
const imageFilePath = path.resolve(dirname, './image.png')
const imageFile = await getFileByPath(imageFilePath)
const { id: uploadedImage } = await _payload.create({
collection: uploadsSlug,
data: {},
file: imageFile,
})
// create a post that uses the upload
await _payload.create({
collection: postsSlug,
data: {
upload: uploadedImage,
},
})
const restrictedCategory = await _payload.create({
collection: categoriesJoinRestrictedSlug,
data: {
name: 'categoryJoinRestricted',
},
})
await _payload.create({
collection: collectionRestrictedSlug,
data: {
title: 'should not allow read',
canRead: false,
category: restrictedCategory.id,
},
})
await _payload.create({
collection: collectionRestrictedSlug,
data: {
title: 'should allow read',
canRead: true,
category: restrictedCategory.id,
},
})
}
export async function clearAndSeedEverything(_payload: Payload) {
return await seedDB({
_payload,
collectionSlugs,
seedFunction: seed,
snapshotKey: 'joinsTest',
})
}