From 91ead7f64f2507eb63afcd7ca94a2b8407203848 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Wed, 12 Apr 2023 23:54:12 +1000 Subject: [PATCH] Modify pokedex.test.ts, clear up redundant code --- src/features/Pokedex/__test__/pokedex.test.ts | 126 +++++++++--------- .../Pokedex/__test__/pokedexApi.test.ts | 2 +- 2 files changed, 65 insertions(+), 63 deletions(-) diff --git a/src/features/Pokedex/__test__/pokedex.test.ts b/src/features/Pokedex/__test__/pokedex.test.ts index a6c6cf6..459ded5 100644 --- a/src/features/Pokedex/__test__/pokedex.test.ts +++ b/src/features/Pokedex/__test__/pokedex.test.ts @@ -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]); - }); }); diff --git a/src/features/Pokedex/__test__/pokedexApi.test.ts b/src/features/Pokedex/__test__/pokedexApi.test.ts index 1877652..7df8755 100644 --- a/src/features/Pokedex/__test__/pokedexApi.test.ts +++ b/src/features/Pokedex/__test__/pokedexApi.test.ts @@ -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', () => {