-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MNG-15 Add API urls, start tests and function for pokemons.js #10
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
691f77c
MNG-15 Add API urls, start tests and function for pokemons.js
daria305 ee8c0cd
Change url end
daria305 59c202b
Make function async
daria305 d35c8d3
Correct url end
daria305 aeba555
MNG-15 Recreate test function for getPokemonById to follow the rule g…
daria305 9aaecc9
MNG-15 Write first version of getPokemonById
daria305 cc73d6e
MNG-15 Fix test async function handling with beforeAll
daria305 9f2621d
MNG-15 Add test for get types by id
daria305 4a533ea
MNG-15 Create getTypeById function
daria305 53f24f5
Refactoring tests and getPokemonById to be more readable.
daria305 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
POKEMON_API_BASE_URL = | ||
POKEMON_API_BASE_URL = "https://pokeapi.co/api/v2" | ||
QUIZ_MAX_TIME_SECONDS = 120 |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
getPokemonById, | ||
getTypeById | ||
} from "./pokemons.js"; | ||
|
||
describe('Test pokemon API to get pokemon', () => { | ||
|
||
it("Given pokemon id is 1 when asking for data, should get id, photoUrl, types, name of the pokemon", async () => { | ||
//given | ||
const pokemonId = 1; | ||
|
||
//when | ||
const pokeData = await getPokemonById(pokemonId) | ||
|
||
//then | ||
expect(pokeData).toEqual({ | ||
id: 1, | ||
name: "bulbasaur", | ||
photoUrl: "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/1.png", | ||
types: [{ | ||
id: 12, | ||
type: "grass" | ||
}, | ||
{ | ||
id: 4, | ||
type: "poison" | ||
} | ||
] | ||
}); | ||
}) | ||
}); | ||
|
||
|
||
describe("Test pokemon API to get pokemon types", () => { | ||
|
||
it("Given the type id is 12 when asking for pokemon data, should return id and name of the type", async () => { | ||
//given | ||
const typeId = 12; | ||
|
||
//when | ||
const typeData = await getTypeById(typeId) | ||
|
||
//then | ||
expect(typeData).toEqual({ | ||
id: 12, | ||
name: "grass" | ||
} | ||
); | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import fetch from "cross-fetch" | ||
|
||
const POKEMON_API_BASE_URL = process.env.POKEMON_API_BASE_URL || "https://pokeapi.co/api/v2"; | ||
|
||
export async function getPokemonById(id) { | ||
const getTypeIdFromUrl = (url) => { | ||
const regex = /\/type\/(\d+)\/$/; | ||
return Number(regex.exec(url)[1]) | ||
}; | ||
|
||
const parseType = (type) => { | ||
return { | ||
id: getTypeIdFromUrl(type.type.url), | ||
type: type.type.name | ||
} | ||
}; | ||
|
||
const res = await fetch(`${POKEMON_API_BASE_URL}/pokemon/${id}`); | ||
const jsonRes = await res.json(); | ||
|
||
return { | ||
id: jsonRes.id, | ||
name: jsonRes.name, | ||
types: jsonRes.types.map(parseType), | ||
photoUrl: jsonRes.sprites.other["official-artwork"].front_default | ||
} | ||
}; | ||
|
||
export async function getTypeById(id) { | ||
const res = await fetch(`${POKEMON_API_BASE_URL}/type/${id}`); | ||
const jsonRes = await res.json(); | ||
const { | ||
id: typeId, | ||
name: typeName | ||
} = jsonRes; | ||
return { | ||
id: typeId, | ||
name: typeName | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't aware of this complexity in code, but I am glad you managed to overcome it, cool!
However you should know that extracting this typeId from url sounds like a bad code example for me. We shouldn't do things like that, but I am glad it works and it even saves two requests to pokeapi, which is a good thing :)
But! There is no need to change that.