|
| 1 | +import { useParams } from 'react-router-dom'; |
| 2 | +import { screen } from '@testing-library/react'; |
| 3 | +import { renderWrapper } from '@src/setupTest'; |
| 4 | +import LibrariesUserManager from './LibrariesUserManager'; |
| 5 | +import { useLibraryAuthZ } from './context'; |
| 6 | +import { useLibrary, useTeamMembers } from '../data/hooks'; |
| 7 | + |
| 8 | +jest.mock('react-router-dom', () => ({ |
| 9 | + ...jest.requireActual('react-router-dom'), |
| 10 | + useParams: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('./context', () => ({ |
| 14 | + useLibraryAuthZ: jest.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +jest.mock('../data/hooks', () => ({ |
| 18 | + useLibrary: jest.fn(), |
| 19 | + useTeamMembers: jest.fn(), |
| 20 | +})); |
| 21 | +jest.mock('../components/RoleCard', () => ({ |
| 22 | + __esModule: true, |
| 23 | + default: ({ title, description }: { title: string, description: string }) => ( |
| 24 | + <div data-testid="role-card"> |
| 25 | + <div>{title}</div> |
| 26 | + <div>{description}</div> |
| 27 | + </div> |
| 28 | + ), |
| 29 | +})); |
| 30 | + |
| 31 | +describe('LibrariesUserManager', () => { |
| 32 | + beforeEach(() => { |
| 33 | + jest.clearAllMocks(); |
| 34 | + |
| 35 | + // Mock route params |
| 36 | + (useParams as jest.Mock).mockReturnValue({ username: 'testuser' }); |
| 37 | + |
| 38 | + // Mock library authz context |
| 39 | + (useLibraryAuthZ as jest.Mock).mockReturnValue({ |
| 40 | + libraryId: 'lib:123', |
| 41 | + permissions: [{ key: 'view' }, { key: 'reuse' }], |
| 42 | + roles: [ |
| 43 | + { |
| 44 | + role: 'admin', |
| 45 | + name: 'Admin', |
| 46 | + description: 'Administrator Role', |
| 47 | + permissions: ['view', 'reuse'], |
| 48 | + }, |
| 49 | + ], |
| 50 | + resources: [ |
| 51 | + { key: 'library', label: 'Library', description: '' }, |
| 52 | + ], |
| 53 | + }); |
| 54 | + |
| 55 | + // Mock library data |
| 56 | + (useLibrary as jest.Mock).mockReturnValue({ |
| 57 | + data: { |
| 58 | + title: 'Test Library', |
| 59 | + org: 'Test Org', |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + // Mock team members |
| 64 | + (useTeamMembers as jest.Mock).mockReturnValue({ |
| 65 | + data: [ |
| 66 | + { |
| 67 | + username: 'testuser', |
| 68 | + email: 'testuser@example.com', |
| 69 | + roles: ['admin'], |
| 70 | + }, |
| 71 | + ], |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('renders the user roles correctly', () => { |
| 76 | + renderWrapper(<LibrariesUserManager />); |
| 77 | + |
| 78 | + // Breadcrumb check |
| 79 | + expect(screen.getByText('Manage Access')).toBeInTheDocument(); |
| 80 | + expect(screen.getByText('Library Team Management')).toBeInTheDocument(); |
| 81 | + expect(screen.getByRole('listitem', { current: 'page' })).toHaveTextContent('testuser'); |
| 82 | + // Page title and subtitle |
| 83 | + expect(screen.getByRole('heading', { level: 1 })).toHaveTextContent('testuser'); |
| 84 | + expect(screen.getByRole('paragraph')).toHaveTextContent('testuser@example.com'); |
| 85 | + |
| 86 | + // RoleCard rendering |
| 87 | + expect(screen.getByTestId('role-card')).toHaveTextContent('Admin'); |
| 88 | + expect(screen.getByTestId('role-card')).toHaveTextContent('Administrator Role'); |
| 89 | + }); |
| 90 | +}); |
0 commit comments