2019-10-19 16:38:07 +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 16:38:07 +02:00
|
|
|
jest.mock('./binder');
|
2019-10-11 13:24:02 +02:00
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
describe('ExtensionPoint test', () => {
|
2019-10-11 13:24:02 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
|
binder.hasExtension.mockReset();
|
|
|
|
|
binder.getExtension.mockReset();
|
|
|
|
|
binder.getExtensions.mockReset();
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should render nothing, if no extension was bound', () => {
|
2019-10-11 13:24:02 +02:00
|
|
|
binder.hasExtension.mockReturnValue(true);
|
|
|
|
|
binder.getExtensions.mockReturnValue([]);
|
|
|
|
|
const rendered = shallow(<ExtensionPoint name="something.special" />);
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(rendered.text()).toBe('');
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should render the given component', () => {
|
2019-10-11 13:24:02 +02:00
|
|
|
const label = () => {
|
|
|
|
|
return <label>Extension One</label>;
|
|
|
|
|
};
|
|
|
|
|
binder.hasExtension.mockReturnValue(true);
|
|
|
|
|
binder.getExtension.mockReturnValue(label);
|
|
|
|
|
|
|
|
|
|
const rendered = mount(<ExtensionPoint name="something.special" />);
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(rendered.text()).toBe('Extension One');
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// We use this wrapper since Enzyme cannot handle React Fragments (see https://github.com/airbnb/enzyme/issues/1213)
|
|
|
|
|
class ExtensionPointEnzymeFix extends ExtensionPoint {
|
|
|
|
|
render() {
|
2019-10-19 16:38:07 +02:00
|
|
|
return <div>{super.render()}</div>;
|
|
|
|
|
}
|
2019-10-11 13:24:02 +02:00
|
|
|
}
|
2019-10-19 16:38:07 +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>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
binder.hasExtension.mockReturnValue(true);
|
|
|
|
|
binder.getExtensions.mockReturnValue([labelOne, labelTwo]);
|
|
|
|
|
|
|
|
|
|
const rendered = mount(
|
2019-10-19 16:38:07 +02:00
|
|
|
<ExtensionPointEnzymeFix name="something.special" renderAll={true} />,
|
2019-10-11 13:24:02 +02:00
|
|
|
);
|
|
|
|
|
const text = rendered.text();
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(text).toContain('Extension One');
|
|
|
|
|
expect(text).toContain('Extension Two');
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +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>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
binder.hasExtension.mockReturnValue(true);
|
|
|
|
|
binder.getExtension.mockReturnValue(label);
|
|
|
|
|
|
|
|
|
|
const rendered = mount(
|
2019-10-19 16:38:07 +02:00
|
|
|
<ExtensionPoint
|
|
|
|
|
name="something.special"
|
|
|
|
|
props={{
|
|
|
|
|
value: 'Awesome',
|
|
|
|
|
}}
|
|
|
|
|
/>,
|
2019-10-11 13:24:02 +02:00
|
|
|
);
|
|
|
|
|
const text = rendered.text();
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(text).toContain('Awesome');
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +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 16:38:07 +02:00
|
|
|
</ExtensionPoint>,
|
2019-10-11 13:24:02 +02:00
|
|
|
);
|
|
|
|
|
const text = rendered.text();
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(text).toContain('Cool stuff');
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +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>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
binder.hasExtension.mockReturnValue(true);
|
|
|
|
|
binder.getExtension.mockReturnValue(label);
|
|
|
|
|
|
|
|
|
|
const rendered = mount(
|
|
|
|
|
<ExtensionPoint name="something.special">
|
|
|
|
|
<p>Cool stuff</p>
|
2019-10-19 16:38:07 +02:00
|
|
|
</ExtensionPoint>,
|
2019-10-11 13:24:02 +02:00
|
|
|
);
|
|
|
|
|
const text = rendered.text();
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(text).toContain('Bound Extension');
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
it('should pass the context of the parent component', () => {
|
|
|
|
|
const UserContext = React.createContext({
|
|
|
|
|
name: 'anonymous',
|
|
|
|
|
});
|
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 = () => {
|
|
|
|
|
return (
|
|
|
|
|
<UserContext.Consumer>
|
|
|
|
|
{({ name }) => <Hello name={name} />}
|
|
|
|
|
</UserContext.Consumer>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
binder.hasExtension.mockReturnValue(true);
|
|
|
|
|
binder.getExtension.mockReturnValue(HelloUser);
|
|
|
|
|
|
|
|
|
|
const App = () => {
|
|
|
|
|
return (
|
2019-10-19 16:38:07 +02:00
|
|
|
<UserContext.Provider
|
|
|
|
|
value={{
|
|
|
|
|
name: 'Trillian',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2019-10-11 13:24:02 +02:00
|
|
|
<ExtensionPoint name="hello" />
|
|
|
|
|
</UserContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const rendered = mount(<App />);
|
|
|
|
|
const text = rendered.text();
|
2019-10-19 16:38:07 +02:00
|
|
|
expect(text).toBe('Hello Trillian');
|
2019-10-11 13:24:02 +02:00
|
|
|
});
|
|
|
|
|
});
|