forked from smartlyio/full-stack-open-pokedex
-
Notifications
You must be signed in to change notification settings - Fork 0
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
11 changed files
with
272 additions
and
218 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,7 +1,7 @@ | ||
import React from 'react' | ||
import React from "react"; | ||
|
||
const ErrorMessage = ({ error }) => ( | ||
<div data-testid="error">An error occured: {error.toString()}</div> | ||
) | ||
); | ||
|
||
export default ErrorMessage | ||
export default ErrorMessage; |
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,7 +1,11 @@ | ||
import React from 'react' | ||
import React from "react"; | ||
|
||
const LoadingSpinner = () => ( | ||
<img className="loading-spinner" alt="Loading..." src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/items/poke-ball.png" /> | ||
) | ||
<img | ||
className="loading-spinner" | ||
alt="Loading..." | ||
src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/items/poke-ball.png" | ||
/> | ||
); | ||
|
||
export default LoadingSpinner | ||
export default LoadingSpinner; |
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,12 +1,10 @@ | ||
import React from 'react' | ||
import React from "react"; | ||
|
||
const PokemonAbility = ({ abilityName }) => ( | ||
<div className="pokemon-ability"> | ||
<div className="pokemon-ability-type">Hidden ability</div> | ||
<div className="pokemon-ability-name"> | ||
{abilityName} | ||
</div> | ||
<div className="pokemon-ability-name">{abilityName}</div> | ||
</div> | ||
) | ||
); | ||
|
||
export default PokemonAbility | ||
export default PokemonAbility; |
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,20 +1,23 @@ | ||
import React from 'react' | ||
import { Link } from 'react-router-dom' | ||
import React from "react"; | ||
import { Link } from "react-router-dom"; | ||
|
||
const PokemonList = ({ pokemonList }) => { | ||
return ( | ||
<div className="list-container"> | ||
{pokemonList.map(({ id, name }) => ( | ||
<Link key={id} to={`/pokemon/${name}`} className="list-item" style={{ backgroundImage: `url(${`https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${id}.png`})` }}> | ||
<div | ||
className="list-item-name" | ||
> | ||
{name} | ||
</div> | ||
<Link | ||
key={id} | ||
to={`/pokemon/${name}`} | ||
className="list-item" | ||
style={{ | ||
backgroundImage: `url(${`https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${id}.png`})`, | ||
}} | ||
> | ||
<div className="list-item-name">{name}</div> | ||
</Link> | ||
))} | ||
</div> | ||
) | ||
} | ||
); | ||
}; | ||
|
||
export default PokemonList | ||
export default PokemonList; |
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,6 +1,6 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import App from './App' | ||
import './styles.css' | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import App from "./App"; | ||
import "./styles.css"; | ||
|
||
ReactDOM.render(<App />, document.getElementById('app')) | ||
ReactDOM.render(<App />, document.getElementById("app")); |
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,20 +1,20 @@ | ||
import { useEffect, useState } from 'react' | ||
import axios from 'axios' | ||
import { useEffect, useState } from "react"; | ||
import axios from "axios"; | ||
|
||
const useApi = (url, mapResults = (result) => result) => { | ||
const [data, setData] = useState() | ||
const [isLoading, setIsLoading] = useState(true) | ||
const [error, setError] = useState() | ||
const [data, setData] = useState(); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [error, setError] = useState(); | ||
useEffect(() => { | ||
setIsLoading(true) | ||
setIsLoading(true); | ||
axios | ||
.get(url) | ||
.then(response => setData(mapResults(response.data))) | ||
.then((response) => setData(mapResults(response.data))) | ||
.catch(setError) | ||
.finally(() => setIsLoading(false)) | ||
}, [url]) | ||
.finally(() => setIsLoading(false)); | ||
}, [url]); | ||
|
||
return { data, isLoading, error } | ||
} | ||
return { data, isLoading, error }; | ||
}; | ||
|
||
export { useApi } | ||
export { useApi }; |
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,41 +1,47 @@ | ||
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' | ||
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') | ||
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 }] | ||
} | ||
} | ||
) | ||
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') | ||
}) | ||
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({}) | ||
it("shows LoadingSpinner", async () => { | ||
axiosMock.get.mockResolvedValueOnce({}); | ||
await act(async () => { | ||
const { getByAltText } = render(<App />) | ||
expect(getByAltText('Loading...')).toBeVisible() | ||
}) | ||
}) | ||
const { getByAltText } = render(<App />); | ||
expect(getByAltText("Loading...")).toBeVisible(); | ||
}); | ||
}); | ||
|
||
it('shows error', async () => { | ||
axiosMock.get.mockRejectedValueOnce(new Error()) | ||
it("shows error", async () => { | ||
axiosMock.get.mockRejectedValueOnce(new Error()); | ||
await act(async () => { | ||
render(<App />) | ||
}) | ||
expect(screen.getByTestId('error')).toBeVisible() | ||
}) | ||
}) | ||
render(<App />); | ||
}); | ||
expect(screen.getByTestId("error")).toBeVisible(); | ||
}); | ||
}); |
Oops, something went wrong.