Skip to content

Commit

Permalink
test: updated tests according to the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
eemaanamir committed Oct 30, 2024
1 parent 320852f commit 7556e19
Show file tree
Hide file tree
Showing 12 changed files with 331 additions and 13,881 deletions.
1 change: 0 additions & 1 deletion src/profile-v2/Certificates.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const Certificates = ({
}) => {
const intl = useIntl();

// Memoizing the renderCertificate function to prevent unnecessary re-renders
const renderCertificate = useCallback(({
certificateType, courseDisplayName, courseOrganization, modifiedDate, downloadUrl, courseId,
}) => {
Expand Down
31 changes: 0 additions & 31 deletions src/profile-v2/ProfilePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ import { injectIntl } from '@edx/frontend-platform/i18n';
import { Alert, Hyperlink } from '@openedx/paragon';
import {
fetchProfile,
saveProfile,
saveProfilePhoto,
deleteProfilePhoto,
openForm,
closeForm,
updateDraft,
} from './data/actions';
import ProfileAvatar from './forms/ProfileAvatar';
import Certificates from './Certificates';
Expand All @@ -35,7 +31,6 @@ const ProfilePage = ({ params, intl }) => {
dateJoined,
yearOfBirth,
courseCertificates,
visibilityCourseCertificates,
name,
profileImage,
savePhotoState,
Expand Down Expand Up @@ -65,22 +60,6 @@ const ProfilePage = ({ params, intl }) => {
dispatch(deleteProfilePhoto(context.authenticatedUser.username));

Check warning on line 60 in src/profile-v2/ProfilePage.jsx

View check run for this annotation

Codecov / codecov/patch

src/profile-v2/ProfilePage.jsx#L60

Added line #L60 was not covered by tests
};

const handleClose = (formId) => {
dispatch(closeForm(formId));
};

const handleOpen = (formId) => {
dispatch(openForm(formId));
};

const handleSubmit = (formId) => {
dispatch(saveProfile(formId, context.authenticatedUser.username));
};

const handleChange = (fieldName, value) => {
dispatch(updateDraft(fieldName, value));
};

const isYOBDisabled = () => {
const currentYear = new Date().getFullYear();
const isAgeOrNotCompliant = !yearOfBirth || ((currentYear - yearOfBirth) < 13);
Expand Down Expand Up @@ -125,13 +104,6 @@ const ProfilePage = ({ params, intl }) => {
return <PageLoading srMessage={intl.formatMessage(messages['profile.loading'])} />;
}

const commonFormProps = {
openHandler: handleOpen,
closeHandler: handleClose,
submitHandler: handleSubmit,
changeHandler: handleChange,
};

return (
<>
<div className="profile-page-bg-banner bg-primary d-md-block align-items-center px-75rem py-4rem h-100 w-100">
Expand Down Expand Up @@ -174,9 +146,7 @@ const ProfilePage = ({ params, intl }) => {
{isBlockVisible(courseCertificates.length) && (
<Certificates
certificates={courseCertificates}
visibilityCourseCertificates={visibilityCourseCertificates}
formId="certificates"
{...commonFormProps}
/>
)}
</div>
Expand All @@ -186,7 +156,6 @@ const ProfilePage = ({ params, intl }) => {

return (
<div className="profile-page">
{/* <Banner /> */}
{renderContent()}
</div>
);
Expand Down
156 changes: 2 additions & 154 deletions src/profile-v2/ProfilePage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ const storeMocks = {
loadingApp: require('./__mocks__/loadingApp.mockStore'),
viewOwnProfile: require('./__mocks__/viewOwnProfile.mockStore'),
viewOtherProfile: require('./__mocks__/viewOtherProfile.mockStore'),
savingEditedBio: require('./__mocks__/savingEditedBio.mockStore'),
};
const requiredProfilePageProps = {
fetchUserAccount: () => {},
fetchProfile: () => {},
saveProfile: () => {},
saveProfilePhoto: () => {},
deleteProfilePhoto: () => {},
openField: () => {},
closeField: () => {},
params: { username: 'staff' },
};

Expand Down Expand Up @@ -66,29 +60,27 @@ beforeEach(() => {
});

const ProfilePageWrapper = ({
contextValue, store, params, requiresParentalConsent,
contextValue, store, params,
}) => (
<AppContext.Provider
value={contextValue}
>
<IntlProvider locale="en">
<Provider store={store}>
<ProfilePage {...requiredProfilePageProps} params={params} requiresParentalConsent={requiresParentalConsent} />
<ProfilePage {...requiredProfilePageProps} params={params} />
</Provider>
</IntlProvider>
</AppContext.Provider>
);

ProfilePageWrapper.defaultProps = {
params: { username: 'staff' },
requiresParentalConsent: null,
};

ProfilePageWrapper.propTypes = {
contextValue: PropTypes.shape({}).isRequired,
store: PropTypes.shape({}).isRequired,
params: PropTypes.shape({}),
requiresParentalConsent: PropTypes.bool,
};

describe('<ProfilePage />', () => {
Expand Down Expand Up @@ -147,92 +139,6 @@ describe('<ProfilePage />', () => {
expect(tree).toMatchSnapshot();
});

it('while saving an edited bio', () => {
const contextValue = {
authenticatedUser: { userId: 123, username: 'staff', administrator: true },
config: getConfig(),
};
const component = (
<ProfilePageWrapper
contextValue={contextValue}
store={mockStore(storeMocks.savingEditedBio)}
/>
);
const { container: tree } = render(component);
expect(tree).toMatchSnapshot();
});

it('while saving an edited bio with error', () => {
const storeData = JSON.parse(JSON.stringify(storeMocks.savingEditedBio));
storeData.profilePage.errors.bio = { userMessage: 'bio error' };
const contextValue = {
authenticatedUser: { userId: 123, username: 'staff', administrator: true },
config: getConfig(),
};
const component = (
<ProfilePageWrapper
contextValue={contextValue}
store={mockStore(storeData)}
/>
);
const { container: tree } = render(component);
expect(tree).toMatchSnapshot();
});

it('test country edit with error', () => {
const storeData = JSON.parse(JSON.stringify(storeMocks.savingEditedBio));
storeData.profilePage.errors.country = { userMessage: 'country error' };
storeData.profilePage.currentlyEditingField = 'country';
const contextValue = {
authenticatedUser: { userId: 123, username: 'staff', administrator: true },
config: getConfig(),
};
const component = (
<ProfilePageWrapper
contextValue={contextValue}
store={mockStore(storeData)}
/>
);
const { container: tree } = render(component);
expect(tree).toMatchSnapshot();
});

it('test education edit with error', () => {
const storeData = JSON.parse(JSON.stringify(storeMocks.savingEditedBio));
storeData.profilePage.errors.levelOfEducation = { userMessage: 'education error' };
storeData.profilePage.currentlyEditingField = 'levelOfEducation';
const contextValue = {
authenticatedUser: { userId: 123, username: 'staff', administrator: true },
config: getConfig(),
};
const component = (
<ProfilePageWrapper
contextValue={contextValue}
store={mockStore(storeData)}
/>
);
const { container: tree } = render(component);
expect(tree).toMatchSnapshot();
});

it('test preferreded language edit with error', () => {
const storeData = JSON.parse(JSON.stringify(storeMocks.savingEditedBio));
storeData.profilePage.errors.languageProficiencies = { userMessage: 'preferred language error' };
storeData.profilePage.currentlyEditingField = 'languageProficiencies';
const contextValue = {
authenticatedUser: { userId: 123, username: 'staff', administrator: true },
config: getConfig(),
};
const component = (
<ProfilePageWrapper
contextValue={contextValue}
store={mockStore(storeData)}
/>
);
const { container: tree } = render(component);
expect(tree).toMatchSnapshot();
});

it('without credentials service', () => {
const config = getConfig();
config.CREDENTIALS_BASE_URL = '';
Expand All @@ -250,64 +156,6 @@ describe('<ProfilePage />', () => {
const { container: tree } = render(component);
expect(tree).toMatchSnapshot();
});
it('test age message alert', () => {
const storeData = JSON.parse(JSON.stringify(storeMocks.viewOwnProfile));
storeData.userAccount.requiresParentalConsent = true;
storeData.profilePage.account.requiresParentalConsent = true;
const contextValue = {
authenticatedUser: { userId: 123, username: 'staff', administrator: true },
config: { ...getConfig(), COLLECT_YEAR_OF_BIRTH: true },
};
const { container } = render(
<ProfilePageWrapper
contextValue={contextValue}
store={mockStore(storeData)}
requiresParentalConsent
/>,
);

expect(container.querySelector('.alert-info')).toHaveClass('show');
});
it('test photo error alert', () => {
const storeData = JSON.parse(JSON.stringify(storeMocks.viewOwnProfile));
storeData.profilePage.errors.photo = { userMessage: 'error' };
const contextValue = {
authenticatedUser: { userId: 123, username: 'staff', administrator: true },
config: { ...getConfig(), COLLECT_YEAR_OF_BIRTH: true },
};
const { container } = render(
<ProfilePageWrapper
contextValue={contextValue}
store={mockStore(storeData)}
requiresParentalConsent
/>,
);

expect(container.querySelector('.alert-danger')).toHaveClass('show');
});

it.each([
['test user with non-disabled country', 'PK'],
['test user with disabled country', 'RU'],
])('test user with %s', (_, accountCountry) => {
const storeData = JSON.parse(JSON.stringify(storeMocks.savingEditedBio));
storeData.profilePage.errors.country = {};
storeData.profilePage.currentlyEditingField = 'country';
storeData.profilePage.disabledCountries = ['RU'];
storeData.profilePage.account.country = accountCountry;
const contextValue = {
authenticatedUser: { userId: 123, username: 'staff', administrator: true },
config: getConfig(),
};
const component = (
<ProfilePageWrapper
contextValue={contextValue}
store={mockStore(storeData)}
/>
);
const { container: tree } = render(component);
expect(tree).toMatchSnapshot();
});
});

describe('handles analytics', () => {
Expand Down
2 changes: 0 additions & 2 deletions src/profile-v2/UsernameDescription.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React from 'react';
import { FormattedMessage } from '@edx/frontend-platform/i18n';
import { VisibilityOff } from '@openedx/paragon/icons';
import { Icon } from '@openedx/paragon';
import { getConfig } from '@edx/frontend-platform';

const UsernameDescription = () => (
Expand Down
Loading

0 comments on commit 7556e19

Please sign in to comment.