-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathPokemonPage.jest.spec.jsx
139 lines (122 loc) · 3.05 KB
/
PokemonPage.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import React from 'react'
import { render, screen } from '@testing-library/react'
import { Router } from 'react-router-dom'
import { createMemoryHistory } from 'history'
import axiosMock from 'axios'
import { act } from 'react-dom/test-utils'
import '@testing-library/jest-dom/extend-expect'
import PokemonPage from '../src/PokemonPage'
jest.mock('axios')
const previous = {
url: 'https://pokeapi.co/api/v2/pokemon/132/',
name: 'ditto',
id: 132
}
const next = {
url: 'https://pokeapi.co/api/v2/pokemon/134/',
name: 'vaporeon',
id: 134
}
const pokemonList = {
id: 133,
abilities: [
{
ability: {
name: 'anticipation',
url: 'https://pokeapi.co/api/v2/ability/107/'
},
is_hidden: true,
slot: 3
},
{
ability: {
name: 'adaptability',
url: 'https://pokeapi.co/api/v2/ability/91/'
},
is_hidden: false,
slot: 2
}
],
name: 'eevee',
stats: [
{
base_stat: 55,
effort: 0,
stat: {
name: 'attack',
url: 'https://pokeapi.co/api/v2/stat/2/'
}
},
{
base_stat: 55,
effort: 0,
stat: {
name: 'hp',
url: 'https://pokeapi.co/api/v2/stat/1/'
}
}
],
types: [
{
slot: 1,
type: {
name: 'normal',
url: 'https://pokeapi.co/api/v2/type/1/'
}
}
],
sprites: { front_default: 'URL' }
}
const history = createMemoryHistory()
describe('<PokemonPage />', () => {
beforeEach(() => {
history.push('/pokemon/eevee')
})
it('should render abilities', async () => {
axiosMock.get.mockResolvedValueOnce({ data: pokemonList })
await act(async () => {
render(
<Router history={history}>
<PokemonPage />
</Router>
)
})
expect(screen.getByText('adaptability')).toBeVisible()
expect(screen.getByText('anticipation')).toBeVisible()
})
it('should render stats', async () => {
axiosMock.get.mockResolvedValueOnce({ data: pokemonList })
await act(async () => {
render(
<Router history={history}>
<PokemonPage />
</Router>
)
})
expect(screen.getByTestId('stats')).toHaveTextContent('hp55attack55')
})
it('should render previous and next urls if they exist', async () => {
axiosMock.get.mockResolvedValueOnce({ data: pokemonList })
await act(async () => {
render(
<Router history={history}>
<PokemonPage previous={previous} next={next} />
</Router>
)
})
expect(screen.getByText('Previous')).toHaveAttribute('href', '/pokemon/ditto')
expect(screen.getByText('Next')).toHaveAttribute('href', '/pokemon/vaporeon')
})
it('should not render previous and next urls if none exist', async () => {
axiosMock.get.mockResolvedValueOnce({ data: pokemonList })
await act(async () => {
render(
<Router history={history}>
<PokemonPage />
</Router>
)
})
expect(screen.queryByText('Previous')).toBeNull()
expect(screen.queryByText('Next')).toBeNull()
})
})