From f45b52dc5a10a0e7f1c506f860317eaba44ea76b Mon Sep 17 00:00:00 2001 From: Georgii Karataev Date: Sun, 17 Dec 2023 17:57:01 +0100 Subject: [PATCH] test: Add tests for remove hosts from group modal --- .../Modals/RemoveHostsFromGroupModal.js | 4 +- .../RemoveHostsFromGroupModal.test.js | 137 ++++++++++++++++++ 2 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 src/components/InventoryGroups/Modals/__tests__/RemoveHostsFromGroupModal.test.js diff --git a/src/components/InventoryGroups/Modals/RemoveHostsFromGroupModal.js b/src/components/InventoryGroups/Modals/RemoveHostsFromGroupModal.js index 17ccf827f..35662bfa6 100644 --- a/src/components/InventoryGroups/Modals/RemoveHostsFromGroupModal.js +++ b/src/components/InventoryGroups/Modals/RemoveHostsFromGroupModal.js @@ -18,13 +18,13 @@ const schema = (hosts) => { name: 'warning-message', label: hostsInGroup.length === 1 ? ( - + {hostsInGroup[0].display_name} will no longer be part of {groupName} and its configuration will be impacted. ) : ( - + {hostsInGroup.length} systems will no longer be part of {groupName} and their configuration will be impacted. diff --git a/src/components/InventoryGroups/Modals/__tests__/RemoveHostsFromGroupModal.test.js b/src/components/InventoryGroups/Modals/__tests__/RemoveHostsFromGroupModal.test.js new file mode 100644 index 000000000..2aff6e8ee --- /dev/null +++ b/src/components/InventoryGroups/Modals/__tests__/RemoveHostsFromGroupModal.test.js @@ -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( + + ); + + 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( + + ); + + 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( + + ); + + await userEvent.click( + screen.getByRole('button', { + name: /close/i, + }) + ); + + expect(setIsModalOpen).toBeCalled(); + }); + + it('can cancel the deletion', async () => { + render( + + ); + + await userEvent.click( + screen.getByRole('button', { + name: /cancel/i, + }) + ); + + expect(setIsModalOpen).toBeCalled(); + }); + + it('can remove single group', async () => { + render( + + ); + + await userEvent.click( + screen.getByRole('button', { + name: /remove/i, + }) + ); + + expect(removeHostsFromGroup).toBeCalledWith('g1', ['h1']); + }); + + it('can remove more groups', async () => { + render( + + ); + + await userEvent.click( + screen.getByRole('button', { + name: /remove/i, + }) + ); + + expect(removeHostsFromGroup).toBeCalledWith('g1', ['h1', 'h2']); + }); +});