-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port Project to use redux-toolkit (#2754)
- Loading branch information
1 parent
2df7dbb
commit c7aaa71
Showing
6 changed files
with
157 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
import { | ||
CurrentProjectState, | ||
defaultState, | ||
ProjectAction, | ||
ProjectActionType, | ||
} from "components/Project/ProjectReduxTypes"; | ||
import { StoreAction, StoreActionTypes } from "rootActions"; | ||
import { newProject } from "types/project"; | ||
import { createSlice } from "@reduxjs/toolkit"; | ||
|
||
export const projectReducer = ( | ||
state = defaultState, | ||
action: ProjectAction | StoreAction | ||
): CurrentProjectState => { | ||
switch (action.type) { | ||
case ProjectActionType.SET_CURRENT_PROJECT: | ||
if (action.payload?.id === state.project.id) { | ||
return { ...state, project: action.payload }; | ||
import { defaultState } from "components/Project/ProjectReduxTypes"; | ||
import { StoreActionTypes } from "rootActions"; | ||
|
||
const projectSlice = createSlice({ | ||
name: "currentProjectState", | ||
initialState: defaultState, | ||
reducers: { | ||
resetAction: () => defaultState, | ||
setProjectAction: (state, action) => { | ||
if (state.project.id !== action.payload.id) { | ||
state.users = []; | ||
} | ||
return { project: action.payload ?? newProject(), users: [] }; | ||
case ProjectActionType.SET_CURRENT_PROJECT_USERS: | ||
return { ...state, users: action.payload ?? [] }; | ||
case StoreActionTypes.RESET: | ||
return defaultState; | ||
default: | ||
return state; | ||
} | ||
}; | ||
state.project = action.payload; | ||
}, | ||
setUsersAction: (state, action) => { | ||
state.users = action.payload; | ||
}, | ||
}, | ||
extraReducers: (builder) => | ||
builder.addCase(StoreActionTypes.RESET, () => defaultState), | ||
}); | ||
|
||
export const { resetAction, setProjectAction, setUsersAction } = | ||
projectSlice.actions; | ||
|
||
export default projectSlice.reducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { PreloadedState } from "redux"; | ||
|
||
import { Project } from "api/models"; | ||
import { defaultState } from "components/App/DefaultState"; | ||
import { | ||
asyncRefreshProjectUsers, | ||
asyncUpdateCurrentProject, | ||
clearCurrentProject, | ||
setNewCurrentProject, | ||
} from "components/Project/ProjectActions"; | ||
import { RootState, setupStore } from "store"; | ||
import { newProject } from "types/project"; | ||
import { newUser } from "types/user"; | ||
|
||
jest.mock("backend", () => ({ | ||
getAllProjectUsers: (...args: any[]) => mockGetAllProjectUsers(...args), | ||
updateProject: (...args: any[]) => mockUpdateProject(...args), | ||
})); | ||
|
||
const mockGetAllProjectUsers = jest.fn(); | ||
const mockUpdateProject = jest.fn(); | ||
const mockProjId = "project-id"; | ||
|
||
// Preloaded values for store when testing | ||
const persistedDefaultState: PreloadedState<RootState> = { | ||
...defaultState, | ||
_persist: { version: 1, rehydrated: false }, | ||
}; | ||
|
||
describe("ProjectActions", () => { | ||
describe("asyncUpdateCurrentProject", () => { | ||
it("updates the backend and correctly affects state for different id", async () => { | ||
const proj: Project = { ...newProject(), id: mockProjId }; | ||
const store = setupStore({ | ||
...persistedDefaultState, | ||
currentProjectState: { project: proj, users: [newUser()] }, | ||
}); | ||
const id = "new-id"; | ||
await store.dispatch(asyncUpdateCurrentProject({ ...proj, id })); | ||
expect(mockUpdateProject).toBeCalledTimes(1); | ||
const { project, users } = store.getState().currentProjectState; | ||
expect(project.id).toEqual(id); | ||
expect(users).toHaveLength(0); | ||
}); | ||
|
||
it("updates the backend and correctly affects state for same id", async () => { | ||
const proj: Project = { ...newProject(), id: mockProjId }; | ||
const store = setupStore({ | ||
...persistedDefaultState, | ||
currentProjectState: { project: proj, users: [newUser()] }, | ||
}); | ||
const name = "new-name"; | ||
await store.dispatch(asyncUpdateCurrentProject({ ...proj, name })); | ||
expect(mockUpdateProject).toBeCalledTimes(1); | ||
const { project, users } = store.getState().currentProjectState; | ||
expect(project.name).toEqual(name); | ||
expect(users).toHaveLength(1); | ||
}); | ||
}); | ||
|
||
describe("asyncRefreshProjectUsers", () => { | ||
it("correctly affects state", async () => { | ||
const proj: Project = { ...newProject(), id: mockProjId }; | ||
const store = setupStore({ | ||
...persistedDefaultState, | ||
currentProjectState: { project: proj, users: [] }, | ||
}); | ||
const mockUsers = [newUser(), newUser(), newUser()]; | ||
mockGetAllProjectUsers.mockResolvedValueOnce(mockUsers); | ||
await store.dispatch(asyncRefreshProjectUsers("mockProjId")); | ||
const { project, users } = store.getState().currentProjectState; | ||
expect(project.id).toEqual(mockProjId); | ||
expect(users).toHaveLength(mockUsers.length); | ||
}); | ||
}); | ||
|
||
describe("clearCurrentProject", () => { | ||
it("correctly affects state", () => { | ||
const nonDefaultState = { | ||
project: { ...newProject(), id: "nonempty-string" }, | ||
users: [newUser()], | ||
}; | ||
const store = setupStore({ | ||
...persistedDefaultState, | ||
currentProjectState: nonDefaultState, | ||
}); | ||
store.dispatch(clearCurrentProject()); | ||
const { project, users } = store.getState().currentProjectState; | ||
expect(project.id).toEqual(""); | ||
expect(users).toHaveLength(0); | ||
}); | ||
}); | ||
|
||
describe("setNewCurrentProject", () => { | ||
it("correctly affects state and doesn't update the backend", () => { | ||
const proj: Project = { ...newProject(), id: mockProjId }; | ||
const store = setupStore(); | ||
store.dispatch(setNewCurrentProject(proj)); | ||
expect(mockUpdateProject).not.toBeCalled(); | ||
const { project } = store.getState().currentProjectState; | ||
expect(project.id).toEqual(mockProjId); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters