Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for api thunks for expected behavior from thunks and api re… #391

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions containers/clusterManagement/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,17 @@ const ClusterManagement: FunctionComponent = () => {
if (clusterCreationStep !== ClusterCreationStep.DETAILS) {
dispatch(createWorkloadCluster())
.unwrap()
.then((response) => {
.then((clusterId) => {
const { type } = clusterMap[clusterId];

addClusterToQueue({
id: response.clusterId,
id: clusterId,
clusterName: managementCluster?.clusterName as string,
clusterType: response.type,
clusterType: type,
status: ClusterStatus.PROVISIONING,
callback: handleGetClusters,
});

dispatch(setClusterCreationStep(clusterCreationStep + 1));
});
}
Expand Down
66 changes: 53 additions & 13 deletions redux/slices/api.slice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { sortBy } from 'lodash';

import {
createCluster,
Expand All @@ -19,6 +20,7 @@ import {
WorkloadCluster,
Cluster,
DraftCluster,
ClusterEnvironment,
} from '../../types/provision';
import { ClusterCache, ClusterNameCache } from '../../types/redux';

Expand All @@ -27,6 +29,7 @@ export interface ApiState {
status?: ClusterStatus;
isProvisioned: boolean;
isError: boolean;
responseError?: string;
lastErrorCondition?: string;
managementCluster?: ManagementCluster;
presentedClusterId?: Cluster['clusterId'];
Expand Down Expand Up @@ -120,39 +123,61 @@ const apiSlice = createSlice({
};
}
},
clearResponseError: (state) => {
state.responseError = undefined;
},
},
extraReducers: (builder) => {
builder
.addCase(createCluster.pending, (state) => {
state.loading = true;
})
.addCase(createCluster.fulfilled, (state, { payload }) => {
.addCase(createCluster.fulfilled, (state) => {
state.loading = false;
state.status = payload.status;
state.managementCluster = payload;
})
.addCase(createCluster.rejected, (state) => {
.addCase(createCluster.rejected, (state, action) => {
state.loading = false;
state.isError = true;
state.responseError = action.error.message;
})
.addCase(createWorkloadCluster.pending, (state) => {
state.loading = true;
})
.addCase(createWorkloadCluster.rejected, (state) => {
.addCase(createWorkloadCluster.rejected, (state, action) => {
state.loading = false;
state.isError = true;
state.responseError = action.error.message;
})
.addCase(createWorkloadCluster.fulfilled, (state, { payload }) => {
.addCase(createWorkloadCluster.fulfilled, (state, { payload: clusterId }) => {
state.loading = false;

const draftCluster = state.clusterMap['draft'];

const provisioningWorkloadCluster: WorkloadCluster = {
...draftCluster,
clusterId,
status: ClusterStatus.PROVISIONING,
environment: draftCluster.environment as ClusterEnvironment,
};

state.clusterMap[clusterId] = provisioningWorkloadCluster;
delete state.clusterMap['draft'];
state.clusterMap[payload.clusterId] = payload;
state.presentedClusterId = payload.clusterId;

state.presentedClusterId = clusterId;
})
.addCase(deleteCluster.pending, (state) => {
if (state.presentedClusterId) {
state.clusterMap[state.presentedClusterId].status = ClusterStatus.DELETING;
}
})
.addCase(deleteCluster.fulfilled, (state) => {
state.loading = false;
})
.addCase(deleteCluster.rejected, (state, action) => {
state.loading = false;
state.isError = true;
state.responseError = action.error.message;
})
.addCase(
getCluster.fulfilled,
(state, { payload: { managementCluster, clusterCache, clusterNameCache } }) => {
Expand All @@ -167,6 +192,10 @@ const apiSlice = createSlice({
state.isProvisioned = managementCluster.status === ClusterStatus.PROVISIONED;
},
)
.addCase(getCluster.rejected, (state, action) => {
state.loading = false;
state.responseError = action.error.message;
})
.addCase(
getClusters.fulfilled,
(state, { payload: { managementCluster, clusterCache, clusterNameCache } }) => {
Expand All @@ -177,19 +206,27 @@ const apiSlice = createSlice({
state.clusterNameCache = clusterNameCache;
},
)
.addCase(getClusters.rejected, (state) => {
.addCase(getClusters.rejected, (state, action) => {
state.loading = false;
state.isError = true;
state.responseError = action.error.message;
state.managementCluster = undefined;
})
.addCase(getCloudDomains.pending, (state) => {
state.loading = true;
})
.addCase(getCloudDomains.rejected, (state, action) => {
state.loading = false;
state.responseError = action.error.message;
})
.addCase(getCloudDomains.fulfilled, (state, { payload }: PayloadAction<Array<string>>) => {
state.cloudDomains = payload;
state.cloudDomains = sortBy(payload);
})
.addCase(getCloudRegions.pending, (state) => {
state.loading = true;
})
.addCase(getCloudRegions.fulfilled, (state, { payload }: PayloadAction<Array<string>>) => {
state.cloudRegions = payload;
state.cloudRegions = sortBy(payload);
state.isAuthenticationValid = true;
state.loading = false;
})
Expand All @@ -204,9 +241,10 @@ const apiSlice = createSlice({
state.instanceSizes = payload;
state.loading = false;
})
.addCase(getInstanceSizes.rejected, (state) => {
.addCase(getInstanceSizes.rejected, (state, action) => {
state.loading = false;
state.instanceSizes = [];
state.responseError = action.error.message;
})
.addCase(getRegionZones.pending, (state) => {
state.loading = true;
Expand All @@ -215,9 +253,10 @@ const apiSlice = createSlice({
state.cloudZones = payload;
state.loading = false;
})
.addCase(getRegionZones.rejected, (state) => {
.addCase(getRegionZones.rejected, (state, action) => {
state.loading = false;
state.cloudZones = [];
state.responseError = action.error.message;
});
},
});
Expand All @@ -236,6 +275,7 @@ export const {
setManagementCluster,
setClusterMap,
setClusterNameCache,
clearResponseError,
} = apiSlice.actions;

export const apiReducer = apiSlice.reducer;
Loading
Loading