Skip to content

Commit 1df1126

Browse files
fix: reset bridge state on unmount
1 parent 13c94fd commit 1df1126

4 files changed

Lines changed: 42 additions & 17 deletions

File tree

app/components/UI/Bridge/Views/BatchSellReview/BatchSellReview.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ jest.mock('@react-navigation/native', () => ({
6161
}));
6262

6363
jest.mock('../../../../../core/redux/slices/bridge', () => ({
64+
resetBridgeState: jest.fn(() => ({
65+
type: 'bridge/resetBridgeState',
66+
})),
6467
selectBatchSellSourceTokens: jest.fn(() => mockSelectedTokens),
6568
selectBatchSellDestStablecoins: jest.fn(() => mockDestinationTokens),
6669
selectBatchSellDestToken: jest.fn(() => mockSelectedDestinationToken),
@@ -234,6 +237,17 @@ describe('BatchSellReview', () => {
234237
});
235238
});
236239

240+
it('resets bridge state on unmount', () => {
241+
const { unmount } = render(<BatchSellReview />);
242+
243+
mockDispatch.mockClear();
244+
unmount();
245+
246+
expect(mockDispatch).toHaveBeenCalledWith({
247+
type: 'bridge/resetBridgeState',
248+
});
249+
});
250+
237251
it('removes a token when more than two source tokens are selected', () => {
238252
mockSelectedTokens = [...defaultSelectedTokens, thirdSelectedToken];
239253
const { getByTestId } = render(<BatchSellReview />);

app/components/UI/Bridge/Views/BatchSellReview/BatchSellReview.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { strings } from '../../../../../../locales/i18n';
2929
import Routes from '../../../../../constants/navigation/Routes';
3030
import { Skeleton } from '../../../../../component-library/components-temp/Skeleton';
3131
import {
32+
resetBridgeState,
3233
selectBatchSellSlippages,
3334
selectBatchSellDestToken,
3435
selectBatchSellDestStablecoins,
@@ -125,6 +126,14 @@ export function BatchSellReview() {
125126
);
126127
}, [selectedTokens]);
127128

129+
// Reset bridge state when component unmounts.
130+
useEffect(
131+
() => () => {
132+
dispatch(resetBridgeState());
133+
},
134+
[dispatch],
135+
);
136+
128137
useEffect(() => {
129138
// Keep Redux slippages aligned with selected tokens when the user removes tokens.
130139
const nextSlippage = selectedTokens.reduce<

app/components/UI/Bridge/Views/BatchSellTokenSelect/BatchSellTokenSelect.test.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,13 @@ jest.mock('../../../../../selectors/networkController', () => ({
108108
}));
109109

110110
jest.mock('../../../../../core/redux/slices/bridge', () => ({
111+
resetBridgeState: jest.fn(() => ({
112+
type: 'bridge/resetBridgeState',
113+
})),
111114
selectBatchSellDestStablecoins: jest.fn(() => mockDestinationStablecoins),
112115
selectBatchSellDestStablecoinsByChain: jest.fn(
113116
() => mockDestinationStablecoinsByChain,
114117
),
115-
setBatchSellDestToken: jest.fn((token: BridgeToken | undefined) => ({
116-
type: 'bridge/setBatchSellDestToken',
117-
payload: token,
118-
})),
119118
setBatchSellSourceTokens: jest.fn((tokens: BridgeToken[]) => ({
120119
type: 'bridge/setBatchSellSourceTokens',
121120
payload: tokens,
@@ -403,16 +402,17 @@ describe('BatchSellTokenSelect', () => {
403402
expect(queryByText('USDC')).not.toBeOnTheScreen();
404403
});
405404

406-
it('resets Batch Sell source and destination tokens on entry', () => {
407-
render(<BatchSellTokenSelect />);
405+
it('resets bridge state on unmount', () => {
406+
const { unmount } = render(<BatchSellTokenSelect />);
408407

409-
expect(mockDispatch).toHaveBeenCalledWith({
410-
type: 'bridge/setBatchSellSourceTokens',
411-
payload: [],
408+
expect(mockDispatch).not.toHaveBeenCalledWith({
409+
type: 'bridge/resetBridgeState',
412410
});
411+
412+
unmount();
413+
413414
expect(mockDispatch).toHaveBeenCalledWith({
414-
type: 'bridge/setBatchSellDestToken',
415-
payload: undefined,
415+
type: 'bridge/resetBridgeState',
416416
});
417417
});
418418

app/components/UI/Bridge/Views/BatchSellTokenSelect/BatchSellTokenSelect.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ import { CaipChainId } from '@metamask/utils';
3131
import { strings } from '../../../../../../locales/i18n';
3232
import Routes from '../../../../../constants/navigation/Routes';
3333
import {
34+
resetBridgeState,
3435
selectBatchSellDestStablecoins,
3536
selectBatchSellDestStablecoinsByChain,
36-
setBatchSellDestToken,
3737
setBatchSellSourceTokens,
3838
} from '../../../../../core/redux/slices/bridge';
3939
import { RootState } from '../../../../../reducers';
@@ -85,11 +85,13 @@ export function BatchSellTokenSelect() {
8585
>(() => sortedEligibleChains[0]?.chainId);
8686
const [selectedTokens, setSelectedTokens] = useState<BridgeToken[]>([]);
8787

88-
// Start each Batch Sell flow from a clean Redux handoff state.
89-
useEffect(() => {
90-
dispatch(setBatchSellSourceTokens([]));
91-
dispatch(setBatchSellDestToken(undefined));
92-
}, [dispatch]);
88+
// Reset bridge state when component unmounts.
89+
useEffect(
90+
() => () => {
91+
dispatch(resetBridgeState());
92+
},
93+
[dispatch],
94+
);
9395

9496
useEffect(() => {
9597
// Default to the highest-value chain once balances load, but preserve a

0 commit comments

Comments
 (0)