Skip to content

Commit

Permalink
test: added not found test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sundasnoreen12 committed Nov 26, 2024
1 parent b92efcd commit e8be5b5
Show file tree
Hide file tree
Showing 4 changed files with 717 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/profile/ProfilePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ class ProfilePage extends React.Component {
});
}

componentDidUpdate() {
const { username, navigate, saveState } = this.props;

if (!username && saveState === 'error') {
navigate('/notfound');
}
}

handleSaveProfilePhoto(formData) {
this.props.saveProfilePhoto(this.context.authenticatedUser.username, formData);
}
Expand Down Expand Up @@ -191,12 +183,19 @@ class ProfilePage extends React.Component {
visibilityBio,
requiresParentalConsent,
isLoadingProfile,
username,
saveState,
navigate,
} = this.props;

if (isLoadingProfile) {
return <PageLoading srMessage={this.props.intl.formatMessage(messages['profile.loading'])} />;
}

if (!username && saveState === 'error' && navigate) {
navigate('/notfound');
}

const commonFormProps = {
openHandler: this.handleOpen,
closeHandler: this.handleClose,
Expand Down
36 changes: 35 additions & 1 deletion src/profile/ProfilePage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import PropTypes from 'prop-types';
import { Provider } from 'react-redux';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { BrowserRouter, useNavigate } from 'react-router-dom';

import messages from '../i18n';
import ProfilePage from './ProfilePage';

const mockStore = configureMockStore([thunk]);
const storeMocks = {
loadingApp: require('./__mocks__/loadingApp.mockStore'),
invalidUser: require('./__mocks__/invalidUser.mockStore'),
viewOwnProfile: require('./__mocks__/viewOwnProfile.mockStore'),
viewOtherProfile: require('./__mocks__/viewOtherProfile.mockStore'),
savingEditedBio: require('./__mocks__/savingEditedBio.mockStore'),
Expand Down Expand Up @@ -65,6 +67,23 @@ beforeEach(() => {
analytics.sendTrackingLogEvent.mockReset();
});

const ProfileWrapper = ({ params, requiresParentalConsent }) => {
const navigate = useNavigate();
return (
<ProfilePage
{...requiredProfilePageProps}
params={params}
requiresParentalConsent={requiresParentalConsent}
navigate={navigate}
/>
);
};

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

const ProfilePageWrapper = ({
contextValue, store, params, requiresParentalConsent,
}) => (
Expand All @@ -73,7 +92,12 @@ const ProfilePageWrapper = ({
>
<IntlProvider locale="en">
<Provider store={store}>
<ProfilePage {...requiredProfilePageProps} params={params} requiresParentalConsent={requiresParentalConsent} />
<BrowserRouter>
<ProfileWrapper
params={params}
requiresParentalConsent={requiresParentalConsent}
/>
</BrowserRouter>
</Provider>
</IntlProvider>
</AppContext.Provider>
Expand Down Expand Up @@ -103,6 +127,16 @@ describe('<ProfilePage />', () => {
expect(tree).toMatchSnapshot();
});

it('successfully redirected to not found page.', () => {
const contextValue = {
authenticatedUser: { userId: 123, username: 'staff', administrator: true },
config: getConfig(),
};
const component = <ProfilePageWrapper contextValue={contextValue} store={mockStore(storeMocks.invalidUser)} />;
const { container: tree } = render(component);
expect(tree).toMatchSnapshot();
});

it('viewing own profile', () => {
const contextValue = {
authenticatedUser: { userId: 123, username: 'staff', administrator: true },
Expand Down
41 changes: 41 additions & 0 deletions src/profile/__mocks__/invalidUser.mockStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module.exports = {
userAccount: {
loading: false,
error: null,
username: 'staff',
email: null,
bio: null,
name: null,
country: null,
socialLinks: null,
profileImage: {
imageUrlMedium: null,
imageUrlLarge: null
},
levelOfEducation: null,
learningGoal: null
},
profilePage: {
errors: {},
saveState: 'error',
savePhotoState: null,
currentlyEditingField: null,
account: {
username: '',
socialLinks: []
},
preferences: {},
courseCertificates: [],
drafts: {},
isLoadingProfile: false,
isAuthenticatedUserProfile: true,
},
router: {
location: {
pathname: '/u/staffTest',
search: '',
hash: ''
},
action: 'POP'
}
};
Loading

0 comments on commit e8be5b5

Please sign in to comment.