|
1 | 1 | import {useCallback, useEffect, useRef, useState} from "react";
|
2 | 2 | import {act, renderHook} from "@testing-library/react";
|
3 | 3 |
|
4 |
| -import {displayFormat, getRawValue, parsePhoneNumber, usePhone} from "../src"; |
| 4 | +import {cleanInput, displayFormat, getRawValue, parsePhoneNumber, usePhone} from "../src"; |
5 | 5 |
|
6 | 6 | const usePhoneTester = ({
|
7 | 7 | country = "us",
|
@@ -47,7 +47,11 @@ const usePhoneTester = ({
|
47 | 47 |
|
48 | 48 | const search = useCallback(setQuery, []);
|
49 | 49 |
|
50 |
| - const select = useCallback(setCountryCode, []); |
| 50 | + const select = useCallback((isoCode: string) => { |
| 51 | + const mask = (countriesList.find(([iso]) => iso === isoCode) as any)[3]; |
| 52 | + setValue(displayFormat(cleanInput(mask, mask).join(""))); |
| 53 | + setCountryCode(isoCode); |
| 54 | + }, [setValue, countriesList]); |
51 | 55 |
|
52 | 56 | useEffect(() => {
|
53 | 57 | if (initiatedRef.current) return;
|
@@ -81,4 +85,33 @@ describe("Verifying the functionality of hooks", () => {
|
81 | 85 | expect(result.current.value).toBe("+1 (111)");
|
82 | 86 | expect((result.current.metadata as any)[0]).toBe("us");
|
83 | 87 | })
|
| 88 | + |
| 89 | + it("Check usePhone for country code update", () => { |
| 90 | + const {result} = renderHook(usePhoneTester, { |
| 91 | + initialProps: { |
| 92 | + initialValue: "17021234567", |
| 93 | + } |
| 94 | + }); |
| 95 | + expect(result.current.value).toBe("+1 (702) 123 4567"); |
| 96 | + expect((result.current.metadata as any)[0]).toBe("us"); |
| 97 | + |
| 98 | + act(() => result.current.select("ua")); |
| 99 | + |
| 100 | + expect(result.current.value).toBe("+380"); |
| 101 | + expect((result.current.metadata as any)[0]).toBe("ua"); |
| 102 | + }) |
| 103 | + |
| 104 | + it("Check usePhone for searching a country", () => { |
| 105 | + const {result} = renderHook(usePhoneTester, { |
| 106 | + initialProps: {} |
| 107 | + }); |
| 108 | + |
| 109 | + act(() => result.current.search("Armenia")); |
| 110 | + |
| 111 | + expect(result.current.countriesList).toHaveLength(1); |
| 112 | + |
| 113 | + act(() => result.current.select(result.current.countriesList[0][0])); |
| 114 | + |
| 115 | + expect((result.current.metadata as any)[0]).toBe("am"); |
| 116 | + }) |
84 | 117 | })
|
0 commit comments