Implemented getPokemon, getPokemonSpecies, getEvolutionChain endpoints and related jest UTs
This commit is contained in:
parent
a0adfb0268
commit
08c8af5f4f
81
src/app/services/__test__/responses/evolution-chain_2.json
Normal file
81
src/app/services/__test__/responses/evolution-chain_2.json
Normal file
@ -0,0 +1,81 @@
|
||||
{
|
||||
"baby_trigger_item": null,
|
||||
"chain": {
|
||||
"evolution_details": [],
|
||||
"evolves_to": [
|
||||
{
|
||||
"evolution_details": [
|
||||
{
|
||||
"gender": null,
|
||||
"held_item": null,
|
||||
"item": null,
|
||||
"known_move": null,
|
||||
"known_move_type": null,
|
||||
"location": null,
|
||||
"min_affection": null,
|
||||
"min_beauty": null,
|
||||
"min_happiness": null,
|
||||
"min_level": 16,
|
||||
"needs_overworld_rain": false,
|
||||
"party_species": null,
|
||||
"party_type": null,
|
||||
"relative_physical_stats": null,
|
||||
"time_of_day": "",
|
||||
"trade_species": null,
|
||||
"trigger": {
|
||||
"name": "level-up",
|
||||
"url": "https://pokeapi.co/api/v2/evolution-trigger/1/"
|
||||
},
|
||||
"turn_upside_down": false
|
||||
}
|
||||
],
|
||||
"evolves_to": [
|
||||
{
|
||||
"evolution_details": [
|
||||
{
|
||||
"gender": null,
|
||||
"held_item": null,
|
||||
"item": null,
|
||||
"known_move": null,
|
||||
"known_move_type": null,
|
||||
"location": null,
|
||||
"min_affection": null,
|
||||
"min_beauty": null,
|
||||
"min_happiness": null,
|
||||
"min_level": 36,
|
||||
"needs_overworld_rain": false,
|
||||
"party_species": null,
|
||||
"party_type": null,
|
||||
"relative_physical_stats": null,
|
||||
"time_of_day": "",
|
||||
"trade_species": null,
|
||||
"trigger": {
|
||||
"name": "level-up",
|
||||
"url": "https://pokeapi.co/api/v2/evolution-trigger/1/"
|
||||
},
|
||||
"turn_upside_down": false
|
||||
}
|
||||
],
|
||||
"evolves_to": [],
|
||||
"is_baby": false,
|
||||
"species": {
|
||||
"name": "charizard",
|
||||
"url": "https://pokeapi.co/api/v2/pokemon-species/6/"
|
||||
}
|
||||
}
|
||||
],
|
||||
"is_baby": false,
|
||||
"species": {
|
||||
"name": "charmeleon",
|
||||
"url": "https://pokeapi.co/api/v2/pokemon-species/5/"
|
||||
}
|
||||
}
|
||||
],
|
||||
"is_baby": false,
|
||||
"species": {
|
||||
"name": "charmander",
|
||||
"url": "https://pokeapi.co/api/v2/pokemon-species/4/"
|
||||
}
|
||||
},
|
||||
"id": 2
|
||||
}
|
1337
src/app/services/__test__/responses/pokemon-species_6.json
Normal file
1337
src/app/services/__test__/responses/pokemon-species_6.json
Normal file
File diff suppressed because it is too large
Load Diff
9141
src/app/services/__test__/responses/pokemon_85.json
Normal file
9141
src/app/services/__test__/responses/pokemon_85.json
Normal file
File diff suppressed because it is too large
Load Diff
@ -4,11 +4,16 @@ import { filterSlice } from '../../features/Filters/filterSlice';
|
||||
import { configureStore } from '@reduxjs/toolkit';
|
||||
|
||||
import { AppStore } from 'app/store';
|
||||
import { TypeListResponseData } from 'types/api';
|
||||
import {
|
||||
EvolutionChainResponseData,
|
||||
PokemonResponseData,
|
||||
PokemonSpeciesResponseData,
|
||||
TypeListResponseData,
|
||||
} from 'types/api';
|
||||
|
||||
let store: AppStore;
|
||||
|
||||
describe('filterApi', () => {
|
||||
describe('pokeApi', () => {
|
||||
beforeEach(() => {
|
||||
store = configureStore({
|
||||
reducer: {
|
||||
@ -22,13 +27,64 @@ describe('filterApi', () => {
|
||||
});
|
||||
|
||||
describe('JEST test against mock API', () => {
|
||||
test('visit https://pokeapi.co/api/v2/type should return correct data in list', async () => {
|
||||
await store.dispatch(pokeApi.endpoints.getTypeList.initiate());
|
||||
describe('test getPokemon query', () => {
|
||||
test('visit https://pokeapi.co/api/v2/pokemon/85 returns Dodrio', async () => {
|
||||
await store.dispatch(pokeApi.endpoints.getPokemon.initiate(85));
|
||||
|
||||
const typeListData = pokeApi.endpoints.getTypeList.select()(
|
||||
store.getState(),
|
||||
).data as TypeListResponseData;
|
||||
expect(typeListData?.results).toHaveLength(typeListData.count + 1);
|
||||
const pokemon = pokeApi.endpoints.getPokemon.select(85)(
|
||||
store.getState(),
|
||||
).data as PokemonResponseData;
|
||||
expect(pokemon?.name).toBe('dodrio');
|
||||
expect(pokemon?.types).toHaveLength(2);
|
||||
expect(pokemon?.types[0].type.name).toBe('normal');
|
||||
expect(pokemon?.types[1].type.name).toBe('flying');
|
||||
expect(pokemon?.sprites.other.dream_world.front_default).toBe(
|
||||
'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/dream-world/85.svg',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('test getPokemonSpecies query', () => {
|
||||
test('visit https://pokeapi.co/api/v2/pokemon-species/6/', async () => {
|
||||
await store.dispatch(pokeApi.endpoints.getPokemonSpecies.initiate(6));
|
||||
|
||||
const pokemonSpecies = pokeApi.endpoints.getPokemonSpecies.select(6)(
|
||||
store.getState(),
|
||||
).data as PokemonSpeciesResponseData;
|
||||
expect(pokemonSpecies?.evolution_chain.url).toBe(
|
||||
'https://pokeapi.co/api/v2/evolution-chain/2/',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('test getTypeList query', () => {
|
||||
test('visit https://pokeapi.co/api/v2/type should return correct data in list', async () => {
|
||||
await store.dispatch(pokeApi.endpoints.getTypeList.initiate());
|
||||
|
||||
const typeListData = pokeApi.endpoints.getTypeList.select()(
|
||||
store.getState(),
|
||||
).data as TypeListResponseData;
|
||||
expect(typeListData?.results).toHaveLength(typeListData.count + 1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('test getEvolutionChain query', () => {
|
||||
test('visit https://pokeapi.co/api/v2/evolution-chain/2/', async () => {
|
||||
await store.dispatch(pokeApi.endpoints.getEvolutionChain.initiate(2));
|
||||
|
||||
const evolutionChainData = pokeApi.endpoints.getEvolutionChain.select(
|
||||
2,
|
||||
)(store.getState()).data as EvolutionChainResponseData;
|
||||
expect(evolutionChainData?.chain.species.url).toBe(
|
||||
'https://pokeapi.co/api/v2/pokemon-species/4/',
|
||||
);
|
||||
expect(evolutionChainData?.chain.evolves_to[0].species.url).toBe(
|
||||
'https://pokeapi.co/api/v2/pokemon-species/5/',
|
||||
);
|
||||
expect(
|
||||
evolutionChainData?.chain.evolves_to[0].evolves_to[0].species.url,
|
||||
).toBe('https://pokeapi.co/api/v2/pokemon-species/6/');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -3,7 +3,13 @@ import {
|
||||
FetchArgs,
|
||||
createApi,
|
||||
} from '@reduxjs/toolkit/query/react';
|
||||
import { RegionListResponseData, TypeListResponseData } from 'types/api';
|
||||
import {
|
||||
RegionListResponseData,
|
||||
TypeListResponseData,
|
||||
PokemonResponseData,
|
||||
EvolutionChainResponseData,
|
||||
PokemonSpeciesResponseData,
|
||||
} from 'types/api';
|
||||
|
||||
export interface pokeApiFullListFetchArgs extends FetchArgs {
|
||||
fetchAllPages?: boolean;
|
||||
@ -88,7 +94,24 @@ export const pokeApi = createApi({
|
||||
};
|
||||
},
|
||||
}),
|
||||
getPokemon: builder.query<PokemonResponseData, number | string>({
|
||||
query: IdOrName => ({ url: `pokemon/${IdOrName}` }),
|
||||
}),
|
||||
getPokemonSpecies: builder.query<
|
||||
PokemonSpeciesResponseData,
|
||||
number | string
|
||||
>({
|
||||
query: IdOrName => ({ url: `pokemon-species/${IdOrName}` }),
|
||||
}),
|
||||
getEvolutionChain: builder.query<EvolutionChainResponseData, number>({
|
||||
query: Id => ({ url: `evolution-chain/${Id}` }),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
|
||||
export const { useGetTypeListQuery } = pokeApi;
|
||||
export const {
|
||||
useGetTypeListQuery,
|
||||
useGetPokemonQuery,
|
||||
useGetPokemonSpeciesQuery,
|
||||
useGetEvolutionChainQuery,
|
||||
} = pokeApi;
|
||||
|
@ -6,6 +6,10 @@ import typeList from 'features/Filters/__test__/responses/typeList.json';
|
||||
import pokemonListPg1 from 'features/Pokedex/__test__/responses/pokemonListPage1.json';
|
||||
import pokemonListPg2 from 'features/Pokedex/__test__/responses/pokemonListPage2.json';
|
||||
|
||||
import dodrio from 'app/services/__test__/responses/pokemon_85.json';
|
||||
import pokemonSpecies6 from 'app/services/__test__/responses/pokemon-species_6.json';
|
||||
import evolutionChain2 from 'app/services/__test__/responses/evolution-chain_2.json';
|
||||
|
||||
export const handlers = [
|
||||
// mock https://pokeapi.co/api/v2/region/1
|
||||
rest.get('https://pokeapi.co/api/v2/region/999999', (req, res, ctx) => {
|
||||
@ -34,4 +38,19 @@ export const handlers = [
|
||||
return res(ctx.json(pokemonListPg2));
|
||||
}
|
||||
}),
|
||||
|
||||
// mock https://pokeapi.co/api/v2/pokemon/85
|
||||
rest.get('https://pokeapi.co/api/v2/pokemon/85', (req, res, ctx) => {
|
||||
return res(ctx.json(dodrio));
|
||||
}),
|
||||
|
||||
// mock https://pokeapi.co/api/v2/pokemon-species/6
|
||||
rest.get('https://pokeapi.co/api/v2/pokemon-species/6', (req, res, ctx) => {
|
||||
return res(ctx.json(pokemonSpecies6));
|
||||
}),
|
||||
|
||||
// mock https://pokeapi.co/api/v2/evolution-chain/2
|
||||
rest.get('https://pokeapi.co/api/v2/evolution-chain/2', (req, res, ctx) => {
|
||||
return res(ctx.json(evolutionChain2));
|
||||
}),
|
||||
];
|
||||
|
@ -60,3 +60,23 @@ export interface PokemonResponseData {
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export type PokemonSpeciesResponseData = {
|
||||
// many fields are ignored
|
||||
evolution_chain: {
|
||||
url: string;
|
||||
};
|
||||
};
|
||||
|
||||
type EvolutionChain = {
|
||||
evolves_to: EvolutionChain[];
|
||||
species: {
|
||||
name: string;
|
||||
url: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type EvolutionChainResponseData = {
|
||||
// many fields are ignored
|
||||
chain: EvolutionChain;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user