@@ -5,7 +5,7 @@ import { getFileByPath } from 'payload'
5
5
import { fileURLToPath } from 'url'
6
6
7
7
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
8
- import type { Category , Post } from './payload-types.js'
8
+ import type { Category , Config , Post } from './payload-types.js'
9
9
10
10
import { devUser } from '../credentials.js'
11
11
import { idToString } from '../helpers/idToString.js'
@@ -80,6 +80,7 @@ describe('Joins Field', () => {
80
80
category : category . id ,
81
81
upload : uploadedImage ,
82
82
categories,
83
+ categoriesLocalized : categories ,
83
84
group : {
84
85
category : category . id ,
85
86
camelCaseCategory : category . id ,
@@ -212,6 +213,89 @@ describe('Joins Field', () => {
212
213
expect ( otherCategoryWithPosts . hasManyPosts . docs [ 0 ] . title ) . toBe ( 'test 14' )
213
214
} )
214
215
216
+ it ( 'should populate joins using find with hasMany localized relationships' , async ( ) => {
217
+ const post_1 = await createPost (
218
+ {
219
+ title : `test es localized 1` ,
220
+ categoriesLocalized : [ category . id ] ,
221
+ group : {
222
+ category : category . id ,
223
+ camelCaseCategory : category . id ,
224
+ } ,
225
+ } ,
226
+ 'es' ,
227
+ )
228
+
229
+ const post_2 = await createPost (
230
+ {
231
+ title : `test es localized 2` ,
232
+ categoriesLocalized : [ otherCategory . id ] ,
233
+ group : {
234
+ category : category . id ,
235
+ camelCaseCategory : category . id ,
236
+ } ,
237
+ } ,
238
+ 'es' ,
239
+ )
240
+
241
+ const resultEn = await payload . find ( {
242
+ collection : 'categories' ,
243
+ where : {
244
+ id : { equals : category . id } ,
245
+ } ,
246
+ } )
247
+ const otherResultEn = await payload . find ( {
248
+ collection : 'categories' ,
249
+ where : {
250
+ id : { equals : otherCategory . id } ,
251
+ } ,
252
+ } )
253
+
254
+ const [ categoryWithPostsEn ] = resultEn . docs
255
+ const [ otherCategoryWithPostsEn ] = otherResultEn . docs
256
+
257
+ expect ( categoryWithPostsEn . hasManyPostsLocalized . docs ) . toHaveLength ( 10 )
258
+ expect ( categoryWithPostsEn . hasManyPostsLocalized . docs [ 0 ] ) . toHaveProperty ( 'title' )
259
+ expect ( categoryWithPostsEn . hasManyPostsLocalized . docs [ 0 ] . title ) . toBe ( 'test 14' )
260
+ expect ( otherCategoryWithPostsEn . hasManyPostsLocalized . docs ) . toHaveLength ( 8 )
261
+ expect ( otherCategoryWithPostsEn . hasManyPostsLocalized . docs [ 0 ] ) . toHaveProperty ( 'title' )
262
+ expect ( otherCategoryWithPostsEn . hasManyPostsLocalized . docs [ 0 ] . title ) . toBe ( 'test 14' )
263
+
264
+ const resultEs = await payload . find ( {
265
+ collection : 'categories' ,
266
+ locale : 'es' ,
267
+ where : {
268
+ id : { equals : category . id } ,
269
+ } ,
270
+ } )
271
+ const otherResultEs = await payload . find ( {
272
+ collection : 'categories' ,
273
+ locale : 'es' ,
274
+ where : {
275
+ id : { equals : otherCategory . id } ,
276
+ } ,
277
+ } )
278
+
279
+ const [ categoryWithPostsEs ] = resultEs . docs
280
+ const [ otherCategoryWithPostsEs ] = otherResultEs . docs
281
+
282
+ expect ( categoryWithPostsEs . hasManyPostsLocalized . docs ) . toHaveLength ( 1 )
283
+ expect ( categoryWithPostsEs . hasManyPostsLocalized . docs [ 0 ] . title ) . toBe ( 'test es localized 1' )
284
+
285
+ expect ( otherCategoryWithPostsEs . hasManyPostsLocalized . docs ) . toHaveLength ( 1 )
286
+ expect ( otherCategoryWithPostsEs . hasManyPostsLocalized . docs [ 0 ] . title ) . toBe ( 'test es localized 2' )
287
+
288
+ // clean up
289
+ await payload . delete ( {
290
+ collection : 'posts' ,
291
+ where : {
292
+ id : {
293
+ in : [ post_1 . id , post_2 . id ] ,
294
+ } ,
295
+ } ,
296
+ } )
297
+ } )
298
+
215
299
it ( 'should not error when deleting documents with joins' , async ( ) => {
216
300
const category = await payload . create ( {
217
301
collection : 'categories' ,
@@ -499,9 +583,10 @@ describe('Joins Field', () => {
499
583
} )
500
584
} )
501
585
502
- async function createPost ( overrides ?: Partial < Post > ) {
586
+ async function createPost ( overrides ?: Partial < Post > , locale ?: Config [ 'locale' ] ) {
503
587
return payload . create ( {
504
588
collection : 'posts' ,
589
+ locale,
505
590
data : {
506
591
title : 'test' ,
507
592
...overrides ,
0 commit comments