Skip to content

Commit

Permalink
add unit test for placeholder to cover error boundary component wrapp…
Browse files Browse the repository at this point in the history
…ing; add 'type' prop in ErrorBoundary to cover case when renderEach function is being used
  • Loading branch information
yavorsk committed Apr 26, 2024
1 parent 45b2bbb commit 855df13
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type ErrorBoundaryProps = {
| React.FC<CustomErrorComponentProps>;
rendering?: ComponentRendering;
sitecoreContext: SitecoreContextValue;
type: string;
};

class ErrorBoundary extends React.Component<ErrorBoundaryProps> {
Expand Down
40 changes: 40 additions & 0 deletions packages/sitecore-jss-react/src/components/Placeholder.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,46 @@ describe('<Placeholder />', () => {
expect(renderedComponent.find('.sc-jss-placeholder-error').length).to.equal(1);
});

it('should render error message on error, only for the errored component', () => {
const componentFactory: ComponentFactory = (componentName: string) => {
const components = new Map<string, React.FC>();

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

components.set('Home', Home);
components.set('ThrowError', () => {
throw Error('an error occured');
});
components.set('Foo', () => <div className="foo-class">foo</div>);

return components.get(componentName) || null;
};

const route = ({
placeholders: {
main: [
{
componentName: 'ThrowError',
},
{
componentName: 'Foo',
},
],
},
} as unknown) as RouteData;
const phKey = 'main';

const renderedComponent = mount(
<Placeholder name={phKey} rendering={route} componentFactory={componentFactory} />
);
expect(renderedComponent.find('.sc-jss-placeholder-error').length).to.equal(1);
expect(renderedComponent.find('div.foo-class').length).to.equal(1);
});

it('should render custom errorComponent on error, if provided', () => {
const componentFactory: ComponentFactory = (componentName: string) => {
const components = new Map<string, React.FC<{ [key: string]: unknown }>>();
Expand Down
23 changes: 14 additions & 9 deletions packages/sitecore-jss-react/src/components/PlaceholderCommon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,20 @@ export class PlaceholderCommon<T extends PlaceholderProps> extends React.Compone
);
})
.filter((element) => element)
.map((element, id) => (
<ErrorBoundary
key={element.type + '-' + id}
customErrorComponent={this.props.errorComponent}
rendering={element.props.rendering as ComponentRendering}
>
{element}
</ErrorBoundary>
));
.map((element, id) => {
// assign type based on passed element - type='text/sitecore' should be ignored when renderEach Placeholder prop function is being used
const type = element.props.type === 'text/sitecore' ? element.props.type : '';
return (
<ErrorBoundary
key={element.type + '-' + id}
customErrorComponent={this.props.errorComponent}
rendering={element.props.rendering as ComponentRendering}
type={type}
>
{element}
</ErrorBoundary>
);
});
}

getComponentForRendering(renderingDefinition: ComponentRendering): ComponentType | null {
Expand Down

0 comments on commit 855df13

Please sign in to comment.