【问题标题】:How to fix Error: useHref() may be used only in the context of a <Router> component如何修复错误:useHref() 只能在 <Router> 组件的上下文中使用
【发布时间】:2022-02-02 15:26:18
【问题描述】:

我该如何解决这个问题。我只是想创建一个测试以确保该组件呈现,但由于某种原因,即使该组件已经在 &lt;Router&gt; 中,也会出现此问题。

我在这里阅读了其他类似的问题,答案都说要把组件放在&lt;Router/&gt; 中,但这对我来说似乎不是问题。请告诉我我错过了什么?

** 我的 app.tsx**

import './App.scss';
import { AuthContexProvider } from './Utils/Contexs/AuthContext';
import { Routes, Route } from 'react-router-dom';
import { LogoLarge } from './Components/Logo/Logo';
import { SignInView } from './Views/SignInView';
import { ErrorPage } from './Views/ErrorView';
import { SignUpView } from './Views/SignUpView';
import { SwipeView } from './Views/SwipeView';
import { ResetPasswordView } from './Views/ResetPasswordView';
import { CreateProfileView } from './Views/CreateProfileView';
import { PrivateRoute } from './Utils/PrivateRoute';
import { ProfileView } from './Views/ProfileView';
import { SwipeContexProvider } from './Utils/Contexs/SwipeContex';
import { MatchesView } from './Views/MatchesView';
import { MessageView } from './Views/MessageView';

function App() {
  return (
    <div className='app'>
      <AuthContexProvider>
        <SwipeContexProvider>
          <header className='appWrapper'>
            <Routes>
              <Route path='/' element={<PrivateRoute component={SwipeView} />} />
              <Route
                path='/signin'
                element={
                  <div className='signInViewWrapper'>
                    <nav>
                      <LogoLarge />
                    </nav>
                    <SignInView />
                  </div>
                }
              />
              <Route
                path='/signup'
                element={
                  <div className='signUpViewWrapper'>
                    <nav>
                      <LogoLarge />
                    </nav>
                    <SignUpView />
                  </div>
                }
              />
              <Route
                path='/resetpassword'
                element={
                  <div className='resetPasswordViewWrapper'>
                    <nav>
                      <LogoLarge />
                    </nav>
                    <ResetPasswordView />
                  </div>
                }
              />
              <Route path='/createprofile' element={<PrivateRoute component={CreateProfileView} />} />
              <Route path='/profile' element={<PrivateRoute component={ProfileView} />} />
              <Route path='/matches' element={<PrivateRoute component={MatchesView} />} />
              <Route path='/matches/:id' element={<PrivateRoute component={MessageView} />} />
              <Route
                path='*'
                element={
                  <div className='appContent'>
                    <ErrorPage />
                  </div>
                }
              />
            </Routes>
          </header>
        </SwipeContexProvider>
      </AuthContexProvider>
    </div>
  );
}

export default App;

注册视图

import { Typography } from '@mui/material';
import { Link } from 'react-router-dom';
import { SignUpForm } from '../Components/Forms/SignUpForm';

//SASS
// import '../Styles/Scss/SignUpView.scss';
import { StyledFormsWrapper } from '../Styles/StyledComponents/StyledFormsWrapper';

export const SignUpView = () => {
  return (
    <StyledFormsWrapper data-testid='todo-1' className='signUpWrapper'>
      <Typography className='actionTitle' variant='h5' gutterBottom component='div' sx={{ fontWeight: 600 }}>
        Create account
      </Typography>
      <Typography variant='subtitle1' gutterBottom component='div'>
        Already have an account? <Link to={'/signin'}>Sign in</Link>
      </Typography>
      <SignUpForm />
    </StyledFormsWrapper>
  );
};

SignUpView.test.js

import React from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import { SignUpView } from '../../Views/SignUpView';

describe('my function or component', () => {
  test('Should render sigUp view component', () => {
    render(<SignUpView />);
  });
});

【问题讨论】:

    标签: reactjs testing jestjs react-router-dom react-testing-library


    【解决方案1】:

    对于测试,您可以提供容器包装器

    import { MemoryRouter } from 'react-router-dom';
    
    test('Should render sigUp view component', () => {
      render(<SignUpView />, {wrapper: MemoryRouter});
    });
    

    this link has some useful examples

    【讨论】:

      【解决方案2】:

      SignUpView 在您的测试中缺少路由上下文。导入内存路由器并包装被测组件,使其具有提供的路由上下文。

      import React from 'react';
      import { render, fireEvent, waitFor, screen } from '@testing-library/react';
      import { MemoryRouter as Router } from 'react-router-dom';
      import { SignUpView } from '../../Views/SignUpView';
      
      describe('my function or component', () => {
        test('Should render sigUp view component', () => {
          render(
            <Router>
              <SignUpView />
            </Router>
          );
        });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-31
        • 2021-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多