diff --git a/assets/js/state/sagas/clusters.js b/assets/js/state/sagas/clusters.js index 3a8caf1e02..ae2bcf8e50 100644 --- a/assets/js/state/sagas/clusters.js +++ b/assets/js/state/sagas/clusters.js @@ -16,17 +16,17 @@ import { } from '@state/clusterChecksSelection'; import { getClusterName } from '@state/selectors/cluster'; -function* checksSelected({ payload }) { +export function* checksSelected({ payload }) { yield put(startSavingClusterChecksSelection()); + const clusterName = yield select(getClusterName(payload.clusterID)); + try { yield call(post, `/clusters/${payload.clusterID}/checks`, { checks: payload.checks, }); yield put(updateSelectedChecks(payload)); - const clusterName = yield select(getClusterName(payload.clusterID)); - yield put( notify({ text: `Checks selection for ${clusterName} saved`, @@ -35,6 +35,12 @@ function* checksSelected({ payload }) { ); yield put(setClusterChecksSelectionSavingSuccess()); } catch (error) { + yield put( + notify({ + text: `Unable to save selection for ${clusterName}`, + icon: '❌', + }) + ); yield put(setClusterChecksSelectionSavingError()); } yield put(stopSavingClusterChecksSelection()); diff --git a/assets/js/state/sagas/clusters.test.js b/assets/js/state/sagas/clusters.test.js index d5e9cf2d3f..ad9745279f 100644 --- a/assets/js/state/sagas/clusters.test.js +++ b/assets/js/state/sagas/clusters.test.js @@ -1,8 +1,24 @@ +import { faker } from '@faker-js/faker'; +import MockAdapter from 'axios-mock-adapter'; import { recordSaga } from '@lib/test-utils'; -import { clusterDeregistered } from '@state/sagas/clusters'; -import { removeCluster } from '@state/clusters'; + +import { networkClient } from '@lib/network'; + import { clusterFactory } from '@lib/test-utils/factories'; +import { notify } from '@state/actions/notifications'; +import { clusterDeregistered, checksSelected } from '@state/sagas/clusters'; +import { removeCluster, updateSelectedChecks } from '@state/clusters'; + +import { + setClusterChecksSelectionSavingError, + setClusterChecksSelectionSavingSuccess, + startSavingClusterChecksSelection, + stopSavingClusterChecksSelection, +} from '@state/clusterChecksSelection'; + +const axiosMock = new MockAdapter(networkClient); + describe('Clusters sagas', () => { it('should remove the cluster', async () => { const { id, name } = clusterFactory.build(); @@ -13,4 +29,65 @@ describe('Clusters sagas', () => { expect(dispatched).toContainEqual(removeCluster({ id })); }); + + it('should save check selection for a cluster', async () => { + const cluster = clusterFactory.build(); + + axiosMock.onPost(`/clusters/${cluster.id}/checks`).reply(202, {}); + + const actionPayload = { + clusterID: cluster.id, + checks: [faker.datatype.uuid(), faker.datatype.uuid()], + }; + const dispatched = await recordSaga( + checksSelected, + { + payload: actionPayload, + }, + { + clustersList: { clusters: [cluster] }, + } + ); + + expect(dispatched).toEqual([ + startSavingClusterChecksSelection(), + updateSelectedChecks(actionPayload), + notify({ + text: `Checks selection for ${cluster.name} saved`, + icon: '💾', + }), + setClusterChecksSelectionSavingSuccess(), + stopSavingClusterChecksSelection(), + ]); + }); + + it('should notify an error when saving a cluster check selection fails', async () => { + const cluster = clusterFactory.build(); + + axiosMock.onPost(`/clusters/${cluster.id}/checks`).reply(400, {}); + + const actionPayload = { + clusterID: cluster.id, + checks: [faker.datatype.uuid(), faker.datatype.uuid()], + }; + const dispatched = await recordSaga( + checksSelected, + { + payload: actionPayload, + }, + { + clustersList: { clusters: [cluster] }, + } + ); + + expect(dispatched).toEqual([ + startSavingClusterChecksSelection(), + notify({ + text: `Unable to save selection for ${cluster.name}`, + icon: '❌', + }), + setClusterChecksSelectionSavingError(), + stopSavingClusterChecksSelection(), + ]); + }); });