Skip to content

Commit 139ad88

Browse files
committed
Add test-cases for searching and selecting a country
1 parent aa52909 commit 139ad88

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

tests/hooks.test.tsx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {useCallback, useEffect, useRef, useState} from "react";
22
import {act, renderHook} from "@testing-library/react";
33

4-
import {displayFormat, getRawValue, parsePhoneNumber, usePhone} from "../src";
4+
import {cleanInput, displayFormat, getRawValue, parsePhoneNumber, usePhone} from "../src";
55

66
const usePhoneTester = ({
77
country = "us",
@@ -47,7 +47,11 @@ const usePhoneTester = ({
4747

4848
const search = useCallback(setQuery, []);
4949

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]);
5155

5256
useEffect(() => {
5357
if (initiatedRef.current) return;
@@ -81,4 +85,33 @@ describe("Verifying the functionality of hooks", () => {
8185
expect(result.current.value).toBe("+1 (111)");
8286
expect((result.current.metadata as any)[0]).toBe("us");
8387
})
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+
})
84117
})

0 commit comments

Comments
 (0)