[MM-39149] Only close sidebar when custom status is set successfully (#5988)

This commit is contained in:
Mylon Suren 2022-02-21 06:24:04 -05:00 committed by GitHub
parent dfbaa55e11
commit b7a98dead9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 11 deletions

View file

@ -1,5 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`SettingsSidebar Custom Status should close sidebar when update custom status succeeds 1`] = `
<DrawerLayoutAdapter
drawerPosition="right"
drawerWidth={710}
forwardRef={
Object {
"current": null,
}
}
onDrawerClose={[Function]}
onDrawerOpen={[Function]}
renderNavigationView={[Function]}
useNativeAnimations={true}
/>
`;
exports[`SettingsSidebar Custom Status should keep sidebar open when update custom status fails 1`] = `
<DrawerLayoutAdapter
drawerPosition="right"
drawerWidth={710}
forwardRef={
Object {
"current": null,
}
}
onDrawerClose={[Function]}
onDrawerOpen={[Function]}
renderNavigationView={[Function]}
useNativeAnimations={true}
/>
`;
exports[`SettingsSidebar should match snapshot 1`] = `
<DrawerLayoutAdapter
drawerPosition="right"

View file

@ -5,6 +5,7 @@ import React from 'react';
import Preferences from '@mm-redux/constants/preferences';
import {CustomStatusDuration} from '@mm-redux/types/users';
import EventEmitter from '@mm-redux/utils/event_emitter';
import {shallowWithIntl} from '@test/intl-test-helper';
import SettingsSidebar from './settings_sidebar.ios';
@ -67,4 +68,44 @@ describe('SettingsSidebar', () => {
expect(wrapper.getElement()).toMatchSnapshot();
});
describe('Custom Status', () => {
const tests = [
{case: 'should keep sidebar open when update custom status fails', error: 'update custom status fail'},
{case: 'should close sidebar when update custom status succeeds', error: undefined},
];
tests.forEach((test) => {
it(test.case, () => {
const wrapper = shallowWithIntl(
<SettingsSidebar
{...baseProps}
isCustomStatusEnabled={true}
customStatus={{
...customStatus,
duration: CustomStatusDuration.DATE_AND_TIME,
expires_at: '2200-04-13T18:09:12.451Z',
}}
/>,
);
const instance = wrapper.instance();
instance.componentDidMount();
const spy = jest.spyOn(instance, 'closeSettingsSidebar');
EventEmitter.emit('set_custom_status', test.error);
if (test.error) {
expect(spy).not.toBeCalled();
expect(wrapper.state('showRetryMessage')).toBe(true);
} else {
expect(spy).toBeCalled();
expect(wrapper.state('showRetryMessage')).toBe(false);
}
expect(wrapper.getElement()).toMatchSnapshot();
});
});
});
});

View file

@ -57,7 +57,7 @@ export default class SettingsSidebarBase extends PureComponent {
componentDidMount() {
this.mounted = true;
EventEmitter.on(NavigationTypes.CLOSE_SETTINGS_SIDEBAR, this.closeSettingsSidebar);
EventEmitter.on(CustomStatus.SET_CUSTOM_STATUS_FAILURE, this.handleSetCustomStatusFailure);
EventEmitter.on(CustomStatus.SET_CUSTOM_STATUS, this.handleSetCustomStatus);
}
componentDidUpdate(prevProps) {
@ -67,13 +67,20 @@ export default class SettingsSidebarBase extends PureComponent {
componentWillUnmount() {
this.mounted = false;
EventEmitter.off(NavigationTypes.CLOSE_SETTINGS_SIDEBAR, this.closeSettingsSidebar);
EventEmitter.off(CustomStatus.SET_CUSTOM_STATUS_FAILURE, this.handleSetCustomStatusFailure);
EventEmitter.off(CustomStatus.SET_CUSTOM_STATUS, this.handleSetCustomStatus);
}
handleSetCustomStatusFailure = () => {
this.setState({
showRetryMessage: true,
});
handleSetCustomStatus = (error) => {
if (error) {
this.setState({showRetryMessage: true});
} else {
this.setState({
showStatus: true,
showRetryMessage: false,
});
this.closeSettingsSidebar();
}
};
handleCustomStatusChange = (prevCustomStatus, customStatus) => {
@ -167,7 +174,6 @@ export default class SettingsSidebarBase extends PureComponent {
};
goToCustomStatusScreen = (intl) => {
this.closeSettingsSidebar();
showModal('CustomStatus', intl.formatMessage({id: 'mobile.routes.custom_status', defaultMessage: 'Set a Status'}));
};

View file

@ -45,4 +45,4 @@ export const durationValues = {
},
};
export const CUSTOM_STATUS_TEXT_CHARACTER_LIMIT = 100;
export const SET_CUSTOM_STATUS_FAILURE = 'set_custom_status_failure';
export const SET_CUSTOM_STATUS = 'set_custom_status';

View file

@ -159,9 +159,7 @@ class CustomStatusModal extends NavigationComponent<Props, State> {
status.expires_at = this.calculateExpiryTime(duration);
}
const {error} = await this.props.actions.setCustomStatus(status);
if (error) {
EventEmitter.emit(CustomStatus.SET_CUSTOM_STATUS_FAILURE);
}
EventEmitter.emit(CustomStatus.SET_CUSTOM_STATUS, error);
}
} else if (customStatus?.emoji) {
this.props.actions.unsetCustomStatus();