diff --git a/config/env.js b/config/env.js index 58728559c..03e8d7e49 100644 --- a/config/env.js +++ b/config/env.js @@ -18,7 +18,8 @@ function getClientEnvironment(publicUrl) { env[key] = JSON.stringify(process.env[key]); // eslint-disable-line no-param-reassign return env; - }, { + }, + { // Useful for determining whether we’re running in production mode. // Most importantly, it switches React into the correct mode. NODE_ENV: NODE_ENV, diff --git a/src/components/CountryList.js b/src/components/CountryList.js index b5fcc0ed7..e8560bf3f 100644 --- a/src/components/CountryList.js +++ b/src/components/CountryList.js @@ -62,11 +62,11 @@ export default class CountryList extends Component { this.listElement.style.top = cssTop; this.listElement.setAttribute('class', 'country-list'); - } + }; - setFlag = (iso2) => { + setFlag = iso2 => { this.props.setFlag(iso2); - } + }; appendListItem = (countries, isPreferred = false) => { const preferredCountriesCount = this.props.preferredCountries.length; @@ -106,21 +106,21 @@ export default class CountryList extends Component { {country.name} - + ++ {country.dialCode} ); }); - } + }; - handleMouseOver = (e) => { + handleMouseOver = e => { if (e.currentTarget.getAttribute('class').indexOf('country') > -1) { const selectedIndex = utils.retrieveLiIndex(e.currentTarget); this.props.changeHighlightCountry(true, selectedIndex); } - } + }; render() { let options = ''; diff --git a/src/components/IntlTelInputApp.js b/src/components/IntlTelInputApp.js index 256cc8014..c0b68b20c 100644 --- a/src/components/IntlTelInputApp.js +++ b/src/components/IntlTelInputApp.js @@ -164,7 +164,7 @@ class IntlTelInputApp extends Component { this.unbindDocumentClick(); } - getTempCountry = (countryCode) => { + getTempCountry = countryCode => { if (countryCode === 'auto') { return 'auto'; } @@ -184,7 +184,7 @@ class IntlTelInputApp extends Component { } return countryData.iso2; - } + }; // set the input value and update the flag // NOTE: preventFormat arg is for public method @@ -193,7 +193,7 @@ class IntlTelInputApp extends Component { // which is used for formatting the number before displaying it this.updateFlagFromNumber(number); this.updateValFromNumber(number, !preventFormat); - } + }; setFlagDropdownRef = ref => { this.flagDropDown = ref; @@ -314,10 +314,10 @@ class IntlTelInputApp extends Component { } } ); - } + }; // get the extension from the current number - getExtension = (number) => { + getExtension = number => { if (window.intlTelInputUtils) { return window.intlTelInputUtils.getExtension( this.getFullNumber(number), @@ -326,7 +326,7 @@ class IntlTelInputApp extends Component { } return ''; - } + }; // format the number to the given format getNumber = (number, format) => { @@ -339,20 +339,20 @@ class IntlTelInputApp extends Component { } return ''; - } + }; // get the input val, adding the dial code if separateDialCode is enabled - getFullNumber = (number) => { + getFullNumber = number => { const prefix = this.props.separateDialCode ? `+${this.selectedCountryData.dialCode}` : ''; return prefix + number; - } + }; // try and extract a valid international dial code from a full telephone number // Note: returns the raw string inc plus character and any whitespace/dots etc - getDialCode = (number) => { + getDialCode = number => { let dialCode = ''; // only interested in international numbers (starting with a plus) @@ -380,7 +380,7 @@ class IntlTelInputApp extends Component { } return dialCode; - } + }; // check if the given number contains an unknown area code from // the North American Numbering Plan i.e. the only dialCode that @@ -422,7 +422,7 @@ class IntlTelInputApp extends Component { } else { this.countries = AllCountries.getCountries(); } - } + }; // process the countryCodes map processCountryCodes = () => { @@ -443,7 +443,7 @@ class IntlTelInputApp extends Component { } } } - } + }; // process preferred countries - iterate through the preferences, // fetching the country data for each one @@ -461,7 +461,7 @@ class IntlTelInputApp extends Component { this.preferredCountries.push(countryData); } } - } + }; // set the initial state of the input value and the selected flag setInitialState = () => { @@ -503,7 +503,7 @@ class IntlTelInputApp extends Component { if (val) { this.updateValFromNumber(val, this.props.formatOnInit, doNotify); } - } + }; initRequests = () => { // if the user has specified the path to the utils script, fetch it on window.load @@ -518,10 +518,12 @@ class IntlTelInputApp extends Component { }); } } else { - import('libphonenumber-js-utils').then(() => { - this.loadUtils(); - this.utilsScriptDeferred.resolve(); - }).catch(() => 'An error occurred while loading the component'); + import('libphonenumber-js-utils') + .then(() => { + this.loadUtils(); + this.utilsScriptDeferred.resolve(); + }) + .catch(() => 'An error occurred while loading the component'); } if (this.tempCountry === 'auto') { @@ -529,7 +531,7 @@ class IntlTelInputApp extends Component { } else { this.autoCountryDeferred.resolve(); } - } + }; loadAutoCountry = () => { // check for localStorage @@ -569,13 +571,13 @@ class IntlTelInputApp extends Component { }); } } - } + }; - cap = (number) => { + cap = number => { const max = this.tel ? this.tel.getAttribute('maxlength') : number; return max && number.length > max ? number.substr(0, max) : number; - } + }; removeEmptyDialCode = () => { const value = this.state.value; @@ -591,10 +593,10 @@ class IntlTelInputApp extends Component { }); } } - } + }; // highlight the next/prev item in the list (and ensure it is visible) - handleUpDownKey = (key) => { + handleUpDownKey = key => { const current = this.flagDropDown.querySelectorAll('.highlight')[0]; const prevElement = current ? current.previousElementSibling : undefined; const nextElement = current ? current.nextElementSibling : undefined; @@ -618,7 +620,7 @@ class IntlTelInputApp extends Component { highlightedCountry: selectedIndex, }); } - } + }; // select the currently highlighted item handleEnterKey = () => { @@ -640,10 +642,10 @@ class IntlTelInputApp extends Component { } ); } - } + }; // find the first list item whose name starts with the query string - searchForCountry = (query) => { + searchForCountry = query => { for (let i = 0, max = this.countries.length; i < max; i++) { if (utils.startsWith(this.countries[i].name, query)) { const listItem = this.flagDropDown.querySelector( @@ -663,9 +665,9 @@ class IntlTelInputApp extends Component { break; } } - } + }; - formatNumber = (number) => { + formatNumber = number => { if (window.intlTelInputUtils && this.selectedCountryData) { let format = window.intlTelInputUtils.numberFormat.INTERNATIONAL; @@ -686,7 +688,7 @@ class IntlTelInputApp extends Component { } return number; - } + }; // update the input's value to the given val (format first if possible) // if doNotify is true, calls notifyPhoneNumberChange with the formatted value @@ -721,7 +723,7 @@ class IntlTelInputApp extends Component { this.unbindDocumentClick(); } ); - } + }; // check if need to select a new flag based on the given number // Note: called from _setInitialState, keyup handler, setNumber @@ -779,7 +781,7 @@ class IntlTelInputApp extends Component { if (countryCode !== null) { this.setFlag(countryCode, isInit); } - } + }; // filter the given countries using the process function filterCountries = (countryArray, processFunc) => { @@ -799,7 +801,7 @@ class IntlTelInputApp extends Component { this.countries.push(AllCountries.getCountries()[i]); } } - } + }; // prepare all of the country data, including onlyCountries and preferredCountries options processCountryData = () => { @@ -815,7 +817,7 @@ class IntlTelInputApp extends Component { // set the preferredCountries property this.processPreferredCountries.call(this); - } + }; handleOnBlur = () => { this.removeEmptyDialCode(); @@ -832,20 +834,20 @@ class IntlTelInputApp extends Component { this.getExtension(value) ); } - } + }; bindDocumentClick = () => { this.isOpening = true; document .querySelector('html') .addEventListener('click', this.handleDocumentClick); - } + }; unbindDocumentClick = () => { document .querySelector('html') .removeEventListener('click', this.handleDocumentClick); - } + }; clickSelectedFlag = () => { if ( @@ -871,7 +873,7 @@ class IntlTelInputApp extends Component { // need to hide dropdown when click on opened flag button this.toggleDropdown(false); } - } + }; // update the input placeholder to an // example number from the currently selected country @@ -903,9 +905,9 @@ class IntlTelInputApp extends Component { placeholder, }); } - } + }; - toggleDropdown = (status) => { + toggleDropdown = status => { this.setState( { showDropdown: !!status, @@ -916,7 +918,7 @@ class IntlTelInputApp extends Component { } } ); - } + }; // check if an element is visible within it's container, else scroll until it is scrollTo = (element, middle) => { @@ -953,7 +955,7 @@ class IntlTelInputApp extends Component { } catch (err) { // do nothing } - } + }; // replace any existing dial code with the new one // Note: called from _setFlag @@ -996,7 +998,7 @@ class IntlTelInputApp extends Component { } return newNumber; - } + }; generateMarkup = () => { this.wrapperClass['allow-dropdown'] = this.allowDropdown; @@ -1008,17 +1010,15 @@ class IntlTelInputApp extends Component { this.dropdownContainer = 'body'; window.addEventListener('scroll', this.handleWindowScroll); } - } + }; - handleSelectedFlagKeydown = (e) => { + handleSelectedFlagKeydown = e => { if ( !this.state.showDropdown && - ( - e.which === this.keys.UP || + (e.which === this.keys.UP || e.which === this.keys.DOWN || e.which === this.keys.SPACE || - e.which === this.keys.ENTER - ) + e.which === this.keys.ENTER) ) { // prevent form from being submitted if "ENTER" was pressed e.preventDefault(); @@ -1033,10 +1033,10 @@ class IntlTelInputApp extends Component { if (e.which === this.keys.TAB) { this.toggleDropdown(false); } - } + }; // validate the input val - assumes the global function isValidNumber (from utilsScript) - isValidNumber = (number) => { + isValidNumber = number => { const val = utils.trim(number); const countryCode = this.nationalMode || this.props.separateDialCode @@ -1048,18 +1048,18 @@ class IntlTelInputApp extends Component { } return false; - } + }; - formatFullNumber = (number) => { + formatFullNumber = number => { return window.intlTelInputUtils ? this.getNumber( number, window.intlTelInputUtils.numberFormat.INTERNATIONAL ) : number; - } + }; - notifyPhoneNumberChange = (newNumber) => { + notifyPhoneNumberChange = newNumber => { if (typeof this.props.onPhoneNumberChange === 'function') { const fullNumber = this.formatFullNumber(newNumber); const isValid = this.isValidNumber(fullNumber); @@ -1072,7 +1072,7 @@ class IntlTelInputApp extends Component { this.getExtension(newNumber) ); } - } + }; // remove the dial code if separateDialCode is enabled beforeSetNumber = (number, props = this.props) => { @@ -1103,7 +1103,7 @@ class IntlTelInputApp extends Component { } return this.cap(number); - } + }; handleWindowScroll = () => { this.setState( @@ -1114,9 +1114,9 @@ class IntlTelInputApp extends Component { window.removeEventListener('scroll', this.handleWindowScroll); } ); - } + }; - handleDocumentKeyDown = (e) => { + handleDocumentKeyDown = e => { let queryTimer; // prevent down key from scrolling the whole page, @@ -1154,19 +1154,21 @@ class IntlTelInputApp extends Component { this.query = ''; }, 1000); } - } + }; - handleDocumentClick = (e) => { + handleDocumentClick = e => { // Click at the outside of country list // and outside of flag button const targetClass = e.target.getAttribute('class'); - if (targetClass === null || + if ( + targetClass === null || (targetClass && - targetClass.indexOf('country') === -1 && - targetClass.indexOf('selected-flag') === -1 && - targetClass.indexOf('iti-flag') === -1 && - targetClass.indexOf('iti-arrow') === -1)) { + targetClass.indexOf('country') === -1 && + targetClass.indexOf('selected-flag') === -1 && + targetClass.indexOf('iti-flag') === -1 && + targetClass.indexOf('iti-arrow') === -1) + ) { this.isOpening = false; } @@ -1174,11 +1176,11 @@ class IntlTelInputApp extends Component { this.toggleDropdown(false); } this.isOpening = false; - } + }; // Either notify phoneNumber changed if component is controlled // or udpate the state and notify change if component is uncontrolled - handleInputChange = (e) => { + handleInputChange = e => { let cursorPosition = e.target.selectionStart; const previousValue = e.target.value; const previousStringBeforeCursor = @@ -1217,14 +1219,14 @@ class IntlTelInputApp extends Component { } ); } - } + }; changeHighlightCountry = (showDropdown, selectedIndex) => { this.setState({ showDropdown, highlightedCountry: selectedIndex, }); - } + }; loadUtils = () => { if (window.intlTelInputUtils) { @@ -1255,7 +1257,7 @@ class IntlTelInputApp extends Component { }; request.send(); - } + }; // this is called when the geoip call returns autoCountryLoaded = () => { @@ -1263,7 +1265,7 @@ class IntlTelInputApp extends Component { this.tempCountry = this.autoCountry; this.autoCountryDeferred.resolve(); } - } + }; render() { this.wrapperClass[this.props.css[0]] = true; diff --git a/src/components/TelInput.js b/src/components/TelInput.js index c6d44c2e6..5f18a4c25 100644 --- a/src/components/TelInput.js +++ b/src/components/TelInput.js @@ -26,10 +26,10 @@ export default class TelInput extends Component { ); } - refHandler = (element) => { + refHandler = element => { this.tel = element; this.props.refCallback(element); - } + }; render() { return ( diff --git a/src/components/__tests__/FlagDropDown.test.js b/src/components/__tests__/FlagDropDown.test.js index e8da45d51..5a614c005 100644 --- a/src/components/__tests__/FlagDropDown.test.js +++ b/src/components/__tests__/FlagDropDown.test.js @@ -10,7 +10,8 @@ import FlagDropDown from '../FlagDropDown'; import CountryList from '../CountryList'; import TelInput from '../TelInput'; -describe('FlagDropDown', function () { // eslint-disable-line func-names +// eslint-disable-next-line func-names +describe('FlagDropDown', function() { let libphonenumberUtils; let xhr; let requests; @@ -22,11 +23,12 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names libphonenumberUtils = fs.readFileSync('./src/libphonenumber.js', 'utf8'); xhr = sinon.useFakeXMLHttpRequest(); requests = []; - xhr.onCreate = (req) => { requests.push(req); }; + xhr.onCreate = req => { + requests.push(req); + }; window.intlTelInputUtils = undefined; - getScript = () => - document.getElementsByTagName('script')[0]; + getScript = () => document.getElementsByTagName('script')[0]; this.params = { css: ['intl-tel-input', 'form-control phoneNumber'], @@ -36,11 +38,7 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names }; this.makeSubject = () => { - return mount( - - ); + return mount(); }; }); @@ -93,11 +91,15 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names const subject = this.makeSubject(); const flagComponent = subject.find(FlagDropDown); - expect(subject.find(CountryList).find('.country-list.hide').length).toBeTruthy(); + expect( + subject.find(CountryList).find('.country-list.hide').length + ).toBeTruthy(); flagComponent.find('.selected-flag').simulate('click'); subject.update(); - expect(subject.find(CountryList).find('.country-list.hide').length).toBeFalsy(); + expect( + subject.find(CountryList).find('.country-list.hide').length + ).toBeFalsy(); }); it('Simulate change to Japan flag in dropdown before & after', () => { @@ -119,25 +121,29 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names const subject = this.makeSubject(); const flagComponent = subject.find(FlagDropDown); - const result = [{ - name: 'South Korea (대한민국)', - iso2: 'kr', - dialCode: '82', - priority: 0, - areaCodes: null, - }, { - name: 'Taiwan (台灣)', - iso2: 'tw', - dialCode: '886', - priority: 0, - areaCodes: null, - }, { - name: 'United States', - iso2: 'us', - dialCode: '1', - priority: 0, - areaCodes: null, - }]; + const result = [ + { + name: 'South Korea (대한민국)', + iso2: 'kr', + dialCode: '82', + priority: 0, + areaCodes: null, + }, + { + name: 'Taiwan (台灣)', + iso2: 'tw', + dialCode: '886', + priority: 0, + areaCodes: null, + }, + { + name: 'United States', + iso2: 'us', + dialCode: '1', + priority: 0, + areaCodes: null, + }, + ]; expect(flagComponent.props().countries).toEqual(result); }); @@ -151,7 +157,7 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names }); it('Set defaultCountry as "auto"', async () => { - const lookup = (callback) => { + const lookup = callback => { callback('jp'); }; @@ -191,8 +197,11 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names ReactTestUtils.Simulate.click(ReactDOM.findDOMNode(flagComponent)); const options = ReactDOM.findDOMNode(dropDownComponent).querySelectorAll( - '.country:not([class="preferred"])'); - const koreaOption = ReactDOM.findDOMNode(dropDownComponent).querySelector('[data-country-code="kr"]'); + '.country:not([class="preferred"])' + ); + const koreaOption = ReactDOM.findDOMNode(dropDownComponent).querySelector( + '[data-country-code="kr"]' + ); let index = -1; @@ -209,7 +218,7 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names it('Simulate change to flag in dropdown by up and down key', () => { const renderedComponent = ReactTestUtils.renderIntoDocument( { const renderedComponent = ReactTestUtils.renderIntoDocument( { const renderedComponent = ReactTestUtils.renderIntoDocument( { const renderedComponent = ReactTestUtils.renderIntoDocument( { const renderedComponent = ReactTestUtils.renderIntoDocument( { let expected = ''; const onSelectFlag = (currentNumber, countryData, fullNumber, isValid) => { - expected = Object.assign({}, { currentNumber, fullNumber, isValid, ...countryData }); + expected = Object.assign( + {}, + { currentNumber, fullNumber, isValid, ...countryData } + ); }; this.params.onSelectFlag = onSelectFlag; @@ -451,9 +491,11 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names const inputComponent = subject.find(TelInput); const countryListComponent = subject.find(CountryList); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); inputComponent.simulate('change', { target: { value: '+8109012345678' } }); @@ -479,12 +521,16 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names this.params.nationalMode = true; const subject = this.makeSubject(); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); - expect(subject.instance().formatNumber('+886 912 345 678')).toBe('0912 345 678'); + expect(subject.instance().formatNumber('+886 912 345 678')).toBe( + '0912 345 678' + ); }); it('should highlight country from preferred list', async () => { diff --git a/src/components/__tests__/RootModal.test.js b/src/components/__tests__/RootModal.test.js index 56324f377..6584a0865 100644 --- a/src/components/__tests__/RootModal.test.js +++ b/src/components/__tests__/RootModal.test.js @@ -2,7 +2,8 @@ import React from 'react'; import { mount } from 'enzyme'; import RootModal from '../RootModal'; -describe('RootModal', function () { // eslint-disable-line func-names +// eslint-disable-next-line func-names +describe('RootModal', function() { beforeEach(() => { jest.resetModules(); @@ -13,7 +14,7 @@ describe('RootModal', function () { // eslint-disable-line func-names , { attachTo: document.body, - }, + } ); }; }); diff --git a/src/components/__tests__/TelInput.test.js b/src/components/__tests__/TelInput.test.js index b052e6972..8c41a2557 100644 --- a/src/components/__tests__/TelInput.test.js +++ b/src/components/__tests__/TelInput.test.js @@ -7,7 +7,8 @@ import IntlTelInput from '../IntlTelInputApp'; import TelInput from '../TelInput'; import FlagDropDown from '../FlagDropDown'; -describe('TelInput', function () { // eslint-disable-line func-names +// eslint-disable-next-line func-names +describe('TelInput', function() { let libphonenumberUtils; let getScript; let xhr; @@ -26,12 +27,13 @@ describe('TelInput', function () { // eslint-disable-line func-names xhr = sinon.useFakeXMLHttpRequest(); requests = []; - xhr.onCreate = (req) => { requests.push(req); }; + xhr.onCreate = req => { + requests.push(req); + }; document.body.innerHTML = '
'; window.intlTelInputUtils = undefined; - getScript = () => - document.getElementsByTagName('script')[0]; + getScript = () => document.getElementsByTagName('script')[0]; this.params = { css: ['intl-tel-input', 'form-control phoneNumber'], @@ -42,13 +44,9 @@ describe('TelInput', function () { // eslint-disable-line func-names utilsScript: 'assets/libphonenumber.js', }; this.makeSubject = () => { - return mount( - , { - attachTo: document.querySelector('#root'), - }, - ); + return mount(, { + attachTo: document.querySelector('#root'), + }); }; }); @@ -68,8 +66,16 @@ describe('TelInput', function () { // eslint-disable-line func-names it('onPhoneNumberChange without utilsScript', () => { let expected = ''; - const onPhoneNumberChange = (isValid, newNumber, countryData, fullNumber, ext) => { - expected = `${isValid},${newNumber},${countryData.iso2},${fullNumber},${ext}`; + const onPhoneNumberChange = ( + isValid, + newNumber, + countryData, + fullNumber, + ext + ) => { + expected = `${isValid},${newNumber},${ + countryData.iso2 + },${fullNumber},${ext}`; }; this.params.utilsScript = ''; @@ -85,9 +91,11 @@ describe('TelInput', function () { // eslint-disable-line func-names const subject = await this.makeSubject(); const inputComponent = subject.find(TelInput); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); expect(inputComponent.props().value).toBe('0999 123 456'); @@ -97,7 +105,9 @@ describe('TelInput', function () { // eslint-disable-line func-names const subject = this.makeSubject(); const inputComponent = subject.find(TelInput); - expect(inputComponent.find('.form-control.phoneNumber').length).toBeTruthy(); + expect( + inputComponent.find('.form-control.phoneNumber').length + ).toBeTruthy(); }); it('should not focused on render', () => { @@ -105,7 +115,10 @@ describe('TelInput', function () { // eslint-disable-line func-names let focused = false; - IntlTelInput.prototype.selectFlag = function selectFlag(countryCode, setFocus = true) { + IntlTelInput.prototype.selectFlag = function selectFlag( + countryCode, + setFocus = true + ) { focused = focused || setFocus; initialSelectFlag.call(this, countryCode, setFocus); }; @@ -121,7 +134,6 @@ describe('TelInput', function () { // eslint-disable-line func-names expect(focused).toBeFalsy(); }); - it('should has "kr" in preferred countries state', () => { this.params = { ...this.params, @@ -166,9 +178,11 @@ describe('TelInput', function () { // eslint-disable-line func-names const subject = this.makeSubject(); const inputComponent = subject.find(TelInput); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); inputComponent.simulate('focus'); @@ -196,9 +210,11 @@ describe('TelInput', function () { // eslint-disable-line func-names const subject = this.makeSubject(); const inputComponent = subject.find(TelInput); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); inputComponent.simulate('focus'); @@ -232,9 +248,11 @@ describe('TelInput', function () { // eslint-disable-line func-names it('utils loaded', () => { this.makeSubject(); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); expect(typeof window.intlTelInputUtils === 'object'); @@ -243,17 +261,27 @@ describe('TelInput', function () { // eslint-disable-line func-names it('onPhoneNumberChange', () => { let expected = ''; - const onPhoneNumberChange = (isValid, newNumber, countryData, fullNumber, ext) => { - expected = `${isValid},${newNumber},${countryData.iso2},${fullNumber},${ext}`; + const onPhoneNumberChange = ( + isValid, + newNumber, + countryData, + fullNumber, + ext + ) => { + expected = `${isValid},${newNumber},${ + countryData.iso2 + },${fullNumber},${ext}`; }; this.params.onPhoneNumberChange = onPhoneNumberChange; const subject = this.makeSubject(); const inputComponent = subject.find(TelInput); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); inputComponent.simulate('change', { target: { value: '+886911222333' } }); @@ -271,17 +299,27 @@ describe('TelInput', function () { // eslint-disable-line func-names it('onPhoneNumberBlur', () => { let expected = ''; - const onPhoneNumberBlur = (isValid, newNumber, countryData, fullNumber, ext) => { - expected = `${isValid},${newNumber},${countryData.iso2},${fullNumber},${ext}`; + const onPhoneNumberBlur = ( + isValid, + newNumber, + countryData, + fullNumber, + ext + ) => { + expected = `${isValid},${newNumber},${ + countryData.iso2 + },${fullNumber},${ext}`; }; this.params.onPhoneNumberBlur = onPhoneNumberBlur; const subject = this.makeSubject(); const inputComponent = subject.find(TelInput); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); inputComponent.simulate('change', { target: { value: '+886911222333' } }); @@ -320,9 +358,11 @@ describe('TelInput', function () { // eslint-disable-line func-names it('isValidNumber', () => { const subject = this.makeSubject(); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); expect(subject.instance().isValidNumber('0910123456')).toBeTruthy(); @@ -337,9 +377,11 @@ describe('TelInput', function () { // eslint-disable-line func-names const subject = this.makeSubject(); const inputComponent = subject.find(TelInput); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); inputComponent.simulate('change', { target: { value: '910123456' } }); @@ -351,9 +393,11 @@ describe('TelInput', function () { // eslint-disable-line func-names const subject = this.makeSubject(); const inputComponent = subject.find(TelInput); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); expect(inputComponent.props().placeholder).toBe('foo'); @@ -482,9 +526,11 @@ describe('TelInput', function () { // eslint-disable-line func-names it('should change input placeholder on customPlaceholder prop change', () => { const subject = this.makeSubject(); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); subject.setProps({ customPlaceholder: () => 'Phone number' }); @@ -505,7 +551,9 @@ describe('TelInput', function () { // eslint-disable-line func-names flagComponent.simulate('click'); expect(subject.instance().wrapperClass.expanded).toBe(true); - const taiwanOption = subject.find(FlagDropDown).find('[data-country-code="tw"]'); + const taiwanOption = subject + .find(FlagDropDown) + .find('[data-country-code="tw"]'); taiwanOption.simulate('click'); expect(subject.instance().wrapperClass.expanded).toBe(false); @@ -538,9 +586,11 @@ describe('TelInput', function () { // eslint-disable-line func-names it('should change props value', () => { const subject = this.makeSubject(); - requests[0].respond(200, + requests[0].respond( + 200, { 'Content-Type': 'text/javascript' }, - libphonenumberUtils); + libphonenumberUtils + ); window.eval(getScript().text); subject.setState({ diff --git a/src/components/__tests__/utils.test.js b/src/components/__tests__/utils.test.js index d03e6275a..19e78ab9f 100644 --- a/src/components/__tests__/utils.test.js +++ b/src/components/__tests__/utils.test.js @@ -117,10 +117,21 @@ describe('utils', () => { areaCodes: null, }; - expect(utils.getCountryData(AllCountries.getCountries(), 'tw')).toEqual(result); - expect(utils.getCountryData(AllCountries.getCountries(), 'zz', true, true)).toBeNull(); - expect(utils.getCountryData(AllCountries.getCountries(), - 'zz', false, false, (country) => `${country}!!`)).toEqual({}); + expect(utils.getCountryData(AllCountries.getCountries(), 'tw')).toEqual( + result + ); + expect( + utils.getCountryData(AllCountries.getCountries(), 'zz', true, true) + ).toBeNull(); + expect( + utils.getCountryData( + AllCountries.getCountries(), + 'zz', + false, + false, + country => `${country}!!` + ) + ).toEqual({}); }); it('hasClass', () => { @@ -164,12 +175,12 @@ describe('utils', () => { expect(utils.findIndex(array, predicate)).toEqual(-1); array = [1, 2, 3]; - predicate = (item) => item === 2; + predicate = item => item === 2; expect(utils.findIndex(array, predicate)).toEqual(1); array = [1, 2, 3]; - predicate = (item) => item === 4; + predicate = item => item === 4; expect(utils.findIndex(array, predicate)).toEqual(-1); }); @@ -179,24 +190,48 @@ describe('utils', () => { let previousString = '912345'; let nextString = '912345'; - expect(utils.getCursorPositionAfterFormating(previousStringBeforeCursor, previousString, nextString)).toEqual(4); + expect( + utils.getCursorPositionAfterFormating( + previousStringBeforeCursor, + previousString, + nextString + ) + ).toEqual(4); previousStringBeforeCursor = '0912 345'; previousString = '0912 345 678'; nextString = '91234678'; - expect(utils.getCursorPositionAfterFormating(previousStringBeforeCursor, previousString, nextString)).toEqual(5); + expect( + utils.getCursorPositionAfterFormating( + previousStringBeforeCursor, + previousString, + nextString + ) + ).toEqual(5); previousStringBeforeCursor = '91234'; previousString = '91234678'; nextString = '0912 345 678'; - expect(utils.getCursorPositionAfterFormating(previousStringBeforeCursor, previousString, nextString)).toEqual(7); + expect( + utils.getCursorPositionAfterFormating( + previousStringBeforeCursor, + previousString, + nextString + ) + ).toEqual(7); previousStringBeforeCursor = '(201) 5'; previousString = '(201) 55-01'; nextString = '201-5501'; - expect(utils.getCursorPositionAfterFormating(previousStringBeforeCursor, previousString, nextString)).toEqual(5); + expect( + utils.getCursorPositionAfterFormating( + previousStringBeforeCursor, + previousString, + nextString + ) + ).toEqual(5); }); }); diff --git a/src/example.js b/src/example.js index abbd9f9b5..09e5202d8 100644 --- a/src/example.js +++ b/src/example.js @@ -86,7 +86,7 @@ class DemoComponent extends Component { format />
-Phone Number: + Phone Number: {this.state.phone1}
@@ -99,7 +99,7 @@ Phone Number: css={['intl-tel-input', 'form-control']} />
-Phone Number: + Phone Number: {this.state.phone2}