Delete Account: added more unit tests

This commit is contained in:
gianantoniopini
2021-05-14 18:21:47 +02:00
parent d11d414504
commit a8cf553217
4 changed files with 98 additions and 47 deletions

View File

@ -77,10 +77,6 @@ class Auth {
this._currentUser = null; this._currentUser = null;
await delay(Constants.defaultDelayInMilliseconds); await delay(Constants.defaultDelayInMilliseconds);
this.onAuthStateChangedObservers.forEach((observer) =>
observer(this._currentUser),
);
} }
} }

View File

@ -66,8 +66,8 @@ const UserProvider = ({ children }) => {
} }
}; };
const logout = () => { const logout = async () => {
firebase.auth().signOut(); await firebase.auth().signOut();
localStorage.removeItem('user'); localStorage.removeItem('user');
setUser(null); setUser(null);
navigate('/'); navigate('/');
@ -123,7 +123,7 @@ const UserProvider = ({ children }) => {
} catch (error) { } catch (error) {
toast.error(error.message); toast.error(error.message);
} finally { } finally {
logout(); await logout();
toast( toast(
"It's sad to see you go, but we respect your privacy. All your data has been deleted successfully. Hope to see you again soon!", "It's sad to see you go, but we respect your privacy. All your data has been deleted successfully. Hope to see you again soon!",
); );

View File

@ -1,4 +1,5 @@
import { navigate as mockNavigateFunction } from '@reach/router'; import { navigate as mockNavigateFunction } from '@reach/router';
import { toast } from 'react-toastify';
import { fireEvent, screen, waitFor } from '@testing-library/react'; import { fireEvent, screen, waitFor } from '@testing-library/react';
import FirebaseStub, { import FirebaseStub, {
@ -6,9 +7,7 @@ import FirebaseStub, {
FunctionsConstants, FunctionsConstants,
} from 'gatsby-plugin-firebase'; } from 'gatsby-plugin-firebase';
import { delay } from '../../../utils/index'; import { setupAndWait } from './helpers/builder';
import { setupAndWait, findAndDismissNotification } from './helpers/builder';
const testTimeoutInMilliseconds = 20000; const testTimeoutInMilliseconds = 20000;
jest.setTimeout(testTimeoutInMilliseconds); jest.setTimeout(testTimeoutInMilliseconds);
@ -17,9 +16,7 @@ async function setup() {
const resumeId = DatabaseConstants.demoStateResume1Id; const resumeId = DatabaseConstants.demoStateResume1Id;
await setupAndWait(resumeId, true, true); await setupAndWait(resumeId, true, true);
const mockFirebaseDeleteUserCloudFunction = jest.fn(async () => { const mockFirebaseDeleteUserCloudFunction = jest.fn(async () => {});
await delay(FunctionsConstants.defaultDelayInMilliseconds);
});
const mockFirebaseFunctionsHttpsCallable = jest.fn((name) => const mockFirebaseFunctionsHttpsCallable = jest.fn((name) =>
name === FunctionsConstants.deleteUserFunctionName name === FunctionsConstants.deleteUserFunctionName
? mockFirebaseDeleteUserCloudFunction ? mockFirebaseDeleteUserCloudFunction
@ -27,19 +24,38 @@ async function setup() {
); );
FirebaseStub.functions().httpsCallable = mockFirebaseFunctionsHttpsCallable; FirebaseStub.functions().httpsCallable = mockFirebaseFunctionsHttpsCallable;
const mockFirebaseCurrentUserDelete = jest.spyOn( const mockFirebaseCurrentUserDelete = jest.fn(async () => {
FirebaseStub.auth().currentUser, throw new Error('Error occurred while deleting user.');
'delete',
);
const button = screen.getByRole('button', {
name: /Delete Account/i,
}); });
FirebaseStub.auth().currentUser.delete = mockFirebaseCurrentUserDelete;
const mockFirebaseAuthSignOut = jest.fn(async () => {
throw new Error('Error occurred while signing out.');
});
FirebaseStub.auth().signOut = mockFirebaseAuthSignOut;
// eslint-disable-next-line no-unused-vars
const mockToastError = jest.fn((content, options) => {});
toast.error = mockToastError;
const deleteAccountRegExp = /Delete Account/i;
const button = screen.getByRole('button', {
name: deleteAccountRegExp,
});
const waitForButtonNameToBeSetToDeleteAccount = async () => {
waitFor(() => {
expect(button).toHaveTextContent(deleteAccountRegExp);
});
};
return { return {
button, button,
mockFirebaseCurrentUserDelete,
mockFirebaseDeleteUserCloudFunction, mockFirebaseDeleteUserCloudFunction,
mockFirebaseCurrentUserDelete,
mockFirebaseAuthSignOut,
mockToastError,
waitForButtonNameToBeSetToDeleteAccount,
}; };
} }
@ -48,44 +64,94 @@ test('prompts for confirmation', async () => {
button, button,
mockFirebaseDeleteUserCloudFunction, mockFirebaseDeleteUserCloudFunction,
mockFirebaseCurrentUserDelete, mockFirebaseCurrentUserDelete,
mockFirebaseAuthSignOut,
mockToastError,
} = await setup(); } = await setup();
fireEvent.click(button); fireEvent.click(button);
expect(button).toHaveTextContent('Are you sure?'); expect(button).toHaveTextContent(/Are you sure?/i);
await waitFor(() => expect(mockFirebaseDeleteUserCloudFunction).not.toHaveBeenCalled();
expect(mockFirebaseDeleteUserCloudFunction).not.toHaveBeenCalledTimes(1), expect(mockFirebaseCurrentUserDelete).not.toHaveBeenCalled();
); expect(mockFirebaseAuthSignOut).not.toHaveBeenCalled();
await waitFor(() => expect(mockNavigateFunction).not.toHaveBeenCalled();
expect(mockFirebaseCurrentUserDelete).not.toHaveBeenCalled(), expect(mockToastError).not.toHaveBeenCalled();
);
}); });
/*
test('calls Firebase delete user cloud function', async () => { test('calls Firebase delete user cloud function', async () => {
const { button, mockFirebaseDeleteUserCloudFunction } = await setup(); const {
button,
mockFirebaseDeleteUserCloudFunction,
waitForButtonNameToBeSetToDeleteAccount,
} = await setup();
fireEvent.click(button); fireEvent.click(button);
fireEvent.click(button); fireEvent.click(button);
await waitFor(() => expect(mockNavigateFunction).toHaveBeenCalledTimes(1)); await waitForButtonNameToBeSetToDeleteAccount();
await findAndDismissNotification();
await waitFor(() => await waitFor(() =>
expect(mockFirebaseDeleteUserCloudFunction).toHaveBeenCalledTimes(1), expect(mockFirebaseDeleteUserCloudFunction).toHaveBeenCalledTimes(1),
); );
}); });
test('calls Firebase current user delete', async () => { test('calls Firebase current user delete', async () => {
const { button, mockFirebaseCurrentUserDelete } = await setup(); const {
button,
mockFirebaseCurrentUserDelete,
waitForButtonNameToBeSetToDeleteAccount,
} = await setup();
fireEvent.click(button); fireEvent.click(button);
fireEvent.click(button); fireEvent.click(button);
await waitFor(() => expect(mockNavigateFunction).toHaveBeenCalledTimes(1)); await waitForButtonNameToBeSetToDeleteAccount();
await findAndDismissNotification();
await waitFor(() => await waitFor(() =>
expect(mockFirebaseCurrentUserDelete).toHaveBeenCalledTimes(1), expect(mockFirebaseCurrentUserDelete).toHaveBeenCalledTimes(1),
); );
}); });
test('calls Firebase auth sign out', async () => {
const {
button,
mockFirebaseAuthSignOut,
waitForButtonNameToBeSetToDeleteAccount,
} = await setup();
fireEvent.click(button);
fireEvent.click(button);
await waitForButtonNameToBeSetToDeleteAccount();
await waitFor(() => expect(mockFirebaseAuthSignOut).toHaveBeenCalledTimes(1));
});
describe('if an error occurs while signing out the current user', () => {
test('does not navigate away', async () => {
const { button, waitForButtonNameToBeSetToDeleteAccount } = await setup();
fireEvent.click(button);
fireEvent.click(button);
await waitForButtonNameToBeSetToDeleteAccount();
expect(mockNavigateFunction).not.toHaveBeenCalled();
});
/*
test('displays toast', async () => {
const {
button,
waitForButtonNameToBeSetToDeleteAccount,
mockToastError,
} = await setup();
fireEvent.click(button);
fireEvent.click(button);
await waitForButtonNameToBeSetToDeleteAccount();
expect(mockToastError).toHaveBeenCalledTimes(2);
expect(mockToastError).toHaveBeenLastCalledWith(
'An error occurred deleting your account.',
);
});
*/ */
});

View File

@ -47,15 +47,6 @@ const expectDatabaseUpdateToHaveCompleted = async (
); );
}; };
const dismissNotification = (notification) => {
fireEvent.click(notification);
};
const findAndDismissNotification = async () => {
const notification = await screen.findByRole('alert');
dismissNotification(notification);
};
const dragAndDropDirectionUp = 'DND_DIRECTION_UP'; const dragAndDropDirectionUp = 'DND_DIRECTION_UP';
const dragAndDropDirectionDown = 'DND_DIRECTION_DOWN'; const dragAndDropDirectionDown = 'DND_DIRECTION_DOWN';
@ -167,6 +158,4 @@ export {
dragAndDropDirectionUp, dragAndDropDirectionUp,
dragAndDropDirectionDown, dragAndDropDirectionDown,
dragAndDropListItem, dragAndDropListItem,
dismissNotification,
findAndDismissNotification,
}; };