-
Notifications
You must be signed in to change notification settings - Fork 1.5k
chore: refactor the submit function on ProfileView #7353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OtavioStasiak
wants to merge
7
commits into
develop
Choose a base branch
from
chore.refactor-submit-function-profileview
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+354
−72
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db4b135
refactor: submit Profile View
OtavioStasiak fbabd85
Merge branch 'develop' into chore.refactor-submit-function-profileview
OtavioStasiak 44cd95a
chore: code improvements
OtavioStasiak 968a3a2
chore: unit tests
OtavioStasiak 37ddc87
Merge branch 'develop' into chore.refactor-submit-function-profileview
OtavioStasiak c8e6e45
Merge branch 'develop' into chore.refactor-submit-function-profileview
OtavioStasiak 9535266
Merge branch 'develop' into chore.refactor-submit-function-profileview
OtavioStasiak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| import React from 'react'; | ||
| import { act, fireEvent, render, waitFor } from '@testing-library/react-native'; | ||
| import { useDispatch } from 'react-redux'; | ||
| import { sha256 } from 'js-sha256'; | ||
|
|
||
| import ProfileView from './index'; | ||
| import { useAppSelector } from '../../lib/hooks/useAppSelector'; | ||
| import { saveUserProfile } from '../../lib/services/restApi'; | ||
| import { twoFactor } from '../../lib/services/twoFactor'; | ||
| import handleSaveUserProfileError from '../../lib/methods/helpers/handleSaveUserProfileError'; | ||
| import EventEmitter from '../../lib/methods/helpers/events'; | ||
| import { setUser } from '../../actions/login'; | ||
|
|
||
| jest.mock('react-redux', () => ({ | ||
| useDispatch: jest.fn() | ||
| })); | ||
|
|
||
| jest.mock('@react-navigation/native', () => ({ | ||
| ...jest.requireActual('@react-navigation/native'), | ||
| useFocusEffect: jest.fn() | ||
| })); | ||
|
|
||
| jest.mock('../../lib/hooks/useAppSelector', () => ({ | ||
| useAppSelector: jest.fn() | ||
| })); | ||
|
|
||
| jest.mock('../../lib/services/restApi', () => ({ | ||
| saveUserProfile: jest.fn() | ||
| })); | ||
|
|
||
| jest.mock('../../lib/services/twoFactor', () => ({ | ||
| twoFactor: jest.fn() | ||
| })); | ||
|
|
||
| jest.mock('../../lib/methods/helpers/handleSaveUserProfileError', () => jest.fn()); | ||
|
|
||
| const mockShowActionSheet = jest.fn(); | ||
| const mockHideActionSheet = jest.fn(); | ||
| jest.mock('../../containers/ActionSheet', () => ({ | ||
| useActionSheet: () => ({ showActionSheet: mockShowActionSheet, hideActionSheet: mockHideActionSheet }) | ||
| })); | ||
|
|
||
| jest.mock('react-native-keyboard-controller', () => { | ||
| const { View } = require('react-native'); | ||
| return { KeyboardAvoidingView: View }; | ||
| }); | ||
|
|
||
| jest.mock('./components/DeleteAccountActionSheetContent', () => () => null); | ||
| jest.mock('./components/ConfirmEmailChangeActionSheetContent', () => () => null); | ||
| jest.mock('../../containers/CustomFields', () => () => null); | ||
| jest.mock('../../containers/Avatar', () => ({ AvatarWithEdit: () => null })); | ||
|
|
||
| const user = { | ||
| id: 'user-id', | ||
| name: 'John Doe', | ||
| username: 'john.doe', | ||
| emails: [{ address: 'john@rocket.chat', verified: true }], | ||
| bio: 'My bio', | ||
| nickname: 'johnny', | ||
| customFields: {} | ||
| }; | ||
|
|
||
| const buildState = () => ({ | ||
| login: { user }, | ||
| app: { isMasterDetail: false }, | ||
| server: { version: '7.0.0' }, | ||
| settings: { | ||
| Accounts_AllowEmailChange: true, | ||
| Accounts_AllowPasswordChange: true, | ||
| Accounts_AllowRealNameChange: true, | ||
| Accounts_AllowUserAvatarChange: true, | ||
| Accounts_AllowUsernameChange: true, | ||
| Accounts_CustomFields: '', | ||
| Accounts_AllowDeleteOwnAccount: true | ||
| } | ||
| }); | ||
|
|
||
| const navigation = { | ||
| setOptions: jest.fn(), | ||
| navigate: jest.fn() | ||
| } as any; | ||
|
|
||
| const dispatch = jest.fn(); | ||
|
|
||
| const renderProfile = () => { | ||
| (useAppSelector as jest.Mock).mockImplementation((selector: (state: any) => unknown) => selector(buildState())); | ||
| return render(<ProfileView navigation={navigation} />); | ||
| }; | ||
|
|
||
| // Make the form dirty + valid so the Save button is enabled, then press it. | ||
| const changeNameAndSubmit = (getByTestId: (id: string) => any, newName = 'Jane Doe') => { | ||
| fireEvent.changeText(getByTestId('profile-view-name'), newName); | ||
| fireEvent.press(getByTestId('profile-view-submit')); | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| (useDispatch as jest.Mock).mockReturnValue(dispatch); | ||
| }); | ||
|
|
||
| describe('ProfileView submit', () => { | ||
| it('saves the changed fields and notifies the user on success', async () => { | ||
| (saveUserProfile as jest.Mock).mockResolvedValue(true); | ||
| const emitSpy = jest.spyOn(EventEmitter, 'emit'); | ||
|
|
||
| const { getByTestId } = renderProfile(); | ||
| changeNameAndSubmit(getByTestId); | ||
|
|
||
| await waitFor(() => expect(saveUserProfile).toHaveBeenCalledWith({ name: 'Jane Doe' }, {})); | ||
| expect(dispatch).toHaveBeenCalledWith(setUser({ ...user, name: 'Jane Doe', customFields: {} })); | ||
| expect(emitSpy).toHaveBeenCalled(); | ||
| expect(handleSaveUserProfileError).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('asks for the current password before changing the email', async () => { | ||
| const { getByTestId } = renderProfile(); | ||
|
|
||
| fireEvent.changeText(getByTestId('profile-view-email'), 'jane@rocket.chat'); | ||
| fireEvent.press(getByTestId('profile-view-submit')); | ||
|
|
||
| await waitFor(() => expect(mockShowActionSheet).toHaveBeenCalled()); | ||
| expect(saveUserProfile).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('retries the save through the 2FA challenge', async () => { | ||
| (saveUserProfile as jest.Mock) | ||
| .mockRejectedValueOnce({ error: 'totp-invalid', details: { method: 'totp' } }) | ||
| .mockResolvedValueOnce(true); | ||
| (twoFactor as jest.Mock).mockResolvedValue({ twoFactorCode: '123456', twoFactorMethod: 'totp' }); | ||
|
|
||
| const { getByTestId } = renderProfile(); | ||
| changeNameAndSubmit(getByTestId); | ||
|
|
||
| await waitFor(() => expect(twoFactor).toHaveBeenCalledWith({ method: 'totp', invalid: false })); | ||
| await waitFor(() => expect(saveUserProfile).toHaveBeenCalledTimes(2)); | ||
| expect(handleSaveUserProfileError).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('handles the save error after a cancelled/non-2FA failure', async () => { | ||
| const error = { error: 'some-other-error' }; | ||
| (saveUserProfile as jest.Mock).mockRejectedValue(error); | ||
|
|
||
| const { getByTestId } = renderProfile(); | ||
| changeNameAndSubmit(getByTestId); | ||
|
|
||
| await waitFor(() => expect(handleSaveUserProfileError).toHaveBeenCalledWith(error, 'saving_profile')); | ||
| expect(twoFactor).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('hashes the password supplied through the confirm-email action sheet', async () => { | ||
| (saveUserProfile as jest.Mock).mockResolvedValue(true); | ||
| // Capture the onSubmit handler passed to the confirm-email action sheet. | ||
| let confirmOnSubmit: ((password: string) => Promise<void>) | undefined; | ||
| mockShowActionSheet.mockImplementation(({ children }: any) => { | ||
| confirmOnSubmit = children?.props?.onSubmit; | ||
| }); | ||
|
|
||
| const { getByTestId } = renderProfile(); | ||
| fireEvent.changeText(getByTestId('profile-view-email'), 'jane@rocket.chat'); | ||
| fireEvent.press(getByTestId('profile-view-submit')); | ||
|
|
||
| await waitFor(() => expect(confirmOnSubmit).toBeDefined()); | ||
| await act(async () => { | ||
| await confirmOnSubmit!('my-secret'); | ||
| }); | ||
|
|
||
| await waitFor(() => | ||
| expect(saveUserProfile).toHaveBeenCalledWith({ email: 'jane@rocket.chat', currentPassword: sha256('my-secret') }, {}) | ||
| ); | ||
| expect(mockHideActionSheet).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.