Skip to content

Commit a411e2e

Browse files
ENGCOM-6389: Removes hardcoded references to country selector component #25541
- Merge Pull Request #25541 from krzksz/magento2:checkout-country-options - Merged commits: 1. 666cca8
2 parents 67fe58f + 666cca8 commit a411e2e

File tree

6 files changed

+237
-100
lines changed

6 files changed

+237
-100
lines changed

app/code/Magento/Checkout/view/frontend/web/js/view/shipping.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ define([
6060
template: 'Magento_Checkout/shipping',
6161
shippingFormTemplate: 'Magento_Checkout/shipping-address/form',
6262
shippingMethodListTemplate: 'Magento_Checkout/shipping-address/shipping-method-list',
63-
shippingMethodItemTemplate: 'Magento_Checkout/shipping-address/shipping-method-item'
63+
shippingMethodItemTemplate: 'Magento_Checkout/shipping-address/shipping-method-item',
64+
imports: {
65+
countryOptions: '${ $.parentName }.shippingAddress.shipping-address-fieldset.country_id:indexedOptions'
66+
}
6467
},
6568
visible: ko.observable(!quote.isVirtual()),
6669
errorValidationMessage: ko.observable(false),
@@ -276,9 +279,7 @@ define([
276279
loginFormSelector = 'form[data-role=email-with-possible-login]',
277280
emailValidationResult = customer.isLoggedIn(),
278281
field,
279-
country = registry.get(this.parentName + '.shippingAddress.shipping-address-fieldset.country_id'),
280-
countryIndexedOptions = country.indexedOptions,
281-
option = countryIndexedOptions[quote.shippingAddress().countryId],
282+
option = _.isObject(this.countryOptions) && this.countryOptions[quote.shippingAddress().countryId],
282283
messageContainer = registry.get('checkout.errors').messageContainer;
283284

284285
if (!quote.shippingMethod()) {

app/code/Magento/Ui/view/base/web/js/form/element/post-code.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
*/
99
define([
1010
'underscore',
11-
'uiRegistry',
1211
'./abstract'
13-
], function (_, registry, Abstract) {
12+
], function (_, Abstract) {
1413
'use strict';
1514

1615
return Abstract.extend({
1716
defaults: {
1817
imports: {
18+
countryOptions: '${ $.parentName }.country_id:indexedOptions',
1919
update: '${ $.parentName }.country_id:value'
2020
}
2121
},
@@ -41,31 +41,32 @@ define([
4141
},
4242

4343
/**
44-
* @param {String} value
44+
* Method called every time country selector's value gets changed.
45+
* Updates all validations and requirements for certain country.
46+
* @param {String} value - Selected country ID.
4547
*/
4648
update: function (value) {
47-
var country = registry.get(this.parentName + '.' + 'country_id'),
48-
options = country.indexedOptions,
49-
option = null;
49+
var isZipCodeOptional,
50+
option;
5051

5152
if (!value) {
5253
return;
5354
}
5455

55-
option = options[value];
56+
option = _.isObject(this.countryOptions) && this.countryOptions[value];
5657

5758
if (!option) {
5859
return;
5960
}
6061

61-
if (option['is_zipcode_optional']) {
62+
isZipCodeOptional = !!option['is_zipcode_optional'];
63+
64+
if (isZipCodeOptional) {
6265
this.error(false);
63-
this.validation = _.omit(this.validation, 'required-entry');
64-
} else {
65-
this.validation['required-entry'] = true;
6666
}
6767

68-
this.required(!option['is_zipcode_optional']);
68+
this.validation['required-entry'] = !isZipCodeOptional;
69+
this.required(!isZipCodeOptional);
6970
}
7071
});
7172
});

app/code/Magento/Ui/view/base/web/js/form/element/region.js

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,81 +18,54 @@ define([
1818
defaults: {
1919
skipValidation: false,
2020
imports: {
21+
countryOptions: '${ $.parentName }.country_id:indexedOptions',
2122
update: '${ $.parentName }.country_id:value'
2223
}
2324
},
2425

2526
/**
26-
* @param {String} value
27+
* Method called every time country selector's value gets changed.
28+
* Updates all validations and requirements for certain country.
29+
* @param {String} value - Selected country ID.
2730
*/
2831
update: function (value) {
29-
var country = registry.get(this.parentName + '.' + 'country_id'),
30-
options = country.indexedOptions,
31-
isRegionRequired,
32+
var isRegionRequired,
3233
option;
3334

3435
if (!value) {
3536
return;
3637
}
37-
option = options[value];
3838

39-
if (typeof option === 'undefined') {
39+
option = _.isObject(this.countryOptions) && this.countryOptions[value];
40+
41+
if (!option) {
4042
return;
4143
}
4244

4345
defaultPostCodeResolver.setUseDefaultPostCode(!option['is_zipcode_optional']);
4446

45-
if (this.skipValidation) {
46-
this.validation['required-entry'] = false;
47-
this.required(false);
48-
} else {
49-
if (option && !option['is_region_required']) {
50-
this.error(false);
51-
this.validation = _.omit(this.validation, 'required-entry');
52-
registry.get(this.customName, function (input) {
53-
input.validation['required-entry'] = false;
54-
input.required(false);
55-
});
56-
} else {
57-
this.validation['required-entry'] = true;
58-
}
47+
if (option['is_region_visible'] === false) {
48+
// Hide select and corresponding text input field if region must not be shown for selected country.
49+
this.setVisible(false);
5950

60-
if (option && !this.options().length) {
61-
registry.get(this.customName, function (input) {
62-
isRegionRequired = !!option['is_region_required'];
63-
input.validation['required-entry'] = isRegionRequired;
64-
input.validation['validate-not-number-first'] = true;
65-
input.required(isRegionRequired);
66-
});
51+
if (this.customEntry) { // eslint-disable-line max-depth
52+
this.toggleInput(false);
6753
}
68-
69-
this.required(!!option['is_region_required']);
7054
}
71-
},
7255

73-
/**
74-
* Filters 'initialOptions' property by 'field' and 'value' passed,
75-
* calls 'setOptions' passing the result to it
76-
*
77-
* @param {*} value
78-
* @param {String} field
79-
*/
80-
filter: function (value, field) {
81-
var superFn = this._super;
82-
83-
registry.get(this.parentName + '.' + 'country_id', function (country) {
84-
var option = country.indexedOptions[value];
56+
isRegionRequired = !this.skipValidation && !!option['is_region_required'];
8557

86-
superFn.call(this, value, field);
58+
if (!isRegionRequired) {
59+
this.error(false);
60+
}
8761

88-
if (option && option['is_region_visible'] === false) {
89-
// hide select and corresponding text input field if region must not be shown for selected country
90-
this.setVisible(false);
62+
this.required(isRegionRequired);
63+
this.validation['required-entry'] = isRegionRequired;
9164

92-
if (this.customEntry) {// eslint-disable-line max-depth
93-
this.toggleInput(false);
94-
}
95-
}
65+
registry.get(this.customName, function (input) {
66+
input.required(isRegionRequired);
67+
input.validation['required-entry'] = isRegionRequired;
68+
input.validation['validate-not-number-first'] = !this.options().length;
9669
}.bind(this));
9770
}
9871
});

dev/tests/js/jasmine/tests/app/code/Magento/Checkout/frontend/js/view/shipping.test.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,13 @@ define(['squire', 'ko', 'jquery', 'uiRegistry', 'jquery/validate'], function (Sq
174174

175175
describe('"validateShippingInformation" method', function () {
176176
it('Check method call on negative cases.', function () {
177+
/* jscs:disable */
177178
var country = {
178-
'indexedOptions': {
179-
'AD':
180-
{
181-
label: 'Andorra',
182-
labeltitle: 'Andorra',
183-
value: 'AD'
184-
}
185-
}
179+
on: function () {},
180+
get: function () {},
181+
set: function () {}
186182
};
183+
/* jscs:enable */
187184

188185
registry.set('test.shippingAddress.shipping-address-fieldset.country_id', country);
189186
registry.set('checkout.errors', {});
@@ -202,9 +199,20 @@ define(['squire', 'ko', 'jquery', 'uiRegistry', 'jquery/validate'], function (Sq
202199
expect(obj.validateShippingInformation()).toBeFalsy();
203200
});
204201
it('Check method call on positive case.', function () {
202+
/* jscs:disable */
203+
var country = {
204+
on: function () {},
205+
get: function () {},
206+
set: function () {}
207+
};
208+
/* jscs:enable */
209+
205210
$('body').append('<form data-role="email-with-possible-login">' +
206211
'<input type="text" name="username" />' +
207212
'</form>');
213+
214+
registry.set('test.shippingAddress.shipping-address-fieldset.country_id', country);
215+
registry.set('checkout.errors', {});
208216
obj.source = {
209217
get: jasmine.createSpy().and.returnValue(true),
210218
set: jasmine.createSpy(),

dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/element/post-code.test.js

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,71 @@ define([
4747
});
4848

4949
describe('update method', function () {
50-
it('check for default', function () {
51-
var value = 'Value',
52-
country = {
53-
indexedOptions: {
54-
'Value': {
55-
'is_zipcode_optional': true
56-
}
57-
}
58-
};
59-
60-
spyOn(mocks['Magento_Ui/js/lib/registry/registry'], 'get').and.returnValue(country);
50+
it('makes field optional when there is no corresponding country', function () {
51+
var value = 'Value';
52+
53+
model.countryOptions = {};
54+
55+
model.update(value);
56+
57+
expect(model.required()).toEqual(false);
58+
});
59+
60+
it('makes field optional when post code is optional for certain country', function () {
61+
var value = 'Value';
62+
63+
model.countryOptions = {
64+
'Value': {
65+
'is_zipcode_optional': true
66+
}
67+
};
68+
6169
model.update(value);
62-
expect(mocks['Magento_Ui/js/lib/registry/registry'].get).toHaveBeenCalled();
63-
expect(model.error()).toEqual(false);
70+
6471
expect(model.required()).toEqual(false);
6572
});
73+
74+
it('removes field required validation when post code is optional for certain country', function () {
75+
var value = 'Value';
76+
77+
model.countryOptions = {
78+
'Value': {
79+
'is_zipcode_optional': true
80+
}
81+
};
82+
83+
model.update(value);
84+
85+
expect(model.validation['required-entry']).toBeFalsy();
86+
});
87+
88+
it('makes field required when post code is required for certain country', function () {
89+
var value = 'Value';
90+
91+
model.countryOptions = {
92+
'Value': {
93+
'is_zipcode_optional': false
94+
}
95+
};
96+
97+
model.update(value);
98+
99+
expect(model.required()).toEqual(true);
100+
});
101+
102+
it('sets field required validation when post code is required for certain country', function () {
103+
var value = 'Value';
104+
105+
model.countryOptions = {
106+
'Value': {
107+
'is_zipcode_optional': false
108+
}
109+
};
110+
111+
model.update(value);
112+
113+
expect(model.validation['required-entry']).toEqual(true);
114+
});
66115
});
67116
});
68117
});

0 commit comments

Comments
 (0)