diff --git a/webpack/assets/javascripts/react_app/components/Layout/components/ImpersonateIcon/ImpersonateIcon.test.js b/webpack/assets/javascripts/react_app/components/Layout/components/ImpersonateIcon/ImpersonateIcon.test.js index 2f79b894c5..ad5791b9cc 100644 --- a/webpack/assets/javascripts/react_app/components/Layout/components/ImpersonateIcon/ImpersonateIcon.test.js +++ b/webpack/assets/javascripts/react_app/components/Layout/components/ImpersonateIcon/ImpersonateIcon.test.js @@ -1,13 +1,54 @@ -import { testComponentSnapshotsWithFixtures } from 'foremanReact/common/testHelpers'; +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import '@testing-library/jest-dom'; import ImpersonateIcon from './ImpersonateIcon'; -const fixtures = { - 'should render': { - stopImpersonationUrl: '/stop_impersonation', - stopImpersonating: () => {}, - }, -}; +// Render the tooltip's child directly; the Popper's async updates otherwise +// fire after the click handler and trip the strict console.error setup. +jest.mock('@patternfly/react-core', () => ({ + ...jest.requireActual('@patternfly/react-core'), + Tooltip: ({ children }) => children, +})); -describe('ImpersonateIcon', () => - testComponentSnapshotsWithFixtures(ImpersonateIcon, fixtures)); +const modalText = + 'You are about to stop impersonating other user. Are you sure?'; + +const renderIcon = (stopImpersonating = () => {}) => + render( + + ); + +describe('ImpersonateIcon', () => { + it('renders the icon with the confirmation modal closed', () => { + const { container } = renderIcon(); + + expect(container.querySelector('span.nav-item-iconic')).toBeInTheDocument(); + expect(screen.queryByText(modalText)).not.toBeInTheDocument(); + }); + + it('opens the confirmation modal when the icon is clicked', async () => { + const { container } = renderIcon(); + + await userEvent.click(container.querySelector('span.nav-item-iconic')); + + expect(screen.getByText(modalText)).toBeInTheDocument(); + expect(screen.getByRole('button', { name: 'Confirm' })).toBeInTheDocument(); + expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument(); + }); + + it('calls stopImpersonating with the url when confirmed', async () => { + const stopImpersonating = jest.fn(); + const { container } = renderIcon(stopImpersonating); + + await userEvent.click(container.querySelector('span.nav-item-iconic')); + await userEvent.click(screen.getByRole('button', { name: 'Confirm' })); + + expect(stopImpersonating).toHaveBeenCalledTimes(1); + expect(stopImpersonating).toHaveBeenCalledWith('/stop_impersonation'); + }); +}); diff --git a/webpack/assets/javascripts/react_app/components/Layout/components/ImpersonateIcon/__snapshots__/ImpersonateIcon.test.js.snap b/webpack/assets/javascripts/react_app/components/Layout/components/ImpersonateIcon/__snapshots__/ImpersonateIcon.test.js.snap deleted file mode 100644 index f503311b4e..0000000000 --- a/webpack/assets/javascripts/react_app/components/Layout/components/ImpersonateIcon/__snapshots__/ImpersonateIcon.test.js.snap +++ /dev/null @@ -1,59 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`ImpersonateIcon should render 1`] = ` - - - - - - - - - - Confirm - , - , - ] - } - appendTo={[Function]} - aria-describedby="" - aria-label="" - aria-labelledby="" - className="" - hasNoBodyWrapper={false} - isOpen={false} - onClose={[Function]} - ouiaId="impersonate-modal" - ouiaSafe={true} - position="top" - showClose={true} - title="Confirm Action" - titleIconVariant={null} - titleLabel="" - variant="small" - > - You are about to stop impersonating other user. Are you sure? - - -`;