Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

reactjs - Unit Testing with Mocha,Enzyme dispatched functions in functional components which uses React Hooks + Redux

I am trying to test a dispatch from 'mapDispatchToProps' defined with a functional component which uses useEffect() hook and the function is called there.

export const MyComponent = (props) => {
    useEffect(() => {
        // Anything in here is fired on component mount.
        props.registerInStore(props.id, false);
        return () => {
            // Anything in here is fired on component unmount.
            props.resetInStore(props.id);
        };
    }, []);

    const handleOnClick = () => {
        props.toggle(props.id);
    };

    return (
        <div >
            {!props.isOpen ? (
                <button
                    onClick={handleOnClick}>
                   Open
                </button>
            ) : (
                <button
                    onClick={handleOnClick}>
                   close
                </button>
            )}
        </div>
    );
};


const mapDispatchToProps = (dispatch) => ({
    registerInStore(id, isOpen) {
        dispatch(registerInStore(id, isOpen));
    },
    resetInStore(id) {
        dispatch(resetInStore(id));
    }
});

export default connect(null, mapDispatchToProps)(MyComponent);

In my unit tests with Mocha and enzyme i also want to test the dispatches inside 'mapDispatchToProps', what i did below does not seem to work :

describe('<MyComponent/>', () => {
    let store = mockStore({
                toggles: [
                {
                    id: 10,
                    isOpen: true
                }
            ]
        }
    });
    const options = {
        context: {store},
        childContextTypes: {store: PropTypes.object.isRequired},
        lifecycleExperimental: true
    };
    const setup = (inputProps = {}) => {
        const props = {
            id: 10,
            isOpen: false,
            registerInStore: expect.createSpy(),
            resetInStore: expect.createSpy(),
            toggle: expect.createSpy(),
            ...inputProps
        };
        const wrapper = mount(<MyComponent {...props} />, options);
        return {
            props,
            wrapper
        };
    };

    afterEach(() => {
        expect.restoreSpies();
    });

       it('should dispatch', async () => {
            const {wrapper}=setup();
            await store.dispatch(wrapper.prop('registerInStore')(10,false));

            /* i tried the commented way too instead of directly dispatching*/
            // wrapper.prop('registerInStore')(10,false);
            //await new Promise((resolve) => setTimeout(resolve, 50));

            const expectedActions = [{type: 'REGISTER_IN_STORE', id: 10, isOpen: false}];
            expect(store.getActions()).toEqual(expectedActions);
           
        });

the store.getActions() is returning an empty array, i am new to React Hooks and testing, what am i doing wrong, any other solutions?. Thanks in Advance.

question from:https://stackoverflow.com/questions/66066633/unit-testing-with-mocha-enzyme-dispatched-functions-in-functional-components-whi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

worked by removing the spies e.g:-

  const setup = (inputProps = {}) => {
        const props = {
            id: 10,
            isOpen: false,
            registerInStore:()=>null,
            resetInStore: ()=>null,
            toggle: ()=>null,
            ...inputProps
        };
        const wrapper = mount(<MyComponent {...props} />, options);
        return {
            props,
            wrapper
        };
    };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...