From db331acbb173b774a0c57d8a875bf7dd803aee1d Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Mon, 8 May 2023 19:59:04 +1000 Subject: [PATCH] Move Filter related tests in original pokedexApi.test.ts into filterApi.test.ts --- .../__test__/responses/typeList.json | 0 src/features/Filters/filterApi.test.ts | 38 ++++++ .../Pokedex/__test__/pokedexApi.test.ts | 122 ------------------ src/mocks/handlers.ts | 2 +- 4 files changed, 39 insertions(+), 123 deletions(-) rename src/features/{Pokedex => Filters}/__test__/responses/typeList.json (100%) create mode 100644 src/features/Filters/filterApi.test.ts delete mode 100644 src/features/Pokedex/__test__/pokedexApi.test.ts diff --git a/src/features/Pokedex/__test__/responses/typeList.json b/src/features/Filters/__test__/responses/typeList.json similarity index 100% rename from src/features/Pokedex/__test__/responses/typeList.json rename to src/features/Filters/__test__/responses/typeList.json diff --git a/src/features/Filters/filterApi.test.ts b/src/features/Filters/filterApi.test.ts new file mode 100644 index 0000000..c8ea7f9 --- /dev/null +++ b/src/features/Filters/filterApi.test.ts @@ -0,0 +1,38 @@ +import { pokedexSlice } from 'features/Pokedex/pokedexSlice'; +import { filterApi } from './filterApi'; +import { filterSlice } from './filterSlice'; +import { configureStore } from '@reduxjs/toolkit'; + +import { AppStore } from 'app/store'; +import { listenerMiddleware } from 'app/listenerMiddleware'; +import { TypeListResponseData } from 'features/Pokedex/types/api'; + +let store: AppStore; + +describe('filterApi', () => { + beforeEach(() => { + store = configureStore({ + reducer: { + pokedex: pokedexSlice.reducer, + filter: filterSlice.reducer, + [filterApi.reducerPath]: filterApi.reducer, + }, + middleware: getDefaultMiddleware => + getDefaultMiddleware().concat( + filterApi.middleware, + listenerMiddleware.middleware, + ), + }); + }); + + describe('JEST test against mock API', () => { + test('visit https://pokeapi.co/api/v2/type should return correct data in list', async () => { + await store.dispatch(filterApi.endpoints.getTypeList.initiate()); + + const typeListData = filterApi.endpoints.getTypeList.select()( + store.getState(), + ).data as TypeListResponseData; + expect(typeListData?.results).toHaveLength(typeListData.count + 1); + }); + }); +}); diff --git a/src/features/Pokedex/__test__/pokedexApi.test.ts b/src/features/Pokedex/__test__/pokedexApi.test.ts deleted file mode 100644 index 7df8755..0000000 --- a/src/features/Pokedex/__test__/pokedexApi.test.ts +++ /dev/null @@ -1,122 +0,0 @@ -import { pokedexApi } from 'features/Pokedex/pokedexApi'; -import { pokedexSlice } from 'features/Pokedex/pokedexSlice'; -import { configureStore } from '@reduxjs/toolkit'; -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'; - -let store: AppStore; -describe('pokedexApi', () => { - beforeEach(() => { - store = configureStore({ - reducer: { - pokedex: pokedexSlice.reducer, - [pokedexApi.reducerPath]: pokedexApi.reducer, - }, - middleware: getDefaultMiddleware => - getDefaultMiddleware().concat( - pokedexApi.middleware, - listenerMiddleware.middleware, - ), - }); - }); - - describe('JEST test against real API', () => { - test('visit https://pokeapi.co/api/v2/region/1 should return correct data', async () => { - await store.dispatch(pokedexApi.endpoints.getRegion.initiate(1)); - - const region1Data = pokedexApi.endpoints.getRegion.select(1)( - store.getState(), - ).data; - expect(region1Data).toEqual(region1); - }); - - test('visit https://pokeapi.co/api/v2/region/kanto should return correct data', async () => { - await store.dispatch(pokedexApi.endpoints.getRegion.initiate('kanto')); - - const region1Data = pokedexApi.endpoints.getRegion.select('kanto')( - store.getState(), - ).data; - expect(region1Data).toEqual(region1); - }); - - test('visit https://pokeapi.co/api/v2/pokemon/1 should return correct data', async () => { - await store.dispatch(pokedexApi.endpoints.getPokemon.initiate(1)); - - const region1Data = pokedexApi.endpoints.getPokemon.select(1)( - store.getState(), - ).data; - expect(region1Data).toEqual(pokemon1); - }); - - test('visit https://pokeapi.co/api/v2/pokemon/bulbasaur should return correct data', async () => { - await store.dispatch( - pokedexApi.endpoints.getPokemon.initiate('bulbasaur'), - ); - - const region1Data = pokedexApi.endpoints.getPokemon.select('bulbasaur')( - store.getState(), - ).data; - expect(region1Data).toEqual(pokemon1); - }); - }); - - describe('JEST test against mock API', () => { - test('visit https://pokeapi.co/api/v2/region/999999 should return correct data', async () => { - await store.dispatch(pokedexApi.endpoints.getRegion.initiate(999999)); - - const region1Data = pokedexApi.endpoints.getRegion.select(999999)( - store.getState(), - ).data; - expect(region1Data).toEqual(region1); - }); - - test('visit https://pokeapi.co/api/v2/region/testregion should return correct data', async () => { - await store.dispatch( - pokedexApi.endpoints.getRegion.initiate('testregion'), - ); - - const region1Data = pokedexApi.endpoints.getRegion.select('testregion')( - store.getState(), - ).data; - expect(region1Data).toEqual(region1); - }); - - test('visit https://pokeapi.co/api/v2/region should return correct data in list', async () => { - await store.dispatch(pokedexApi.endpoints.getRegionList.initiate()); - - const regionListData = pokedexApi.endpoints.getRegionList.select()( - store.getState(), - ).data as RegionListResponseData; - expect(regionListData?.results).toHaveLength(regionListData.count); - }); - - test('visit https://pokeapi.co/api/v2/type should return correct data in list', async () => { - await store.dispatch(pokedexApi.endpoints.getTypeList.initiate()); - - const typeListData = pokedexApi.endpoints.getTypeList.select()( - store.getState(), - ).data as TypeListResponseData; - expect(typeListData?.results).toHaveLength(typeListData.count + 1); - }); - - test('visit https://pokeapi.co/api/v2/pokemon should return correct data in list', async () => { - await store.dispatch(pokedexApi.endpoints.getPokemonList.initiate()); - - const pokemonListData = pokedexApi.endpoints.getPokemonList.select()( - store.getState(), - ).data; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - expect(pokemonListData?.results).toHaveLength(pokemonListData.count); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - expect(pokemonListData?.next).toBeUndefined(); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - expect(pokemonListData?.previous).toBeUndefined(); - }); - }); -}); diff --git a/src/mocks/handlers.ts b/src/mocks/handlers.ts index 631f321..edc96a2 100644 --- a/src/mocks/handlers.ts +++ b/src/mocks/handlers.ts @@ -2,7 +2,7 @@ import { rest } from 'msw'; import region1 from 'features/Pokedex/__test__/responses/region1.json'; import regionList from 'features/Pokedex/__test__/responses/regionList.json'; -import typeList from 'features/Pokedex/__test__/responses/typeList.json'; +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';