From 2fc0bd68d448d3b9c3f7deef629ce069a286620f Mon Sep 17 00:00:00 2001 From: Awais Ansari Date: Sat, 14 Sep 2024 15:45:59 +0500 Subject: [PATCH 1/4] feat: added country disabling feature --- src/profile/data/reducers.js | 1 + src/profile/data/selectors.js | 7 ++++++- src/profile/forms/Country.jsx | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/profile/data/reducers.js b/src/profile/data/reducers.js index 2e6e9eca5..88a1bc9c0 100644 --- a/src/profile/data/reducers.js +++ b/src/profile/data/reducers.js @@ -22,6 +22,7 @@ export const initialState = { drafts: {}, isLoadingProfile: true, isAuthenticatedUserProfile: false, + disabledCountries: ['RU'], }; const profilePage = (state = initialState, action = {}) => { diff --git a/src/profile/data/selectors.js b/src/profile/data/selectors.js index b04493e6e..211cdf0d4 100644 --- a/src/profile/data/selectors.js +++ b/src/profile/data/selectors.js @@ -23,6 +23,7 @@ export const isLoadingProfileSelector = state => state.profilePage.isLoadingProf export const currentlyEditingFieldSelector = state => state.profilePage.currentlyEditingField; export const accountErrorsSelector = state => state.profilePage.errors; export const isAuthenticatedUserProfileSelector = state => state.profilePage.isAuthenticatedUserProfile; +export const disabledCountriesSelector = state => state.profilePage.disabledCountries; export const editableFormModeSelector = createSelector( profileAccountSelector, @@ -130,10 +131,14 @@ export const countrySelector = createSelector( editableFormSelector, sortedCountriesSelector, countryMessagesSelector, - (editableForm, sortedCountries, countryMessages) => ({ + disabledCountriesSelector, + profileAccountSelector, + (editableForm, sortedCountries, countryMessages, disabledCountries, account) => ({ ...editableForm, sortedCountries, countryMessages, + disabledCountries, + committedCountry: account.country, }), ); diff --git a/src/profile/forms/Country.jsx b/src/profile/forms/Country.jsx index 0048017e6..d9ee4725b 100644 --- a/src/profile/forms/Country.jsx +++ b/src/profile/forms/Country.jsx @@ -35,7 +35,11 @@ class Country extends React.Component { handleSubmit(e) { e.preventDefault(); - this.props.submitHandler(this.props.formId); + const { country, disabledCountries, formId, submitHandler } = this.props; + + if (!disabledCountries.includes(country)) { + submitHandler(formId); + } } handleClose() { @@ -46,6 +50,26 @@ class Country extends React.Component { this.props.openHandler(this.props.formId); } + isDisabledCountry = (country) => { + const { disabledCountries } = this.props; + return disabledCountries.includes(country); + }; + + filteredCountries = (countryList) => { + const { disabledCountries, committedCountry } = this.props; + + if (!disabledCountries.length) { + return countryList; + } + + return countryList.filter(({ code }) => { + const isDisabled = this.isDisabledCountry(code); + const isUserCountry = code === committedCountry; + + return !isDisabled || isUserCountry; + }); + }; + render() { const { formId, @@ -58,6 +82,7 @@ class Country extends React.Component { sortedCountries, countryMessages, } = this.props; + const filteredCountries = this.filteredCountries(sortedCountries); return ( - {sortedCountries.map(({ code, name }) => ( - + {filteredCountries.map(({ code, name }) => ( + ))} {error !== null && ( @@ -157,7 +182,9 @@ Country.propTypes = { code: PropTypes.string.isRequired, name: PropTypes.string.isRequired, })).isRequired, + disabledCountries: PropTypes.arrayOf(PropTypes.string), countryMessages: PropTypes.objectOf(PropTypes.string).isRequired, + committedCountry: PropTypes.string, // Actions changeHandler: PropTypes.func.isRequired, @@ -175,6 +202,8 @@ Country.defaultProps = { country: null, visibilityCountry: 'private', error: null, + disabledCountries: [], + committedCountry: '', }; export default connect( From 24f8d26f5d3cbd61efd66f39513236893a6634f1 Mon Sep 17 00:00:00 2001 From: Awais Ansari Date: Sat, 14 Sep 2024 15:56:52 +0500 Subject: [PATCH 2/4] fix: lint errors --- src/profile/forms/Country.jsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/profile/forms/Country.jsx b/src/profile/forms/Country.jsx index d9ee4725b..ffeaa24e5 100644 --- a/src/profile/forms/Country.jsx +++ b/src/profile/forms/Country.jsx @@ -35,7 +35,9 @@ class Country extends React.Component { handleSubmit(e) { e.preventDefault(); - const { country, disabledCountries, formId, submitHandler } = this.props; + const { + country, disabledCountries, formId, submitHandler, + } = this.props; if (!disabledCountries.includes(country)) { submitHandler(formId); @@ -50,7 +52,7 @@ class Country extends React.Component { this.props.openHandler(this.props.formId); } - isDisabledCountry = (country) => { + isDisabledCountry = (country) => { const { disabledCountries } = this.props; return disabledCountries.includes(country); }; From 0197581902d7d9812120fcf0531c487b995f9c05 Mon Sep 17 00:00:00 2001 From: Awais Ansari Date: Sat, 14 Sep 2024 18:14:43 +0500 Subject: [PATCH 3/4] test: added test case for disabled countries --- src/profile/ProfilePage.test.jsx | 39 + .../__mocks__/savingEditedBio.mockStore.js | 3 +- .../__snapshots__/ProfilePage.test.jsx.snap | 4230 +++++++++++++++++ 3 files changed, 4271 insertions(+), 1 deletion(-) diff --git a/src/profile/ProfilePage.test.jsx b/src/profile/ProfilePage.test.jsx index 997a92e86..ae1152a4a 100644 --- a/src/profile/ProfilePage.test.jsx +++ b/src/profile/ProfilePage.test.jsx @@ -285,6 +285,45 @@ describe('', () => { expect(container.querySelector('.alert-danger')).toHaveClass('show'); }); + + it('test user with non disabled country', () => { + const storeData = JSON.parse(JSON.stringify(storeMocks.savingEditedBio)); + storeData.profilePage.errors.country = {}; + storeData.profilePage.currentlyEditingField = 'country'; + storeData.profilePage.disabledCountries = ['RU']; + const contextValue = { + authenticatedUser: { userId: 123, username: 'staff', administrator: true }, + config: getConfig(), + }; + const component = ( + + ); + const { container: tree } = render(component); + expect(tree).toMatchSnapshot(); + }); + + it('test user with disabled country', () => { + const storeData = JSON.parse(JSON.stringify(storeMocks.savingEditedBio)); + storeData.profilePage.errors.country = {}; + storeData.profilePage.currentlyEditingField = 'country'; + storeData.profilePage.disabledCountries = ['RU']; + storeData.profilePage.account.country = 'RU'; + const contextValue = { + authenticatedUser: { userId: 123, username: 'staff', administrator: true }, + config: getConfig(), + }; + const component = ( + + ); + const { container: tree } = render(component); + expect(tree).toMatchSnapshot(); + }); }); describe('handles analytics', () => { diff --git a/src/profile/__mocks__/savingEditedBio.mockStore.js b/src/profile/__mocks__/savingEditedBio.mockStore.js index 63551e823..a104762d6 100644 --- a/src/profile/__mocks__/savingEditedBio.mockStore.js +++ b/src/profile/__mocks__/savingEditedBio.mockStore.js @@ -125,7 +125,8 @@ module.exports = { } ], drafts: {}, - isLoadingProfile: false + isLoadingProfile: false, + disabledCountries: [], }, router: { location: { diff --git a/src/profile/__snapshots__/ProfilePage.test.jsx.snap b/src/profile/__snapshots__/ProfilePage.test.jsx.snap index bb9c11d0d..5e26ad9dc 100644 --- a/src/profile/__snapshots__/ProfilePage.test.jsx.snap +++ b/src/profile/__snapshots__/ProfilePage.test.jsx.snap @@ -5107,6 +5107,4236 @@ exports[` Renders correctly in various states test preferreded la `; +exports[` Renders correctly in various states test user with disabled country 1`] = ` +
+
+
+
+
+
+
+
+
+
+ +
+ profile avatar +
+
+ +
+
+
+
+
+
+ +

+ staff +

+

+ Member since + 2017 +

+
+
+
+
+
+
+
+
+
+ +

+ staff +

+

+ Member since + 2017 +

+
+
+
+
+
+
+
+

+ Full Name + +

+

+ + + + Just me + +

+
+

+ Lemon Seltzer +

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

+ Primary Language Spoken + +

+

+ + + + Everyone on localhost + +

+
+

+ Yoruba +

+
+
+
+
+
+

+ Education + +

+

+ + + + Just me + +

+
+

+ Elementary/primary school +

+
+
+
+
+
+

+ Social Links + +

+

+ + + + Everyone on localhost + +

+
+ +
+
+
+
+
+
+
+

+ About Me + +

+

+ + + + Everyone on localhost + +

+
+

+ This is my bio +

+
+
+
+
+
+

+ My Certificates + +

+

+ + + + Everyone on localhost + +

+
+
+
+
+
+
+
+

+ Verified Certificate +

+

+ edX Demonstration Course +

+
+

+ From +

+

+ edX +

+
+

+ Completed on + 3/4/2019 +

+ +
+
+
+
+
+
+
+
+
+
+
+`; + +exports[` Renders correctly in various states test user with non disabled country 1`] = ` +
+
+
+
+
+
+
+
+
+
+ +
+ profile avatar +
+
+ +
+
+
+
+
+
+ +

+ staff +

+

+ Member since + 2017 +

+
+
+
+
+
+
+
+
+
+ +

+ staff +

+

+ Member since + 2017 +

+
+
+
+
+
+
+
+

+ Full Name + +

+

+ + + + Just me + +

+
+

+ Lemon Seltzer +

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

+ Primary Language Spoken + +

+

+ + + + Everyone on localhost + +

+
+

+ Yoruba +

+
+
+
+
+
+

+ Education + +

+

+ + + + Just me + +

+
+

+ Elementary/primary school +

+
+
+
+
+
+

+ Social Links + +

+

+ + + + Everyone on localhost + +

+
+ +
+
+
+
+
+
+
+

+ About Me + +

+

+ + + + Everyone on localhost + +

+
+

+ This is my bio +

+
+
+
+
+
+

+ My Certificates + +

+

+ + + + Everyone on localhost + +

+
+
+
+
+
+
+
+

+ Verified Certificate +

+

+ edX Demonstration Course +

+
+

+ From +

+

+ edX +

+
+

+ Completed on + 3/4/2019 +

+ +
+
+
+
+
+
+
+
+
+
+
+`; + exports[` Renders correctly in various states viewing other profile with all fields 1`] = `
Date: Mon, 16 Sep 2024 19:53:07 +0500 Subject: [PATCH 4/4] refactor: combined test cases --- src/profile/ProfilePage.test.jsx | 26 ++++--------------- .../__snapshots__/ProfilePage.test.jsx.snap | 4 +-- 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/src/profile/ProfilePage.test.jsx b/src/profile/ProfilePage.test.jsx index ae1152a4a..e347ba8c5 100644 --- a/src/profile/ProfilePage.test.jsx +++ b/src/profile/ProfilePage.test.jsx @@ -286,31 +286,15 @@ describe('', () => { expect(container.querySelector('.alert-danger')).toHaveClass('show'); }); - it('test user with non disabled country', () => { + 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']; - const contextValue = { - authenticatedUser: { userId: 123, username: 'staff', administrator: true }, - config: getConfig(), - }; - const component = ( - - ); - const { container: tree } = render(component); - expect(tree).toMatchSnapshot(); - }); - - it('test user with disabled country', () => { - const storeData = JSON.parse(JSON.stringify(storeMocks.savingEditedBio)); - storeData.profilePage.errors.country = {}; - storeData.profilePage.currentlyEditingField = 'country'; - storeData.profilePage.disabledCountries = ['RU']; - storeData.profilePage.account.country = 'RU'; + storeData.profilePage.account.country = accountCountry; const contextValue = { authenticatedUser: { userId: 123, username: 'staff', administrator: true }, config: getConfig(), diff --git a/src/profile/__snapshots__/ProfilePage.test.jsx.snap b/src/profile/__snapshots__/ProfilePage.test.jsx.snap index 5e26ad9dc..843a2c931 100644 --- a/src/profile/__snapshots__/ProfilePage.test.jsx.snap +++ b/src/profile/__snapshots__/ProfilePage.test.jsx.snap @@ -5107,7 +5107,7 @@ exports[` Renders correctly in various states test preferreded la
`; -exports[` Renders correctly in various states test user with disabled country 1`] = ` +exports[` Renders correctly in various states test user with test user with disabled country 1`] = `
Renders correctly in various states test user with disa
`; -exports[` Renders correctly in various states test user with non disabled country 1`] = ` +exports[` Renders correctly in various states test user with test user with non-disabled country 1`] = `