|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | +/*eslint-disable max-nested-callbacks*/ |
| 6 | +/*jscs:disable jsDoc*/ |
| 7 | +define([ |
| 8 | + 'jquery', |
| 9 | + 'Magento_Directory/js/region-updater' |
| 10 | +], function ($) { |
| 11 | + 'use strict'; |
| 12 | + |
| 13 | + var regionJson = { |
| 14 | + 'config': { |
| 15 | + 'show_all_regions': true, |
| 16 | + 'regions_required': [ |
| 17 | + 'US' |
| 18 | + ] |
| 19 | + }, |
| 20 | + 'US': { |
| 21 | + '1': { |
| 22 | + 'id' : 1, |
| 23 | + 'code': 'AL', |
| 24 | + 'name': 'Alabama' |
| 25 | + }, |
| 26 | + '2': { |
| 27 | + 'id' : 2, |
| 28 | + 'code': 'AK', |
| 29 | + 'name': 'Alaska' |
| 30 | + }, |
| 31 | + '3': { |
| 32 | + 'id' : 3, |
| 33 | + 'code': 'AS', |
| 34 | + 'name': 'American Samoa' |
| 35 | + } |
| 36 | + }, |
| 37 | + 'DE': { |
| 38 | + '81': { |
| 39 | + 'id' : 4, |
| 40 | + 'code': 'BAY', |
| 41 | + 'name': 'Bayern' |
| 42 | + }, |
| 43 | + '82': { |
| 44 | + 'id' : 5, |
| 45 | + 'code': 'BER', |
| 46 | + 'name': 'Berlin' |
| 47 | + }, |
| 48 | + '83': { |
| 49 | + 'id' : 6, |
| 50 | + 'code': 'BRG', |
| 51 | + 'name': 'Brandenburg' |
| 52 | + } |
| 53 | + } |
| 54 | + }, |
| 55 | + defaultCountry = 'GB', |
| 56 | + countries = { |
| 57 | + '': '', |
| 58 | + 'US': 'United States', |
| 59 | + 'GB': 'United Kingdom', |
| 60 | + 'DE': 'Germany', |
| 61 | + 'IT': 'Italy' |
| 62 | + }, |
| 63 | + regions = { |
| 64 | + '': 'Please select a region, state or province.' |
| 65 | + }, |
| 66 | + countryEl, |
| 67 | + regionSelectEl, |
| 68 | + regionInputEl, |
| 69 | + postalCodeEl, |
| 70 | + formEl, |
| 71 | + containerEl; |
| 72 | + |
| 73 | + function createFormField() { |
| 74 | + var fieldWrapperEl = document.createElement('div'), |
| 75 | + labelEl = document.createElement('label'), |
| 76 | + inputControlEl = document.createElement('div'), |
| 77 | + i; |
| 78 | + |
| 79 | + fieldWrapperEl.appendChild(labelEl); |
| 80 | + fieldWrapperEl.appendChild(inputControlEl); |
| 81 | + |
| 82 | + for (i = 0; i < arguments.length; i++) { |
| 83 | + inputControlEl.appendChild(arguments[i]); |
| 84 | + } |
| 85 | + labelEl.setAttribute('class', 'label'); |
| 86 | + fieldWrapperEl.setAttribute('class', 'field required'); |
| 87 | + inputControlEl.setAttribute('class', 'control'); |
| 88 | + |
| 89 | + return fieldWrapperEl; |
| 90 | + } |
| 91 | + |
| 92 | + function buildSelectOptions(select, options, defaultOption) { |
| 93 | + var optionValue, |
| 94 | + optionEl; |
| 95 | + |
| 96 | + defaultOption = typeof defaultOption === 'undefined' ? '' : defaultOption; |
| 97 | + |
| 98 | + // eslint-disable-next-line guard-for-in |
| 99 | + for (optionValue in options) { |
| 100 | + if (options.hasOwnProperty(optionValue)) { |
| 101 | + optionEl = document.createElement('option'); |
| 102 | + optionEl.setAttribute('value', optionValue); |
| 103 | + optionEl.textContent = countries[optionValue]; |
| 104 | + // eslint-disable-next-line max-depth |
| 105 | + if (defaultOption === optionValue) { |
| 106 | + optionEl.setAttribute('selected', 'selected'); |
| 107 | + } |
| 108 | + select.add(optionEl); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + function init(config) { |
| 114 | + var defaultConfig = { |
| 115 | + 'optionalRegionAllowed': true, |
| 116 | + 'regionListId': '#' + regionSelectEl.id, |
| 117 | + 'regionInputId': '#' + regionInputEl.id, |
| 118 | + 'postcodeId': '#' + postalCodeEl.id, |
| 119 | + 'form': '#' + formEl.id, |
| 120 | + 'regionJson': regionJson, |
| 121 | + 'defaultRegion': 0, |
| 122 | + 'countriesWithOptionalZip': ['GB'] |
| 123 | + }; |
| 124 | + |
| 125 | + $(countryEl).regionUpdater($.extend({}, defaultConfig, config || {})); |
| 126 | + } |
| 127 | + |
| 128 | + beforeEach(function () { |
| 129 | + containerEl = document.createElement('div'); |
| 130 | + formEl = document.createElement('form'); |
| 131 | + regionSelectEl = document.createElement('select'); |
| 132 | + regionInputEl = document.createElement('input'); |
| 133 | + postalCodeEl = document.createElement('input'); |
| 134 | + countryEl = document.createElement('select'); |
| 135 | + regionSelectEl.setAttribute('id', 'region_id'); |
| 136 | + regionSelectEl.setAttribute('style', 'display:none;'); |
| 137 | + regionInputEl.setAttribute('id', 'region'); |
| 138 | + regionInputEl.setAttribute('style', 'display:none;'); |
| 139 | + countryEl.setAttribute('id', 'country'); |
| 140 | + postalCodeEl.setAttribute('id', 'zip'); |
| 141 | + formEl.setAttribute('id', 'test_form'); |
| 142 | + formEl.appendChild(createFormField(countryEl)); |
| 143 | + formEl.appendChild(createFormField(regionSelectEl, regionInputEl)); |
| 144 | + formEl.appendChild(createFormField(postalCodeEl)); |
| 145 | + containerEl.appendChild(formEl); |
| 146 | + buildSelectOptions(regionSelectEl, regions); |
| 147 | + buildSelectOptions(countryEl, countries, defaultCountry); |
| 148 | + document.body.appendChild(containerEl); |
| 149 | + }); |
| 150 | + |
| 151 | + afterEach(function () { |
| 152 | + $(containerEl).remove(); |
| 153 | + formEl = undefined; |
| 154 | + containerEl = undefined; |
| 155 | + regionSelectEl = undefined; |
| 156 | + regionInputEl = undefined; |
| 157 | + postalCodeEl = undefined; |
| 158 | + countryEl = undefined; |
| 159 | + }); |
| 160 | + |
| 161 | + describe('Magento_Directory/js/region-updater', function () { |
| 162 | + it('Check that default country is selected', function () { |
| 163 | + init(); |
| 164 | + expect($(countryEl).val()).toBe(defaultCountry); |
| 165 | + }); |
| 166 | + it('Check that region list is not displayed when selected country has no predefined regions', function () { |
| 167 | + init(); |
| 168 | + $(countryEl).val('GB').trigger('change'); |
| 169 | + expect($(regionInputEl).is(':visible')).toBe(true); |
| 170 | + expect($(regionInputEl).is(':disabled')).toBe(false); |
| 171 | + expect($(regionSelectEl).is(':visible')).toBe(false); |
| 172 | + expect($(regionSelectEl).is(':disabled')).toBe(true); |
| 173 | + }); |
| 174 | + it('Check country that has predefined and optional regions', function () { |
| 175 | + init(); |
| 176 | + $(countryEl).val('DE').trigger('change'); |
| 177 | + expect($(regionSelectEl).is(':visible')).toBe(true); |
| 178 | + expect($(regionSelectEl).is(':disabled')).toBe(false); |
| 179 | + expect($(regionSelectEl).hasClass('required-entry')).toBe(false); |
| 180 | + expect($(regionInputEl).is(':visible')).toBe(false); |
| 181 | + expect( |
| 182 | + $(regionSelectEl).find('option') |
| 183 | + .map(function () { |
| 184 | + return this.textContent; |
| 185 | + }) |
| 186 | + .get() |
| 187 | + ).toContain('Berlin'); |
| 188 | + }); |
| 189 | + it('Check country that has predefined and required regions', function () { |
| 190 | + init(); |
| 191 | + $(countryEl).val('US').trigger('change'); |
| 192 | + expect($(regionSelectEl).is(':visible')).toBe(true); |
| 193 | + expect($(regionSelectEl).is(':disabled')).toBe(false); |
| 194 | + expect($(regionSelectEl).hasClass('required-entry')).toBe(true); |
| 195 | + expect($(regionInputEl).is(':visible')).toBe(false); |
| 196 | + expect( |
| 197 | + $(regionSelectEl).find('option') |
| 198 | + .map(function () { |
| 199 | + return this.textContent; |
| 200 | + }) |
| 201 | + .get() |
| 202 | + ).toContain('Alaska'); |
| 203 | + }); |
| 204 | + it('Check that region fields are not displayed for country with optional regions if configured', function () { |
| 205 | + init({ |
| 206 | + optionalRegionAllowed: false |
| 207 | + }); |
| 208 | + $(countryEl).val('DE').trigger('change'); |
| 209 | + expect($(regionSelectEl).is(':visible')).toBe(false); |
| 210 | + expect($(regionInputEl).is(':visible')).toBe(false); |
| 211 | + }); |
| 212 | + it('Check that initial values are not overwritten - region input', function () { |
| 213 | + $(countryEl).val('GB'); |
| 214 | + $(regionInputEl).val('Liverpool'); |
| 215 | + $(postalCodeEl).val('L13 0AL'); |
| 216 | + init(); |
| 217 | + expect($(countryEl).val()).toBe('GB'); |
| 218 | + expect($(regionInputEl).val()).toBe('Liverpool'); |
| 219 | + expect($(postalCodeEl).val()).toBe('L13 0AL'); |
| 220 | + }); |
| 221 | + it('Check that initial values are not overwritten - region select', function () { |
| 222 | + $(countryEl).val('US'); |
| 223 | + $(postalCodeEl).val('99501'); |
| 224 | + init({ |
| 225 | + defaultRegion: '2' |
| 226 | + }); |
| 227 | + expect($(countryEl).val()).toBe('US'); |
| 228 | + expect($(regionSelectEl).find('option:selected').text()).toBe('Alaska'); |
| 229 | + expect($(postalCodeEl).val()).toBe('99501'); |
| 230 | + }); |
| 231 | + it('Check that region values are cleared out on country change - region input', function () { |
| 232 | + $(countryEl).val('GB'); |
| 233 | + $(regionInputEl).val('Liverpool'); |
| 234 | + init(); |
| 235 | + $(countryEl).val('IT').trigger('change'); |
| 236 | + expect($(countryEl).val()).toBe('IT'); |
| 237 | + expect($(regionInputEl).val()).toBe(''); |
| 238 | + }); |
| 239 | + it('Check that region values are cleared out on country change - region select', function () { |
| 240 | + $(countryEl).val('US'); |
| 241 | + init({ |
| 242 | + defaultRegion: '2' |
| 243 | + }); |
| 244 | + $(countryEl).val('DE').trigger('change'); |
| 245 | + expect($(countryEl).val()).toBe('DE'); |
| 246 | + expect($(regionSelectEl).val()).toBe(''); |
| 247 | + }); |
| 248 | + }); |
| 249 | +}); |
0 commit comments