-
Notifications
You must be signed in to change notification settings - Fork 279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[sitecore-jss-react] [sitecore-jss-nextjs] Default Placeholder Content for empty fields in editMode metadata #1831
Changes from 6 commits
de2dc7c
e01330f
c0d4681
32bfea9
be9b0be
2fd41cc
801ebf6
237d1bd
c1164a7
79a0b4e
085e09b
aca9f14
881362c
54f0282
abbb0d7
a1edea7
6a94a33
052fc3b
74960b3
48a2af2
62bf073
2a05d7c
534a0f2
b669ab5
f18852a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import { mount } from 'enzyme'; | |
import { RouterContext } from 'next/dist/shared/lib/router-context.shared-runtime'; | ||
import { Link } from './Link'; | ||
import { spy } from 'sinon'; | ||
import { describe } from 'node:test'; | ||
|
||
const Router = (): NextRouter => ({ | ||
pathname: '/', | ||
|
@@ -361,7 +362,7 @@ describe('<Link />', () => { | |
expect(rendered).to.have.length(0); | ||
}); | ||
|
||
it('should render field metadata component when metadata property is present', () => { | ||
describe('editMode metadata', () => { | ||
const testMetadata = { | ||
contextItem: { | ||
id: '{09A07660-6834-476C-B93B-584248D3003B}', | ||
|
@@ -374,29 +375,96 @@ describe('<Link />', () => { | |
rawValue: 'Test1', | ||
}; | ||
|
||
const field = { | ||
value: { | ||
href: '/lorem', | ||
text: 'ipsum', | ||
class: 'my-link', | ||
}, | ||
metadata: testMetadata, | ||
}; | ||
|
||
const rendered = mount( | ||
<Page> | ||
<Link field={field} /> | ||
</Page> | ||
); | ||
|
||
expect(rendered.html()).to.equal( | ||
[ | ||
`<code type="text/sitecore" chrometype="field" class="scpm" kind="open">${JSON.stringify( | ||
testMetadata | ||
)}</code>`, | ||
'<a href="/lorem" class="my-link">ipsum</a>', | ||
'<code type="text/sitecore" chrometype="field" class="scpm" kind="close"></code>', | ||
].join('') | ||
); | ||
it('should render field metadata component when metadata property is present', () => { | ||
const field = { | ||
value: { | ||
href: '/lorem', | ||
text: 'ipsum', | ||
class: 'my-link', | ||
}, | ||
metadata: testMetadata, | ||
}; | ||
|
||
const rendered = mount( | ||
<Page> | ||
<Link field={field} /> | ||
</Page> | ||
); | ||
|
||
expect(rendered.html()).to.equal( | ||
[ | ||
`<code type="text/sitecore" chrometype="field" class="scpm" kind="open">${JSON.stringify( | ||
testMetadata | ||
)}</code>`, | ||
'<a href="/lorem" class="my-link">ipsum</a>', | ||
'<code type="text/sitecore" chrometype="field" class="scpm" kind="close"></code>', | ||
].join('') | ||
); | ||
}); | ||
|
||
it('should render default empty field placeholder when field value is empty in edit mode metadata', () => { | ||
const field = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to add a test case to cover: when field is the value itself (GenericFIeldValue) type (applicable for rest of unit tests below) |
||
value: '', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Link component you can't have empty string value. { href: undefined } or undefined. (applicable for the rest of unit tests below) |
||
metadata: testMetadata, | ||
}; | ||
|
||
const rendered = mount( | ||
<Page> | ||
<Link field={field} /> | ||
</Page> | ||
); | ||
|
||
expect(rendered.html()).to.equal( | ||
[ | ||
`<code type="text/sitecore" chrometype="field" class="scpm" kind="open">${JSON.stringify( | ||
testMetadata | ||
)}</code>`, | ||
'<span>[No text in field]</span>', | ||
`<code type="text/sitecore" chrometype="field" class="scpm" kind="close"></code>`, | ||
].join('') | ||
); | ||
}); | ||
|
||
it('should render custom empty field placeholder when provided, when field value is empty in edit mode metadata', () => { | ||
const field = { | ||
value: '', | ||
metadata: testMetadata, | ||
}; | ||
|
||
const EmptyValueEditingPlaceholder: React.FC = () => ( | ||
<span className="empty-field-value-placeholder">Custom Empty field value</span> | ||
); | ||
|
||
const rendered = mount( | ||
<Page> | ||
<Link field={field} emptyValueEditingPlaceholder={EmptyValueEditingPlaceholder} /> | ||
</Page> | ||
); | ||
|
||
expect(rendered.html()).to.equal( | ||
[ | ||
`<code type="text/sitecore" chrometype="field" class="scpm" kind="open">${JSON.stringify( | ||
testMetadata | ||
)}</code>`, | ||
'<span class="empty-field-value-placeholder">Custom Empty field value</span>', | ||
`<code type="text/sitecore" chrometype="field" class="scpm" kind="close"></code>`, | ||
].join('') | ||
); | ||
}); | ||
|
||
it('should render nothing when field value is empty, when editing is explicitly disabled in edit mode metadata ', () => { | ||
const field = { | ||
value: '', | ||
metadata: testMetadata, | ||
}; | ||
|
||
const rendered = mount( | ||
<Page> | ||
<Link field={field} editable={false} /> | ||
</Page> | ||
); | ||
|
||
expect(rendered.html()).to.equal(''); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,10 @@ export const Link = forwardRef<HTMLAnchorElement, LinkProps>( | |
|
||
if ( | ||
!field || | ||
(!(field as LinkFieldValue).editable && !field.value && !(field as LinkFieldValue).href) | ||
(!(field as LinkFieldValue).editable && | ||
!field.value && | ||
!(field as LinkFieldValue).href && | ||
!field.metadata) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to add a metadata check in other components? For example Next.js RichText component, it has a similar check. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where is the similar check for nextjs RichText? (link does not show it). I think we don't need it for RichText - we don't have similar 'return null' as it is in here in Link. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. i added it |
||
) { | ||
return null; | ||
} | ||
|
yavorsk marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ import chaiString from 'chai-string'; | |
import { mount } from 'enzyme'; | ||
import React from 'react'; | ||
import { NextImage } from './NextImage'; | ||
import { ImageField } from '@sitecore-jss/sitecore-jss-react'; | ||
import { | ||
ImageField, | ||
DefaultEmptyFieldEditingComponentImage, | ||
} from '@sitecore-jss/sitecore-jss-react'; | ||
import { ImageLoader } from 'next/image'; | ||
import { spy, match } from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
|
@@ -287,7 +290,7 @@ describe('<NextImage />', () => { | |
}); | ||
}); | ||
|
||
it('should render field metadata component when metadata property is present', () => { | ||
describe('editMode metadata', () => { | ||
const testMetadata = { | ||
contextItem: { | ||
id: '{09A07660-6834-476C-B93B-584248D3003B}', | ||
|
@@ -300,21 +303,78 @@ describe('<NextImage />', () => { | |
rawValue: 'Test1', | ||
}; | ||
|
||
const field = { | ||
value: { src: '/assets/img/test0.png', alt: 'my image' }, | ||
metadata: testMetadata, | ||
}; | ||
it('should render field metadata component when metadata property is present', () => { | ||
const field = { | ||
value: { src: '/assets/img/test0.png', alt: 'my image' }, | ||
metadata: testMetadata, | ||
}; | ||
|
||
const rendered = mount(<NextImage field={field} fill={true} />); | ||
|
||
expect(rendered.html()).to.equal( | ||
[ | ||
`<code type="text/sitecore" chrometype="field" class="scpm" kind="open">${JSON.stringify( | ||
testMetadata | ||
)}</code>`, | ||
'<img alt="my image" loading="lazy" decoding="async" data-nimg="fill" style="position: absolute; height: 100%; width: 100%; left: 0px; top: 0px; right: 0px; bottom: 0px; color: transparent;" sizes="100vw" srcset="/_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=640&q=75 640w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=750&q=75 750w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=828&q=75 828w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=1080&q=75 1080w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=1200&q=75 1200w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=1920&q=75 1920w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=2048&q=75 2048w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=3840&q=75 3840w" src="/_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=3840&q=75">', | ||
'<code type="text/sitecore" chrometype="field" class="scpm" kind="close"></code>', | ||
].join('') | ||
); | ||
}); | ||
|
||
it('should render default empty field placeholder for Image when field value is empty in edit mode metadata', () => { | ||
const field = { | ||
value: '', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String value is not expected for Image. Empty value can be in case if it's undefined or its { src: undefined } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to add a test case to cover: when field is the value itself (GenericFIeldValue) type (applicable for rest of unit tests below) |
||
metadata: testMetadata, | ||
}; | ||
|
||
const rendered = mount(<NextImage field={field} />); | ||
const defaultEmptyImagePlaceholder = mount(<DefaultEmptyFieldEditingComponentImage />); | ||
expect(rendered.html()).to.equal( | ||
[ | ||
`<code type="text/sitecore" chrometype="field" class="scpm" kind="open">${JSON.stringify( | ||
testMetadata | ||
)}</code>`, | ||
defaultEmptyImagePlaceholder.html(), | ||
'<code type="text/sitecore" chrometype="field" class="scpm" kind="close"></code>', | ||
].join('') | ||
); | ||
}); | ||
|
||
it('should render custom empty field placeholder when provided, when field value is empty in edit mode metadata', () => { | ||
const field = { | ||
value: '', | ||
metadata: testMetadata, | ||
}; | ||
|
||
const EmptyValueEditingPlaceholder: React.FC = () => ( | ||
<span className="empty-field-value-placeholder">Custom Empty field value</span> | ||
); | ||
|
||
const rendered = mount( | ||
<NextImage field={field} emptyValueEditingPlaceholder={EmptyValueEditingPlaceholder} /> | ||
); | ||
|
||
expect(rendered.html()).to.equal( | ||
[ | ||
`<code type="text/sitecore" chrometype="field" class="scpm" kind="open">${JSON.stringify( | ||
testMetadata | ||
)}</code>`, | ||
'<span class="empty-field-value-placeholder">Custom Empty field value</span>', | ||
'<code type="text/sitecore" chrometype="field" class="scpm" kind="close"></code>', | ||
].join('') | ||
); | ||
}); | ||
|
||
const rendered = mount(<NextImage field={field} fill={true} />); | ||
|
||
expect(rendered.html()).to.equal( | ||
[ | ||
`<code type="text/sitecore" chrometype="field" class="scpm" kind="open">${JSON.stringify( | ||
testMetadata | ||
)}</code>`, | ||
'<img alt="my image" loading="lazy" decoding="async" data-nimg="fill" style="position: absolute; height: 100%; width: 100%; left: 0px; top: 0px; right: 0px; bottom: 0px; color: transparent;" sizes="100vw" srcset="/_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=640&q=75 640w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=750&q=75 750w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=828&q=75 828w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=1080&q=75 1080w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=1200&q=75 1200w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=1920&q=75 1920w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=2048&q=75 2048w, /_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=3840&q=75 3840w" src="/_next/image?url=%2Fassets%2Fimg%2Ftest0.png&w=3840&q=75">', | ||
'<code type="text/sitecore" chrometype="field" class="scpm" kind="close"></code>', | ||
].join('') | ||
); | ||
it('should render nothing when field value is empty, when editing is explicitly disabled in edit mode metadata ', () => { | ||
const field = { | ||
value: '', | ||
metadata: testMetadata, | ||
}; | ||
|
||
const rendered = mount(<NextImage field={field} editable={false} />); | ||
|
||
expect(rendered.html()).to.equal(''); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.