From 4a1519e691dad560a0c1dce0c59d57d7d2435d83 Mon Sep 17 00:00:00 2001
From: Illia Kovalenko <23364749+illiakovalenko@users.noreply.github.com>
Date: Thu, 11 Jul 2024 12:53:53 +0300
Subject: [PATCH] [React][Next.js] DateField empty value is not treated as
empty (#1836)
* [React][Next.js] DateField empty value is not treated as empty
* Updated CHANGELOG
* Fixed lint step
---
CHANGELOG.md | 1 +
.../src/components/Date.test.tsx | 135 ++++++++++++------
.../withEmptyFieldEditingComponent.test.tsx | 60 ++++++++
packages/sitecore-jss/src/layout/index.ts | 7 +-
.../sitecore-jss/src/layout/utils.test.ts | 30 +++-
packages/sitecore-jss/src/layout/utils.ts | 9 +-
6 files changed, 197 insertions(+), 45 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b4307b6442..2fc4744b15 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,7 @@ Our versioning strategy is as follows:
* `[templates/nextjs]` `[templates/react]` `[templates/vue]` `[templates/angular]` Changed formatting in temp/config to prevent parse issues in Unix systems ([#1787](https://github.com/Sitecore/jss/pull/1787))([#1791](https://github.com/Sitecore/jss/pull/1791))
* `[templates/nextjs-sxa]` The banner variant of image component is fixed with supporting metadata mode. ([#1826](https://github.com/Sitecore/jss/pull/1826))
+* `[sitecore-jss]` `[sitecore-jss-react]` DateField empty value is not treated as empty ([#1836](https://github.com/Sitecore/jss/pull/1836))
### 🎉 New Features & Improvements
diff --git a/packages/sitecore-jss-react/src/components/Date.test.tsx b/packages/sitecore-jss-react/src/components/Date.test.tsx
index d62d7f5e17..d2872c9360 100644
--- a/packages/sitecore-jss-react/src/components/Date.test.tsx
+++ b/packages/sitecore-jss-react/src/components/Date.test.tsx
@@ -4,6 +4,7 @@ import { mount, shallow } from 'enzyme';
import React from 'react';
import { DateField } from './Date';
import { describe } from 'node:test';
+import { EMPTY_DATE_FIELD_VALUE } from '@sitecore-jss/sitecore-jss/layout';
describe('${JSON.stringify(
- testMetadata
- )}
`,
- '[No text in field]',
- '',
- ].join('')
- );
- });
-
- it('should render custom empty field component when provided, when field value is empty', () => {
- const field = {
- value: '',
- metadata: testMetadata,
- };
-
- const EmptyFieldEditingComponent: React.FC = () => (
- Custom Empty field value
- );
-
- const rendered = mount(
-
${JSON.stringify(
- testMetadata
- )}
`,
- 'Custom Empty field value',
- '',
- ].join('')
- );
+ describe('empty value', () => {
+ describe('Should render default component', () => {
+ it('field value is empty string', () => {
+ const field = {
+ value: '',
+ metadata: testMetadata,
+ };
+
+ const rendered = mount(
${JSON.stringify(
+ testMetadata
+ )}
`,
+ '[No text in field]',
+ '',
+ ].join('')
+ );
+ });
+
+ it('field value is default empty date value', () => {
+ const field = {
+ value: EMPTY_DATE_FIELD_VALUE,
+ metadata: testMetadata,
+ };
+
+ const rendered = mount(
${JSON.stringify(
+ testMetadata
+ )}
`,
+ '[No text in field]',
+ '',
+ ].join('')
+ );
+ });
+ });
+
+ describe('Should render custom component', () => {
+ it('field value is empty string', () => {
+ const field = {
+ value: '',
+ metadata: testMetadata,
+ };
+
+ const EmptyFieldEditingComponent: React.FC = () => (
+ Custom Empty field value
+ );
+
+ const rendered = mount(
+
${JSON.stringify(
+ testMetadata
+ )}
`,
+ 'Custom Empty field value',
+ '',
+ ].join('')
+ );
+ });
+
+ it('field value is defaule empty date value', () => {
+ const field = {
+ value: EMPTY_DATE_FIELD_VALUE,
+ metadata: testMetadata,
+ };
+
+ const EmptyFieldEditingComponent: React.FC = () => (
+ Custom Empty field value
+ );
+
+ const rendered = mount(
+
${JSON.stringify(
+ testMetadata
+ )}
`,
+ 'Custom Empty field value',
+ '',
+ ].join('')
+ );
+ });
+ });
});
it('should render nothing when field value is empty, when editing is explicitly disabled ', () => {
diff --git a/packages/sitecore-jss-react/src/enhancers/withEmptyFieldEditingComponent.test.tsx b/packages/sitecore-jss-react/src/enhancers/withEmptyFieldEditingComponent.test.tsx
index 2cd08c9c60..0c03c34c17 100644
--- a/packages/sitecore-jss-react/src/enhancers/withEmptyFieldEditingComponent.test.tsx
+++ b/packages/sitecore-jss-react/src/enhancers/withEmptyFieldEditingComponent.test.tsx
@@ -5,6 +5,7 @@ import { mount } from 'enzyme';
import { withEmptyFieldEditingComponent } from './withEmptyFieldEditingComponent';
import { DefaultEmptyFieldEditingComponentText } from '../components/DefaultEmptyFieldEditingComponents';
import { describe } from 'node:test';
+import { EMPTY_DATE_FIELD_VALUE } from '@sitecore-jss/sitecore-jss/layout';
describe('withEmptyFieldEditingComponent', () => {
describe('Metadata', () => {
@@ -165,6 +166,65 @@ describe('withEmptyFieldEditingComponent', () => {
expect(rendered.html()).to.equal('
bar
bar