Modify pokedex.test.ts, clear up redundant code

develop
Jason Zhu 2023-04-12 23:54:12 +10:00
parent 8696392dce
commit 91ead7f64f
2 changed files with 65 additions and 63 deletions

View File

@ -5,78 +5,80 @@ import {
import { PokemonResponseData } from 'features/Pokedex/types/api';
import pokemon3_Venusaur from 'features/Pokedex/__test__/pokemon3_Venusaur.json';
import pokemon4_Charmander from 'features/Pokedex/__test__/pokemon4_charmander.json';
import { AppDispatch, AppStore } from 'app/store';
import { configureStore, Store } from '@reduxjs/toolkit';
import { AppStore } from 'app/store';
import { configureStore } from '@reduxjs/toolkit';
import { pokedexSlice } from 'features/Pokedex/pokedexSlice';
import { pokedexApi } from 'features/Pokedex/pokedexApi';
import { listenerMiddleware } from 'app/listenerMiddleware';
let store: AppStore;
let dispatch: AppDispatch;
describe('filterPokemonByType works correctly', () => {
beforeEach(() => {
store = configureStore({
reducer: {
pokedex: pokedexSlice.reducer,
[pokedexApi.reducerPath]: pokedexApi.reducer,
},
middleware: getDefaultMiddleware =>
getDefaultMiddleware().concat(
pokedexApi.middleware,
listenerMiddleware.middleware,
),
describe('pokedex Component', () => {
describe('filterPokemonByType works correctly', () => {
beforeEach(() => {
store = configureStore({
reducer: {
pokedex: pokedexSlice.reducer,
[pokedexApi.reducerPath]: pokedexApi.reducer,
},
middleware: getDefaultMiddleware =>
getDefaultMiddleware().concat(
pokedexApi.middleware,
listenerMiddleware.middleware,
),
});
});
const pokemonList: PokemonResponseData[] = [
pokemon3_Venusaur,
pokemon4_Charmander,
];
it('should return all Pokemon if the selected type is "All Types"', () => {
const selectedType = 'All Types';
const filteredList = filterPokemonByType(pokemonList, selectedType);
expect(filteredList).toEqual(pokemonList);
});
it('should return only Pokemon of the selected type', () => {
const selectedType = 'fire';
const filteredList = filterPokemonByType(pokemonList, selectedType);
const allPokemonAreOfTypeFire = filteredList.every(pokemon =>
pokemon.types.some(type => type.type.name === selectedType),
);
expect(allPokemonAreOfTypeFire).toBe(true);
});
});
const pokemonList: PokemonResponseData[] = [
pokemon3_Venusaur,
pokemon4_Charmander,
];
describe('sortPokemonsByIdOrName works correctly', () => {
beforeEach(() => {
store = configureStore({
reducer: {
pokedex: pokedexSlice.reducer,
[pokedexApi.reducerPath]: pokedexApi.reducer,
},
middleware: getDefaultMiddleware =>
getDefaultMiddleware().concat(
pokedexApi.middleware,
listenerMiddleware.middleware,
),
});
});
it('should return all Pokemon if the selected type is "All Types"', () => {
const selectedType = 'All Types';
const filteredList = filterPokemonByType(pokemonList, selectedType);
expect(filteredList).toEqual(pokemonList);
});
const pokemonList: PokemonResponseData[] = [
pokemon3_Venusaur,
pokemon4_Charmander,
];
it('should sort by id if the selected sort is "id"', () => {
const selectedSort = 'id';
const sortedList = sortPokemonsByIdOrName(pokemonList, selectedSort);
expect(sortedList).toEqual([pokemon3_Venusaur, pokemon4_Charmander]);
});
it('should return only Pokemon of the selected type', () => {
const selectedType = 'fire';
const filteredList = filterPokemonByType(pokemonList, selectedType);
const allPokemonAreOfTypeFire = filteredList.every(pokemon =>
pokemon.types.some(type => type.type.name === selectedType),
);
expect(allPokemonAreOfTypeFire).toBe(true);
});
});
describe('sortPokemonsByIdOrName works correctly', () => {
beforeEach(() => {
store = configureStore({
reducer: {
pokedex: pokedexSlice.reducer,
[pokedexApi.reducerPath]: pokedexApi.reducer,
},
middleware: getDefaultMiddleware =>
getDefaultMiddleware().concat(
pokedexApi.middleware,
listenerMiddleware.middleware,
),
it('should sort by name if the selected sort is "name"', () => {
const selectedSort = 'name';
const sortedList = sortPokemonsByIdOrName(pokemonList, selectedSort);
expect(sortedList).toEqual([pokemon4_Charmander, pokemon3_Venusaur]);
});
});
const pokemonList: PokemonResponseData[] = [
pokemon3_Venusaur,
pokemon4_Charmander,
];
it('should sort by id if the selected sort is "id"', () => {
const selectedSort = 'id';
const sortedList = sortPokemonsByIdOrName(pokemonList, selectedSort);
expect(sortedList).toEqual([pokemon3_Venusaur, pokemon4_Charmander]);
});
it('should sort by name if the selected sort is "name"', () => {
const selectedSort = 'name';
const sortedList = sortPokemonsByIdOrName(pokemonList, selectedSort);
expect(sortedList).toEqual([pokemon4_Charmander, pokemon3_Venusaur]);
});
});

View File

@ -5,7 +5,7 @@ import region1 from 'features/Pokedex/__test__/responses/region1.json';
import pokemon1 from 'features/Pokedex/__test__/responses/pokemon1.json';
import { RegionListResponseData, TypeListResponseData } from '../types/api';
import { AppStore } from 'app/store';
import { listenerMiddleware } from '../../../app/listenerMiddleware';
import { listenerMiddleware } from 'app/listenerMiddleware';
let store: AppStore;
describe('pokedexApi', () => {