2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2019-10-19 17:15:53 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import ExtensionPoint from "./ExtensionPoint";
|
|
|
|
|
import { shallow, mount } from "enzyme";
|
|
|
|
|
import "@scm-manager/ui-tests/enzyme";
|
|
|
|
|
import binder from "./binder";
|
2019-10-11 13:24:02 +02:00
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
jest.mock("./binder");
|
2019-10-11 13:24:02 +02:00
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
const mockedBinder = binder as jest.Mocked<typeof binder>;
|
|
|
|
|
|
|
|
|
|
describe("ExtensionPoint test", () => {
|
2019-10-11 13:24:02 +02:00
|
|
|
beforeEach(() => {
|
2019-10-19 17:15:53 +02:00
|
|
|
mockedBinder.hasExtension.mockReset();
|
|
|
|
|
mockedBinder.getExtension.mockReset();
|
|
|
|
|
mockedBinder.getExtensions.mockReset();
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
it("should render nothing, if no extension was bound", () => {
|
|
|
|
|
mockedBinder.hasExtension.mockReturnValue(true);
|
|
|
|
|
mockedBinder.getExtensions.mockReturnValue([]);
|
2019-10-11 13:24:02 +02:00
|
|
|
const rendered = shallow(<ExtensionPoint name="something.special" />);
|
2019-10-19 17:15:53 +02:00
|
|
|
expect(rendered.text()).toBe("");
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
it("should render the given component", () => {
|
2019-10-11 13:24:02 +02:00
|
|
|
const label = () => {
|
|
|
|
|
return <label>Extension One</label>;
|
|
|
|
|
};
|
2019-10-19 17:15:53 +02:00
|
|
|
mockedBinder.hasExtension.mockReturnValue(true);
|
|
|
|
|
mockedBinder.getExtension.mockReturnValue(label);
|
2019-10-11 13:24:02 +02:00
|
|
|
|
|
|
|
|
const rendered = mount(<ExtensionPoint name="something.special" />);
|
2019-10-19 17:15:53 +02:00
|
|
|
expect(rendered.text()).toBe("Extension One");
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
it("should render the given components", () => {
|
2019-10-11 13:24:02 +02:00
|
|
|
const labelOne = () => {
|
|
|
|
|
return <label>Extension One</label>;
|
|
|
|
|
};
|
|
|
|
|
const labelTwo = () => {
|
|
|
|
|
return <label>Extension Two</label>;
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
mockedBinder.hasExtension.mockReturnValue(true);
|
|
|
|
|
mockedBinder.getExtensions.mockReturnValue([labelOne, labelTwo]);
|
2019-10-11 13:24:02 +02:00
|
|
|
|
2020-02-19 10:06:10 +01:00
|
|
|
const rendered = mount(<ExtensionPoint name="something.special" renderAll={true} />);
|
2019-10-11 13:24:02 +02:00
|
|
|
const text = rendered.text();
|
2019-10-19 17:15:53 +02:00
|
|
|
expect(text).toContain("Extension One");
|
|
|
|
|
expect(text).toContain("Extension Two");
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
it("should render the given component, with the given props", () => {
|
2019-10-11 13:24:02 +02:00
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
value: string;
|
2019-10-11 13:24:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const label = (props: Props) => {
|
|
|
|
|
return <label>{props.value}</label>;
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
mockedBinder.hasExtension.mockReturnValue(true);
|
|
|
|
|
mockedBinder.getExtension.mockReturnValue(label);
|
2019-10-11 13:24:02 +02:00
|
|
|
|
|
|
|
|
const rendered = mount(
|
2019-10-19 16:38:07 +02:00
|
|
|
<ExtensionPoint
|
|
|
|
|
name="something.special"
|
|
|
|
|
props={{
|
2019-10-19 17:15:53 +02:00
|
|
|
value: "Awesome"
|
2019-10-19 16:38:07 +02:00
|
|
|
}}
|
2019-10-19 17:15:53 +02:00
|
|
|
/>
|
2019-10-11 13:24:02 +02:00
|
|
|
);
|
|
|
|
|
const text = rendered.text();
|
2019-10-19 17:15:53 +02:00
|
|
|
expect(text).toContain("Awesome");
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
it("should render children, if no extension is bound", () => {
|
2019-10-11 13:24:02 +02:00
|
|
|
const rendered = mount(
|
|
|
|
|
<ExtensionPoint name="something.special">
|
|
|
|
|
<p>Cool stuff</p>
|
2019-10-19 17:15:53 +02:00
|
|
|
</ExtensionPoint>
|
2019-10-11 13:24:02 +02:00
|
|
|
);
|
|
|
|
|
const text = rendered.text();
|
2019-10-19 17:15:53 +02:00
|
|
|
expect(text).toContain("Cool stuff");
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
it("should not render children, if an extension was bound", () => {
|
2019-10-11 13:24:02 +02:00
|
|
|
const label = () => {
|
|
|
|
|
return <label>Bound Extension</label>;
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
mockedBinder.hasExtension.mockReturnValue(true);
|
|
|
|
|
mockedBinder.getExtension.mockReturnValue(label);
|
2019-10-11 13:24:02 +02:00
|
|
|
|
|
|
|
|
const rendered = mount(
|
|
|
|
|
<ExtensionPoint name="something.special">
|
|
|
|
|
<p>Cool stuff</p>
|
2019-10-19 17:15:53 +02:00
|
|
|
</ExtensionPoint>
|
2019-10-11 13:24:02 +02:00
|
|
|
);
|
|
|
|
|
const text = rendered.text();
|
2019-10-19 17:15:53 +02:00
|
|
|
expect(text).toContain("Bound Extension");
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
it("should pass the context of the parent component", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const UserContext = React.createContext({
|
2019-10-19 17:15:53 +02:00
|
|
|
name: "anonymous"
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
2019-10-11 13:24:02 +02:00
|
|
|
|
|
|
|
|
type HelloProps = {
|
2019-10-19 16:38:07 +02:00
|
|
|
name: string;
|
2019-10-11 13:24:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const Hello = (props: HelloProps) => {
|
|
|
|
|
return <label>Hello {props.name}</label>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const HelloUser = () => {
|
2019-10-21 10:57:56 +02:00
|
|
|
return <UserContext.Consumer>{({ name }) => <Hello name={name} />}</UserContext.Consumer>;
|
2019-10-11 13:24:02 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-19 17:15:53 +02:00
|
|
|
mockedBinder.hasExtension.mockReturnValue(true);
|
|
|
|
|
mockedBinder.getExtension.mockReturnValue(HelloUser);
|
2019-10-11 13:24:02 +02:00
|
|
|
|
|
|
|
|
const App = () => {
|
|
|
|
|
return (
|
2019-10-19 16:38:07 +02:00
|
|
|
<UserContext.Provider
|
|
|
|
|
value={{
|
2019-10-19 17:15:53 +02:00
|
|
|
name: "Trillian"
|
2019-10-19 16:38:07 +02:00
|
|
|
}}
|
|
|
|
|
>
|
2019-10-11 13:24:02 +02:00
|
|
|
<ExtensionPoint name="hello" />
|
|
|
|
|
</UserContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const rendered = mount(<App />);
|
|
|
|
|
const text = rendered.text();
|
2019-10-19 17:15:53 +02:00
|
|
|
expect(text).toBe("Hello Trillian");
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
2020-02-19 10:06:10 +01:00
|
|
|
|
|
|
|
|
it("should not render nothing without extension and without default", () => {
|
|
|
|
|
mockedBinder.hasExtension.mockReturnValue(false);
|
|
|
|
|
|
|
|
|
|
const rendered = mount(<ExtensionPoint name="something.special" />);
|
|
|
|
|
const text = rendered.text();
|
|
|
|
|
expect(text).toBe("");
|
|
|
|
|
});
|
2020-03-30 15:00:30 +02:00
|
|
|
|
|
|
|
|
it("should render an instance", () => {
|
|
|
|
|
const Label = () => {
|
|
|
|
|
return <label>Extension One</label>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mockedBinder.hasExtension.mockReturnValue(true);
|
|
|
|
|
mockedBinder.getExtension.mockReturnValue(<Label />);
|
|
|
|
|
|
|
|
|
|
const rendered = mount(<ExtensionPoint name="something.special" />);
|
|
|
|
|
expect(rendered.text()).toBe("Extension One");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should render an instance with props", () => {
|
2020-03-31 17:11:16 +02:00
|
|
|
const Label = ({ name }: { name: string }) => {
|
2020-03-30 15:00:30 +02:00
|
|
|
return <label>Extension {name}</label>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mockedBinder.hasExtension.mockReturnValue(true);
|
|
|
|
|
mockedBinder.getExtension.mockReturnValue(<Label name="One" />);
|
|
|
|
|
|
2020-03-31 17:11:16 +02:00
|
|
|
const rendered = mount(<ExtensionPoint name="something.special" props={{ name: "Two" }} />);
|
2020-03-30 15:00:30 +02:00
|
|
|
expect(rendered.text()).toBe("Extension Two");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should transform extension, before render", () => {
|
2020-03-31 17:11:16 +02:00
|
|
|
const label = ({ name = "One" }: { name: string }) => {
|
2020-03-30 15:00:30 +02:00
|
|
|
return <label>Extension {name}</label>;
|
|
|
|
|
};
|
|
|
|
|
mockedBinder.hasExtension.mockReturnValue(true);
|
|
|
|
|
mockedBinder.getExtension.mockReturnValue(label);
|
|
|
|
|
|
|
|
|
|
const transformer = (props: object) => {
|
|
|
|
|
return {
|
|
|
|
|
...props,
|
|
|
|
|
name: "Two"
|
2020-03-31 17:11:16 +02:00
|
|
|
};
|
2020-03-30 15:00:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const rendered = mount(<ExtensionPoint name="something.special" propTransformer={transformer} />);
|
|
|
|
|
expect(rendered.text()).toBe("Extension Two");
|
2020-03-31 17:11:16 +02:00
|
|
|
});
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|