mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-09-12 14:20:08 +02:00
Factoring gsheet read
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import axios from "axios"
|
||||
|
||||
import mockStore from "../../utils/mockStore"
|
||||
import javGameList, {
|
||||
import JeuxJavList, {
|
||||
initialState,
|
||||
getRequesting,
|
||||
getSuccess,
|
||||
getFailure,
|
||||
fetchJavGameList,
|
||||
} from "../javGameList"
|
||||
fetchJeuxJavList,
|
||||
} from "../jeuxJavList"
|
||||
|
||||
jest.mock("axios")
|
||||
|
||||
@@ -34,14 +34,14 @@ const mockData = [
|
||||
]
|
||||
const mockError = "Oops! Something went wrong."
|
||||
|
||||
describe("javGameList reducer", () => {
|
||||
describe("JeuxJavList reducer", () => {
|
||||
it("should handle initial state", () => {
|
||||
// @ts-expect-error
|
||||
expect(javGameList(undefined, {})).toEqual(initialState)
|
||||
expect(JeuxJavList(undefined, {})).toEqual(initialState)
|
||||
})
|
||||
|
||||
it("should handle requesting correctly", () => {
|
||||
expect(javGameList(undefined, { type: getRequesting.type })).toEqual({
|
||||
expect(JeuxJavList(undefined, { type: getRequesting.type })).toEqual({
|
||||
readyStatus: "request",
|
||||
items: [],
|
||||
error: null,
|
||||
@@ -49,7 +49,7 @@ describe("javGameList reducer", () => {
|
||||
})
|
||||
|
||||
it("should handle success correctly", () => {
|
||||
expect(javGameList(undefined, { type: getSuccess.type, payload: mockData })).toEqual({
|
||||
expect(JeuxJavList(undefined, { type: getSuccess.type, payload: mockData })).toEqual({
|
||||
...initialState,
|
||||
readyStatus: "success",
|
||||
items: mockData,
|
||||
@@ -57,7 +57,7 @@ describe("javGameList reducer", () => {
|
||||
})
|
||||
|
||||
it("should handle failure correctly", () => {
|
||||
expect(javGameList(undefined, { type: getFailure.type, payload: mockError })).toEqual({
|
||||
expect(JeuxJavList(undefined, { type: getFailure.type, payload: mockError })).toEqual({
|
||||
...initialState,
|
||||
readyStatus: "failure",
|
||||
error: mockError,
|
||||
@@ -65,8 +65,8 @@ describe("javGameList reducer", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("javGameList action", () => {
|
||||
it("fetches javGame list successful", async () => {
|
||||
describe("JeuxJavList action", () => {
|
||||
it("fetches JeuxJav list successful", async () => {
|
||||
const { dispatch, getActions } = mockStore()
|
||||
const expectedActions = [
|
||||
{ type: getRequesting.type },
|
||||
@@ -76,11 +76,11 @@ describe("javGameList action", () => {
|
||||
// @ts-expect-error
|
||||
axios.get.mockResolvedValue({ data: mockData })
|
||||
|
||||
await dispatch(fetchJavGameList())
|
||||
await dispatch(fetchJeuxJavList())
|
||||
expect(getActions()).toEqual(expectedActions)
|
||||
})
|
||||
|
||||
it("fetches javGame list failed", async () => {
|
||||
it("fetches JeuxJav list failed", async () => {
|
||||
const { dispatch, getActions } = mockStore()
|
||||
const expectedActions = [
|
||||
{ type: getRequesting.type },
|
||||
@@ -90,7 +90,7 @@ describe("javGameList action", () => {
|
||||
// @ts-expect-error
|
||||
axios.get.mockRejectedValue({ message: mockError })
|
||||
|
||||
await dispatch(fetchJavGameList())
|
||||
await dispatch(fetchJeuxJavList())
|
||||
expect(getActions()).toEqual(expectedActions)
|
||||
})
|
||||
})
|
@@ -1,57 +0,0 @@
|
||||
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
|
||||
|
||||
import { JavGame, getJavGameList } from "../services/javGames"
|
||||
import { AppThunk, AppState } from "."
|
||||
|
||||
interface JavGameList {
|
||||
readyStatus: string
|
||||
items: JavGame[]
|
||||
error: string | null
|
||||
}
|
||||
|
||||
export const initialState: JavGameList = {
|
||||
readyStatus: "invalid",
|
||||
items: [],
|
||||
error: null,
|
||||
}
|
||||
|
||||
const javGameList = createSlice({
|
||||
name: "javGameList",
|
||||
initialState,
|
||||
reducers: {
|
||||
getRequesting: (state: JavGameList) => {
|
||||
state.readyStatus = "request"
|
||||
},
|
||||
getSuccess: (state, { payload }: PayloadAction<JavGame[]>) => {
|
||||
state.readyStatus = "success"
|
||||
state.items = 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 = (): AppThunk => async (dispatch) => {
|
||||
dispatch(getRequesting())
|
||||
|
||||
const { error, data } = await getJavGameList()
|
||||
|
||||
if (error) {
|
||||
dispatch(getFailure(error.message))
|
||||
} else {
|
||||
dispatch(getSuccess(data as JavGame[]))
|
||||
}
|
||||
}
|
||||
|
||||
const shouldFetchJavGameList = (state: AppState) => state.javGameList.readyStatus !== "success"
|
||||
|
||||
export const fetchJavGameListIfNeed = (): AppThunk => (dispatch, getState) => {
|
||||
if (shouldFetchJavGameList(getState())) return dispatch(fetchJavGameList())
|
||||
|
||||
return null
|
||||
}
|
57
src/store/jeuxJavList.ts
Normal file
57
src/store/jeuxJavList.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
|
||||
|
||||
import { JeuxJav, getJeuxJavList } from "../services/jeuxJav"
|
||||
import { AppThunk, AppState } from "."
|
||||
|
||||
interface JeuxJavList {
|
||||
readyStatus: string
|
||||
items: JeuxJav[]
|
||||
error: string | null
|
||||
}
|
||||
|
||||
export const initialState: JeuxJavList = {
|
||||
readyStatus: "invalid",
|
||||
items: [],
|
||||
error: null,
|
||||
}
|
||||
|
||||
const jeuxJavList = createSlice({
|
||||
name: "jeuxJavList",
|
||||
initialState,
|
||||
reducers: {
|
||||
getRequesting: (state: JeuxJavList) => {
|
||||
state.readyStatus = "request"
|
||||
},
|
||||
getSuccess: (state, { payload }: PayloadAction<JeuxJav[]>) => {
|
||||
state.readyStatus = "success"
|
||||
state.items = payload
|
||||
},
|
||||
getFailure: (state, { payload }: PayloadAction<string>) => {
|
||||
state.readyStatus = "failure"
|
||||
state.error = payload
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
export default jeuxJavList.reducer
|
||||
export const { getRequesting, getSuccess, getFailure } = jeuxJavList.actions
|
||||
|
||||
export const fetchJeuxJavList = (): AppThunk => async (dispatch) => {
|
||||
dispatch(getRequesting())
|
||||
|
||||
const { error, data } = await getJeuxJavList()
|
||||
|
||||
if (error) {
|
||||
dispatch(getFailure(error.message))
|
||||
} else {
|
||||
dispatch(getSuccess(data as JeuxJav[]))
|
||||
}
|
||||
}
|
||||
|
||||
const shouldFetchJeuxJavList = (state: AppState) => state.jeuxJavList.readyStatus !== "success"
|
||||
|
||||
export const fetchJeuxJavListIfNeed = (): AppThunk => (dispatch, getState) => {
|
||||
if (shouldFetchJeuxJavList(getState())) return dispatch(fetchJeuxJavList())
|
||||
|
||||
return null
|
||||
}
|
@@ -3,14 +3,14 @@ import { connectRouter } from "connected-react-router"
|
||||
|
||||
import userList from "./userList"
|
||||
import userData from "./userData"
|
||||
import javGameList from "./javGameList"
|
||||
import jeuxJavList from "./jeuxJavList"
|
||||
|
||||
// Use inferred return type for making correctly Redux types
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export default (history: History) => ({
|
||||
userList,
|
||||
userData,
|
||||
javGameList,
|
||||
jeuxJavList,
|
||||
router: connectRouter(history) as any,
|
||||
// Register more reducers...
|
||||
})
|
||||
|
Reference in New Issue
Block a user