/* * MIT License * * Copyright (c) 2020-present Cloudogu GmbH and Contributors * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import React from "react"; import ExtensionPoint from "./ExtensionPoint"; import { shallow, mount } from "enzyme"; import "@scm-manager/ui-tests/enzyme"; import binder from "./binder"; jest.mock("./binder"); const mockedBinder = binder as jest.Mocked; describe("ExtensionPoint test", () => { beforeEach(() => { mockedBinder.hasExtension.mockReset(); mockedBinder.getExtension.mockReset(); mockedBinder.getExtensions.mockReset(); }); it("should render nothing, if no extension was bound", () => { mockedBinder.hasExtension.mockReturnValue(true); mockedBinder.getExtensions.mockReturnValue([]); const rendered = shallow(); expect(rendered.text()).toBe(""); }); it("should render the given component", () => { const label = () => { return ; }; mockedBinder.hasExtension.mockReturnValue(true); mockedBinder.getExtension.mockReturnValue(label); const rendered = mount(); expect(rendered.text()).toBe("Extension One"); }); it("should render the given components", () => { const labelOne = () => { return ; }; const labelTwo = () => { return ; }; mockedBinder.hasExtension.mockReturnValue(true); mockedBinder.getExtensions.mockReturnValue([labelOne, labelTwo]); const rendered = mount(); const text = rendered.text(); expect(text).toContain("Extension One"); expect(text).toContain("Extension Two"); }); it("should render the given component, with the given props", () => { type Props = { value: string; }; const label = (props: Props) => { return ; }; mockedBinder.hasExtension.mockReturnValue(true); mockedBinder.getExtension.mockReturnValue(label); const rendered = mount( ); const text = rendered.text(); expect(text).toContain("Awesome"); }); it("should render children, if no extension is bound", () => { const rendered = mount(

Cool stuff

); const text = rendered.text(); expect(text).toContain("Cool stuff"); }); it("should not render children, if an extension was bound", () => { const label = () => { return ; }; mockedBinder.hasExtension.mockReturnValue(true); mockedBinder.getExtension.mockReturnValue(label); const rendered = mount(

Cool stuff

); const text = rendered.text(); expect(text).toContain("Bound Extension"); }); it("should pass the context of the parent component", () => { const UserContext = React.createContext({ name: "anonymous" }); type HelloProps = { name: string; }; const Hello = (props: HelloProps) => { return ; }; const HelloUser = () => { return {({ name }) => }; }; mockedBinder.hasExtension.mockReturnValue(true); mockedBinder.getExtension.mockReturnValue(HelloUser); const App = () => { return ( ); }; const rendered = mount(); 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(""); }); it("should render an instance", () => { const Label = () => { return ; }; mockedBinder.hasExtension.mockReturnValue(true); mockedBinder.getExtension.mockReturnValue(