-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,49 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { MemoryRouter } from 'react-router'; | ||
import fetchMock from 'jest-fetch-mock'; | ||
import App from './App'; | ||
import mockResponse from './__mocks__/subreddit-reactjs-response.json'; | ||
|
||
test('renders learn react link', () => { | ||
const { getByText } = render(<App />); | ||
const linkElement = getByText(/learn react/i); | ||
expect(linkElement).toBeInTheDocument(); | ||
fetchMock.enableMocks(); | ||
|
||
function setup() { | ||
return render( | ||
<MemoryRouter> | ||
<App /> | ||
</MemoryRouter> | ||
); | ||
} | ||
|
||
describe('Header', () => { | ||
test('"How it works" link points to the correct page', () => { | ||
setup(); | ||
|
||
const link = screen.getByRole('link', { name: /how it works/i }); | ||
userEvent.click(link); | ||
|
||
expect( | ||
screen.getByRole('heading', { name: /how it works/i }) | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
describe('Subreddit form', () => { | ||
test('loads posts and renders them on the page', async () => { | ||
fetch.once(JSON.stringify(mockResponse)); | ||
setup(); | ||
|
||
const subredditInput = screen.getByLabelText('r /'); | ||
userEvent.type(subredditInput, 'reactjs'); | ||
|
||
const submitButton = screen.getByRole('button', { name: /search/i }); | ||
userEvent.click(submitButton); | ||
|
||
const loadingMessage = screen.getByText(/is loading/i); | ||
expect(loadingMessage).toBeInTheDocument(); | ||
|
||
expect(await screen.findByText(/Number of top posts: 25/i)).toBeInTheDocument(); | ||
expect(fetch).toHaveBeenCalledWith('https://www.reddit.com/r/reactjs/top.json'); | ||
}); | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters