Skip to content
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

[RAV][nextjs] Ensure component works when default personalization variant is hidden #1383

Merged
merged 4 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,35 @@ describe('<sc-placeholder />', () => {
});
}));

it('should skip rendering components with no name', async(() => {
const phKey = 'main';
const route = {
placeholders: {
main: [
{
componentName: 'Home',
},
{
componentName: null,
},
],
},
};

comp.name = phKey;
comp.rendering = (route as unknown) as ComponentRendering;
fixture.detectChanges();

fixture.whenStable().then(() => {
fixture.detectChanges();

expect(de.children.length).toBe(1);

const homeDiv = de.query(By.directive(TestHomeComponent));
expect(homeDiv).not.toBeNull();
});
}));

it('should render null for unknown placeholder', async(() => {
const phKey = 'unknown';
const route = {
Expand Down
34 changes: 34 additions & 0 deletions packages/sitecore-jss-react/src/components/Placeholder.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,40 @@ it('should render MissingComponent for unknown rendering', () => {
expect(renderedComponent.find('.missing-component').length).to.equal(1);
});

it('should render nothing for rendering without a name', () => {
const componentFactory: ComponentFactory = (componentName: string) => {
const components = new Map<string, React.FC<{ [key: string]: unknown }>>();

const Home: React.FC<{ rendering?: RouteData }> = ({ rendering }) => (
<div className="home-mock"></div>
);

components.set('Home', Home);
return components.get(componentName) || null;
};

const route: any = {
placeholders: {
main: [
{
componentName: 'Home',
},
{
componentName: null,
},
],
},
};
const phKey = 'main';

const renderedComponent = mount(
<div className="empty-test">
<Placeholder name={phKey} rendering={route} componentFactory={componentFactory} />
</div>
);
expect(renderedComponent.children().length).to.equal(1);
});

it('should render HiddenRendering when rendering is hidden', () => {
const route: any = {
placeholders: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ export class PlaceholderCommon<T extends PlaceholderProps> extends React.Compone

if (componentRendering.componentName === HIDDEN_RENDERING_NAME) {
component = hiddenRenderingComponent ?? HiddenRendering;
} else if (!componentRendering.componentName) {
component = () => <></>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to render null value, did you try to set null?

Copy link
Contributor Author

@art-alexeyenko art-alexeyenko Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think either one should work (null is set anyway, so another piece of code would need to be changed), but this approach is a better fit for the code flow we have now (there's a null check below that actually triggers the issue).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it helps differentiate between an "empty" component (all good - means it's hidden) and "null" component (throws error, causes error frame to render).

} else {
component = this.getComponentForRendering(componentRendering);
}
Expand Down
23 changes: 23 additions & 0 deletions packages/sitecore-jss-vue/src/components/Placeholder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,29 @@ describe('<Placeholder />', () => {
warnSpy.mockReset();
});

it('should render nothing when component name is empty', () => {
const route = {
placeholders: {
main: [{ componentName: 'Home' }, { componentName: 'SfcHome' }, { componentName: null }],
},
};

const testComponent = {
render() {
return h(Placeholder, {
name: 'main',
rendering: route as any,
componentFactory,
});
},
};

const renderedComponent = mount(testComponent);
expect(renderedComponent.html()).toMatchSnapshot();

warnSpy.mockReset();
});

it('should render specific missing component for unknown components', () => {
const missingComponent = defineComponent({
inheritAttrs: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export function getVNodesForRenderingData(
component = getComponentForRendering(rendering, componentFactory);
}

if (!component) {
if (rendering.componentName && !component) {
console.error(
`Placeholder ${placeholderName} contains unknown component ${rendering.componentName}. Ensure that a Vue component exists for it, and that it is mapped in your component factory.`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ exports[`<Placeholder /> missing component should render default missing compone
</div>
`;

exports[`<Placeholder /> missing component should render nothing when component name is empty 1`] = `
<div class="home-mock">
</div>
<div class="home-mock-sfc">
</div>
`;

exports[`<Placeholder /> missing component should render specific missing component for unknown components 1`] = `
<div class="home-mock">
</div>
Expand Down