Skip to content

Commit

Permalink
[Tests] debug: add tests to cover memo components with/without defa…
Browse files Browse the repository at this point in the history
…ultProps

See #2471
  • Loading branch information
ljharb committed Nov 13, 2020
1 parent 141852c commit 503c417
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions packages/enzyme-test-suite/test/shared/methods/debug.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import inspect from 'object-inspect';

Expand Down Expand Up @@ -208,6 +209,93 @@ export default function describeDebug({
});
});
});

describe('defaultProps vs no defaultProps', () => {
function Child({ children }) {
return <main>{children}</main>;
}

function LazyC({ type }) {
return (
<Child>
<div>{type}</div>
</Child>
);
}

const ComponentWithDefaultProps = React.memo && Object.assign(
React.memo(LazyC),
{
defaultProps: {
type: 'block',
},
propTypes: {
type: PropTypes.oneOf(['block', 'inline']),
},
},
);

const ComponentWithoutDefaultProps = React.memo && Object.assign(
React.memo(LazyC),
{
propTypes: {
type: PropTypes.oneOf(['block', 'inline']),
},
},
);

[ComponentWithDefaultProps, ComponentWithoutDefaultProps].forEach((C) => {
const isWithout = C === ComponentWithoutDefaultProps;

// TODO: remove this ternary, pick either variant
// see https://github.com/enzymejs/enzyme/issues/2471 for details
const Name = isWithout ? 'Memo(LazyC)' : 'LazyC';

it(`produces the expected tree ${isWithout ? 'without' : 'with'} defaultProps, no prop provided`, () => {
const wrapper = Wrap(<C />);

expect(wrapper.debug()).to.equal(isShallow
? `<Child>
${isWithout
? '<div />'
: `<div>
block
</div>`}
</Child>`
: `<${Name}${isWithout ? '' : ' type="block"'}>
<Child>
<main>
${isWithout
? '<div />'
: `<div>
block
</div>`}
</main>
</Child>
</${Name}>`);
});

it(`produces the expected tree ${isWithout ? 'without' : 'with'} defaultProps, prop provided`, () => {
const wrapper = Wrap(<C type="inline" />);

expect(wrapper.debug()).to.equal(isShallow
? `<Child>
<div>
inline
</div>
</Child>`
: `<${Name} type="inline">
<Child>
<main>
<div>
inline
</div>
</main>
</Child>
</${Name}>`);
});
});
});
});
});
}

0 comments on commit 503c417

Please sign in to comment.