Skip to content

Commit 204dcba

Browse files
committed
Add and update necessary test suits for the disableParentheses feature
1 parent 313d8e7 commit 204dcba

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

tests/hooks.test.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const usePhoneTester = ({
1212
onlyCountries = [],
1313
excludeCountries = [],
1414
preferredCountries = [],
15+
disableParentheses = false,
1516
}) => {
1617
const initiatedRef = useRef<boolean>(false);
1718
const [query, setQuery] = useState<string>("");
@@ -31,6 +32,7 @@ const usePhoneTester = ({
3132
onlyCountries,
3233
excludeCountries,
3334
preferredCountries,
35+
disableParentheses,
3436
});
3537

3638
const update = useCallback((value: string) => {
@@ -50,7 +52,8 @@ const usePhoneTester = ({
5052
const search = useCallback(setQuery, []);
5153

5254
const select = useCallback((isoCode: string) => {
53-
const mask = (countriesList.find(([iso]) => iso === isoCode) as any)[3];
55+
const pattern = (countriesList.find(([iso]) => iso === isoCode) as any)[3];
56+
const mask = disableParentheses ? pattern.replace(/[()]/g, "") : pattern;
5457
setValue(displayFormat(cleanInput(mask, mask).join("")));
5558
setCountryCode(isoCode);
5659
}, [setValue, countriesList]);
@@ -121,6 +124,24 @@ describe("Verifying the functionality of hooks", () => {
121124
expect((result.current.metadata as any)[0]).toBe("am");
122125
})
123126

127+
it("Check usePhone without parentheses", () => {
128+
const {result} = renderHook(usePhoneTester, {
129+
initialProps: {
130+
country: "au",
131+
disableParentheses: true,
132+
}
133+
});
134+
135+
act(() => result.current.update("6104111"));
136+
137+
expect(result.current.value).toBe("+61 0 4111");
138+
expect((result.current.metadata as any)[0]).toBe("au");
139+
140+
act(() => result.current.select("ms"));
141+
142+
expect(result.current.value).toBe("+1 664");
143+
})
144+
124145
it("Check usePhone for country detection", () => {
125146
const {result} = renderHook(usePhoneTester, {
126147
initialProps: {}

tests/utils.test.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,27 @@ describe("Verifying the basic functionality", () => {
88
const metadata = getMetadata(rawValue);
99

1010
const formattedNumber = getFormattedNumber(rawValue, (metadata as any)[3]);
11+
const formattedNumberWithoutParentheses = formattedNumber.replace(/[()]/g, "");
1112
const formattedNumberOverloaded = getFormattedNumber(rawValue);
1213
const parsedPhoneNumber = parsePhoneNumber(formattedNumber);
13-
const rawPhoneNumber = getRawValue(formattedNumber);
14+
const parsedPhoneNumberWithoutParentheses = parsePhoneNumber(formattedNumberWithoutParentheses);
15+
const rawPhoneNumber1 = getRawValue(formattedNumber);
16+
const rawPhoneNumber2 = getRawValue(formattedNumberWithoutParentheses);
1417

1518
assert(formattedNumber === formattedNumberOverloaded);
1619
assert(formattedNumber !== null && formattedNumber === "+1 (702) 123 4567");
1720
assert(parsedPhoneNumber !== null && parsedPhoneNumber.countryCode === 1);
1821
assert(parsedPhoneNumber.areaCode === "702" && parsedPhoneNumber.phoneNumber === "1234567");
19-
assert(rawPhoneNumber === rawValue);
22+
assert(parsedPhoneNumberWithoutParentheses !== null && parsedPhoneNumberWithoutParentheses.countryCode === 1);
23+
assert(parsedPhoneNumberWithoutParentheses.areaCode === "702" && parsedPhoneNumberWithoutParentheses.phoneNumber === "1234567");
24+
assert(rawPhoneNumber1 === rawValue);
25+
assert(rawPhoneNumber2 === rawValue);
2026
})
2127

2228
it("Check the phone number validity", () => {
29+
assert(checkValidity(parsePhoneNumber("+1 702 123 4567")) === true);
30+
assert(checkValidity(parsePhoneNumber("+1 702 123 456")) === false);
31+
2332
assert(checkValidity(parsePhoneNumber("+1 (702) 123 4567")) === true);
2433
assert(checkValidity(parsePhoneNumber("+1 (702) 123 456")) === false);
2534

0 commit comments

Comments
 (0)