Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/components/Modal/BaseModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,15 @@ function BaseModal({
};
}, [isVisible, wasVisible, onClose, type, isModalCovering, handleDismissModal, coveringModalID]);

const hideModalOnUnmount = (callHideCallback: boolean) => {
// Unmounting while visible skips the dismiss effect, so resolve the ready-to-focus promise here or delayed composer focus waits on it forever.
handleDismissModal();
hideModal(callHideCallback);
};

useEffect(() => {
hideModalCallbackRef.current = hideModal;
}, [hideModal]);
hideModalCallbackRef.current = hideModalOnUnmount;
}, [hideModalOnUnmount]);

useEffect(() => {
isVisibleRef.current = isVisible;
Expand Down
41 changes: 41 additions & 0 deletions tests/ui/BaseModalTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import type ReanimatedModalProps from '@components/Modal/ReanimatedModal/types';

import type UseResponsiveLayout from '@hooks/useResponsiveLayout';

import type ComposerFocusManagerType from '@libs/ComposerFocusManager';

import type * as ModalActions from '@userActions/Modal';

import CONST from '@src/CONST';

import React from 'react';
import {View} from 'react-native';

import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

describe('BaseModal', () => {
afterEach(() => {
jest.resetModules();
Expand Down Expand Up @@ -368,4 +372,41 @@ describe('BaseModal', () => {
unmount();
expect(setModalCovering).toHaveBeenLastCalledWith(coveringModalID, false);
});

it('resolves its ready-to-focus promise when it is unmounted while still visible', async () => {
// Given a shown confirm modal, which makes every delayed composer focus wait until the modal is gone
const modalProps: ReanimatedModalProps[] = [];
jest.doMock('react', () => React);
jest.doMock('@components/Modal/ReanimatedModal', () => ({
__esModule: true,
default: (props: ReanimatedModalProps) => {
modalProps.push(props);
return null;
},
}));
const {default: BaseModal} = jest.requireActual<{default: typeof BaseModalComponent}>('@components/Modal/BaseModal');
const {default: ComposerFocusManager} = jest.requireActual<{default: typeof ComposerFocusManagerType}>('@libs/ComposerFocusManager');
const {unmount} = render(
<BaseModal
isVisible
type={CONST.MODAL.MODAL_TYPE.CONFIRM}
>
{null}
</BaseModal>,
);
modalProps.at(-1)?.onModalWillShow?.();
let isReadyToFocus = false;
ComposerFocusManager.isReadyToFocus().then(() => {
isReadyToFocus = true;
});
await waitForBatchedUpdates();
expect(isReadyToFocus).toBe(false);

// When it is unmounted without its isVisible prop turning false, which is how the global modal stack closes a modal
unmount();
await waitForBatchedUpdates();

// Then the wait is over, so a composer that asks for focus afterwards, like the inline edit composer, still gets it
expect(isReadyToFocus).toBe(true);
});
});
Loading