-
-
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 ExportProject to use redux-toolkit; Remove redux from CreateProj…
…ect (#2747)
- Loading branch information
1 parent
ad96ccc
commit 42b599c
Showing
15 changed files
with
225 additions
and
342 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
62 changes: 38 additions & 24 deletions
62
src/components/ProjectExport/Redux/ExportProjectReducer.ts
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,29 +1,43 @@ | ||
import { createSlice } from "@reduxjs/toolkit"; | ||
|
||
import { | ||
defaultState, | ||
ExportProjectAction, | ||
ExportProjectState, | ||
ExportStatus, | ||
} from "components/ProjectExport/Redux/ExportProjectReduxTypes"; | ||
import { StoreAction, StoreActionTypes } from "rootActions"; | ||
import { StoreActionTypes } from "rootActions"; | ||
|
||
const exportProjectSlice = createSlice({ | ||
name: "exportProjectState", | ||
initialState: defaultState, | ||
reducers: { | ||
downloadingAction: (state, action) => { | ||
state.projectId = action.payload; | ||
state.status = ExportStatus.Downloading; | ||
}, | ||
exportingAction: (state, action) => { | ||
state.projectId = action.payload; | ||
state.status = ExportStatus.Exporting; | ||
}, | ||
failureAction: (state, action) => { | ||
state.projectId = action.payload; | ||
state.status = ExportStatus.Failure; | ||
}, | ||
resetAction: () => defaultState, | ||
successAction: (state, action) => { | ||
state.projectId = action.payload; | ||
state.status = ExportStatus.Success; | ||
}, | ||
}, | ||
extraReducers: (builder) => | ||
builder.addCase(StoreActionTypes.RESET, () => defaultState), | ||
}); | ||
|
||
export const { | ||
downloadingAction, | ||
exportingAction, | ||
failureAction, | ||
resetAction, | ||
successAction, | ||
} = exportProjectSlice.actions; | ||
|
||
export const exportProjectReducer = ( | ||
state: ExportProjectState = defaultState, | ||
action: StoreAction | ExportProjectAction | ||
): ExportProjectState => { | ||
switch (action.type) { | ||
case ExportStatus.Exporting: | ||
case ExportStatus.Downloading: | ||
case ExportStatus.Success: | ||
case ExportStatus.Failure: | ||
return { | ||
...defaultState, | ||
projectId: action.projectId ?? "", | ||
status: action.type, | ||
}; | ||
case ExportStatus.Default: | ||
case StoreActionTypes.RESET: | ||
return defaultState; | ||
default: | ||
return state; | ||
} | ||
}; | ||
export default exportProjectSlice.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
85 changes: 85 additions & 0 deletions
85
src/components/ProjectExport/Redux/tests/ExportProjectActions.test.tsx
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,85 @@ | ||
import { PreloadedState } from "redux"; | ||
|
||
import { defaultState } from "components/App/DefaultState"; | ||
import { | ||
asyncDownloadExport, | ||
asyncExportProject, | ||
asyncResetExport, | ||
} from "components/ProjectExport/Redux/ExportProjectActions"; | ||
import { ExportStatus } from "components/ProjectExport/Redux/ExportProjectReduxTypes"; | ||
import { RootState, setupStore } from "store"; | ||
|
||
jest.mock("backend", () => ({ | ||
deleteLift: jest.fn, | ||
downloadLift: (...args: any[]) => mockDownloadList(...args), | ||
exportLift: (...args: any[]) => mockExportLift(...args), | ||
})); | ||
|
||
const mockDownloadList = jest.fn(); | ||
const mockExportLift = jest.fn(); | ||
const mockProjId = "project-id"; | ||
|
||
// Preloaded values for store when testing | ||
const persistedDefaultState: PreloadedState<RootState> = { | ||
...defaultState, | ||
_persist: { version: 1, rehydrated: false }, | ||
}; | ||
|
||
describe("ExportProjectActions", () => { | ||
describe("asyncDownloadExport", () => { | ||
it("correctly affects state on success", async () => { | ||
const store = setupStore(); | ||
mockDownloadList.mockResolvedValueOnce({}); | ||
await store.dispatch(asyncDownloadExport(mockProjId)); | ||
const { projectId, status } = store.getState().exportProjectState; | ||
expect(projectId).toEqual(mockProjId); | ||
expect(status).toEqual(ExportStatus.Downloading); | ||
}); | ||
|
||
it("correctly affects state on failure", async () => { | ||
const store = setupStore(); | ||
mockDownloadList.mockRejectedValueOnce({}); | ||
await store.dispatch(asyncDownloadExport(mockProjId)); | ||
const { projectId, status } = store.getState().exportProjectState; | ||
expect(projectId).toEqual(mockProjId); | ||
expect(status).toEqual(ExportStatus.Failure); | ||
}); | ||
}); | ||
|
||
describe("asyncExportProject", () => { | ||
it("correctly affects state on success", async () => { | ||
const store = setupStore(); | ||
mockExportLift.mockResolvedValueOnce({}); | ||
await store.dispatch(asyncExportProject(mockProjId)); | ||
const { projectId, status } = store.getState().exportProjectState; | ||
expect(projectId).toEqual(mockProjId); | ||
expect(status).toEqual(ExportStatus.Exporting); | ||
}); | ||
|
||
it("correctly affects state on failure", async () => { | ||
const store = setupStore(); | ||
mockExportLift.mockRejectedValueOnce({}); | ||
await store.dispatch(asyncExportProject(mockProjId)); | ||
const { projectId, status } = store.getState().exportProjectState; | ||
expect(projectId).toEqual(mockProjId); | ||
expect(status).toEqual(ExportStatus.Failure); | ||
}); | ||
}); | ||
|
||
describe("asyncResetExport", () => { | ||
it("correctly affects state", async () => { | ||
const nonDefaultState = { | ||
projectId: "nonempty-string", | ||
status: ExportStatus.Success, | ||
}; | ||
const store = setupStore({ | ||
...persistedDefaultState, | ||
exportProjectState: nonDefaultState, | ||
}); | ||
await store.dispatch(asyncResetExport()); | ||
const { projectId, status } = store.getState().exportProjectState; | ||
expect(projectId).toEqual(""); | ||
expect(status).toEqual(ExportStatus.Default); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.