gpt4 book ai didi

reactjs - 当我尝试测试 react-bootstrap 组件时,出现 TypeError: Enzyme::Selector 需要字符串、对象或组件构造函数

转载 作者:行者123 更新时间:2023-12-04 11:31:00 26 4
gpt4 key购买 nike

我正进入(状态

TypeError: Enzyme::Selector expects a string, object, or Component Constructor when I try to test a component



我尝试使用 ReactWrapper ,使用 div 的类并创建一个 id。没有任何效果。
import React from "react";
import Dashboard from "../Containers/Dashboard/Dashboard";
import { BrowserRouter as Router } from 'react-router-dom'
import { configure, shallow, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import Container from 'react-bootstrap';
let container = null;
configure({adapter: new Adapter()});

describe('<Dashboard />', () => {
let wrapper;

beforeEach(() => {
const fakeTopic = [
{
"name": "javascript",
"display_name": "JavaScript",
"short_description": "JavaScript (JS) is a lightweight interpreted programming language with first-class functions.",
"description": "JavaScript (JS) is a lightweight interpreted or JIT-compiled programming language with first-class functions. While it is most well-known as the scripting language for Web pages, many non-browser environments also use it, such as Node.js, Apache CouchDB and Adobe Acrobat. JavaScript is a prototype-based, multi-paradigm, dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles.",
"created_by": "Brendan Eich",
"released": "December 4, 1995",
"created_at": "2016-11-28T18:41:00Z",
"updated_at": "2019-11-06T15:05:24Z",
"featured": true,
"curated": true,
"score": 7954.724
}
]
wrapper = shallow(<Router><Dashboard jsTrendingTopics={fakeTopic} ><Container></Container></Dashboard></Router>);
});

it('should render <Container /> when recieving topics', () => {

expect(wrapper.find(Container)).toBeTruthy();
});
});

这是我要测试的组件:
    return (
<Container id="container1">
{props.jsTrendingTopics.length > 0 &&
<CardDeck>
<Row>
{props.jsTrendingTopics.map((jsTrendingTopic, index) =>
<Col md="auto" className="margin-top margin-left" key={index}>
<Card style={{ width: '18rem' }}>
<Card.Img variant="top" src={require(`../../assets/${jsTrendingTopic.name}.png`)} />
<Card.Body>
<Card.Title>{jsTrendingTopic.display_name}</Card.Title>
<Link to={`/language/${jsTrendingTopic.name}`}>
<Button variant="primary">Details</Button>
</Link>
</Card.Body>
</Card>
</Col>
)}
</Row>
</CardDeck>
}

</Container>
)
}

我希望找到该组件或只是看看它是否存在。至少有些东西。

最佳答案

您的导入可能不正确,请参阅 index.tsx .尝试修改如下:

import { Container } from 'react-bootstrap';
当您遇到此类错误(或 similarly )时,
您可以使用 wrapper.debug() 进行调查:
console.log({ Container })  // Would have printed "{ Container: undefined }"
console.log(wrapper.debug()) // Also very useful

关于reactjs - 当我尝试测试 react-bootstrap 组件时,出现 TypeError: Enzyme::Selector 需要字符串、对象或组件构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58745844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com