Skip to content

Commit

Permalink
Revert "Revert "feat: Use country-state-city to populate state dropdo…
Browse files Browse the repository at this point in the history
…wn in StateProvinceFormInput"" (#856)

* Revert "Revert "feat: Use country-state-city to populate state dropdown in St…"

This reverts commit 9dc662f.

* test: Update CardHolderInformation test with new getCountryStatesMap
  • Loading branch information
julianajlk authored Apr 4, 2024
1 parent 54a8d04 commit 5552cbf
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 127 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"bootstrap": "4.6.1",
"classnames": "^2.3.1",
"core-js": "^3.23.5",
"country-state-city": "^3.2.1",
"form-urlencoded": "^6.0.6",
"lodash.camelcase": "^4.3.0",
"lodash.snakecase": "^4.1.1",
Expand Down
11 changes: 6 additions & 5 deletions src/payment/checkout/payment-form/CardHolderInformation.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import { createStore } from 'redux';
import CardHolderInformation from './CardHolderInformation';
import PaymentForm from './PaymentForm';
import createRootReducer from '../../../data/reducers';
import countryStatesMap from './utils/countryStatesMap';
import { getCountryStatesMap, isPostalCodeRequired } from './utils/form-validators';

import '../../__factories__/userAccount.factory';

jest.mock('@edx/frontend-platform/analytics', () => ({
sendTrackEvent: jest.fn(),
}));
jest.mock('./utils/countryStatesMap', () => ({
__esModule: true,
default: jest.fn(),
jest.mock('./utils/form-validators', () => ({
getCountryStatesMap: jest.fn(),
isPostalCodeRequired: jest.fn(),
}));

configureI18n({
Expand Down Expand Up @@ -73,7 +73,8 @@ describe('<CardHolderInformation />', () => {

fireEvent.change(screen.getByLabelText('Country (required)'), { target: { value: 'US' } });

expect(countryStatesMap).toHaveBeenCalledWith('US');
expect(getCountryStatesMap).toHaveBeenCalledWith('US');
expect(isPostalCodeRequired).toHaveBeenCalledWith('US');
});
});
describe('purchasedForOrganization field', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/payment/checkout/payment-form/StateProvinceFormInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import PropTypes from 'prop-types';
import { Field } from 'redux-form';
import { injectIntl, intlShape, FormattedMessage } from '@edx/frontend-platform/i18n';

import { getCountryStatesMap } from './utils/form-validators';
import FormInput from './FormInput';
import FormSelect from './FormSelect';
import getStates from './utils/countryStatesMap';
import messages from './StateProvinceFormInput.messages';

class StateProvinceFormInput extends React.Component {
getOptions() {
const options = [];
const { country } = this.props;
const states = getStates(country);
const states = getCountryStatesMap(country);

if (states) {
options.push([(
Expand Down
25 changes: 22 additions & 3 deletions src/payment/checkout/payment-form/StripePaymentForm.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,35 @@ describe('<StripePaymentForm />', () => {
lastName: '',
address: '',
city: '',
country: 'GB',
country: 'AQ', // Antarctica does not have states, postal code not required
optionalField: '',
},
{
firstName: '',
lastName: '',
address: '',
city: '',
country: 'GB', // United Kingdom has states, state becomes required, postal code is required
postalCode: '',
state: '',
optionalField: '',
},
{
firstName: '',
lastName: '',
address: '',
city: '',
country: 'CA', // Canada state and postal code are required
postalCode: '',
state: '',
optionalField: '',
},
{
firstName: '',
lastName: '',
address: '',
city: '',
country: 'US',
country: 'US', // United States state and postal code are required
postalCode: '',
state: '',
optionalField: '',
Expand All @@ -144,7 +163,7 @@ describe('<StripePaymentForm />', () => {
lastName: '',
address: '',
city: '',
country: 'IN',
country: 'IN', // India state is required
state: '',
optionalField: '',
},
Expand Down
115 changes: 0 additions & 115 deletions src/payment/checkout/payment-form/utils/countryStatesMap.js

This file was deleted.

20 changes: 18 additions & 2 deletions src/payment/checkout/payment-form/utils/form-validators.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import getStates from './countryStatesMap';
// eslint-disable-next-line import/no-extraneous-dependencies
import { State } from 'country-state-city';

export const getCountryStatesMap = (country) => {
const states = State.getStatesOfCountry(country);

if (!states.length) {
return null;
}
const statesMap = {};
states.forEach((state) => {
statesMap[state.isoCode] = state.name;
});
return country && statesMap;
};

// eslint-disable-next-line import/prefer-default-export
export function isPostalCodeRequired(selectedCountry) {
Expand Down Expand Up @@ -39,7 +53,9 @@ export function getRequiredFields(fieldValues, isBulkOrder = false, enableStripe
requiredFields.postalCode = postalCode;
}

if (getStates(country)) {
// By using the country-state-city library to populate states, every country that
// has states from the ISO 3166-2 list will have states as a required field
if (getCountryStatesMap(country)) {
requiredFields.state = state;
}

Expand Down

0 comments on commit 5552cbf

Please sign in to comment.