|
| 1 | +import { screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { renderWithIntl } from '@src/testUtils'; |
| 4 | +import EditTeamMemberModal from './EditTeamMemberModal'; |
| 5 | +import messages from '../messages'; |
| 6 | +import { CourseTeamMember } from '../types'; |
| 7 | +import { useRoles } from '../data/apiHook'; |
| 8 | + |
| 9 | +// Mocks |
| 10 | +jest.mock('react-router-dom', () => ({ |
| 11 | + useParams: () => ({ courseId: 'course-v1:test+course+run' }), |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock('../data/apiHook', () => ({ |
| 15 | + useRoles: jest.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +const mockUser: CourseTeamMember = { |
| 19 | + username: 'test_user', |
| 20 | + fullName: 'Test User', |
| 21 | + email: 'test@example.com', |
| 22 | + roles: ['Staff', 'Admin'], |
| 23 | +}; |
| 24 | + |
| 25 | +const mockRoles = [ |
| 26 | + { id: 'instructor', name: 'Instructor' }, |
| 27 | + { id: 'staff', name: 'Staff' }, |
| 28 | + { id: 'admin', name: 'Admin' }, |
| 29 | + { id: 'beta_testers', name: 'Beta Testers' }, |
| 30 | + { id: 'data_researcher', name: 'Data Researcher' }, |
| 31 | +]; |
| 32 | + |
| 33 | +describe('EditTeamMemberModal', () => { |
| 34 | + const defaultProps = { |
| 35 | + isOpen: true, |
| 36 | + user: mockUser, |
| 37 | + onClose: jest.fn(), |
| 38 | + }; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + jest.clearAllMocks(); |
| 42 | + (useRoles as jest.Mock).mockReturnValue({ data: mockRoles }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('renders modal with correct title', () => { |
| 46 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} />); |
| 47 | + |
| 48 | + const expectedTitle = messages.editTeamTitle.defaultMessage.replace('{username}', mockUser.username); |
| 49 | + expect(screen.getByText(expectedTitle)).toBeInTheDocument(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('renders modal header and body correctly', () => { |
| 53 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} />); |
| 54 | + |
| 55 | + const expectedTitle = messages.editTeamTitle.defaultMessage.replace('{username}', mockUser.username); |
| 56 | + expect(screen.getByText(expectedTitle)).toBeInTheDocument(); |
| 57 | + |
| 58 | + // Check that header has correct styling |
| 59 | + const headerElement = screen.getByText(expectedTitle); |
| 60 | + expect(headerElement).toHaveClass('text-white'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('renders edit instructions with username', () => { |
| 64 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} />); |
| 65 | + |
| 66 | + const expectedInstructions = messages.editInstructions.defaultMessage.replace('{username}', mockUser.username); |
| 67 | + expect(screen.getByText(expectedInstructions)).toBeInTheDocument(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('renders current user roles as checkboxes', () => { |
| 71 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} />); |
| 72 | + |
| 73 | + mockUser.roles.forEach((role) => { |
| 74 | + expect(screen.getByRole('checkbox', { name: role })).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('renders add role label', () => { |
| 79 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} />); |
| 80 | + |
| 81 | + expect(screen.getByText(messages.addRole.defaultMessage)).toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('renders role selection dropdown with filtered roles', () => { |
| 85 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} />); |
| 86 | + |
| 87 | + const selectElement = screen.getByRole('combobox'); |
| 88 | + expect(selectElement).toBeInTheDocument(); |
| 89 | + |
| 90 | + // Verify placeholder is present |
| 91 | + expect(screen.getByText(messages.rolePlaceholder.defaultMessage)).toBeInTheDocument(); |
| 92 | + |
| 93 | + // Verify only roles not already assigned to user are available |
| 94 | + const availableRoles = mockRoles.filter(role => !mockUser.roles.includes(role.name)); |
| 95 | + availableRoles.forEach((role) => { |
| 96 | + expect(screen.getByRole('option', { name: role.name })).toBeInTheDocument(); |
| 97 | + }); |
| 98 | + |
| 99 | + // Verify user's current roles are not in the dropdown options |
| 100 | + mockUser.roles.forEach((roleName) => { |
| 101 | + const roleInMockData = mockRoles.find(role => role.name === roleName); |
| 102 | + if (roleInMockData) { |
| 103 | + expect(screen.queryByRole('option', { name: roleName })).not.toBeInTheDocument(); |
| 104 | + } |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('renders cancel and save buttons', () => { |
| 109 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} />); |
| 110 | + |
| 111 | + expect(screen.getByRole('button', { name: messages.cancelButton.defaultMessage })).toBeInTheDocument(); |
| 112 | + expect(screen.getByRole('button', { name: messages.saveButton.defaultMessage })).toBeInTheDocument(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('calls onClose when cancel button is clicked', async () => { |
| 116 | + const mockOnClose = jest.fn(); |
| 117 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} onClose={mockOnClose} />); |
| 118 | + |
| 119 | + const user = userEvent.setup(); |
| 120 | + const cancelButton = screen.getByRole('button', { name: messages.cancelButton.defaultMessage }); |
| 121 | + await user.click(cancelButton); |
| 122 | + |
| 123 | + expect(mockOnClose).toHaveBeenCalledTimes(1); |
| 124 | + }); |
| 125 | + |
| 126 | + it('calls onClose when save button is clicked', async () => { |
| 127 | + const mockOnClose = jest.fn(); |
| 128 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} onClose={mockOnClose} />); |
| 129 | + |
| 130 | + const user = userEvent.setup(); |
| 131 | + const saveButton = screen.getByRole('button', { name: messages.saveButton.defaultMessage }); |
| 132 | + await user.click(saveButton); |
| 133 | + |
| 134 | + expect(mockOnClose).toHaveBeenCalledTimes(1); |
| 135 | + }); |
| 136 | + |
| 137 | + it('does not render when isOpen is false', () => { |
| 138 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} isOpen={false} />); |
| 139 | + |
| 140 | + const expectedTitle = messages.editTeamTitle.defaultMessage.replace('{username}', mockUser.username); |
| 141 | + expect(screen.queryByText(expectedTitle)).not.toBeInTheDocument(); |
| 142 | + }); |
| 143 | + |
| 144 | + it('renders correctly when no roles data is available', () => { |
| 145 | + (useRoles as jest.Mock).mockReturnValue({ data: [] }); |
| 146 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} />); |
| 147 | + |
| 148 | + // Should only show placeholder in dropdown |
| 149 | + expect(screen.getByText(messages.rolePlaceholder.defaultMessage)).toBeInTheDocument(); |
| 150 | + |
| 151 | + // Select should be disabled when no roles are available |
| 152 | + const selectElement = screen.getByRole('combobox'); |
| 153 | + expect(selectElement).toBeDisabled(); |
| 154 | + |
| 155 | + // Should still show current user roles as checkboxes |
| 156 | + mockUser.roles.forEach((role) => { |
| 157 | + expect(screen.getByRole('checkbox', { name: role })).toBeInTheDocument(); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + it('renders correctly when useRoles returns undefined data', () => { |
| 162 | + (useRoles as jest.Mock).mockReturnValue({ data: undefined }); |
| 163 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} />); |
| 164 | + |
| 165 | + // Should only show placeholder in dropdown |
| 166 | + expect(screen.getByText(messages.rolePlaceholder.defaultMessage)).toBeInTheDocument(); |
| 167 | + |
| 168 | + // Select should be disabled when no roles are available |
| 169 | + const selectElement = screen.getByRole('combobox'); |
| 170 | + expect(selectElement).toBeDisabled(); |
| 171 | + |
| 172 | + // Should still show current user roles as checkboxes |
| 173 | + mockUser.roles.forEach((role) => { |
| 174 | + expect(screen.getByRole('checkbox', { name: role })).toBeInTheDocument(); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + it('handles user with all available roles assigned', () => { |
| 179 | + const userWithAllRoles = { |
| 180 | + ...mockUser, |
| 181 | + roles: mockRoles.map(role => role.name), |
| 182 | + }; |
| 183 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} user={userWithAllRoles} />); |
| 184 | + |
| 185 | + // Should show all roles as checkboxes |
| 186 | + userWithAllRoles.roles.forEach((role) => { |
| 187 | + expect(screen.getByRole('checkbox', { name: role })).toBeInTheDocument(); |
| 188 | + }); |
| 189 | + |
| 190 | + // Dropdown should only have placeholder since all roles are assigned |
| 191 | + expect(screen.getByText(messages.rolePlaceholder.defaultMessage)).toBeInTheDocument(); |
| 192 | + const options = screen.getAllByRole('option'); |
| 193 | + expect(options).toHaveLength(1); // Only placeholder option |
| 194 | + }); |
| 195 | + |
| 196 | + it('shows select role placeholder in dropdown', () => { |
| 197 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} />); |
| 198 | + |
| 199 | + const selectElement = screen.getByRole('combobox'); |
| 200 | + expect(selectElement).toHaveAttribute('placeholder', messages.rolePlaceholder.defaultMessage); |
| 201 | + }); |
| 202 | + |
| 203 | + it('enables select when roles are available for assignment', () => { |
| 204 | + renderWithIntl(<EditTeamMemberModal {...defaultProps} />); |
| 205 | + |
| 206 | + // Should be enabled when there are roles available to assign |
| 207 | + const selectElement = screen.getByRole('combobox'); |
| 208 | + expect(selectElement).not.toBeDisabled(); |
| 209 | + }); |
| 210 | +}); |
0 commit comments