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