From af164159fb52f4b0ef97e2fa34b881f97bc07310 Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Mon, 13 Mar 2023 17:34:51 -0400 Subject: [PATCH] fix: undefined point fields saving as empty object (#2313) --- src/fields/hooks/afterRead/promise.ts | 2 + src/fields/hooks/beforeValidate/promise.ts | 15 +++++ test/fields/e2e.spec.ts | 72 +++++++++++++++++++++- 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/fields/hooks/afterRead/promise.ts b/src/fields/hooks/afterRead/promise.ts index 1f6390d6d4..5aaa02e19f 100644 --- a/src/fields/hooks/afterRead/promise.ts +++ b/src/fields/hooks/afterRead/promise.ts @@ -130,6 +130,8 @@ export const promise = async ({ const pointDoc = siblingDoc[field.name] as Record; if (Array.isArray(pointDoc?.coordinates) && pointDoc.coordinates.length === 2) { siblingDoc[field.name] = pointDoc.coordinates; + } else { + siblingDoc[field.name] = undefined; } break; diff --git a/src/fields/hooks/beforeValidate/promise.ts b/src/fields/hooks/beforeValidate/promise.ts index 0d5d7cf816..929c05daf9 100644 --- a/src/fields/hooks/beforeValidate/promise.ts +++ b/src/fields/hooks/beforeValidate/promise.ts @@ -56,6 +56,21 @@ export const promise = async ({ break; } + case 'point': { + if (Array.isArray(siblingData[field.name])) { + siblingData[field.name] = (siblingData[field.name] as string[]).map((coordinate, i) => { + if (typeof coordinate === 'string') { + const value = siblingData[field.name][i] as string; + const trimmed = value.trim(); + return (trimmed.length === 0) ? null : parseFloat(trimmed); + } + return coordinate; + }); + } + + break; + } + case 'checkbox': { if (siblingData[field.name] === 'true') siblingData[field.name] = true; if (siblingData[field.name] === 'false') siblingData[field.name] = false; diff --git a/test/fields/e2e.spec.ts b/test/fields/e2e.spec.ts index 551531e56c..f478459f74 100644 --- a/test/fields/e2e.spec.ts +++ b/test/fields/e2e.spec.ts @@ -1,6 +1,7 @@ import type { Page } from '@playwright/test'; import { expect, test } from '@playwright/test'; import path from 'path'; +import payload from '../../src'; import { AdminUrlUtil } from '../helpers/adminUrlUtil'; import { initPayloadE2E } from '../helpers/configHelpers'; import { login, saveDocAndAssert } from '../helpers'; @@ -136,8 +137,26 @@ describe('fields', () => { describe('point', () => { let url: AdminUrlUtil; - beforeAll(() => { + let filledGroupPoint; + let emptyGroupPoint; + beforeAll(async () => { url = new AdminUrlUtil(serverURL, pointFieldsSlug); + filledGroupPoint = await payload.create({ + collection: pointFieldsSlug, + data: { + point: [5, 5], + localized: [4, 2], + group: { point: [4, 2] }, + }, + }); + emptyGroupPoint = await payload.create({ + collection: pointFieldsSlug, + data: { + point: [5, 5], + localized: [3, -2], + group: {}, + }, + }); }); test('should save point', async () => { @@ -161,6 +180,57 @@ describe('fields', () => { await groupLatField.fill('-8'); await saveDocAndAssert(page); + await expect(await longField.getAttribute('value')).toEqual('9'); + await expect(await latField.getAttribute('value')).toEqual('-2'); + await expect(await localizedLongField.getAttribute('value')).toEqual('1'); + await expect(await localizedLatField.getAttribute('value')).toEqual('-1'); + await expect(await groupLongitude.getAttribute('value')).toEqual('3'); + await expect(await groupLatField.getAttribute('value')).toEqual('-8'); + }); + + test('should update point', async () => { + await page.goto(url.edit(emptyGroupPoint.id)); + const longField = page.locator('#field-longitude-point'); + await longField.fill('9'); + + const latField = page.locator('#field-latitude-point'); + await latField.fill('-2'); + + const localizedLongField = page.locator('#field-longitude-localized'); + await localizedLongField.fill('2'); + + const localizedLatField = page.locator('#field-latitude-localized'); + await localizedLatField.fill('-2'); + + const groupLongitude = page.locator('#field-longitude-group__point'); + await groupLongitude.fill('3'); + + const groupLatField = page.locator('#field-latitude-group__point'); + await groupLatField.fill('-8'); + + await saveDocAndAssert(page); + + await expect(await longField.getAttribute('value')).toEqual('9'); + await expect(await latField.getAttribute('value')).toEqual('-2'); + await expect(await localizedLongField.getAttribute('value')).toEqual('2'); + await expect(await localizedLatField.getAttribute('value')).toEqual('-2'); + await expect(await groupLongitude.getAttribute('value')).toEqual('3'); + await expect(await groupLatField.getAttribute('value')).toEqual('-8'); + }); + + test('should be able to clear a value point', async () => { + await page.goto(url.edit(filledGroupPoint.id)); + + const groupLongitude = page.locator('#field-longitude-group__point'); + await groupLongitude.fill(''); + + const groupLatField = page.locator('#field-latitude-group__point'); + await groupLatField.fill(''); + + await saveDocAndAssert(page); + + await expect(await groupLongitude.getAttribute('value')).toEqual(''); + await expect(await groupLatField.getAttribute('value')).toEqual(''); }); });