Skip to content

Commit

Permalink
Add automated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EMaksy committed Aug 25, 2023
1 parent ee1ab37 commit 47d5510
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 1 deletion.
43 changes: 43 additions & 0 deletions assets/js/components/HostDetails/HostDetails.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import userEvent from '@testing-library/user-event';
import 'intersection-observer';
import '@testing-library/jest-dom';
import MockAdapter from 'axios-mock-adapter';
import { faker } from '@faker-js/faker';

import {
withState,
Expand Down Expand Up @@ -49,6 +50,48 @@ describe('HostDetails component', () => {
).toBeVisible();
});

it('should disable start execution button when checks are not selected', () => {
const host = hostFactory.build({ selected_checks: [] });
const { id: hostID } = host;
const state = {
...defaultInitialState,
hostsList: {
hosts: [host],
},
};
const [StatefulHostDetails] = withState(<HostDetails />, state);

renderWithRouterMatch(StatefulHostDetails, {
path: '/hosts/:hostID',
route: `/hosts/${hostID}`,
});

const startExecutionButton = screen.getByText('Start Execution');
expect(startExecutionButton).toBeDisabled();
});

it('should enable start execution button when checks are selected', () => {
const host = hostFactory.build({
selected_checks: [faker.animal.bear(), faker.animal.bear()],
});
const { id: hostID } = host;
const state = {
...defaultInitialState,
hostsList: {
hosts: [host],
},
};
const [StatefulHostDetails] = withState(<HostDetails />, state);

renderWithRouterMatch(StatefulHostDetails, {
path: '/hosts/:hostID',
route: `/hosts/${hostID}`,
});

const startExecutionButton = screen.getByText('Start Execution');
expect(startExecutionButton).toBeEnabled();
});

describe('agent version', () => {
it('should not show any warning message if the agent version is correct', () => {
const hosts = hostFactory.buildList(1, { agent_version: '2.0.0' });
Expand Down
40 changes: 40 additions & 0 deletions assets/js/components/HostDetails/HostSettingsPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,44 @@ describe('HostSettingsPage component', () => {

expect(screen.getByText('Save Checks Selection')).toBeVisible();
});

it('should render HostSettingsPage with a disabled start execution button, as no checks are selected', () => {
const hosts = hostFactory.buildList(2, {
provider: 'azure',
selected_checks: [],
});
const state = {
...defaultInitialState,
hostsList: { hosts },
};
const { id: hostID } = hosts[1];

const [StatefulHostSettingsPage] = withState(<HostSettingsPage />, state);

renderWithRouterMatch(StatefulHostSettingsPage, {
path: 'hosts/:hostID/settings',
route: `/hosts/${hostID}/settings`,
});
const startExecutionButton = screen.getByText('Start Execution');
expect(startExecutionButton).toBeDisabled();
});
it('should render HostSettingsPage with a disabled start execution button, as checks are selected', () => {
const hosts = hostFactory.buildList(2, {
provider: 'azure',
selected_checks: [faker.animal.bear(), faker.animal.bear()],
});
const state = {
...defaultInitialState,
hostsList: { hosts },
};
const { id: hostID } = hosts[1];
const [StatefulHostSettingsPage] = withState(<HostSettingsPage />, state);

renderWithRouterMatch(StatefulHostSettingsPage, {
path: 'hosts/:hostID/settings',
route: `/hosts/${hostID}/settings`,
});
const startExecutionButton = screen.getByText('Start Execution');
expect(startExecutionButton).toBeEnabled();
});
});
29 changes: 29 additions & 0 deletions assets/js/state/lastExecutions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { faker } from '@faker-js/faker';

import lastExecutionsReducer, {
setExecutionRequested,
setHostChecksExecutionRequested,
setLastExecutionLoading,
setLastExecutionEmpty,
setLastExecutionError,
setLastExecution,
setExecutionStarted,
REQUESTED_EXECUTION_STATE,
} from './lastExecutions';

describe('lastExecutions reducer', () => {
Expand Down Expand Up @@ -183,4 +185,31 @@ describe('lastExecutions reducer', () => {

expect(lastExecutionsReducer(initialState, action)).toEqual(expectedState);
});

it('should set requested state on host execution', () => {
const initialState = {};
const hostID = faker.datatype.uuid();
const checks = [faker.datatype.uuid(), faker.datatype.uuid()];

const agentID = faker.datatype.uuid();

const host = {
agent_id: agentID,
id: hostID,
};
const data = { host, checks };
const expectedState = {
[hostID]: {
data: {
status: REQUESTED_EXECUTION_STATE,
targets: [{ agent_id: host, checks }],
},
loading: false,
error: null,
},
};

const action = setHostChecksExecutionRequested(data);
expect(lastExecutionsReducer(initialState, action)).toEqual(expectedState);
});
});
54 changes: 53 additions & 1 deletion assets/js/state/sagas/lastExecutions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ import {
setLastExecutionEmpty,
setLastExecutionError,
setExecutionRequested,
setHostChecksExecutionRequested,
} from '@state/lastExecutions';
import { notify } from '@state/actions/notifications';
import { updateLastExecution, requestExecution } from './lastExecutions';
import { hostFactory } from '@lib/test-utils/factories';

import {
updateLastExecution,
requestExecution,
requestHostExecution,
} from './lastExecutions';

const axiosMock = new MockAdapter(networkClient);
const lastExecutionURL = (groupID) =>
Expand All @@ -21,6 +28,9 @@ const lastExecutionURL = (groupID) =>
const triggerChecksExecutionURL = (clusterId) =>
`/clusters/${clusterId}/checks/request_execution`;

const hostTriggerChecksExecutionURL = (hostID) =>
`/hosts/${hostID}/checks/request_execution`;

describe('lastExecutions saga', () => {
beforeEach(() => {
axiosMock.reset();
Expand Down Expand Up @@ -140,4 +150,46 @@ describe('lastExecutions saga', () => {
})
);
});

it('should set the last host execution to requested state', async () => {
const host = hostFactory.build();
const { id: hostID, hostname: hostName } = host;
const checks = [faker.datatype.uuid(), faker.datatype.uuid()];

axiosMock.onPost(hostTriggerChecksExecutionURL(hostID)).reply(202, {});
const payload = { checks, host };

const dispatched = await recordSaga(requestHostExecution, {
payload,
});
expect(dispatched).toContainEqual(setHostChecksExecutionRequested(payload));
expect(dispatched).toContainEqual(
notify({
text: `Checks execution requested, host: ${hostName}`,
icon: '🐰',
})
);
});

it('should not set the host last execution to requested state on failure', async () => {
const host = hostFactory.build();
const { id: hostID, hostname: hostName } = host;
const checks = [faker.datatype.uuid(), faker.datatype.uuid()];

axiosMock.onPost(hostTriggerChecksExecutionURL(hostID)).reply(400, {});

const payload = { checks, host };
const dispatched = await recordSaga(requestHostExecution, {
payload,
});
expect(dispatched).not.toContainEqual(
setHostChecksExecutionRequested(payload)
);
expect(dispatched).toContainEqual(
notify({
text: `Unable to start execution for host: ${hostName}`,
icon: '❌',
})
);
});
});

0 comments on commit 47d5510

Please sign in to comment.