Rename JavGame to Game because all games are not from or at Jav

This commit is contained in:
pikiou
2022-03-11 13:29:04 +01:00
parent 57057780d8
commit ed8f20072f
17 changed files with 112 additions and 127 deletions

View File

@@ -2,18 +2,18 @@ import axios from "axios"
import _ from "lodash"
import mockStore from "../../utils/mockStore"
import JavGameList, {
import GameList, {
initialState,
getRequesting,
getSuccess,
getFailure,
fetchJavGameList,
} from "../javGameList"
import { JavGame } from "../../services/javGames"
fetchGameList,
} from "../gameList"
import { Game } from "../../services/games"
jest.mock("axios")
const mockData: JavGame[] = [
const mockData: Game[] = [
{
id: 5,
title: "6 qui prend!",
@@ -27,22 +27,19 @@ const mockData: JavGame[] = [
bggPhoto:
"https://cf.geekdo-images.com/thumb/img/lzczxR5cw7an7tRWeHdOrRtLyes=/fit-in/200x150/pic772547.jpg",
bggId: 432,
copies: 1,
lendAvailability: 1,
notStored: 0,
ean: "3421272101313",
},
]
const mockError = "Oops! Something went wrong."
describe("JavGameList reducer", () => {
describe("GameList reducer", () => {
it("should handle initial state", () => {
// @ts-expect-error
expect(JavGameList(undefined, {})).toEqual(initialState)
expect(GameList(undefined, {})).toEqual(initialState)
})
it("should handle requesting correctly", () => {
expect(JavGameList(undefined, { type: getRequesting.type })).toEqual({
expect(GameList(undefined, { type: getRequesting.type })).toEqual({
readyStatus: "request",
ids: [],
entities: {},
@@ -50,7 +47,7 @@ describe("JavGameList reducer", () => {
})
it("should handle success correctly", () => {
expect(JavGameList(undefined, { type: getSuccess.type, payload: mockData })).toEqual({
expect(GameList(undefined, { type: getSuccess.type, payload: mockData })).toEqual({
...initialState,
readyStatus: "success",
ids: _.map(mockData, "id"),
@@ -59,7 +56,7 @@ describe("JavGameList reducer", () => {
})
it("should handle failure correctly", () => {
expect(JavGameList(undefined, { type: getFailure.type, payload: mockError })).toEqual({
expect(GameList(undefined, { type: getFailure.type, payload: mockError })).toEqual({
...initialState,
readyStatus: "failure",
error: mockError,
@@ -67,8 +64,8 @@ describe("JavGameList reducer", () => {
})
})
describe("JavGameList action", () => {
it("fetches JavGame list successful", async () => {
describe("GameList action", () => {
it("fetches Game list successful", async () => {
const { dispatch, getActions } = mockStore()
const expectedActions = [
{ type: getRequesting.type, payload: undefined },
@@ -78,11 +75,11 @@ describe("JavGameList action", () => {
// @ts-expect-error
axios.get.mockResolvedValue({ data: mockData })
await dispatch(fetchJavGameList())
await dispatch(fetchGameList())
expect(getActions()).toEqual(expectedActions)
})
it("fetches JavGame list failed", async () => {
it("fetches Game list failed", async () => {
const { dispatch, getActions } = mockStore()
const expectedActions = [
{ type: getRequesting.type },
@@ -92,7 +89,7 @@ describe("JavGameList action", () => {
// @ts-expect-error
axios.get.mockRejectedValue({ message: mockError })
await dispatch(fetchJavGameList())
await dispatch(fetchGameList())
expect(getActions()).toEqual(expectedActions)
})
})

49
src/store/gameList.ts Normal file
View File

@@ -0,0 +1,49 @@
import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementListFetch } from "./utils"
import { Game } from "../services/games"
import { AppThunk, AppState } from "."
import { gameListGet } from "../services/gamesAccessors"
const gameAdapter = createEntityAdapter<Game>()
export const initialState = gameAdapter.getInitialState({
readyStatus: "idle",
} as StateRequest)
const gameList = createSlice({
name: "gameList",
initialState,
reducers: {
getRequesting: (state) => {
state.readyStatus = "request"
},
getSuccess: (state, { payload }: PayloadAction<Game[]>) => {
state.readyStatus = "success"
gameAdapter.setAll(state, payload)
},
getFailure: (state, { payload }: PayloadAction<string>) => {
state.readyStatus = "failure"
state.error = payload
},
},
})
export default gameList.reducer
export const { getRequesting, getSuccess, getFailure } = gameList.actions
export const fetchGameList = elementListFetch(
gameListGet,
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors du chargement des jeux JAV: ${error.message}`)
)
const shouldFetchGameList = (state: AppState) => state.gameList.readyStatus !== "success"
export const fetchGameListIfNeed = (): AppThunk => (dispatch, getState) => {
if (shouldFetchGameList(getState())) return dispatch(fetchGameList())
return null
}

View File

@@ -1,49 +0,0 @@
import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementListFetch } from "./utils"
import { JavGame } from "../services/javGames"
import { AppThunk, AppState } from "."
import { javGameListGet } from "../services/javGamesAccessors"
const javGameAdapter = createEntityAdapter<JavGame>()
export const initialState = javGameAdapter.getInitialState({
readyStatus: "idle",
} as StateRequest)
const javGameList = createSlice({
name: "javGameList",
initialState,
reducers: {
getRequesting: (state) => {
state.readyStatus = "request"
},
getSuccess: (state, { payload }: PayloadAction<JavGame[]>) => {
state.readyStatus = "success"
javGameAdapter.setAll(state, payload)
},
getFailure: (state, { payload }: PayloadAction<string>) => {
state.readyStatus = "failure"
state.error = payload
},
},
})
export default javGameList.reducer
export const { getRequesting, getSuccess, getFailure } = javGameList.actions
export const fetchJavGameList = elementListFetch(
javGameListGet,
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors du chargement des jeux JAV: ${error.message}`)
)
const shouldFetchJavGameList = (state: AppState) => state.javGameList.readyStatus !== "success"
export const fetchJavGameListIfNeed = (): AppThunk => (dispatch, getState) => {
if (shouldFetchJavGameList(getState())) return dispatch(fetchJavGameList())
return null
}

View File

@@ -2,7 +2,7 @@ import { History } from "history"
import { connectRouter } from "connected-react-router"
import auth from "./auth"
import javGameList from "./javGameList"
import gameList from "./gameList"
import announcementList from "./announcementList"
import preVolunteerAdd from "./preVolunteerAdd"
import preVolunteerCount from "./preVolunteerCount"
@@ -25,7 +25,7 @@ import wishList from "./wishList"
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export default (history: History) => ({
auth,
javGameList,
gameList,
announcementList,
preVolunteerAdd,
preVolunteerCount,