diff --git a/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/HeaderToolbar.test.js b/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/HeaderToolbar.test.js index 3896865c87..91dac4033d 100644 --- a/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/HeaderToolbar.test.js +++ b/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/HeaderToolbar.test.js @@ -1,19 +1,68 @@ -import { testComponentSnapshotsWithFixtures } from 'foremanReact/common/testHelpers'; -import { hasTaxonomiesMock } from '../../Layout.fixtures'; -import { noop } from '../../../../common/helpers'; +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import { hasTaxonomiesMock } from '../../Layout.fixtures'; import HeaderToolbar from './HeaderToolbar'; -const fixtures = { - 'render HeaderToolbar': { - ...hasTaxonomiesMock.data, - currentLocation: hasTaxonomiesMock.currentLocation, - currentOrganization: hasTaxonomiesMock.currentOrganization, - isLoading: false, - }, +// Stub the connected/async children so the toolbar's own composition can be +// tested without a redux store or the notifications API. +jest.mock('../TaxonomySwitcher/TaxonomySwitcher', () => () => ( +
+)); +jest.mock('./UserDropdowns', () => ({ user }) => ( +
{user.current_user.name}
+)); +jest.mock('../ImpersonateIcon', () => ({ stopImpersonationUrl }) => ( +
{stopImpersonationUrl}
+)); +jest.mock( + '../../../notifications/components/NotificationIcon', + () => () =>
+); +jest.mock('../../../notifications', () => () => ( +
+)); +jest.mock('../../../notifications/NotificationsContext', () => ({ + NotificationsContextWrapper: ({ children }) =>
{children}
, +})); + +const props = { + ...hasTaxonomiesMock.data, + currentLocation: hasTaxonomiesMock.currentLocation, + currentOrganization: hasTaxonomiesMock.currentOrganization, + isLoading: false, }; describe('HeaderToolbar', () => { - describe('rendering', () => - testComponentSnapshotsWithFixtures(HeaderToolbar, fixtures)); + it('render HeaderToolbar', () => { + render(); + + expect(screen.getByTestId('taxonomy-switcher')).toBeInTheDocument(); + expect(screen.getByTestId('notification-icon')).toBeInTheDocument(); + expect(screen.getByTestId('notifications')).toBeInTheDocument(); + + // user dropdown receives the current user + expect(screen.getByTestId('user-dropdowns')).toHaveTextContent( + 'Admin User' + ); + + // the user is impersonated, so the impersonate icon renders with its url + expect(screen.getByTestId('impersonate-icon')).toHaveTextContent( + '/users/stop_impersonation' + ); + }); + + it('hides the impersonate icon when not impersonated', () => { + render( + + ); + + expect(screen.queryByTestId('impersonate-icon')).not.toBeInTheDocument(); + // the rest of the toolbar still renders + expect(screen.getByTestId('user-dropdowns')).toBeInTheDocument(); + }); }); diff --git a/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/UserDropdowns.test.js b/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/UserDropdowns.test.js index a68f9a5ec7..90c3ec8d62 100644 --- a/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/UserDropdowns.test.js +++ b/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/UserDropdowns.test.js @@ -1,10 +1,20 @@ -import { testComponentSnapshotsWithFixtures } from '../../../../common/testHelpers'; +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import '@testing-library/jest-dom'; import UserDropdowns from './UserDropdowns'; import { userDropdownProps } from '../../Layout.fixtures'; -const fixtures = { - render: userDropdownProps, -}; -describe('UserDropdown', () => - testComponentSnapshotsWithFixtures(UserDropdowns, fixtures)); +describe('UserDropdown', () => { + it('render', () => { + render(); + + // the toggle shows the current user's name + expect(screen.getByText('G L')).toBeInTheDocument(); + + // the open menu shows the account item linking to its url + const accountItem = screen.getByRole('menuitem', { name: 'My Account' }); + expect(accountItem).toBeInTheDocument(); + expect(accountItem).toHaveAttribute('href', '/'); + }); +}); diff --git a/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/__snapshots__/HeaderToolbar.test.js.snap b/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/__snapshots__/HeaderToolbar.test.js.snap deleted file mode 100644 index 056271262b..0000000000 --- a/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/__snapshots__/HeaderToolbar.test.js.snap +++ /dev/null @@ -1,123 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`HeaderToolbar rendering render HeaderToolbar 1`] = ` - - - - - - - - - - - - - - - - - - - - - -`; diff --git a/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/__snapshots__/UserDropdowns.test.js.snap b/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/__snapshots__/UserDropdowns.test.js.snap deleted file mode 100644 index 433cc648bc..0000000000 --- a/webpack/assets/javascripts/react_app/components/Layout/components/Toolbar/__snapshots__/UserDropdowns.test.js.snap +++ /dev/null @@ -1,40 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`UserDropdown render 1`] = ` - - My Account - , - , - ] - } - isOpen={true} - isPlain={true} - notification_url="/" - onSelect={[Function]} - ouiaId="user-info-dropdown" - position="right" - toggle={ - - - - - G L - - } -/> -`;