Cómo probar un cambio de URL con en React Router DOM.
Quiero probar, cuando se hace clic en el botón ‘Restablecer filtros’, ir a /offerings, pero no sé cómo probar este componente <Link />
de react-router-dom.
Botón
<Link to="/offerings">
<StyledButton trackingId="reset-filters">Restablecer filtros</StyledButton>
</Link>
Archivo de prueba:
it('debería ir a /offerings cuando se hace clic en el botón después de iniciar sesión', () => {
const wrapper = mountWithTheme(
<Provider
store={configureMockStore({
user: {
userState: {
loggedIn: true,
},
},
})}
>
<BrowserRouter>
<MarketplaceEmptyState resetFilter={resetFilter} />
</BrowserRouter>
</Provider>
);
wrapper.find('MarketplaceEmptyState').setState({
allStatusFetched: true,
});
expect(
wrapper
.find('[data-cy="empty-state-filter-copy"]')
.first()
.text()
).toEqual(`Por favor, restablezca los filtros.`);
wrapper.find('button[id="reset-filters"]').simulate('click');
const leftClick = {button: 0} << -----------------------------|
userEvent.click(screen.getByText(/ofertas/i), leftClick) << ------ Intenté esto pero no funciona
});
davy.ai
Para probar el componente
<link>
dereact-router-dom
, se puede usar el componenteMemoryRouter
dereact-router
en el archivo de prueba. Reemplace el componente<browserrouter>
por<memoryrouter initialentries="{['/']}" initialindex="{0}">
. Esto le permitirá simular clics en el componente<link>
y comprobar que se dirige a la URL correcta.Aquí hay un archivo de prueba actualizado con los cambios:
En la prueba, se simula un clic en el componente
<link>
utilizandouserEvent.click(screen.getByText(/offerings/i), leftClick)
, luego se verifica que la URL haya cambiado a/offerings
utilizandoexpect(wrapper.find('MemoryRouter').instance().history.location.pathname).toEqual('/offerings');
.