Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

420
Visualizações
How to test a react router link with testing library?

I want to test a react router link with testing library, I tried:

App.js:

import React from 'react'
import { Routes, NavLink, Route } from 'react-router-dom'

function App() {
  return (
    <div>
      <nav>
        <NavLink to={"/"}>Home</NavLink>
        <NavLink to={"/about"}>About</NavLink>
      </nav>
      <Routes>
        <Route path="/" element="home page" />
        <Route path="/about" element="about page" />
      </Routes>
    </div>
  )
}

export default App

App.test.js:

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { createMemoryHistory } from 'history';
import { Route, Router } from "react-router-dom"
import App from './App';

test('first home link', () => {
  const history = createMemoryHistory({ initialEntries: ["/"] });
  render(
    <Router location={history.location} navigator={history}>
      <App />
    </Router>
  );
  const linkElement = screen.getByRole("link", { name: /about/i });

  userEvent.click(linkElement);
  
  screen.debug();
});

Output of screen.debug:

    <body>
      <div>
        <div>
          <nav>
            <a
              aria-current="page"
              class="active"     
              href="/"
            >
              Home
            </a>
            <a
              class=""
              href="/about"
            >
              About
            </a>
          </nav>
          home page
        </div>
      </div>
    </body>

the page didn't change to about page like you see on the output the screen.debug

Is there is a way to fix that?

dependencies:

  1. "@testing-library/jest-dom": "^5.16.4"
  2. "@testing-library/react": "^13.3.0"
  3. "@testing-library/user-event": "^13.5.0"
  4. "history": "^5.3.0"
  5. "react": "^18.1.0"
  6. "react-router-dom": "^6.3.0"
  7. "react-dom": "^18.1.0"
about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda