diff --git a/webapp/channels/src/components/admin_console/__snapshots__/custom_enable_disable_guest_accounts_setting.test.tsx.snap b/webapp/channels/src/components/admin_console/__snapshots__/custom_enable_disable_guest_accounts_setting.test.tsx.snap index 298845b552..4cbc997cbb 100644 --- a/webapp/channels/src/components/admin_console/__snapshots__/custom_enable_disable_guest_accounts_setting.test.tsx.snap +++ b/webapp/channels/src/components/admin_console/__snapshots__/custom_enable_disable_guest_accounts_setting.test.tsx.snap @@ -1,99 +1,129 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`components/AdminConsole/CustomEnableDisableGuestAccountsSetting initial state with false 1`] = ` - - + + `; -exports[`components/AdminConsole/CustomEnableDisableGuestAccountsSetting initial state with true 1`] = ` - - + + `; diff --git a/webapp/channels/src/components/admin_console/custom_enable_disable_guest_accounts_setting.test.tsx b/webapp/channels/src/components/admin_console/custom_enable_disable_guest_accounts_setting.test.tsx index aa78b476af..4225627a7d 100644 --- a/webapp/channels/src/components/admin_console/custom_enable_disable_guest_accounts_setting.test.tsx +++ b/webapp/channels/src/components/admin_console/custom_enable_disable_guest_accounts_setting.test.tsx @@ -1,10 +1,12 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import {shallow} from 'enzyme'; +import {fireEvent, screen} from '@testing-library/react'; import React from 'react'; import {FormattedMessage} from 'react-intl'; +import {renderWithContext} from 'tests/react_testing_utils'; + import CustomEnableDisableGuestAccountsSetting from './custom_enable_disable_guest_accounts_setting'; describe('components/AdminConsole/CustomEnableDisableGuestAccountsSetting', () => { @@ -25,61 +27,66 @@ describe('components/AdminConsole/CustomEnableDisableGuestAccountsSetting', () = /> ); - describe('initial state', () => { - test('with true', () => { - const props = { - ...baseProps, - value: true, - }; - - const wrapper = shallow( - , + describe('renders correctly', () => { + test('when enabled', () => { + const {container} = renderWithContext( + , ); - expect(wrapper).toMatchSnapshot(); + expect(container).toMatchSnapshot(); }); - test('with false', () => { - const props = { - ...baseProps, - value: false, - }; - - const wrapper = shallow( - , + test('when disabled', () => { + const {container} = renderWithContext( + , ); - expect(wrapper).toMatchSnapshot(); + expect(container).toMatchSnapshot(); }); }); describe('handleChange', () => { test('should enable without show confirmation modal or warning', () => { const props = { - ...baseProps, showConfirm: true, onChange: jest.fn(), }; - const wrapper = shallow( - , + renderWithContext( + , ); - wrapper.instance().handleChange('MySetting', true); - expect(props.onChange).toBeCalledWith(baseProps.id, true, false, false, ''); + const trueRadio = screen.getByTestId('MySettingtrue'); + fireEvent.click(trueRadio); + + expect(props.onChange).toHaveBeenCalledWith(baseProps.id, true, false, false, ''); }); test('should show confirmation modal and warning when disabling', () => { const props = { - ...baseProps, + value: true, showConfirm: true, onChange: jest.fn(), }; - const wrapper = shallow( - , + renderWithContext( + , ); - wrapper.instance().handleChange('MySetting', false); - expect(props.onChange).toBeCalledWith(baseProps.id, false, true, false, warningMessage); + const falseRadio = screen.getByTestId('MySettingfalse'); + fireEvent.click(falseRadio); + + expect(props.onChange).toHaveBeenCalledWith(baseProps.id, false, true, false, warningMessage); }); test('should call onChange with doSubmit = true when confirm is true', () => { @@ -89,12 +96,19 @@ describe('components/AdminConsole/CustomEnableDisableGuestAccountsSetting', () = showConfirm: true, }; - const wrapper = shallow( - , + renderWithContext( + , ); - wrapper.instance().handleChange('MySetting', false, true); - expect(props.onChange).toBeCalledWith(baseProps.id, false, true, true, warningMessage); + const falseRadio = screen.getByTestId('MySettingfalse'); + fireEvent.click(falseRadio); + + const confirmButton = screen.getByText('Save and Disable Guest Access'); + fireEvent.click(confirmButton); + + expect(props.onChange).toHaveBeenCalledWith(baseProps.id, false, true, true, warningMessage); }); }); }); diff --git a/webapp/channels/src/components/admin_console/custom_enable_disable_guest_accounts_setting.tsx b/webapp/channels/src/components/admin_console/custom_enable_disable_guest_accounts_setting.tsx index e77edf7e93..728fdca543 100644 --- a/webapp/channels/src/components/admin_console/custom_enable_disable_guest_accounts_setting.tsx +++ b/webapp/channels/src/components/admin_console/custom_enable_disable_guest_accounts_setting.tsx @@ -1,7 +1,7 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import React from 'react'; +import React, {useCallback} from 'react'; import {FormattedMessage} from 'react-intl'; import ConfirmModal from 'components/confirm_modal'; @@ -19,9 +19,17 @@ type Props = { showConfirm: boolean; } -export default class CustomEnableDisableGuestAccountsSetting extends React.PureComponent { - public handleChange = (id: string, value: boolean, submit = false) => { - const confirmNeeded = value === false; // Requires confirmation if disabling guest accounts +const CustomEnableDisableGuestAccountsSetting = ({ + id, + value, + onChange, + cancelSubmit, + disabled, + setByEnv, + showConfirm, +}: Props) => { + const handleChange = useCallback((targetId: string, newValue: boolean, submit = false) => { + const confirmNeeded = newValue === false; // Requires confirmation if disabling guest accounts let warning: React.ReactNode | string = ''; if (confirmNeeded) { warning = ( @@ -31,61 +39,63 @@ export default class CustomEnableDisableGuestAccountsSetting extends React.PureC /> ); } - this.props.onChange(id, value, confirmNeeded, submit, warning); - }; + onChange(targetId, newValue, confirmNeeded, submit, warning); + }, [onChange]); - public render() { - const label = ( - - ); - const helpText = ( - - ); + const handleConfirm = useCallback(() => { + handleChange(id, false, true); + }, [handleChange, id]); - return ( - <> - - - } - message={ - - } - confirmButtonText={ - - } - onConfirm={() => { - this.handleChange(this.props.id, false, true); - this.setState({showConfirm: false}); - }} - onCancel={this.props.cancelSubmit} - /> - - ); - } -} + const label = ( + + ); + + const helpText = ( + + ); + + return ( + <> + + + } + message={ + + } + confirmButtonText={ + + } + onConfirm={handleConfirm} + onCancel={cancelSubmit} + /> + + ); +}; + +export default CustomEnableDisableGuestAccountsSetting;