Skip to content

Commit ece826f

Browse files
authored
Merge pull request #58161 from linhvovan29546/fix/57870-rbr-appears-after-remove-second-contact
fix: RBR appears in profile after remove second contact
2 parents 288fd33 + 35fca53 commit ece826f

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/pages/settings/Profile/Contacts/ContactMethodDetailsPage.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) {
9292
const loginData = useMemo(() => loginList?.[contactMethod], [loginList, contactMethod]);
9393
const isDefaultContactMethod = useMemo(() => session?.email === loginData?.partnerUserID, [session?.email, loginData?.partnerUserID]);
9494
const validateLoginError = getEarliestErrorField(loginData, 'validateLogin');
95+
const prevPendingDeletedLogin = usePrevious(loginData?.pendingFields?.deletedLogin);
9596

9697
/**
9798
* Attempt to set this contact method as user's "Default contact method"
@@ -167,10 +168,12 @@ function ContactMethodDetailsPage({route}: ContactMethodDetailsPageProps) {
167168
}, [loginData?.validatedDate, loginData?.errorFields?.addedLogin]);
168169

169170
useEffect(() => {
170-
if (loginData?.validatedDate) {
171+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
172+
if (loginData?.validatedDate || prevPendingDeletedLogin) {
171173
return;
172174
}
173175
resetContactMethodValidateCodeSentState(contactMethod);
176+
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps -- The prevPendingDeletedLogin is a ref, so no need to add it to dependencies.
174177
}, [contactMethod, loginData?.validatedDate]);
175178

176179
const getThreeDotsMenuItems = useCallback(() => {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import {render} from '@testing-library/react-native';
2+
import Onyx from 'react-native-onyx';
3+
// eslint-disable-next-line no-restricted-syntax
4+
import * as UserActions from '@libs/actions/User';
5+
import ContactMethodDetailsPage from '@pages/settings/Profile/Contacts/ContactMethodDetailsPage';
6+
import ONYXKEYS from '@src/ONYXKEYS';
7+
import type {MockFetch} from '../utils/TestHelper';
8+
import {getGlobalFetchMock} from '../utils/TestHelper';
9+
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
10+
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';
11+
12+
jest.mock('@libs/Navigation/Navigation', () => ({
13+
goBack: jest.fn(),
14+
}));
15+
16+
jest.mock('@libs/actions/User', () => {
17+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
18+
const originalModule = jest.requireActual('@libs/actions/User');
19+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
20+
return {
21+
...originalModule,
22+
resetContactMethodValidateCodeSentState: jest.fn(),
23+
};
24+
});
25+
26+
const fakeEmail = '[email protected]';
27+
const mockRoute = {
28+
params: {
29+
backTo: '',
30+
contactMethod: fakeEmail,
31+
},
32+
};
33+
const mockLoginList = {
34+
[fakeEmail]: {
35+
partnerName: 'expensify.com',
36+
partnerUserID: fakeEmail,
37+
validatedDate: 'fake-validatedDate',
38+
},
39+
};
40+
41+
describe('ContactMethodDetailsPage', () => {
42+
let mockFetch: MockFetch;
43+
beforeAll(() => {
44+
Onyx.init({
45+
keys: ONYXKEYS,
46+
});
47+
});
48+
beforeEach(() => {
49+
global.fetch = getGlobalFetchMock();
50+
mockFetch = fetch as MockFetch;
51+
return Onyx.clear().then(waitForBatchedUpdates);
52+
});
53+
54+
function ContactMethodDetailsPageRenderer() {
55+
return (
56+
<ContactMethodDetailsPage
57+
// @ts-expect-error - Ignoring type errors for testing purposes
58+
route={mockRoute}
59+
/>
60+
);
61+
}
62+
63+
it('should not call resetContactMethodValidateCodeSentState when we got a delete pending field', async () => {
64+
// Given a login list with a validated contact method
65+
Onyx.merge(ONYXKEYS.LOGIN_LIST, mockLoginList);
66+
await waitForBatchedUpdates();
67+
68+
// Given the page is rendered
69+
render(<ContactMethodDetailsPageRenderer />);
70+
71+
// When a deleteContactMethod called
72+
UserActions.deleteContactMethod(fakeEmail, mockLoginList);
73+
await waitForBatchedUpdatesWithAct();
74+
75+
// When the deletion is successful
76+
mockFetch?.succeed();
77+
await waitForBatchedUpdates();
78+
mockFetch?.resume();
79+
await waitForBatchedUpdates();
80+
81+
// Then resetContactMethodValidateCodeSentState should not be called
82+
expect(UserActions.resetContactMethodValidateCodeSentState).not.toHaveBeenCalled();
83+
});
84+
});

0 commit comments

Comments
 (0)