Skip to content

Commit

Permalink
Check current URL thx to BrowserRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrotoff committed Mar 14, 2023
1 parent 5afdef3 commit 83d8464
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Heroes.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { act, render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react';
import { MemoryRouter } from 'react-router';
import { BrowserRouter } from 'react-router-dom';

import * as Marvel from './api/Marvel';
import { flushPromises } from './utils/flushPromises';
Expand Down Expand Up @@ -77,11 +78,12 @@ test('render "No results found :("', async () => {

test('click on Details link', async () => {
render(
<MemoryRouter>
<BrowserRouter>
<Router />
</MemoryRouter>
</BrowserRouter>
);

expect(window.location.href).toEqual('http://localhost/');
expect(document.title).toEqual('Page 0 - Marvel Heroes');
await screen.findByText('3-D Man');

Expand All @@ -90,6 +92,7 @@ test('click on Details link', async () => {

screen.getByText(pleaseWait);

expect(window.location.href).toEqual('http://localhost/heroes/1011334');
expect(document.title).toEqual('... - Marvel Heroes');
await waitFor(() => expect(document.title).toEqual('3-D Man - Marvel Heroes'));

Expand Down
10 changes: 8 additions & 2 deletions src/HeroesPagination.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { act, render, screen, waitForElementToBeRemoved } from '@testing-library/react';
import { MemoryRouter } from 'react-router';
import { BrowserRouter } from 'react-router-dom';

import * as Marvel from './api/Marvel';
import { Router } from './Router';
Expand Down Expand Up @@ -71,40 +72,45 @@ test('render given a page query param', async () => {

test('click on Previous & Next links', async () => {
render(
<MemoryRouter>
<BrowserRouter>
<Router />
</MemoryRouter>
</BrowserRouter>
);

const prevLink = () => screen.getByText<HTMLLinkElement>('‹ Previous');
const nextLink = screen.getByText<HTMLLinkElement>('Next ›');

expect(window.location.href).toEqual('http://localhost/');
expect(document.title).toEqual('Page 0 - Marvel Heroes');
await screen.findByText('3-D Man');
expect(prevLink().disabled).toEqual(true);

act(() => nextLink.click());
expect(window.location.href).toEqual('http://localhost/1');
expect(document.title).toEqual('Page 1 - Marvel Heroes');
screen.getByText(pleaseWait);
await screen.findByText('Anita Blake');
expect(screen.queryByText(pleaseWait)).toBeNull();
expect(prevLink().disabled).toBeUndefined();

act(() => nextLink.click());
expect(window.location.href).toEqual('http://localhost/2');
expect(document.title).toEqual('Page 2 - Marvel Heroes');
screen.getByText(pleaseWait);
await screen.findByText('Beast');
expect(screen.queryByText(pleaseWait)).toBeNull();
expect(prevLink().disabled).toBeUndefined();

act(() => prevLink().click());
expect(window.location.href).toEqual('http://localhost/1');
expect(document.title).toEqual('Page 1 - Marvel Heroes');
screen.getByText(pleaseWait);
await screen.findByText('Anita Blake');
expect(screen.queryByText(pleaseWait)).toBeNull();
expect(prevLink().disabled).toBeUndefined();

act(() => prevLink().click());
expect(window.location.href).toEqual('http://localhost/0');
expect(document.title).toEqual('Page 0 - Marvel Heroes');
screen.getByText(pleaseWait);
await screen.findByText('3-D Man');
Expand Down

0 comments on commit 83d8464

Please sign in to comment.