-
Notifications
You must be signed in to change notification settings - Fork 1.3k
using recompose with enzyme #407
Comments
Yes I know. Please read about what is |
use |
@Isaddo When I change |
Just think about this const C = ({ propA, propB }) => <div>{propA + propB}</div>
const B = ({ propA }) => <C propB={'propB'} propA={propA} />
const A = () => <B propA={'propA'} />
const wrapper = shallow(<A />) It is almost the same what you have with enhancers. Every enhancer here - HOC creates a new Component so at top level component you have props only from top enhancer |
To test all the props you need to use something like const ToolboxClass = toClass(Toolbox) // otherwise won't work with stateless
const Blabla = compose(
withState('labelText', 'setLabelText', ''),
withState('hover', 'hovering', false),
withState('labelLeft', 'setLabelLeftPosition', 0)
)(ToolboxClass)
const wrapper = mount(<Blabla />)
console.log(wrapper.find(ToolboxClass).props()) |
I did it! Thank you so much @istarkov |
@istarkov Have anyway to access Base Component if I import a Composed Component? |
I always export base component, enhancer and enhanced component, |
Just for completeness this is what worked for me. // Toolbox.js
export const enhance = compose(
withState('labelText', 'setLabelText', ''),
withState('hover', 'hovering', false),
withState('labelLeft', 'setLabelLeftPosition', 0)
);
export Toolbox = /* ... */
export default enhance(Toolbox);
// Toolbox.test.js
import { Toolbox as ToolboxComponent, enhance } from './Toolbox';
const ToolboxClass = toClass(ToolboxComponent); // otherwise won't work with stateless
const Toolbox = enhance(ToolboxClass);
const wrapper = mount(<Toolbox />);
console.log(wrapper.find(ToolboxClass).props()); |
@marcusstenbeck this is really helpful. I 've searched a while to find this. I have one question if you don't mind. What if I want to pass some dummy props in my tests. For example let's say i have prop
Here without recompose i 'd use some dummy |
I prefer to test my smart recomposed component. Because often, there is logic there that needs testing. Exporting the dumb component and testing that seems like a shortcut to me. I use this So if you have a src file of: // ToolBox.js
const enhance = compose(
withState('labelText', 'setLabelText', 'my label'),
withState('hover', 'hovering', false),
withState('labelLeft', 'setLabelLeftPosition', 0),
);
export const ToolBox = enhance(
({ labelText, hover, labelLeft, ...etc }) =>
<div>{ labelText }{ hover }{ labelLeft }</div>
); You can have a test file of (I'm writing an example here, not sure if this is working, but this is what my tests basically look like): // ToolBox.spec.js
it('has the props', () => {
// shallow the entire component, recompose and everything.
// I do still use mount sometimes for testing lifecycles or other reasons, but rarely.
// I'm using the ::until syntax (it is stage-0) but you can also add to ShallowWrapper.prototype
// ::until('ToolBox') will continue to shallow render single children until it finds the matcher
const wrapper = shallow(<ToolBox />)::until('ToolBox');
expect(wrapper.text()).toEqual('my label false 0')
}); You have to mix that approach with solid HOC mocks for common HOCs like // __mocks__/react-redux.js
import React from 'react';
let state = { };
const setTestState = (newState) => { state = newState; };
const connect =
(mapStateToProps, mapDispatchToProps) => (BaseComponent) => {
return (props) => {
const mappedState = typeof mapStateToProps === 'function'
? mapStateToProps(state, props)
: {};
const mappedDispatchToProps = typeof mapDispatchToProps === 'function'
? mapDispatchToProps((action) => action, props)
: mapDispatchToProps;
return (
<BaseComponent
{...props}
{...mappedState}
{...mappedDispatchToProps}
/>
);
};
};
module.exports = {
setTestState,
connect
}; |
@timkindberg this looked promising, and I tried to use the module https://github.com/Stupidism/enzyme-shallow-until This is the enhancer I try to test:
And here is the test:
the props are always: Any ideas? UPDATE:I figured it out! so finally, my test looks like this:
Hope this comes in handy for whoever's struggling with these kind of tests :) |
My enhance it's look like this
When I use enyzme to test component and I console.log the props like this
It's show only first props
{ labelText: '', setLabelText: [Function] }
Did you know what is happening?
The text was updated successfully, but these errors were encountered: