Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage #109

Merged
merged 26 commits into from
Apr 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repo_token: h4mSYiVLxUBNL77JwRz2ohH8Of8e4qSuc
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules/*
Gruntfile.js
package.json
webpack.*.js
coverage/*
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ node_modules/
/src/main.js
.grunt/
npm-debug.log

coverage
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: node_js
node_js:
- "4.1.2"
after_success: npm run coverage
after_script: npm run coveralls
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

[![Build Status](https://travis-ci.org/patw0929/react-intl-tel-input.svg)](https://travis-ci.org/patw0929/react-intl-tel-input)
[![npm version](https://badge.fury.io/js/react-intl-tel-input.svg)](http://badge.fury.io/js/react-intl-tel-input)
[![Coverage Status](https://coveralls.io/repos/github/patw0929/react-intl-tel-input/badge.svg?branch=master)](https://coveralls.io/github/patw0929/react-intl-tel-input?branch=master)
[![npm](https://img.shields.io/npm/l/express.svg?maxAge=2592000)]()

[![NPM](https://nodei.co/npm/react-intl-tel-input.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/react-intl-tel-input/)

Rewrite [International Telephone Input](https://github.com/jackocnr/intl-tel-input) in React.js.

Expand Down Expand Up @@ -53,10 +57,22 @@ Please see the [Demo Page](http://patw0929.github.io/react-intl-tel-input/)

To build, watch and serve the examples (which will also watch the component source), run `npm run example`.


## Contributing

To contribute to react-intl-tel-input, clone this repo locally and commit your code on a separate branch. Please write tests for your code, and run the linter before opening a pull-request:

```bash
npm test
npm run lint
```


## Inspired by

[International Telephone Input](https://github.com/jackocnr/intl-tel-input) - [@jackocnr](https://github.com/jackocnr)


## License

MIT
Expand Down
6 changes: 3 additions & 3 deletions example/assets/example.js

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@
"underscore.deferred": "^0.4.0"
},
"devDependencies": {
"babel-cli": "^6.0.0",
"babel-core": "^6.0.0",
"babel-eslint": "^6.0.0-beta.6",
"babel-jest": "^6.0.0",
"babel-istanbul": "^0.5.9",
"babel-loader": "^6.0.0",
"babel-plugin-transform-class-properties": "^6.5.2",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "6.4.3",
"chai": "3.4.1",
"coveralls": "^2.11.9",
"css-loader": "~0.9.0",
"css-modules-require-hook": "^4.0.0",
"eslint": "^2.2.0",
Expand All @@ -59,10 +61,12 @@
"jsdom": "^7.2.2",
"load-grunt-tasks": "~0.6.0",
"mocha": "2.3.4",
"mock-local-storage": "^1.0.2",
"node-sass": "^3.4.2",
"react-addons-test-utils": "^0.14.0",
"react-hot-loader": "^1.0.7",
"sass-loader": "^1.0.1",
"sinon": "^1.17.3",
"style-loader": "~0.8.0",
"uglify-loader": "^1.3.0",
"url-loader": "~0.5.5",
Expand All @@ -78,7 +82,10 @@
"example": "grunt serve:example",
"lint": "eslint ./; true",
"gh-pages": "grunt gh-pages",
"test": "./node_modules/.bin/mocha tests/*-test.js --require tests/setup.js --compilers js:babel-register"
"test": "./node_modules/.bin/mocha --compilers js:babel-core/register --require ./tests/setup.js ./node_modules/mock-local-storage/lib/mock-localstorage.js --recursive tests/*-test.js",
"test:watch": "./node_modules/.bin/mocha --compilers js:babel-core/register --require ./tests/setup.js ./node_modules/mock-local-storage/lib/mock-localstorage.js --recursive tests/*-test.js --watch",
"coverage": "node_modules/.bin/babel-node node_modules/.bin/babel-istanbul cover node_modules/.bin/_mocha -- --require ./tests/setup.js ./node_modules/mock-local-storage/lib/mock-localstorage.js --recursive tests/*-test.js",
"coveralls": "cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},
"license": "MIT"
}
15 changes: 0 additions & 15 deletions preprocessor.js

This file was deleted.

42 changes: 42 additions & 0 deletions src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export default {
},

retrieveLiIndex(node) {
if (!node) {
return -1;
}

const children = node.parentNode.childNodes;
let num = 0;
for (let i = 0, max = children.length; i < max; i++) {
Expand Down Expand Up @@ -170,4 +174,42 @@ export default {
el.className = el.className.replace(reg, ' ');
}
},

// get the number of numeric digits to the right of the cursor so we can reposition
// the cursor correctly after the reformat has happened
getDigitsOnRight(val, selectionEnd) {
let digitsOnRight = 0;
for (let i = selectionEnd, max = val.length; i < max; i++) {
if (this.isNumeric(val.charAt(i))) {
digitsOnRight++;
}
}
return digitsOnRight;
},

// we start from the position in guessCursor, and work our way left
// until we hit the originalLeftChars or a number to make sure that
// after reformatting the cursor has the same char on the left in the case of a delete etc
getCursorFromLeftChar(val, guessCursor, originalLeftChars) {
for (let i = guessCursor; i > 0; i--) {
const leftChar = val.charAt(i - 1);
if (this.isNumeric(leftChar) || val.substr(i - 2, 2) === originalLeftChars) {
return i;
}
}
return 0;
},

// after a reformat we need to make sure there are still the same number
// of digits to the right of the cursor
getCursorFromDigitsOnRight(val, digitsOnRight) {
for (let i = val.length - 1; i >= 0; i--) {
if (this.isNumeric(val.charAt(i))) {
if (--digitsOnRight === 0) {
return i;
}
}
}
return 0;
},
};
47 changes: 3 additions & 44 deletions src/containers/IntlTelInputApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ export default class IntlTelInputApp extends Component {
this.selectedCountryData = {};
this.addCountryCode = this.addCountryCode.bind(this);
this.autoCountryLoaded = this.autoCountryLoaded.bind(this);
this.getCursorFromLeftChar = this.getCursorFromLeftChar.bind(this);
this.getCursorFromDigitsOnRight = this.getCursorFromDigitsOnRight.bind(this);
this.getDigitsOnRight = this.getDigitsOnRight.bind(this);
this.getDialCode = this.getDialCode.bind(this);
this.handleKeyPress = this.handleKeyPress.bind(this);
this.handleKeyUp = this.handleKeyUp.bind(this);
Expand Down Expand Up @@ -248,44 +245,6 @@ export default class IntlTelInputApp extends Component {
this.updateVal(number, format, addSuffix, preventConversion, isAllowedKey);
}

// get the number of numeric digits to the right of the cursor so we can reposition
// the cursor correctly after the reformat has happened
getDigitsOnRight(val, selectionEnd) {
let digitsOnRight = 0;
for (let i = selectionEnd, max = val.length; i < max; i++) {
if (utils.isNumeric(val.charAt(i))) {
digitsOnRight++;
}
}
return digitsOnRight;
}

// we start from the position in guessCursor, and work our way left
// until we hit the originalLeftChars or a number to make sure that
// after reformatting the cursor has the same char on the left in the case of a delete etc
getCursorFromLeftChar(val, guessCursor, originalLeftChars) {
for (let i = guessCursor; i > 0; i--) {
const leftChar = val.charAt(i - 1);
if (utils.isNumeric(leftChar) || val.substr(i - 2, 2) === originalLeftChars) {
return i;
}
}
return 0;
}

// after a reformat we need to make sure there are still the same number
// of digits to the right of the cursor
getCursorFromDigitsOnRight(val, digitsOnRight) {
for (let i = val.length - 1; i >= 0; i--) {
if (utils.isNumeric(val.charAt(i))) {
if (--digitsOnRight === 0) {
return i;
}
}
}
return 0;
}

// process preferred countries - iterate through the preferences,
// fetching the country data for each one
setPreferredCountries() {
Expand Down Expand Up @@ -707,7 +666,7 @@ export default class IntlTelInputApp extends Component {
// we use the right instead of the left so that
// A) we dont have to account for the new digit (or multiple digits if paste event),
// and B) we're always on the right side of formatting suffixes
digitsOnRight = this.getDigitsOnRight(val, input.selectionEnd);
digitsOnRight = utils.getDigitsOnRight(val, input.selectionEnd);
// if handling a new number character: insert it in the right place
if (newNumericChar) {
// replace any selection they may have made with the new char
Expand Down Expand Up @@ -741,11 +700,11 @@ export default class IntlTelInputApp extends Component {
newCursor = val.length;
} else {
// else count in the same number of digits from the right
newCursor = this.getCursorFromDigitsOnRight(val, digitsOnRight);
newCursor = utils.getCursorFromDigitsOnRight(val, digitsOnRight);

// but if delete/paste etc, keep going left until hit the same left char as before
if (!newNumericChar) {
newCursor = this.getCursorFromLeftChar(val, newCursor, originalLeftChars);
newCursor = utils.getCursorFromLeftChar(val, newCursor, originalLeftChars);
}
}
// set the new cursor
Expand Down
Loading