|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { IntlProvider } from '@edx/frontend-platform/i18n'; |
| 4 | +import { useNavigate } from 'react-router-dom'; |
| 5 | +import { useLibraryAuthZ } from '@src/authz-module/libraries-manager/context'; |
| 6 | +import { useTeamMembers } from '@src/authz-module/data/hooks'; |
| 7 | +import { |
| 8 | + EmailCell, |
| 9 | + NameCell, |
| 10 | + ActionCell, |
| 11 | + RolesCell, |
| 12 | +} from './Cells'; |
| 13 | + |
| 14 | +jest.mock('react-router-dom', () => ({ |
| 15 | + useNavigate: jest.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +jest.mock('@src/authz-module/libraries-manager/context', () => ({ |
| 19 | + useLibraryAuthZ: jest.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +jest.mock('@src/authz-module/data/hooks', () => ({ |
| 23 | + useTeamMembers: jest.fn(), |
| 24 | +})); |
| 25 | + |
| 26 | +jest.mock('../hooks/useQuerySettings', () => ({ |
| 27 | + useQuerySettings: jest.fn(() => ({ |
| 28 | + querySettings: { page: 1, limit: 10 }, |
| 29 | + })), |
| 30 | +})); |
| 31 | + |
| 32 | +const mockNavigate = useNavigate as jest.Mock; |
| 33 | +const mockUseLibraryAuthZ = useLibraryAuthZ as jest.Mock; |
| 34 | +const mockUseTeamMembers = useTeamMembers as jest.Mock; |
| 35 | + |
| 36 | +const renderWithIntl = (component: React.ReactElement) => render( |
| 37 | + <IntlProvider locale="en" messages={{}}> |
| 38 | + {component} |
| 39 | + </IntlProvider>, |
| 40 | +); |
| 41 | + |
| 42 | +const mockTeamMember = { |
| 43 | + username: 'john.doe', |
| 44 | + fullName: 'John Doe', |
| 45 | + |
| 46 | + roles: ['instructor', 'author'], |
| 47 | + createdAt: '2023-01-01T00:00:00Z', |
| 48 | +}; |
| 49 | + |
| 50 | +const mockSkeletonMember = { |
| 51 | + username: 'skeleton', |
| 52 | + fullName: '', |
| 53 | + email: '', |
| 54 | + roles: [], |
| 55 | + createdAt: '', |
| 56 | +}; |
| 57 | + |
| 58 | +const mockCellProps = { |
| 59 | + row: { original: mockTeamMember }, |
| 60 | +}; |
| 61 | + |
| 62 | +const mockSkeletonCellProps = { |
| 63 | + row: { original: mockSkeletonMember }, |
| 64 | +}; |
| 65 | + |
| 66 | +describe('Table Cells', () => { |
| 67 | + beforeEach(() => { |
| 68 | + jest.clearAllMocks(); |
| 69 | + mockUseLibraryAuthZ.mockReturnValue({ |
| 70 | + username: 'current.user', |
| 71 | + libraryId: 'lib123', |
| 72 | + canManageTeam: true, |
| 73 | + roles: [ |
| 74 | + { role: 'instructor', name: 'Instructor' }, |
| 75 | + { role: 'author', name: 'Author' }, |
| 76 | + ], |
| 77 | + }); |
| 78 | + mockUseTeamMembers.mockReturnValue({ isLoading: false }); |
| 79 | + mockNavigate.mockReturnValue(jest.fn()); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('EmailCell', () => { |
| 83 | + it('displays user email', () => { |
| 84 | + renderWithIntl(<EmailCell {...mockCellProps} />); |
| 85 | + expect(screen.getByText('[email protected]')).toBeInTheDocument(); |
| 86 | + }); |
| 87 | + it('shows loading skeleton for loading state', () => { |
| 88 | + renderWithIntl(<EmailCell {...mockSkeletonCellProps} />); |
| 89 | + expect(document.querySelector('.react-loading-skeleton')).toBeInTheDocument(); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('NameCell', () => { |
| 94 | + it('displays username for regular user', () => { |
| 95 | + renderWithIntl(<NameCell {...mockCellProps} />); |
| 96 | + expect(screen.getByText('john.doe')).toBeInTheDocument(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('displays current user indicator for logged in user', () => { |
| 100 | + const currentUserProps = { |
| 101 | + ...mockCellProps, |
| 102 | + row: { original: { ...mockTeamMember, username: 'current.user' } }, |
| 103 | + }; |
| 104 | + renderWithIntl(<NameCell {...currentUserProps} />); |
| 105 | + expect(screen.getByText('current.user')).toBeInTheDocument(); |
| 106 | + expect(screen.getByText('current.user').parentElement).toBeInTheDocument(); |
| 107 | + }); |
| 108 | + it('shows loading skeleton for loading state', () => { |
| 109 | + renderWithIntl(<NameCell {...mockSkeletonCellProps} />); |
| 110 | + expect(document.querySelector('.react-loading-skeleton')).toBeInTheDocument(); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('ActionCell', () => { |
| 115 | + it('renders edit button for manageable team member', () => { |
| 116 | + renderWithIntl(<ActionCell {...mockCellProps} />); |
| 117 | + const editButton = screen.getByRole('button'); |
| 118 | + expect(editButton).toBeInTheDocument(); |
| 119 | + expect(document.querySelector('.pgn__icon')).toBeInTheDocument(); |
| 120 | + expect(document.querySelector('svg')).toBeInTheDocument(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('navigates to user page when edit button is clicked', async () => { |
| 124 | + const user = userEvent.setup(); |
| 125 | + const navigateMock = jest.fn(); |
| 126 | + mockNavigate.mockReturnValue(navigateMock); |
| 127 | + renderWithIntl(<ActionCell {...mockCellProps} />); |
| 128 | + const editButton = screen.getByRole('button'); |
| 129 | + await user.click(editButton); |
| 130 | + expect(navigateMock).toHaveBeenCalledWith('/authz/libraries/lib123/john.doe'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('does not render edit button for current user', () => { |
| 134 | + const currentUserProps = { |
| 135 | + ...mockCellProps, |
| 136 | + row: { original: { ...mockTeamMember, username: 'current.user' } }, |
| 137 | + }; |
| 138 | + renderWithIntl(<ActionCell {...currentUserProps} />); |
| 139 | + expect(screen.queryByRole('button')).not.toBeInTheDocument(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('does not render edit button when user cannot manage team', () => { |
| 143 | + mockUseLibraryAuthZ.mockReturnValue({ |
| 144 | + username: 'current.user', |
| 145 | + libraryId: 'lib123', |
| 146 | + canManageTeam: false, |
| 147 | + roles: [], |
| 148 | + }); |
| 149 | + renderWithIntl(<ActionCell {...mockCellProps} />); |
| 150 | + expect(screen.queryByRole('button')).not.toBeInTheDocument(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('does not render edit button during loading', () => { |
| 154 | + mockUseTeamMembers.mockReturnValue({ isLoading: true }); |
| 155 | + |
| 156 | + renderWithIntl(<ActionCell {...mockCellProps} />); |
| 157 | + expect(screen.queryByRole('button')).not.toBeInTheDocument(); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe('RolesCell', () => { |
| 162 | + it('displays role chips for user roles', () => { |
| 163 | + renderWithIntl(<RolesCell {...mockCellProps} />); |
| 164 | + expect(screen.getByText('Instructor')).toBeInTheDocument(); |
| 165 | + expect(screen.getByText('Author')).toBeInTheDocument(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('shows loading skeleton for loading state', () => { |
| 169 | + renderWithIntl(<RolesCell {...mockSkeletonCellProps} />); |
| 170 | + expect(document.querySelector('.react-loading-skeleton')).toBeInTheDocument(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('handles user with no roles', () => { |
| 174 | + const noRolesProps = { |
| 175 | + ...mockCellProps, |
| 176 | + row: { original: { ...mockTeamMember, roles: [] } }, |
| 177 | + }; |
| 178 | + renderWithIntl(<RolesCell {...noRolesProps} />); |
| 179 | + expect(screen.queryByText('Instructor')).not.toBeInTheDocument(); |
| 180 | + expect(screen.queryByText('Author')).not.toBeInTheDocument(); |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
0 commit comments