|
| 1 | +import { screen } from '@testing-library/react'; |
| 2 | +import { renderWrapper } from '@src/setupTest'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import RoleCard from '.'; |
| 5 | + |
| 6 | +jest.mock('@openedx/paragon/icons', () => ({ |
| 7 | + Delete: () => <svg data-testid="delete-icon" />, |
| 8 | + Person: () => <svg data-testid="person-icon" />, |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('./constants', () => ({ |
| 12 | + actionsDictionary: { |
| 13 | + view: () => <svg data-testid="view-icon" />, |
| 14 | + manage: () => <svg data-testid="manage-icon" />, |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +describe('RoleCard', () => { |
| 19 | + const defaultProps = { |
| 20 | + title: 'Admin', |
| 21 | + objectName: 'Test Library', |
| 22 | + description: 'Can manage everything', |
| 23 | + showDelete: true, |
| 24 | + userCounter: 2, |
| 25 | + permissions: [ |
| 26 | + { |
| 27 | + key: 'library', |
| 28 | + label: 'Library Resource', |
| 29 | + actions: [ |
| 30 | + { key: 'view', label: 'View' }, |
| 31 | + { key: 'manage', label: 'Manage', disabled: true }, |
| 32 | + ], |
| 33 | + }, |
| 34 | + ], |
| 35 | + }; |
| 36 | + |
| 37 | + it('renders all role card sections correctly', async () => { |
| 38 | + const user = userEvent.setup(); |
| 39 | + renderWrapper(<RoleCard {...defaultProps} />); |
| 40 | + |
| 41 | + // Title |
| 42 | + expect(screen.getByText('Admin')).toBeInTheDocument(); |
| 43 | + |
| 44 | + // User counter with icon |
| 45 | + expect(screen.getByText('2')).toBeInTheDocument(); |
| 46 | + expect(screen.getByTestId('person-icon')).toBeInTheDocument(); |
| 47 | + |
| 48 | + // Subtitle (object name) |
| 49 | + expect(screen.getByText('Test Library')).toBeInTheDocument(); |
| 50 | + |
| 51 | + // Description |
| 52 | + expect(screen.getByText('Can manage everything')).toBeInTheDocument(); |
| 53 | + |
| 54 | + // Delete button |
| 55 | + expect(screen.getByRole('button', { name: /delete role action/i })).toBeInTheDocument(); |
| 56 | + |
| 57 | + // Collapsible title |
| 58 | + expect(screen.getByText('Permissions')).toBeInTheDocument(); |
| 59 | + |
| 60 | + await user.click(screen.getByText('Permissions')); |
| 61 | + |
| 62 | + // Resource label |
| 63 | + expect(screen.getByText('Library Resource')).toBeInTheDocument(); |
| 64 | + |
| 65 | + // Action chips |
| 66 | + expect(screen.getByText('View')).toBeInTheDocument(); |
| 67 | + expect(screen.getByText('Manage')).toBeInTheDocument(); |
| 68 | + |
| 69 | + // Action icons |
| 70 | + expect(screen.getByTestId('view-icon')).toBeInTheDocument(); |
| 71 | + expect(screen.getByTestId('manage-icon')).toBeInTheDocument(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('does not show delete button when showDelete is false', () => { |
| 75 | + renderWrapper(<RoleCard {...defaultProps} showDelete={false} />); |
| 76 | + expect(screen.queryByRole('button', { name: /delete role action/i })).not.toBeInTheDocument(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('handles no userCounter gracefully', () => { |
| 80 | + renderWrapper(<RoleCard {...defaultProps} userCounter={null} />); |
| 81 | + expect(screen.queryByTestId('person-icon')).not.toBeInTheDocument(); |
| 82 | + expect(screen.queryByText('2')).not.toBeInTheDocument(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('handles empty permissions gracefully', () => { |
| 86 | + renderWrapper(<RoleCard {...defaultProps} permissions={[]} />); |
| 87 | + expect(screen.queryByText('Library Resource')).not.toBeInTheDocument(); |
| 88 | + }); |
| 89 | +}); |
0 commit comments