diff --git a/scm-ui/ui-extensions/src/ExtensionPoint.test.tsx b/scm-ui/ui-extensions/src/ExtensionPoint.test.tsx index c40362070f..171b60b862 100644 --- a/scm-ui/ui-extensions/src/ExtensionPoint.test.tsx +++ b/scm-ui/ui-extensions/src/ExtensionPoint.test.tsx @@ -33,12 +33,6 @@ describe("ExtensionPoint test", () => { expect(rendered.text()).toBe("Extension One"); }); - // We use this wrapper since Enzyme cannot handle React Fragments (see https://github.com/airbnb/enzyme/issues/1213) - class ExtensionPointEnzymeFix extends ExtensionPoint { - render() { - return
{super.render()}
; - } - } it("should render the given components", () => { const labelOne = () => { return ; @@ -50,7 +44,7 @@ describe("ExtensionPoint test", () => { mockedBinder.hasExtension.mockReturnValue(true); mockedBinder.getExtensions.mockReturnValue([labelOne, labelTwo]); - const rendered = mount(); + const rendered = mount(); const text = rendered.text(); expect(text).toContain("Extension One"); expect(text).toContain("Extension Two"); @@ -143,4 +137,12 @@ describe("ExtensionPoint test", () => { const text = rendered.text(); expect(text).toBe("Hello Trillian"); }); + + it("should not render nothing without extension and without default", () => { + mockedBinder.hasExtension.mockReturnValue(false); + + const rendered = mount(); + const text = rendered.text(); + expect(text).toBe(""); + }); }); diff --git a/scm-ui/ui-extensions/src/ExtensionPoint.tsx b/scm-ui/ui-extensions/src/ExtensionPoint.tsx index 9ae68a1224..24caef63f9 100644 --- a/scm-ui/ui-extensions/src/ExtensionPoint.tsx +++ b/scm-ui/ui-extensions/src/ExtensionPoint.tsx @@ -1,53 +1,51 @@ import * as React from "react"; -import binder from "./binder"; +import { Binder } from "./binder"; +import { FC, ReactNode } from "react"; +import useBinder from "./useBinder"; type Props = { name: string; renderAll?: boolean; props?: object; - children?: React.ReactNode; +}; + +const renderAllExtensions = (binder: Binder, name: string, props?: object) => { + const extensions = binder.getExtensions(name, props); + return ( + <> + {extensions.map((Component, index) => { + return ; + })} + + ); +}; + +const renderSingleExtension = (binder: Binder, name: string, props?: object) => { + const Component = binder.getExtension(name, props); + if (!Component) { + return null; + } + return ; +}; + +const renderDefault = (children: ReactNode) => { + if (children) { + return <>{children}; + } + return null; }; /** * ExtensionPoint renders components which are bound to an extension point. */ -class ExtensionPoint extends React.Component { - renderAll(name: string, props?: object) { - const extensions = binder.getExtensions(name, props); - return ( - <> - {extensions.map((Component, index) => { - return ; - })} - - ); +const ExtensionPoint: FC = ({ name, renderAll, props, children }) => { + const binder = useBinder(); + if (!binder.hasExtension(name, props)) { + return renderDefault(children); + } else if (renderAll) { + return renderAllExtensions(binder, name, props); } - - renderSingle(name: string, props?: object) { - const Component = binder.getExtension(name, props); - if (!Component) { - return null; - } - return ; - } - - renderDefault() { - const { children } = this.props; - if (children) { - return <>{children}; - } - return null; - } - - render() { - const { name, renderAll, props } = this.props; - if (!binder.hasExtension(name, props)) { - return this.renderDefault(); - } else if (renderAll) { - return this.renderAll(name, props); - } - return this.renderSingle(name, props); - } -} + return renderSingleExtension(binder, name, props); +}; export default ExtensionPoint; diff --git a/scm-ui/ui-extensions/src/binder.test.ts b/scm-ui/ui-extensions/src/binder.test.ts index 2bcd302a36..6915f04282 100644 --- a/scm-ui/ui-extensions/src/binder.test.ts +++ b/scm-ui/ui-extensions/src/binder.test.ts @@ -4,7 +4,7 @@ describe("binder tests", () => { let binder: Binder; beforeEach(() => { - binder = new Binder(); + binder = new Binder("testing"); }); it("should return an empty array for non existing extension points", () => {