|
| 1 | +import { mockAppRoot } from '@rocket.chat/mock-providers'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | + |
| 5 | +import CreateTeamModal from './CreateTeamModal'; |
| 6 | +import CreateTeamModalOld from '../../../sidebar/header/CreateTeam'; |
| 7 | + |
| 8 | +jest.mock('../../../lib/utils/goToRoomById', () => ({ |
| 9 | + goToRoomById: jest.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +type CreateTeamModalComponentType = typeof CreateTeamModal | typeof CreateTeamModalOld; |
| 13 | +// eslint-disable-next-line @typescript-eslint/naming-convention |
| 14 | + |
| 15 | +describe.each([ |
| 16 | + ['CreateTeamModal', CreateTeamModalOld], |
| 17 | + ['CreateTeamModal in NavbarV2', CreateTeamModal], |
| 18 | +] as const)( |
| 19 | + '%s', |
| 20 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 21 | + (_name: string, CreateTeamModalComponent: CreateTeamModalComponentType) => { |
| 22 | + it('should render with encryption option disabled and set to off when E2E_Enable=false and E2E_Enabled_Default_PrivateRooms=false', async () => { |
| 23 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 24 | + wrapper: mockAppRoot().withSetting('E2E_Enable', false).withSetting('E2E_Enabled_Default_PrivateRooms', false).build(), |
| 25 | + }); |
| 26 | + |
| 27 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 28 | + |
| 29 | + const encrypted = screen.getByLabelText('Teams_New_Encrypted_Label') as HTMLInputElement; |
| 30 | + expect(encrypted).toBeInTheDocument(); |
| 31 | + expect(encrypted).not.toBeChecked(); |
| 32 | + expect(encrypted).toBeDisabled(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should render with encryption option enabled and set to off when E2E_Enable=true and E2E_Enabled_Default_PrivateRooms=false', async () => { |
| 36 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 37 | + wrapper: mockAppRoot().withSetting('E2E_Enable', true).withSetting('E2E_Enabled_Default_PrivateRooms', false).build(), |
| 38 | + }); |
| 39 | + |
| 40 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 41 | + |
| 42 | + const encrypted = screen.getByLabelText('Teams_New_Encrypted_Label') as HTMLInputElement; |
| 43 | + expect(encrypted).toBeInTheDocument(); |
| 44 | + expect(encrypted).not.toBeChecked(); |
| 45 | + expect(encrypted).toBeEnabled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should render with encryption option disabled and set to off when E2E_Enable=false and E2E_Enabled_Default_PrivateRooms=true', async () => { |
| 49 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 50 | + wrapper: mockAppRoot().withSetting('E2E_Enable', false).withSetting('E2E_Enabled_Default_PrivateRooms', true).build(), |
| 51 | + }); |
| 52 | + |
| 53 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 54 | + |
| 55 | + const encrypted = screen.getByLabelText('Teams_New_Encrypted_Label') as HTMLInputElement; |
| 56 | + expect(encrypted).toBeInTheDocument(); |
| 57 | + |
| 58 | + expect(encrypted).not.toBeChecked(); |
| 59 | + expect(encrypted).toBeDisabled(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should render with encryption option enabled and set to on when E2E_Enable=true and E2E_Enabled_Default_PrivateRooms=True', async () => { |
| 63 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 64 | + wrapper: mockAppRoot().withSetting('E2E_Enable', true).withSetting('E2E_Enabled_Default_PrivateRooms', true).build(), |
| 65 | + }); |
| 66 | + |
| 67 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 68 | + |
| 69 | + const encrypted = screen.getByLabelText('Teams_New_Encrypted_Label') as HTMLInputElement; |
| 70 | + expect(encrypted).toBeChecked(); |
| 71 | + expect(encrypted).toBeEnabled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('when Private goes ON → OFF: forces Encrypted OFF and disables it (E2E_Enable=true, E2E_Enabled_Default_PrivateRooms=true)', async () => { |
| 75 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 76 | + wrapper: mockAppRoot().withSetting('E2E_Enable', true).withSetting('E2E_Enabled_Default_PrivateRooms', true).build(), |
| 77 | + }); |
| 78 | + |
| 79 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 80 | + |
| 81 | + const encrypted = screen.getByLabelText('Teams_New_Encrypted_Label') as HTMLInputElement; |
| 82 | + const priv = screen.getByLabelText('Teams_New_Private_Label') as HTMLInputElement; |
| 83 | + |
| 84 | + // initial: private=true, encrypted ON and enabled |
| 85 | + expect(priv).toBeChecked(); |
| 86 | + expect(encrypted).toBeChecked(); |
| 87 | + expect(encrypted).toBeEnabled(); |
| 88 | + |
| 89 | + // Private ON -> OFF: encrypted must become OFF and disabled |
| 90 | + await userEvent.click(priv); |
| 91 | + expect(priv).not.toBeChecked(); |
| 92 | + expect(encrypted).not.toBeChecked(); |
| 93 | + expect(encrypted).toBeDisabled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('when Private goes OFF → ON: keeps Encrypted OFF but re-enables it (E2E_Enable=true, E2E_Enabled_Default_PrivateRooms=true)', async () => { |
| 97 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 98 | + wrapper: mockAppRoot().withSetting('E2E_Enable', true).withSetting('E2E_Enabled_Default_PrivateRooms', true).build(), |
| 99 | + }); |
| 100 | + |
| 101 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 102 | + |
| 103 | + const encrypted = screen.getByLabelText('Teams_New_Encrypted_Label') as HTMLInputElement; |
| 104 | + const priv = screen.getByLabelText('Teams_New_Private_Label') as HTMLInputElement; |
| 105 | + |
| 106 | + // turn private OFF to simulate user path from non-private |
| 107 | + await userEvent.click(priv); |
| 108 | + expect(priv).not.toBeChecked(); |
| 109 | + expect(encrypted).not.toBeChecked(); |
| 110 | + expect(encrypted).toBeDisabled(); |
| 111 | + |
| 112 | + // turn private back ON -> encrypted should remain OFF but become enabled |
| 113 | + await userEvent.click(priv); |
| 114 | + expect(priv).toBeChecked(); |
| 115 | + expect(encrypted).not.toBeChecked(); |
| 116 | + expect(encrypted).toBeEnabled(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('private team: toggling Broadcast on/off does not change or disable Encrypted', async () => { |
| 120 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 121 | + wrapper: mockAppRoot().withSetting('E2E_Enable', true).withSetting('E2E_Enabled_Default_PrivateRooms', true).build(), |
| 122 | + }); |
| 123 | + |
| 124 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 125 | + |
| 126 | + const encrypted = screen.getByLabelText('Teams_New_Encrypted_Label') as HTMLInputElement; |
| 127 | + const broadcast = screen.getByLabelText('Teams_New_Broadcast_Label') as HTMLInputElement; |
| 128 | + const priv = screen.getByLabelText('Teams_New_Private_Label') as HTMLInputElement; |
| 129 | + |
| 130 | + expect(priv).toBeChecked(); |
| 131 | + expect(encrypted).toBeChecked(); |
| 132 | + expect(encrypted).toBeEnabled(); |
| 133 | + expect(broadcast).not.toBeChecked(); |
| 134 | + |
| 135 | + // Broadcast: OFF -> ON (Encrypted unchanged + enabled) |
| 136 | + await userEvent.click(broadcast); |
| 137 | + expect(broadcast).toBeChecked(); |
| 138 | + expect(encrypted).toBeChecked(); |
| 139 | + expect(encrypted).toBeEnabled(); |
| 140 | + |
| 141 | + // Broadcast: ON -> OFF (Encrypted unchanged + enabled) |
| 142 | + await userEvent.click(broadcast); |
| 143 | + expect(broadcast).not.toBeChecked(); |
| 144 | + expect(encrypted).toBeChecked(); |
| 145 | + expect(encrypted).toBeEnabled(); |
| 146 | + |
| 147 | + // User can still toggle Encrypted freely while Broadcast is OFF |
| 148 | + await userEvent.click(encrypted); |
| 149 | + expect(encrypted).not.toBeChecked(); |
| 150 | + |
| 151 | + // User can still toggle Encrypted freely while Broadcast is ON |
| 152 | + await userEvent.click(broadcast); |
| 153 | + expect(broadcast).toBeChecked(); |
| 154 | + expect(encrypted).not.toBeChecked(); |
| 155 | + expect(encrypted).toBeEnabled(); |
| 156 | + }); |
| 157 | + |
| 158 | + it('non-private team: Encrypted remains OFF and disabled regardless of Broadcast state', async () => { |
| 159 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 160 | + wrapper: mockAppRoot().withSetting('E2E_Enable', true).withSetting('E2E_Enabled_Default_PrivateRooms', true).build(), |
| 161 | + }); |
| 162 | + |
| 163 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 164 | + |
| 165 | + const encrypted = screen.getByLabelText('Teams_New_Encrypted_Label') as HTMLInputElement; |
| 166 | + const broadcast = screen.getByLabelText('Teams_New_Broadcast_Label') as HTMLInputElement; |
| 167 | + const priv = screen.getByLabelText('Teams_New_Private_Label') as HTMLInputElement; |
| 168 | + |
| 169 | + // Switch to non-private |
| 170 | + await userEvent.click(priv); |
| 171 | + expect(priv).not.toBeChecked(); |
| 172 | + |
| 173 | + // Encrypted must be OFF + disabled (non-private cannot be encrypted) |
| 174 | + expect(encrypted).not.toBeChecked(); |
| 175 | + expect(encrypted).toBeDisabled(); |
| 176 | + |
| 177 | + // Broadcast: OFF -> ON (Encrypted stays OFF + disabled) |
| 178 | + await userEvent.click(broadcast); |
| 179 | + expect(broadcast).toBeChecked(); |
| 180 | + expect(encrypted).not.toBeChecked(); |
| 181 | + expect(encrypted).toBeDisabled(); |
| 182 | + |
| 183 | + // Broadcast: ON -> OFF (Encrypted still OFF + disabled) |
| 184 | + await userEvent.click(broadcast); |
| 185 | + expect(broadcast).not.toBeChecked(); |
| 186 | + expect(encrypted).not.toBeChecked(); |
| 187 | + expect(encrypted).toBeDisabled(); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should disable and turn on ReadOnly toggle when Broadcast is ON and no set-readonly permission', async () => { |
| 191 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 192 | + wrapper: mockAppRoot().build(), |
| 193 | + }); |
| 194 | + |
| 195 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 196 | + |
| 197 | + const broadcast = screen.getByLabelText('Teams_New_Broadcast_Label') as HTMLInputElement; |
| 198 | + const readOnly = screen.getByLabelText('Teams_New_Read_only_Label') as HTMLInputElement; |
| 199 | + |
| 200 | + expect(readOnly).not.toBeChecked(); |
| 201 | + |
| 202 | + // Broadcast: OFF -> ON (ReadOnly stays ON + disabled) |
| 203 | + await userEvent.click(broadcast); |
| 204 | + expect(broadcast).toBeChecked(); |
| 205 | + expect(readOnly).toBeChecked(); |
| 206 | + expect(readOnly).toBeDisabled(); |
| 207 | + }); |
| 208 | + |
| 209 | + it('should disable and turn on ReadOnly toggle when Broadcast is ON with set-readonly permission', async () => { |
| 210 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 211 | + wrapper: mockAppRoot().withPermission('set-readonly').build(), |
| 212 | + }); |
| 213 | + |
| 214 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 215 | + |
| 216 | + const broadcast = screen.getByLabelText('Teams_New_Broadcast_Label') as HTMLInputElement; |
| 217 | + const readOnly = screen.getByLabelText('Teams_New_Read_only_Label') as HTMLInputElement; |
| 218 | + |
| 219 | + expect(readOnly).not.toBeChecked(); |
| 220 | + |
| 221 | + // Broadcast: OFF -> ON (ReadOnly stays ON + disabled) |
| 222 | + await userEvent.click(broadcast); |
| 223 | + expect(broadcast).toBeChecked(); |
| 224 | + expect(readOnly).toBeChecked(); |
| 225 | + expect(readOnly).toBeDisabled(); |
| 226 | + }); |
| 227 | + |
| 228 | + it('should disable and turn off ReadOnly toggle when Broadcast is OFF with no set-readonly permission', async () => { |
| 229 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 230 | + wrapper: mockAppRoot().build(), |
| 231 | + }); |
| 232 | + |
| 233 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 234 | + |
| 235 | + const broadcast = screen.getByLabelText('Teams_New_Broadcast_Label') as HTMLInputElement; |
| 236 | + const readOnly = screen.getByLabelText('Teams_New_Read_only_Label') as HTMLInputElement; |
| 237 | + |
| 238 | + expect(broadcast).not.toBeChecked(); |
| 239 | + expect(readOnly).not.toBeChecked(); |
| 240 | + expect(readOnly).toBeDisabled(); |
| 241 | + }); |
| 242 | + |
| 243 | + it('should enable ReadOnly toggle when Broadcast is OFF with set-readonly permission', async () => { |
| 244 | + render(<CreateTeamModalComponent onClose={() => null} />, { |
| 245 | + wrapper: mockAppRoot().withPermission('set-readonly').build(), |
| 246 | + }); |
| 247 | + |
| 248 | + await userEvent.click(screen.getByText('Advanced_settings')); |
| 249 | + |
| 250 | + const broadcast = screen.getByLabelText('Teams_New_Broadcast_Label') as HTMLInputElement; |
| 251 | + const readOnly = screen.getByLabelText('Teams_New_Read_only_Label') as HTMLInputElement; |
| 252 | + |
| 253 | + expect(broadcast).not.toBeChecked(); |
| 254 | + expect(readOnly).not.toBeChecked(); |
| 255 | + expect(readOnly).toBeEnabled(); |
| 256 | + }); |
| 257 | + }, |
| 258 | +); |
0 commit comments