Skip to content

Commit

Permalink
Fixed up testing doc from #6903
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Sep 19, 2019
1 parent d6224d6 commit a5dd83f
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions packages/react-router/docs/guides/testing.md
Expand Up @@ -113,23 +113,23 @@ it("navigates home when you click the logo", async => {
// would take care of setting up the DOM elements
const root = document.createElement('div');
document.body.appendChild(root);

// Render app
render(
<MemoryRouter initialEntries={['/my/initial/route']}>
<App />
<MemoryRouter>,
root
);

// Interact with page
act(() => {
// Find the link (perhaps using the text content)
const goHomeLink = document.querySelector('#nav-logo-home');
// Click it
goHomeLink.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});

// Check correct page content showed up
expect(document.body.textContent).toBe('Home');
});
Expand All @@ -141,17 +141,16 @@ You shouldn't have to access the `location` or `history` objects very often in t

```diff
// app.test.js

test('clicking filter links updates product query params', () => {
+ let history, location;
let history, location;
render(
<MemoryRouter initialEntries={['/my/initial/route']}>
<App />
+ <Route path="*" render={({ location, location }) => {
+ history = history;
+ location = location;
+ return null;
+ }} />
<Route path="*" render={({ location, location }) => {
history = history;
location = location;
return null;
}} />
</MemoryRouter>,
node
);
Expand All @@ -160,12 +159,12 @@ test('clicking filter links updates product query params', () => {
// example: click a <Link> to /products?id=1234
});

+ // assert about url
+ expect(location.pathname).toBe('/products');
+ const searchParams = new URLSearchParams(location.search);
+ expect(searchParams.has('id')).toBe(true);
+ expect(searchParams.get('id')).toEqual('1234');
})
// assert about url
expect(location.pathname).toBe('/products');
const searchParams = new URLSearchParams(location.search);
expect(searchParams.has('id')).toBe(true);
expect(searchParams.get('id')).toEqual('1234');
});
```

### Alternatives
Expand All @@ -175,19 +174,19 @@ test('clicking filter links updates product query params', () => {

```js
// app.test.js
import { createMemoryHistory } from 'history';
import { Router } from 'react-router';
import { createMemoryHistory } from "history";
import { Router } from "react-router";

test('redirects to login page', () => {
test("redirects to login page", () => {
const history = createMemoryHistory();
render(
<Router history={history}>
<App signedInUser={null} />
</Router>,
node
);
expect(history.location.pathname).toBe('/login');
})
expect(history.location.pathname).toBe("/login");
});
```

## React Testing Library
Expand Down

0 comments on commit a5dd83f

Please sign in to comment.