Skip to content

Commit

Permalink
chore: Migrate Account Settings (#4498)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioanabrooks authored Oct 4, 2023
1 parent daddcc6 commit 980770d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import isEqual from 'lodash/isEqual.js';

import { ConsoleLogger as Logger } from '@aws-amplify/core/internals/utils';
import {
changePassword,
ValidatorOptions,
getDefaultConfirmPasswordValidators,
getDefaultPasswordValidators,
getLogger,
runFieldValidators,
} from '@aws-amplify/ui';

Expand All @@ -18,7 +18,7 @@ import { ChangePasswordProps, ValidateParams } from './types';
import DEFAULTS from './defaults';
import { defaultChangePasswordDisplayText } from '../utils';

const logger = new Logger('ChangePassword');
const logger = getLogger('ChangePassword');

const getIsDisabled = (
formValues: FormValues,
Expand Down Expand Up @@ -173,7 +173,7 @@ function ChangePassword({
setErrorMessage(null);
}

changePassword({ user, currentPassword, newPassword })
changePassword({ currentPassword, newPassword })
.then(() => {
// notify success to the parent
onSuccess?.();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ describe('ChangePassword', () => {
fireEvent.submit(submitButton);

expect(changePasswordSpy).toBeCalledWith({
user,
currentPassword: 'oldpassword',
newPassword: 'newpassword',
});
Expand Down Expand Up @@ -343,7 +342,6 @@ describe('ChangePassword', () => {
fireEvent.submit(submitButton);

expect(changePasswordSpy).toBeCalledWith({
user,
currentPassword: 'oldpassword',
newPassword: 'newpassword',
});
Expand Down
34 changes: 15 additions & 19 deletions packages/ui/src/helpers/accountSettings/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,42 @@
import * as Auth from '@aws-amplify/auth';
import * as Auth from 'aws-amplify/auth';

import { changePassword, deleteUser } from '../utils';
import { AmplifyUser } from '../../../types';

// mock `aws-amplify` to prevent logging auth errors during test runs
jest.mock('aws-amplify');

const changePasswordSpy = jest.spyOn(Auth, 'changePassword');
const changePasswordSpy = jest.spyOn(Auth, 'updatePassword');
const deleteUserSpy = jest.spyOn(Auth, 'deleteUser');

describe('changePassword', () => {
const user = { username: 'testuser' } as AmplifyUser;
const currentPassword = 'oldpassword';
const newPassword = 'newpassword';

it('should resolve if Auth.changePassword is successful', async () => {
changePasswordSpy.mockResolvedValue('SUCCESS');
it('should resolve if Auth.updatePassword is successful', async () => {
changePasswordSpy.mockResolvedValue();

await expect(
changePassword({ user, currentPassword, newPassword })
changePassword({ currentPassword, newPassword })
).resolves.toBeUndefined();

expect(changePasswordSpy).toHaveBeenCalledWith(
user,
currentPassword,
newPassword
);
expect(changePasswordSpy).toHaveBeenCalledWith({
newPassword: newPassword,
oldPassword: currentPassword,
});
});

it('should reject with error if Auth.changePassword fails', async () => {
it('should reject with error if Auth.updatePassword fails', async () => {
const error = new Error('change password failed');
changePasswordSpy.mockRejectedValue(error);

await expect(
changePassword({ user, currentPassword, newPassword })
changePassword({ currentPassword, newPassword })
).rejects.toEqual(error);

expect(changePasswordSpy).toHaveBeenCalledWith(
user,
currentPassword,
newPassword
);
expect(changePasswordSpy).toHaveBeenCalledWith({
newPassword: newPassword,
oldPassword: currentPassword,
});
});
});

Expand Down
21 changes: 8 additions & 13 deletions packages/ui/src/helpers/accountSettings/utils.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
import * as Auth from '@aws-amplify/auth';
import { updatePassword, deleteUser as deleteAuthUser } from 'aws-amplify/auth';

import { AmplifyUser } from '../../types';
import { getLogger } from '../utils';

const logger = getLogger('Auth');

type ChangePasswordInput = {
user: AmplifyUser;
currentPassword: string;
newPassword: string;
};

export const changePassword = async ({
user: _,
currentPassword,
newPassword,
}: ChangePasswordInput): Promise<void> => {
try {
logger.debug('calling Auth.changePassword');
logger.debug('calling Auth.updatePassword');
/**
* Auth.changePassword returns `Promise<"SUCCESS">`. We're not interested
* Auth.updatePassword returns `Promise<"SUCCESS">`. We're not interested
* in its resolved string value, so we just return Promise.resolve() on success.
*/
// await Auth.changePassword(user, currentPassword, newPassword);
const input: Auth.UpdatePasswordInput = {
await updatePassword({
oldPassword: currentPassword,
newPassword,
};
await Auth.updatePassword(input);
logger.debug('Auth.changePassword was successful');
});
logger.debug('Auth.updatePassword was successful');
return Promise.resolve();
} catch (e) {
logger.debug('Auth.changePassword failed with error', e);
logger.debug('Auth.updatePassword failed with error', e);
return Promise.reject(e);
}
};
Expand All @@ -40,7 +35,7 @@ export const deleteUser = async () => {
try {
logger.debug('calling Auth.deleteUser');
await Promise.resolve();
// await Auth.deleteUser();
await deleteAuthUser();
logger.debug('Auth.deleteUser was successful');
return Promise.resolve();
} catch (e) {
Expand Down
100 changes: 50 additions & 50 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 980770d

Please sign in to comment.