fix: prioritizes value key when filtering / querying for relationships (#4727)

* fix: object equality query by prioritizing value key in relationship queries

* chore: adds e2e & int test

* chore: updates test for REST querying on poly relationships
This commit is contained in:
Patrik
2024-01-11 15:56:07 -05:00
committed by GitHub
parent 35956eb837
commit d0f7677d5f
5 changed files with 135 additions and 9 deletions

View File

@@ -3,6 +3,8 @@ import type { Page } from '@playwright/test'
import { expect, test } from '@playwright/test'
import path from 'path'
import type { RelationshipField, TextField } from './payload-types'
import payload from '../../packages/payload/src'
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync'
import wait from '../../packages/payload/src/utilities/wait'
@@ -1377,6 +1379,7 @@ describe('fields', () => {
describe('relationship', () => {
let url: AdminUrlUtil
const tableRowLocator = 'table > tbody > tr'
beforeAll(async () => {
url = new AdminUrlUtil(serverURL, 'relationship-fields')
@@ -1709,6 +1712,37 @@ describe('fields', () => {
const firstOptionText = await firstOption.textContent()
expect(firstOptionText.trim()).toBe('Seeded text document')
})
test('should allow filtering by relationship field / equals', async () => {
const textDoc = await createTextFieldDoc()
await createRelationshipFieldDoc({ value: textDoc.id, relationTo: 'text-fields' })
await page.goto(url.list)
await page.locator('.list-controls__toggle-columns').click()
await page.locator('.list-controls__toggle-where').click()
await page.waitForSelector('.list-controls__where.rah-static--height-auto')
await page.locator('.where-builder__add-first-filter').click()
const conditionField = page.locator('.condition__field')
await conditionField.click()
const dropdownFieldOptions = conditionField.locator('.rs__option')
await dropdownFieldOptions.locator('text=Relationship').nth(0).click()
const operatorField = page.locator('.condition__operator')
await operatorField.click()
const dropdownOperatorOptions = operatorField.locator('.rs__option')
await dropdownOperatorOptions.locator('text=equals').click()
const valueField = page.locator('.condition__value')
await valueField.click()
const dropdownValueOptions = valueField.locator('.rs__option')
await dropdownValueOptions.locator('text=some text').click()
await expect(page.locator(tableRowLocator)).toHaveCount(1)
})
})
describe('upload', () => {
@@ -1950,3 +1984,27 @@ describe('fields', () => {
})
})
})
async function createTextFieldDoc(overrides?: Partial<TextField>): Promise<TextField> {
return payload.create({
collection: 'text-fields',
data: {
text: 'some text',
localizedText: 'some localized text',
...overrides,
},
}) as unknown as Promise<TextField>
}
async function createRelationshipFieldDoc(
relationship: RelationshipField['relationship'],
overrides?: Partial<RelationshipField>,
): Promise<RelationshipField> {
return payload.create({
collection: 'relationship-fields',
data: {
relationship,
...overrides,
},
}) as unknown as Promise<RelationshipField>
}

View File

@@ -12,6 +12,7 @@ import type {
import payload from '../../packages/payload/src'
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync'
import { devUser } from '../credentials'
import { initPayloadTest } from '../helpers/configHelpers'
import { RESTClient } from '../helpers/rest'
import config, {
@@ -23,15 +24,35 @@ import config, {
slug,
} from './config'
let apiUrl
let jwt
let client: RESTClient
const headers = {
'Content-Type': 'application/json',
}
const { email, password } = devUser
type EasierChained = { id: string; relation: EasierChained }
describe('Relationships', () => {
beforeAll(async () => {
const { serverURL } = await initPayloadTest({ __dirname, init: { local: false } })
apiUrl = `${serverURL}/api`
client = new RESTClient(config, { serverURL, defaultSlug: slug })
await client.login()
const response = await fetch(`${apiUrl}/users/login`, {
body: JSON.stringify({
email,
password,
}),
headers,
method: 'post',
})
const data = await response.json()
jwt = data.token
})
afterAll(async () => {
@@ -582,7 +603,7 @@ describe('Relationships', () => {
},
})
const query = await client.find({
const queryOne = await client.find({
slug: 'polymorphic-relationships',
query: {
and: [
@@ -600,7 +621,26 @@ describe('Relationships', () => {
},
})
expect(query.result.docs).toHaveLength(1)
const queryTwo = await client.find({
slug: 'polymorphic-relationships',
query: {
and: [
{
'polymorphic.relationTo': {
equals: 'movies',
},
},
{
'polymorphic.value': {
equals: movie.id,
},
},
],
},
})
expect(queryOne.result.docs).toHaveLength(1)
expect(queryTwo.result.docs).toHaveLength(1)
})
})
})