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

test: Add tests for remove hosts from group modal #2126

Merged
merged 2 commits into from
Jan 2, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const schema = (hosts) => {
name: 'warning-message',
label:
hostsInGroup.length === 1 ? (
<Text>
<Text data-testid="desc">
<strong>{hostsInGroup[0].display_name}</strong> will no longer be
part of <strong>{groupName}</strong> and its configuration will be
impacted.
</Text>
) : (
<Text>
<Text data-testid="desc">
<strong>{hostsInGroup.length}</strong> systems will no longer be
part of <strong>{groupName}</strong> and their configuration will
be impacted.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { removeHostsFromGroup } from '../../utils/api';
import RemoveHostsFromGroupModal from '../RemoveHostsFromGroupModal';

jest.mock('react-redux');
jest.mock('../../utils/api');

describe('RemoveHostsFromGroupModal', () => {
const setIsModalOpen = jest.fn();

afterEach(() => {
jest.clearAllMocks();
});

it('shows correct text, single group', () => {
render(
<RemoveHostsFromGroupModal
isModalOpen
setIsModalOpen={setIsModalOpen}
modalState={[{ display_name: 'host-1', groups: [{ name: 'group-1' }] }]}
/>
);

expect(screen.getByTestId('desc')).toHaveTextContent(
'host-1 will no longer be part of group-1 and its configuration will be impacted.'
);
});

it('shows correct text, more groups', () => {
render(
<RemoveHostsFromGroupModal
isModalOpen
setIsModalOpen={setIsModalOpen}
modalState={[
{ display_name: 'host-1', groups: [{ name: 'group-1' }] },
{ display_name: 'host-2', groups: [{ name: 'group-1' }] },
]}
/>
);

expect(screen.getByTestId('desc')).toHaveTextContent(
'2 systems will no longer be part of group-1 and their configuration will be impacted.'
);
});

it('can close the modal', async () => {
render(
<RemoveHostsFromGroupModal
isModalOpen
setIsModalOpen={setIsModalOpen}
modalState={[{ display_name: 'host-1', groups: [{ name: 'group-1' }] }]}
/>
);

await userEvent.click(
screen.getByRole('button', {
name: /close/i,
})
);

expect(setIsModalOpen).toBeCalled();
});

it('can cancel the deletion', async () => {
render(
<RemoveHostsFromGroupModal
isModalOpen
setIsModalOpen={setIsModalOpen}
modalState={[{ display_name: 'host-1', groups: [{ name: 'group-1' }] }]}
/>
);

await userEvent.click(
screen.getByRole('button', {
name: /cancel/i,
})
);

expect(setIsModalOpen).toBeCalled();
});

it('can remove single group', async () => {
render(
<RemoveHostsFromGroupModal
isModalOpen
setIsModalOpen={setIsModalOpen}
modalState={[
{
display_name: 'host-1',
id: 'h1',
groups: [{ name: 'group-1', id: 'g1' }],
},
]}
/>
);

await userEvent.click(
screen.getByRole('button', {
name: /remove/i,
})
);

expect(removeHostsFromGroup).toBeCalledWith('g1', ['h1']);
});

it('can remove more groups', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('can remove more groups', async () => {
it('can remove multiple groups', async () => {

A tiny nitpick :)

render(
<RemoveHostsFromGroupModal
isModalOpen
setIsModalOpen={setIsModalOpen}
modalState={[
{
display_name: 'host-1',
id: 'h1',
groups: [{ name: 'group-1', id: 'g1' }],
},
{
display_name: 'host-2',
id: 'h2',
groups: [{ name: 'group-1', id: 'g1' }],
},
]}
/>
);

await userEvent.click(
screen.getByRole('button', {
name: /remove/i,
})
);

expect(removeHostsFromGroup).toBeCalledWith('g1', ['h1', 'h2']);
});
});