Skip to content

Commit

Permalink
useractions.unit.test : profile update actions
Browse files Browse the repository at this point in the history
  • Loading branch information
cfiguer055 committed Jun 20, 2024
1 parent bddabad commit 3bd5c66
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions SmartClothingApp/__test__/userActions.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
EmailAuthProvider,
reauthenticateWithCredential,
updatePassword,
signOut,
} from 'firebase/auth';
import { toastError } from "../src/actions/toastActions.js";
import {
Expand Down Expand Up @@ -74,6 +75,7 @@ jest.mock('firebase/auth', () => ({
reauthenticateWithCredential: jest.fn(),
updatePassword: jest.fn(),
storeUID: jest.fn(),
signOut: jest.fn(),
auth: {
updateEmail: jest.fn(),
},
Expand Down Expand Up @@ -205,6 +207,7 @@ describe('Async User Actions', () => {
it('should call storeUID with user UID on successful login', async () => {
const user = { uid: 'testUID', email: '[email protected]', displayName: 'John Doe' };
const store = mockStore({});

signInWithEmailAndPassword.mockResolvedValue({ user });

await store.dispatch(startLoginWithEmail('[email protected]', 'password123'));
Expand All @@ -219,8 +222,13 @@ describe('Async User Actions', () => {

await store.dispatch(startLoginWithEmail('[email protected]', 'password123'));

await flushPromises();

const actions = store.getActions();
expect(actions[0]).toEqual(toastError('The password is invalid or the user does not have a password.'));

const expectedErrorsMessage = firebaseErrorsMessages[error.code] || "Wrong password.";

expect(actions[0]).toEqual(toastError(expectedErrorsMessage));
});

it('should dispatch LOGOUT and toastError on successful logout', async () => {
Expand All @@ -236,25 +244,38 @@ describe('Async User Actions', () => {

});




// ### Profile Update Actions

describe('Profile Update Actions', () => {
it('should dispatch UPDATE_PROFILE on successful profile update', async () => {
// const store = mockStore({});
const store = mockStore({});

// updateProfile.mockResolvedValue();
updateProfile.mockResolvedValue();

// await store.dispatch(startUpdateProfile('John', 'Doe'));
await store.dispatch(startUpdateProfile('John', 'Doe'));

// const action = store.getActions();
// expect(action[0]).toEqual({
// type: UPDATE_PROFILE,
// payload: ['John', 'Doe'],
// });
const action = store.getActions();
expect(action[0]).toEqual({
type: UPDATE_PROFILE,
payload: ['John', 'Doe'],
});
});

it('should dispatch toastError with appropriate message on profile update failure', async () => {
const error = { code: '' };
const store = mockStore({});

updateProfile.mockRejectedValue(new Error('Error updating profile!'));

await store.dispatch(startUpdateProfile('John', 'Doe'));

await flushPromises();

const action = store.getActions();
expect(action[0]).toEqual(toastError('Error updating profile!'));
});
});

Expand Down

0 comments on commit 3bd5c66

Please sign in to comment.