Skip to content

Commit

Permalink
Deregister host saga
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Jul 6, 2023
1 parent c2dd815 commit 9f4d1d9
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
31 changes: 29 additions & 2 deletions assets/js/state/sagas/hosts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { put, takeEvery } from 'redux-saga/effects';
import { HOST_DEREGISTERED, removeHost } from '@state/hosts';
import { put, call, takeEvery } from 'redux-saga/effects';
import {
DEREGISTER_HOST,
HOST_DEREGISTERED,
removeHost,
setHostDeregistering,
setHostNotDeregistering,
} from '@state/hosts';
import { del } from '@lib/network';
import { notify } from '@state/actions/notifications';

export function* hostDeregistered({ payload }) {
Expand All @@ -12,6 +19,26 @@ export function* hostDeregistered({ payload }) {
);
}

export function* deregisterHost({ payload }) {
yield put(setHostDeregistering(payload));
try {
yield call(del, `/hosts/${payload.id}`);
} catch (error) {
yield put(
notify({
text: `Error deregistering host ${payload?.hostname}.`,
icon: '❌',
})
);
} finally {
yield put(setHostNotDeregistering(payload));
}
}

export function* watchHostDeregistered() {
yield takeEvery(HOST_DEREGISTERED, hostDeregistered);
}

export function* watchDeregisterHost() {
yield takeEvery(DEREGISTER_HOST, deregisterHost);
}
58 changes: 56 additions & 2 deletions assets/js/state/sagas/hosts.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import MockAdapter from 'axios-mock-adapter';

import { recordSaga } from '@lib/test-utils';
import { hostDeregistered } from '@state/sagas/hosts';
import { removeHost } from '@state/hosts';
import { networkClient } from '@lib/network';
import { deregisterHost, hostDeregistered } from '@state/sagas/hosts';
import {
removeHost,
setHostDeregistering,
setHostNotDeregistering,
} from '@state/hosts';
import { notify } from '@state/actions/notifications';
import { hostFactory } from '@lib/test-utils/factories';

const axiosMock = new MockAdapter(networkClient);

describe('Hosts sagas', () => {
beforeEach(() => {
axiosMock.reset();
jest.spyOn(console, 'error').mockImplementation(() => null);
});

afterEach(() => {
/* eslint-disable-next-line */
console.error.mockRestore();
});

it('should remove the host', async () => {
const { id, hostname } = hostFactory.build();
const payload = { id, hostname };
Expand All @@ -14,4 +34,38 @@ describe('Hosts sagas', () => {

expect(dispatched).toContainEqual(removeHost(payload));
});

it('should send host deregister request', async () => {
const host = hostFactory.build();

axiosMock.onDelete(`/hosts/${host.id}`).reply(204, {});

const dispatched = await recordSaga(deregisterHost, {
payload: host,
});

expect(dispatched).toEqual([
setHostDeregistering(host),
setHostNotDeregistering(host),
]);
});

it('should notify error on host deregistration request', async () => {
const host = hostFactory.build();

axiosMock.onDelete(`/hosts/${host.id}`).reply(404, {});

const dispatched = await recordSaga(deregisterHost, {
payload: host,
});

expect(dispatched).toEqual([
setHostDeregistering(host),
notify({
text: `Error deregistering host ${host.hostname}.`,
icon: '❌',
}),
setHostNotDeregistering(host),
]);
});
});
3 changes: 2 additions & 1 deletion assets/js/state/sagas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import { watchAcceptEula } from '@state/sagas/eula';
import { watchCatalogUpdate } from '@state/sagas/catalog';
import { watchSapSystem } from '@state/sagas/sapSystems';
import { watchDatabase } from '@state/sagas/databases';
import { watchHostDeregistered } from '@state/sagas/hosts';
import { watchHostDeregistered, watchDeregisterHost } from '@state/sagas/hosts';
import { watchClusterDeregistered } from '@state/sagas/clusters';

import {
Expand Down Expand Up @@ -410,5 +410,6 @@ export default function* rootSaga() {
watchAcceptEula(),
refreshHealthSummaryOnComnponentsHealthChange(),
watchPerformLogin(),
watchDeregisterHost(),
]);
}

0 comments on commit 9f4d1d9

Please sign in to comment.