-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathApp.jest.spec.jsx
41 lines (37 loc) · 1.12 KB
/
App.jest.spec.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import React from 'react'
import { render, screen } from '@testing-library/react'
import axiosMock from 'axios'
import { act } from 'react-dom/test-utils'
import '@testing-library/jest-dom/extend-expect'
import App from '../src/App'
jest.mock('axios')
describe('<App />', () => {
it('fetches data', async () => {
axiosMock.get.mockResolvedValueOnce(
{
data: {
results: [{ url: 'https://pokeapi.co/api/v2/pokemon/1/', name: 'bulbasaur', id: 1 }]
}
}
)
await act(async () => {
render(<App />)
})
expect(axiosMock.get).toHaveBeenCalledTimes(1)
expect(axiosMock.get).toHaveBeenCalledWith('https://pokeapi.co/api/v2/pokemon/?limit=50')
})
it('shows LoadingSpinner', async () => {
axiosMock.get.mockResolvedValueOnce({})
await act(async () => {
const { getByAltText } = render(<App />)
expect(getByAltText('Loading...')).toBeVisible()
})
})
it('shows error', async () => {
axiosMock.get.mockRejectedValueOnce(new Error())
await act(async () => {
render(<App />)
})
expect(screen.getByTestId('error')).toBeVisible()
})
})