Skip to content

Commit 81d333f

Browse files
authored
test: add test for sorting by a virtual field with a reference (#12351)
1 parent 4fe3423 commit 81d333f

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

test/database/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ export default buildConfigWithDefaults({
460460
{
461461
slug: 'virtual-relations',
462462
admin: { useAsTitle: 'postTitle' },
463+
access: { read: () => true },
463464
fields: [
464465
{
465466
name: 'postTitle',

test/database/int.spec.ts

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,8 +2151,6 @@ describe('database', () => {
21512151
expect(descDocs[0]?.id).toBe(doc_2.id)
21522152
})
21532153

2154-
it.todo('should allow to sort by a virtual field with reference')
2155-
21562154
it('should allow virtual field 2x deep', async () => {
21572155
const category = await payload.create({
21582156
collection: 'categories',
@@ -2213,6 +2211,77 @@ describe('database', () => {
22132211
})
22142212
expect(globalData.postTitle).toBe('post')
22152213
})
2214+
2215+
it('should allow to sort by a virtual field with a refence, Local / GraphQL', async () => {
2216+
const post_1 = await payload.create({ collection: 'posts', data: { title: 'A' } })
2217+
const post_2 = await payload.create({ collection: 'posts', data: { title: 'B' } })
2218+
const doc_1 = await payload.create({
2219+
collection: 'virtual-relations',
2220+
data: { post: post_1 },
2221+
})
2222+
const doc_2 = await payload.create({
2223+
collection: 'virtual-relations',
2224+
data: { post: post_2 },
2225+
})
2226+
2227+
const queryDesc = `query {
2228+
VirtualRelations(
2229+
where: {OR: [{ id: { equals: ${JSON.stringify(doc_1.id)} } }, { id: { equals: ${JSON.stringify(doc_2.id)} } }],
2230+
}, sort: "-postTitle") {
2231+
docs {
2232+
id
2233+
}
2234+
}
2235+
}`
2236+
2237+
const {
2238+
data: {
2239+
VirtualRelations: { docs: graphqlDesc },
2240+
},
2241+
} = await restClient
2242+
.GRAPHQL_POST({ body: JSON.stringify({ query: queryDesc }) })
2243+
.then((res) => res.json())
2244+
2245+
const { docs: localDesc } = await payload.find({
2246+
collection: 'virtual-relations',
2247+
sort: '-postTitle',
2248+
where: { id: { in: [doc_1.id, doc_2.id] } },
2249+
})
2250+
2251+
expect(graphqlDesc[0].id).toBe(doc_2.id)
2252+
expect(graphqlDesc[1].id).toBe(doc_1.id)
2253+
expect(localDesc[0].id).toBe(doc_2.id)
2254+
expect(localDesc[1].id).toBe(doc_1.id)
2255+
2256+
const queryAsc = `query {
2257+
VirtualRelations(
2258+
where: {OR: [{ id: { equals: ${JSON.stringify(doc_1.id)} } }, { id: { equals: ${JSON.stringify(doc_2.id)} } }],
2259+
}, sort: "postTitle") {
2260+
docs {
2261+
id
2262+
}
2263+
}
2264+
}`
2265+
2266+
const {
2267+
data: {
2268+
VirtualRelations: { docs: graphqlAsc },
2269+
},
2270+
} = await restClient
2271+
.GRAPHQL_POST({ body: JSON.stringify({ query: queryAsc }) })
2272+
.then((res) => res.json())
2273+
2274+
const { docs: localAsc } = await payload.find({
2275+
collection: 'virtual-relations',
2276+
sort: 'postTitle',
2277+
where: { id: { in: [doc_1.id, doc_2.id] } },
2278+
})
2279+
2280+
expect(graphqlAsc[1].id).toBe(doc_2.id)
2281+
expect(graphqlAsc[0].id).toBe(doc_1.id)
2282+
expect(localAsc[1].id).toBe(doc_2.id)
2283+
expect(localAsc[0].id).toBe(doc_1.id)
2284+
})
22162285
})
22172286

22182287
it('should convert numbers to text', async () => {

0 commit comments

Comments
 (0)