[英]react testing library and export const Component
I've pretty much built my entire app using this format to export my components我几乎使用这种格式构建了我的整个应用程序来导出我的组件
export const PageLayout = (props) => {
const isAuthenticated = useIsAuthenticated();
return <div>{isAuthenticated ? <Main /> : <Landing />}</div>;
};
but when testing with react test library, I have to change the format of my export or else the component won't render at all in test但是在使用 React 测试库进行测试时,我必须更改导出格式,否则该组件根本不会在测试中呈现
const PageLayout = (props) => {
const isAuthenticated = useIsAuthenticated();
return <div>{isAuthenticated ? <Main /> : <Landing />}</div>;
};
export default PageLayout
If I have to go with second export, then I'll have to change all of the imports in my project and I'd really prefer not to do that so is there a way to use react test library without changing my import / exports?如果我必须 go 进行第二次导出,那么我将不得不更改项目中的所有导入,我真的不想这样做,那么有没有一种方法可以在不更改导入/导出的情况下使用反应测试库?
example code snippet:示例代码片段:
describe("Login page", () => {
it("renders login button", () => {
render(<PageLayout />);
const buttonText = screen.getByText(/sign in/i, { exact: false });
expect(buttonText).toBeVisible();
});
I'm pretty sure you can do something like this:我很确定你可以这样做:
import { render, screen } from '@testing-library/react'; import {PageLayout} from './PageLayout'; test('renders learn react link', () => { render(<PageLayout />); const linkElement = screen.getByText(/learn react/i); expect(linkElement).toBeInTheDocument(); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.