错误:不变违反:元素类型在测试期间无效

时间:2019-09-19 13:52:35

标签: reactjs redux jestjs enzyme

文件:

//index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './containers/App/App.js';
import { Provider } from 'react-redux';
import { store } from './store';

ReactDOM.render(
    (
        <Provider store={store}>
            <App />
        </Provider>
    ),
    document.getElementById('root') || document.createElement("div")
);


//App.js

import React, { useEffect, useState } from "react";
import { getUser } from "../../firebase/user";
import Dashboard from "../Dashboard";
import SignIn from "../SignIn";
import { useSelector, useDispatch } from "react-redux";
import "../../styles/Global.scss";
import "../../styles/App.scss";
import "../../styles/Transitions.scss";
import "../../styles/Form.scss";
import "../../styles/Icons.scss";
import "../../styles/Cursors.scss";

const App = () => {

    const dispatch = useDispatch();

    const userId = useSelector(({ user: { userId } }) => userId);

    const [ content, setContent ] = useState("");

    useEffect(() => { dispatch(getUser()) }, []);

    useEffect(() => {
        if (userId) {
            setContent("dashboard");
        } else if (!userId && userId !== undefined) {
            setContent("sign-in");
        }
    }, [userId])

    console.log("Update App");

    const Content = () => {

        console.log("Update App Content")

        if (userId === undefined) {
            return null;
        }
        if (userId) {

            return <Dashboard />;
        } else {

            return <SignIn />;
        }
    }; 

    return (
        <div className={`content-wrapper ${content}`}>
            {Content()}
        </div>
    );
};

export default App;

//App.test.js

import React from "react";
import { shallow } from "enzyme";

import App from "./App";

describe('App', () => {
    it('should render correctly', () => {
        const Component = shallow(<App />);

        expect(Component).toMatchSnapshot();
    });
});

完整错误:

第一部分:

不变违规:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记了从定义文件中导出组件,或者可能混淆了默认导入和命名导入。

第二部分(?!):

警告:React.createElement:类型无效-预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记了从定义文件中导出组件,或者可能混淆了默认导入和命名导入。

在index.js:11上检查代码。

我知道在测试与笑话和酶以及Redux提供程序包装程序等的反应时,有很多有关此错误的问题。请阅读其中的很多内容,但不幸的是,这些都无济于事。有什么想法吗?

0 个答案:

没有答案