Skip to content
Closed
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
6 changes: 1 addition & 5 deletions src/hooks/useDiscardChangesConfirmation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {NavigationAction} from '@react-navigation/native';
import {useFocusEffect, useIsFocused} from '@react-navigation/native';
import {useEffect, useRef} from 'react';
import {ModalActions} from '@components/Modal/Global/ModalContext';
import {isInternalPopstateInProgress} from '@components/Modal/internalPopstateGuard';
import useBeforeRemove from '@hooks/useBeforeRemove';
import useConfirmModal from '@hooks/useConfirmModal';
import useLocalize from '@hooks/useLocalize';
Expand Down Expand Up @@ -120,11 +119,8 @@ function useDiscardChangesConfirmation({getHasUnsavedChanges, onCancel, onVisibi
* already moved — this listener restores it with `history.go(1)`, and dismisses the prompt as Cancel when the back happened over it.
*/
useEffect(() => {
// Register exactly once: re-registering on render would move this listener behind `withInternalPopstate`'s one-shot flag reset, breaking the internal-popstate detection
// Register exactly once so the listener keeps its original browser event ordering.
const handlePopState = () => {
if (isInternalPopstateInProgress()) {
return;
}
if (isRestoringHistory.current) {
isRestoringHistory.current = false;
return;
Expand Down
21 changes: 0 additions & 21 deletions tests/unit/hooks/useDiscardChangesConfirmation.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {act, renderHook} from '@testing-library/react-native';
import {withInternalPopstate} from '@components/Modal/internalPopstateGuard';
import type {DiscardChangesConfirmation} from '@hooks/useDiscardChangesConfirmation/types';
import type UseDiscardChangesConfirmationOptions from '@hooks/useDiscardChangesConfirmation/types';

Expand Down Expand Up @@ -304,25 +303,5 @@ describe('useDiscardChangesConfirmation (web)', () => {
expect(historyGoSpy).not.toHaveBeenCalled();
expect(mockShowConfirmModal).not.toHaveBeenCalled();
});

it('ignores internal popstate events from modal history cleanup', () => {
renderDiscardHook(() => true);

invokeBeforeRemove('RESET');

act(() => {
withInternalPopstate(() => {
window.dispatchEvent(new PopStateEvent('popstate'));
});
});

// The internal popstate must not consume the pending URL restore
expect(historyGoSpy).not.toHaveBeenCalled();

dispatchPopstate();

expect(historyGoSpy).toHaveBeenCalledTimes(1);
expect(historyGoSpy).toHaveBeenCalledWith(1);
});
});
});
Loading