Skip to content

Commit

Permalink
Add tests for current cluster checks selection saga
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkopliku committed Jul 12, 2023
1 parent de9467b commit 3e5f7ed
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 5 deletions.
12 changes: 9 additions & 3 deletions assets/js/state/sagas/clusters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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());
Expand Down
81 changes: 79 additions & 2 deletions assets/js/state/sagas/clusters.test.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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(),
]);
});
});

0 comments on commit 3e5f7ed

Please sign in to comment.