Skip to content

Commit 77821ed

Browse files
Implement unit-tests and setup a CI (GH-6)
2 parents b39215e + 6093f2e commit 77821ed

File tree

10 files changed

+32015
-7
lines changed

10 files changed

+32015
-7
lines changed

.github/workflows/tests.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
tests:
11+
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
node-version: [ 16.x, 18.x, 20.x ]
17+
react-version: [ rc16, rc17, rc18 ]
18+
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v3
22+
23+
- name: Set up Python
24+
uses: actions/setup-python@v2
25+
26+
- name: Install Tox
27+
run: |
28+
pip install --upgrade pip
29+
pip install tox tox-gh-actions
30+
31+
- name: Set up Node
32+
uses: actions/setup-node@v3
33+
with:
34+
node-version: ${{ matrix.node-version }}
35+
registry-url: https://registry.npmjs.org/
36+
37+
- name: Install dependencies
38+
run: npm install
39+
40+
- name: Run Tox Jest
41+
run: tox -e ${{ matrix.react-version }}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![React](https://img.shields.io/badge/react-%E2%89%A516-blue)](https://www.npmjs.com/package/react-phone-hooks)
55
[![types](https://img.shields.io/npm/types/react-phone-hooks)](https://www.npmjs.com/package/react-phone-hooks)
66
[![License](https://img.shields.io/npm/l/react-phone-hooks)](https://github.com/typesnippet/react-phone-hooks/blob/master/LICENSE)
7-
[![Tests](https://github.com/pysnippet/react-phone-hooks/actions/workflows/tests.yml/badge.svg)](https://github.com/pysnippet/react-phone-hooks/actions/workflows/tests.yml)
7+
[![Tests](https://github.com/typesnippet/react-phone-hooks/actions/workflows/tests.yml/badge.svg)](https://github.com/typesnippet/react-phone-hooks/actions/workflows/tests.yml)
88

99
This comprehensive toolkit features custom hooks and utility functions tailored for phone number formatting, parsing,
1010
and validation. It supports international standards, making it suitable for phone number processing applications across
@@ -18,4 +18,4 @@ list. Be proud of what you build!
1818

1919
## License
2020

21-
Copyright (C) 2023 Artyom Vancyan. [MIT](https://github.com/pysnippet/react-phone-hooks/blob/master/LICENSE)
21+
Copyright (C) 2023 Artyom Vancyan. [MIT](https://github.com/typesnippet/react-phone-hooks/blob/master/LICENSE)

development/src/phone-hooks/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const getRawValue = (value: PhoneNumber | string) => {
2727
}
2828

2929
export const displayFormat = (value: string) => {
30+
/** Returns the formatted value that can be displayed as an actual input value */
3031
return value.replace(/[.\s\D]+$/, "").replace(/(\(\d+)$/, "$1)");
3132
}
3233

@@ -35,6 +36,11 @@ export const cleanInput = (input: any, pattern: string) => {
3536
return Array.from(pattern, c => input[0] === c || slots.has(c) ? input.shift() || c : c);
3637
}
3738

39+
export const getFormattedNumber = (rawValue: any, pattern: string) => {
40+
/** Returns the reformatted input value based on the given pattern */
41+
return displayFormat(cleanInput(rawValue, pattern.replaceAll(/\d/g, ".")).join(""));
42+
}
43+
3844
export const checkValidity = (metadata: PhoneNumber, strict: boolean = false) => {
3945
/** Checks if both the area code and phone number match the validation pattern */
4046
const pattern = (validations as any)[metadata.isoCode as keyof typeof validations][Number(strict)];
@@ -134,11 +140,11 @@ export const usePhone = ({
134140
i = clean(target.value.slice(0, i)).findIndex(c => slots.has(c));
135141
return i < 0 ? prev[prev.length - 1] : backRef.current ? prev[i - 1] || first : i;
136142
});
137-
target.value = displayFormat(clean(target.value).join(""));
143+
target.value = getFormattedNumber(target.value, pattern);
138144
target.setSelectionRange(i, j);
139145
backRef.current = false;
140146
setValue(target.value);
141-
}, [clean, first, prev])
147+
}, [clean, first, pattern, prev])
142148

143149
return {
144150
clean,

jestconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"transform": {
3+
"^.+\\.tsx?$": "ts-jest"
4+
},
5+
"testRegex": "/tests/.*\\.test\\.(tsx?)$",
6+
"moduleFileExtensions": [
7+
"ts",
8+
"js",
9+
"tsx",
10+
"json",
11+
"node"
12+
],
13+
"testEnvironment": "jsdom"
14+
}

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,20 @@
5959
"rename": "bash -c 'for file in *.js; do mv $file \"${file%.js}.$0.js\"; done'",
6060
"build": "tsc --module commonjs && npm run rename -- cjs && tsc --declaration",
6161
"prebuild": "rm -r metadata stylesheet.json index* types* styles* || true",
62-
"postbuild": "cp resources/stylesheet.json stylesheet.json"
62+
"postbuild": "cp resources/stylesheet.json stylesheet.json",
63+
"test": "jest --config jestconfig.json"
6364
},
6465
"license": "MIT",
6566
"peerDependencies": {
6667
"react": ">=16"
6768
},
6869
"devDependencies": {
70+
"@testing-library/react": "^14.1.2",
71+
"@types/jest": "^29.5.7",
6972
"@types/react": "^18.2.34",
73+
"jest": "^29.7.0",
74+
"jest-environment-jsdom": "^29.7.0",
75+
"ts-jest": "^29.1.1",
7076
"tslib": "^2.6.2",
7177
"tsx": "^3.12.10",
7278
"typescript": "^5.2.2"

0 commit comments

Comments
 (0)