mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-09-11 22:06:29 +02:00
Rename JavGame to Game because all games are not from or at Jav
This commit is contained in:
@@ -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
49
src/store/gameList.ts
Normal 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
|
||||
}
|
@@ -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
|
||||
}
|
@@ -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,
|
||||
|
Reference in New Issue
Block a user