Move Filter related tests in original pokedexApi.test.ts into filterApi.test.ts

develop
Jason Zhu 2023-05-08 19:59:04 +10:00
parent 1d58f41b5f
commit db331acbb1
4 changed files with 39 additions and 123 deletions

View File

@ -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);
});
});
});

View File

@ -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();
});
});
});

View File

@ -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';