|
| 1 | +import { screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | + |
| 4 | +import { renderWithProviders } from '../../__helpers__/test-utils'; |
| 5 | +import { mockGitHubCloudAccount } from '../../__mocks__/account-mocks'; |
| 6 | + |
| 7 | +import * as links from '../../utils/system/links'; |
| 8 | +import { AccountHeader, type AccountHeaderProps } from './AccountHeader'; |
| 9 | + |
| 10 | +describe('renderer/components/notifications/AccountHeader.tsx', () => { |
| 11 | + const props: AccountHeaderProps = { |
| 12 | + account: mockGitHubCloudAccount, |
| 13 | + error: null, |
| 14 | + notificationCount: 3, |
| 15 | + isCollapsed: false, |
| 16 | + onToggle: vi.fn(), |
| 17 | + }; |
| 18 | + |
| 19 | + it('renders the managed GitHub account identity', () => { |
| 20 | + renderWithProviders( |
| 21 | + <AccountHeader |
| 22 | + {...props} |
| 23 | + account={{ |
| 24 | + ...mockGitHubCloudAccount, |
| 25 | + user: { |
| 26 | + ...mockGitHubCloudAccount.user!, |
| 27 | + login: 'octocat_gitify', |
| 28 | + name: 'Mona Lisa Octocat', |
| 29 | + }, |
| 30 | + }} |
| 31 | + />, |
| 32 | + ); |
| 33 | + |
| 34 | + expect(screen.getByText('octocat_gitify')).toBeInTheDocument(); |
| 35 | + expect(screen.getByAltText('octocat_gitify')).toBeInTheDocument(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should open profile when clicked', async () => { |
| 39 | + const openAccountProfileSpy = vi.spyOn(links, 'openAccountProfile').mockImplementation(vi.fn()); |
| 40 | + const onToggle = vi.fn(); |
| 41 | + |
| 42 | + renderWithProviders(<AccountHeader {...props} onToggle={onToggle} />); |
| 43 | + |
| 44 | + await userEvent.click(screen.getByTestId('account-profile')); |
| 45 | + |
| 46 | + expect(openAccountProfileSpy).toHaveBeenCalledWith(mockGitHubCloudAccount); |
| 47 | + // The header's own click toggles collapse; the profile button must not. |
| 48 | + expect(onToggle).not.toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should open my issues when clicked', async () => { |
| 52 | + const openHostIssuesSpy = vi.spyOn(links, 'openHostIssues').mockImplementation(vi.fn()); |
| 53 | + |
| 54 | + renderWithProviders(<AccountHeader {...props} />); |
| 55 | + |
| 56 | + await userEvent.click(screen.getByTestId('account-issues')); |
| 57 | + |
| 58 | + expect(openHostIssuesSpy).toHaveBeenCalledWith(mockGitHubCloudAccount); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should open my pull requests when clicked', async () => { |
| 62 | + const openHostPullsSpy = vi.spyOn(links, 'openHostPulls').mockImplementation(vi.fn()); |
| 63 | + |
| 64 | + renderWithProviders(<AccountHeader {...props} />); |
| 65 | + |
| 66 | + await userEvent.click(screen.getByTestId('account-pull-requests')); |
| 67 | + |
| 68 | + expect(openHostPullsSpy).toHaveBeenCalledWith(mockGitHubCloudAccount); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should request a collapse toggle when toggled', async () => { |
| 72 | + const onToggle = vi.fn(); |
| 73 | + |
| 74 | + renderWithProviders(<AccountHeader {...props} onToggle={onToggle} />); |
| 75 | + |
| 76 | + await userEvent.click(screen.getByTestId('account-toggle')); |
| 77 | + |
| 78 | + expect(onToggle).toHaveBeenCalledTimes(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should label the toggle by collapsed state', () => { |
| 82 | + const { unmount } = renderWithProviders(<AccountHeader {...props} />)!; |
| 83 | + |
| 84 | + expect(screen.getByTestId('account-toggle')).toHaveAttribute( |
| 85 | + 'title', |
| 86 | + 'Hide account notifications', |
| 87 | + ); |
| 88 | + |
| 89 | + unmount(); |
| 90 | + renderWithProviders(<AccountHeader {...props} isCollapsed />); |
| 91 | + |
| 92 | + expect(screen.getByTestId('account-toggle')).toHaveAttribute( |
| 93 | + 'title', |
| 94 | + 'Show account notifications', |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should render an error background when the account errored', () => { |
| 99 | + const tree = renderWithProviders( |
| 100 | + <AccountHeader |
| 101 | + {...props} |
| 102 | + error={{ title: 'Error title', descriptions: ['Error description'], emojis: ['🔥'] }} |
| 103 | + notificationCount={0} |
| 104 | + />, |
| 105 | + ); |
| 106 | + |
| 107 | + expect(tree!.container).toMatchSnapshot(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments