import {mount} from 'enzyme'; import PropTypes from 'prop-types'; import React, {Component} from 'react'; import Overridable, {parametrize, OverridableContext} from '.'; // export from index.js to test it class ExampleComponent extends Component { static propTypes = { title: PropTypes.string.isRequired, color: PropTypes.oneOf(['blue', 'green', 'red']), children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]), }; render() { const {title, color = 'blue', children = null} = this.props; return ( <>
{title}
{children}
); } } const OverridableExampleComponent = Overridable.component('ExampleComponent', ExampleComponent); const assertTitleStyle = (element, title, style) => { expect( element.filterWhere(el => { const styleAttr = el.prop('style'); // simple obj comparison for equality const sameStyle = JSON.stringify(styleAttr) === JSON.stringify(style); return el.prop('children') === title && sameStyle; }) ).toHaveLength(1); }; describe('Tests for default cmp behaviour', () => { test('it should render the default cmp with default props and passed children', () => { const Cmp = mount(

First TODO Second TODO

); assertTitleStyle(Cmp.find('div'), 'TODOs', {color: 'blue'}); const children = Cmp.find('p').children(); expect(children).toHaveLength(2); expect(children.get(0)).toEqual(First TODO); expect(children.get(1)).toEqual(Second TODO); }); test('it should render the default component with passed props and no children', () => { const Cmp = mount(); assertTitleStyle(Cmp.find('div'), 'TODOs', {color: 'green'}); const children = Cmp.find('p').children(); expect(children).toHaveLength(0); }); }); describe('Tests for parametrized', () => { /** * With using parametrize, the final component will be rendered: * * * * */ test('it should render the cmp with id `ExampleComponent` with new props', () => { const parametrized = parametrize(OverridableExampleComponent, { title: 'New title', color: 'red', }); const overriddenCmps = {ExampleComponent: parametrized}; const mounted = mount(

This should be rendered

); const ParametrizedCmp = mounted.childAt(0); expect(ParametrizedCmp.name()).toEqual('Parametrized(Overridable(ExampleComponent))'); const ExampleCmp = ParametrizedCmp.childAt(0); expect(ExampleCmp.prop('title')).toEqual('New title'); expect(ExampleCmp.prop('color')).toEqual('red'); assertTitleStyle(ExampleCmp.find('div'), 'New title', {color: 'red'}); const children = ExampleCmp.find('p').children(); expect(children).toHaveLength(1); }); test('it should render the cmp with id `ExampleComponent` with new props passed as function', () => { const parametrized = parametrize(OverridableExampleComponent, ({title, ...props}) => ({ title: `Other ${title}`, ...props, })); const overriddenCmps = {ExampleComponent: parametrized}; const mounted = mount(

This should be rendered

); const ExampleCmp = mounted.childAt(0).childAt(0); assertTitleStyle(ExampleCmp.find('div'), 'Other TODOs', {color: 'blue'}); const children = ExampleCmp.find('p').children(); expect(children).toHaveLength(1); }); test('it should recalculate function props when the passed props change', () => { const ParametrizedComponent = parametrize(ExampleComponent, ({title}) => ({ title: `Test Title ${title}`, })); const mounted = mount(); expect(mounted.find(ExampleComponent).prop('title')).toEqual('Test Title First'); mounted.setProps({title: 'Second'}); expect(mounted.find(ExampleComponent).prop('title')).toEqual('Test Title Second'); }); }); describe('Tests for Overridable render elements', () => { /** * When overriding render elements with Overridable, the final component will be rendered: * * * * * */ test('it should render the cmp with id `ExampleComponent.container` with the overridden render elements', () => { // eslint-disable-next-line react/prop-types const overriddenRenderElement = ({cmpTitle, cmpColor}) => (

{cmpTitle}

); const overriddenCmps = {'ExampleComponent.container': overriddenRenderElement}; const mounted = mount(
  • It should NOT appear!
); const ExampleCmp = mounted.childAt(0); expect(ExampleCmp.prop('title')).toEqual('My TODOs'); expect(ExampleCmp.prop('color')).toEqual('red'); assertTitleStyle(ExampleCmp.find('h1'), 'My TODOs', {cmpColor: 'red'}); expect(ExampleCmp.find('ul')).toHaveLength(0); }); test('it should render the overridden cmp when the original cmp has no children', () => { const NoChildrenComponent = () => ; const overriddenRenderElement = () =>

A title

; const overriddenCmps = {'NoChildrenComponent.container': overriddenRenderElement}; const mounted = mount( ); const h4 = mounted.childAt(0).find('h4'); expect(h4.prop('children')).toEqual('A title'); }); test('it should throw when the overridden cmp with id `ExampleComponent` contains multiple children', () => { const WrongComponent = () => (
Title
Content
); expect(() => mount(WrongComponent)).toThrow(); }); }); describe('Tests for Overridable.component', () => { /** * When overriding template with Overridable, the final component will be rendered: * * * */ test('it should render the passed cmp instead of default cmp with id `ExampleComponent`', () => { const NewComponent = () =>

Hardcoded TODOs title

; const overriddenCmps = {ExampleComponent: NewComponent}; const mounted = mount(
  • It should NOT appear!
); const NewCmp = mounted.childAt(0); expect(NewCmp.prop('title')).toEqual('Ignored title'); expect(NewCmp.prop('color')).toEqual('red'); const h3 = NewCmp.find('h3'); expect(h3).toHaveLength(1); expect(h3.prop('children')).toEqual('Hardcoded TODOs title'); expect(NewCmp.find('ul')).toHaveLength(0); }); }); describe('Tests for ref forwarding', () => { class RefChild extends Component { render() { return
; } } test('it should forward a ref to the cloned child', () => { const ref = React.createRef(); mount( ); expect(ref.current).toBeInstanceOf(RefChild); }); test('it should forward a ref to the overridden component', () => { const ref = React.createRef(); mount(
); expect(ref.current).toBeInstanceOf(RefChild); }); test('it should render normally when no ref is given', () => { const mounted = mount( ); expect(mounted.find('.ref-child')).toHaveLength(1); }); });