From e8be5b59265fba2223bde107f0765dd7537353a8 Mon Sep 17 00:00:00 2001 From: sundasnoreen12 Date: Tue, 26 Nov 2024 13:14:24 +0500 Subject: [PATCH] test: added not found test case --- src/profile/ProfilePage.jsx | 15 +- src/profile/ProfilePage.test.jsx | 36 +- .../__mocks__/invalidUser.mockStore.js | 41 ++ .../__snapshots__/ProfilePage.test.jsx.snap | 634 ++++++++++++++++++ 4 files changed, 717 insertions(+), 9 deletions(-) create mode 100644 src/profile/__mocks__/invalidUser.mockStore.js diff --git a/src/profile/ProfilePage.jsx b/src/profile/ProfilePage.jsx index d8b2f10..2dfe72d 100644 --- a/src/profile/ProfilePage.jsx +++ b/src/profile/ProfilePage.jsx @@ -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); } @@ -191,12 +183,19 @@ class ProfilePage extends React.Component { visibilityBio, requiresParentalConsent, isLoadingProfile, + username, + saveState, + navigate, } = this.props; if (isLoadingProfile) { return ; } + if (!username && saveState === 'error' && navigate) { + navigate('/notfound'); + } + const commonFormProps = { openHandler: this.handleOpen, closeHandler: this.handleClose, diff --git a/src/profile/ProfilePage.test.jsx b/src/profile/ProfilePage.test.jsx index e347ba8..7f1a771 100644 --- a/src/profile/ProfilePage.test.jsx +++ b/src/profile/ProfilePage.test.jsx @@ -9,6 +9,7 @@ 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'; @@ -16,6 +17,7 @@ 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'), @@ -65,6 +67,23 @@ beforeEach(() => { analytics.sendTrackingLogEvent.mockReset(); }); +const ProfileWrapper = ({ params, requiresParentalConsent }) => { + const navigate = useNavigate(); + return ( + + ); +}; + +ProfileWrapper.propTypes = { + params: PropTypes.shape({}).isRequired, + requiresParentalConsent: PropTypes.bool.isRequired, +}; + const ProfilePageWrapper = ({ contextValue, store, params, requiresParentalConsent, }) => ( @@ -73,7 +92,12 @@ const ProfilePageWrapper = ({ > - + + + @@ -103,6 +127,16 @@ describe('', () => { expect(tree).toMatchSnapshot(); }); + it('successfully redirected to not found page.', () => { + const contextValue = { + authenticatedUser: { userId: 123, username: 'staff', administrator: true }, + config: getConfig(), + }; + const component = ; + const { container: tree } = render(component); + expect(tree).toMatchSnapshot(); + }); + it('viewing own profile', () => { const contextValue = { authenticatedUser: { userId: 123, username: 'staff', administrator: true }, diff --git a/src/profile/__mocks__/invalidUser.mockStore.js b/src/profile/__mocks__/invalidUser.mockStore.js new file mode 100644 index 0000000..88a0438 --- /dev/null +++ b/src/profile/__mocks__/invalidUser.mockStore.js @@ -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' + } +}; diff --git a/src/profile/__snapshots__/ProfilePage.test.jsx.snap b/src/profile/__snapshots__/ProfilePage.test.jsx.snap index 843a2c9..077f5ab 100644 --- a/src/profile/__snapshots__/ProfilePage.test.jsx.snap +++ b/src/profile/__snapshots__/ProfilePage.test.jsx.snap @@ -29,6 +29,640 @@ exports[` Renders correctly in various states app loading 1`] = ` `; +exports[` Renders correctly in various states successfully redirected to not found page. 1`] = ` +
+
+
+
+
+
+
+
+
+
+ +
+
+
+ +
+
+
+
+
+
+ +

+ staff +

+
+ + + +
+ Your profile information is only visible to you. Only your username is visible to others on localhost. +
+
+
+
+
+ +
+
+
+
+
+ +

+ staff +

+
+ + + +
+ Your profile information is only visible to you. Only your username is visible to others on localhost. +
+
+
+
+
+ +
+
+
+

+ Full Name +

+
+
+ +
+ + This is the name that appears in your account and on your certificates. + +
+
+
+
+
+

+ Location +

+
+
+ +
+
+
+
+
+
+

+ Primary Language Spoken +

+
+
+ +
+
+
+
+
+
+

+ Education +

+
+
+ +
+
+
+
+
+
+

+ Social Links +

+
+
    +
  • +
    + +
    +
  • +
  • +
    + +
    +
  • +
  • +
    + +
    +
  • +
+
+
+
+
+
+
+
+

+ About Me +

+
+
+ +
+
+
+
+
+
+

+ My Certificates + +

+

+ + + + Everyone on localhost + +

+
+ You don't have any certificates yet. +
+
+
+
+
+
+
+`; + exports[` Renders correctly in various states test country edit with error 1`] = `