|
| 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