How to test a react component which uses react-spring?

回眸只為那壹抹淺笑 提交于 2019-12-11 15:28:12

问题


I'm trying to test my SpringComponent. This component uses react-spring for animations. When I run my test I get the following error:

"Jest encountered an unexpected token".

If I comment my Spring code the error disappears and my test passes.

This is the testing code:

import React from "react";
import SpringComponent from "../components/SpringComponent";


test('renders without crashing', () => {
    expect(true).toBeTruthy();
});

And the SpringComponent code:

import React from "react";
import {Spring} from "react-spring/renderprops-universal";

class SpringComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            content: this.props.content,
        }
    }

    componentDidUpdate(prevProps) {
    }

    render() {
        let content = this.state.content;
        console.log("content: " + content);
        return <div>
            <Spring
                from={{opacity: 0}}
                to={{opacity: 1}}
            >
                {props => <div style={props}>
                    {content}
                </div>}
            </Spring>
        </div>
    }

}

export default SpringComponent;

I wonder what I must do to test this code. Should I mock something?

来源:https://stackoverflow.com/questions/56220746/how-to-test-a-react-component-which-uses-react-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!