Skip to content

Commit

Permalink
test: added tests for reducers
Browse files Browse the repository at this point in the history
  • Loading branch information
eemaanamir committed Nov 3, 2024
1 parent 06707df commit ce0d090
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 18 deletions.
11 changes: 10 additions & 1 deletion src/profile-v2/Certificates.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Certificates = ({
const intl = useIntl();

const renderCertificate = useCallback(({
certificateType, courseDisplayName, courseOrganization, modifiedDate, downloadUrl, courseId,
certificateType, courseDisplayName, courseOrganization, modifiedDate, downloadUrl, courseId, uuid,
}) => {
const certificateIllustration = (() => {
switch (certificateType) {
Expand Down Expand Up @@ -86,6 +86,15 @@ const Certificates = ({
{intl.formatMessage(messages['profile.certificates.view.certificate'])}
</Hyperlink>
</div>
<p className="small mb-0 pt-3">
<FormattedMessage
id="profile.certificate.uuid"
defaultMessage="Credential ID {certificate_uuid}"
values={{
certificate_uuid: uuid,
}}
/>
</p>
</div>
</div>
</div>
Expand Down
10 changes: 5 additions & 5 deletions src/profile-v2/ProfilePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const ProfilePage = ({ params, intl }) => {
}

return (
<Hyperlink className="btn btn-brand btn-rounded font-weight-normal" target="_blank" showLaunchIcon={false} destination={viewMyRecordsUrl}>
<Hyperlink className="btn btn-brand btn-rounded font-weight-normal px-4 py-0625rem text-nowrap" target="_blank" showLaunchIcon={false} destination={viewMyRecordsUrl}>
{intl.formatMessage(messages['profile.viewMyRecords'])}
</Hyperlink>
);
Expand Down Expand Up @@ -107,11 +107,11 @@ const ProfilePage = ({ params, intl }) => {
return (
<>
<div className="profile-page-bg-banner bg-primary d-md-block align-items-center px-75rem py-4rem h-100 w-100">
<div className="col container-fluid w-100 h-100 bg-white py-0 pl-25rem pr-0 rounded-75">
<div className="col container-fluid w-100 h-100 bg-white py-0 pl-25rem pr-25rem rounded-75">
<div className="col h-100 w-100 py-4 px-0 justify-content-start g-15rem">
<div className="row-auto d-flex align-items-center h-100 w-100 justify-content-start g-15rem">
<div className="row-auto d-flex flex-wrap align-items-center h-100 w-100 justify-content-start g-15rem">
<ProfileAvatar
className=""
className="col p-0"
src={profileImage.src}
isDefault={profileImage.isDefault}
onSave={handleSaveProfilePhoto}
Expand All @@ -129,7 +129,7 @@ const ProfilePage = ({ params, intl }) => {
<CertificateCount count={courseCertificates.length} />
</div>
</div>
<div className="pr-25rem">
<div className="col-auto p-0">
{renderViewMyRecordsButton()}
</div>
</div>
Expand Down
32 changes: 21 additions & 11 deletions src/profile-v2/__snapshots__/ProfilePage.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ exports[`<ProfilePage /> Renders correctly in various states viewing other profi
class="profile-page-bg-banner bg-primary d-md-block align-items-center px-75rem py-4rem h-100 w-100"
>
<div
class="col container-fluid w-100 h-100 bg-white py-0 pl-25rem pr-0 rounded-75"
class="col container-fluid w-100 h-100 bg-white py-0 pl-25rem pr-25rem rounded-75"
>
<div
class="col h-100 w-100 py-4 px-0 justify-content-start g-15rem"
>
<div
class="row-auto d-flex align-items-center h-100 w-100 justify-content-start g-15rem"
class="row-auto d-flex flex-wrap align-items-center h-100 w-100 justify-content-start g-15rem"
>
<div
class="profile-avatar-wrap position-relative"
Expand Down Expand Up @@ -100,10 +100,10 @@ exports[`<ProfilePage /> Renders correctly in various states viewing other profi
</div>
</div>
<div
class="pr-25rem"
class="col-auto p-0"
>
<a
class="pgn__hyperlink default-link standalone-link btn btn-brand btn-rounded font-weight-normal"
class="pgn__hyperlink default-link standalone-link btn btn-brand btn-rounded font-weight-normal px-4 py-0625rem text-nowrap"
href="http://localhost:18150/records"
rel="noopener noreferrer"
target="_blank"
Expand Down Expand Up @@ -169,13 +169,13 @@ exports[`<ProfilePage /> Renders correctly in various states viewing own profile
class="profile-page-bg-banner bg-primary d-md-block align-items-center px-75rem py-4rem h-100 w-100"
>
<div
class="col container-fluid w-100 h-100 bg-white py-0 pl-25rem pr-0 rounded-75"
class="col container-fluid w-100 h-100 bg-white py-0 pl-25rem pr-25rem rounded-75"
>
<div
class="col h-100 w-100 py-4 px-0 justify-content-start g-15rem"
>
<div
class="row-auto d-flex align-items-center h-100 w-100 justify-content-start g-15rem"
class="row-auto d-flex flex-wrap align-items-center h-100 w-100 justify-content-start g-15rem"
>
<div
class="profile-avatar-wrap position-relative"
Expand Down Expand Up @@ -263,10 +263,10 @@ exports[`<ProfilePage /> Renders correctly in various states viewing own profile
</div>
</div>
<div
class="pr-25rem"
class="col-auto p-0"
>
<a
class="pgn__hyperlink default-link standalone-link btn btn-brand btn-rounded font-weight-normal"
class="pgn__hyperlink default-link standalone-link btn btn-brand btn-rounded font-weight-normal px-4 py-0625rem text-nowrap"
href="http://localhost:18150/records"
rel="noopener noreferrer"
target="_blank"
Expand Down Expand Up @@ -371,6 +371,11 @@ exports[`<ProfilePage /> Renders correctly in various states viewing own profile
View Certificate
</a>
</div>
<p
class="small mb-0 pt-3"
>
Credential ID
</p>
</div>
</div>
</div>
Expand All @@ -391,13 +396,13 @@ exports[`<ProfilePage /> Renders correctly in various states without credentials
class="profile-page-bg-banner bg-primary d-md-block align-items-center px-75rem py-4rem h-100 w-100"
>
<div
class="col container-fluid w-100 h-100 bg-white py-0 pl-25rem pr-0 rounded-75"
class="col container-fluid w-100 h-100 bg-white py-0 pl-25rem pr-25rem rounded-75"
>
<div
class="col h-100 w-100 py-4 px-0 justify-content-start g-15rem"
>
<div
class="row-auto d-flex align-items-center h-100 w-100 justify-content-start g-15rem"
class="row-auto d-flex flex-wrap align-items-center h-100 w-100 justify-content-start g-15rem"
>
<div
class="profile-avatar-wrap position-relative"
Expand Down Expand Up @@ -485,7 +490,7 @@ exports[`<ProfilePage /> Renders correctly in various states without credentials
</div>
</div>
<div
class="pr-25rem"
class="col-auto p-0"
/>
</div>
<div
Expand Down Expand Up @@ -584,6 +589,11 @@ exports[`<ProfilePage /> Renders correctly in various states without credentials
View Certificate
</a>
</div>
<p
class="small mb-0 pt-3"
>
Credential ID
</p>
</div>
</div>
</div>
Expand Down
140 changes: 140 additions & 0 deletions src/profile-v2/data/reducers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import profilePage, { initialState } from './reducers';
import {
SAVE_PROFILE_PHOTO,
DELETE_PROFILE_PHOTO,
FETCH_PROFILE,
} from './actions';

describe('profilePage reducer', () => {
it('should return the initial state by default', () => {
expect(profilePage(undefined, {})).toEqual(initialState);
});

describe('FETCH_PROFILE actions', () => {
it('should handle FETCH_PROFILE.BEGIN', () => {
const action = { type: FETCH_PROFILE.BEGIN };
const expectedState = {
...initialState,
// Uncomment isLoadingProfile: true if this functionality is required.
};
expect(profilePage(initialState, action)).toEqual(expectedState);
});

it('should handle FETCH_PROFILE.SUCCESS', () => {
const action = {
type: FETCH_PROFILE.SUCCESS,
account: { name: 'John Doe' },
preferences: { theme: 'dark' },
courseCertificates: ['cert1', 'cert2'],
isAuthenticatedUserProfile: true,
};
const expectedState = {
...initialState,
account: action.account,
preferences: action.preferences,
courseCertificates: action.courseCertificates,
isLoadingProfile: false,
isAuthenticatedUserProfile: action.isAuthenticatedUserProfile,
};
expect(profilePage(initialState, action)).toEqual(expectedState);
});
});

describe('SAVE_PROFILE_PHOTO actions', () => {
it('should handle SAVE_PROFILE_PHOTO.BEGIN', () => {
const action = { type: SAVE_PROFILE_PHOTO.BEGIN };
const expectedState = {
...initialState,
savePhotoState: 'pending',
errors: {},
};
expect(profilePage(initialState, action)).toEqual(expectedState);
});

it('should handle SAVE_PROFILE_PHOTO.SUCCESS', () => {
const action = {
type: SAVE_PROFILE_PHOTO.SUCCESS,
payload: { profileImage: 'new-image-url.jpg' },
};
const expectedState = {
...initialState,
account: { ...initialState.account, profileImage: action.payload.profileImage },
savePhotoState: 'complete',
errors: {},
};
expect(profilePage(initialState, action)).toEqual(expectedState);
});

it('should handle SAVE_PROFILE_PHOTO.FAILURE', () => {
const action = {
type: SAVE_PROFILE_PHOTO.FAILURE,
payload: { error: 'Photo upload failed' },
};
const expectedState = {
...initialState,
savePhotoState: 'error',
errors: { photo: action.payload.error },
};
expect(profilePage(initialState, action)).toEqual(expectedState);
});

it('should handle SAVE_PROFILE_PHOTO.RESET', () => {
const action = { type: SAVE_PROFILE_PHOTO.RESET };
const expectedState = {
...initialState,
savePhotoState: null,
errors: {},
};
expect(profilePage(initialState, action)).toEqual(expectedState);
});
});

describe('DELETE_PROFILE_PHOTO actions', () => {
it('should handle DELETE_PROFILE_PHOTO.BEGIN', () => {
const action = { type: DELETE_PROFILE_PHOTO.BEGIN };
const expectedState = {
...initialState,
savePhotoState: 'pending',
errors: {},
};
expect(profilePage(initialState, action)).toEqual(expectedState);
});

it('should handle DELETE_PROFILE_PHOTO.SUCCESS', () => {
const action = {
type: DELETE_PROFILE_PHOTO.SUCCESS,
payload: { profileImage: 'default-image-url.jpg' },
};
const expectedState = {
...initialState,
account: { ...initialState.account, profileImage: action.payload.profileImage },
savePhotoState: 'complete',
errors: {},
};
expect(profilePage(initialState, action)).toEqual(expectedState);
});

it('should handle DELETE_PROFILE_PHOTO.FAILURE', () => {
const action = {
type: DELETE_PROFILE_PHOTO.FAILURE,
payload: { errors: { delete: 'Failed to delete photo' } },
};
const expectedState = {
...initialState,
savePhotoState: 'error',
errors: { ...initialState.errors, delete: action.payload.errors.delete },
};
expect(profilePage(initialState, action)).toEqual(expectedState);
});

it('should handle DELETE_PROFILE_PHOTO.RESET', () => {
const action = { type: DELETE_PROFILE_PHOTO.RESET };
const expectedState = {
...initialState,
savePhotoState: null,
errors: {},
};
expect(profilePage(initialState, action)).toEqual(expectedState);
});
});
});
2 changes: 1 addition & 1 deletion src/profile-v2/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
top: 1rem;
right: 1rem;
bottom: 0;
width: 13.5rem;
width: 15.15rem;
opacity: .06;
background-size: 90%;
background-repeat: no-repeat;
Expand Down

0 comments on commit ce0d090

Please sign in to comment.