1
+ import assert from "assert" ;
2
+
1
3
import { useCallback , useEffect , useRef , useState } from "react" ;
2
- import { act , renderHook } from "@testing-library/react" ;
4
+ import { act , render , renderHook , screen } from "@testing-library/react" ;
5
+ import userEvent from "@testing-library/user-event" ;
3
6
4
- import { cleanInput , displayFormat , getRawValue , parsePhoneNumber , usePhone } from "../src" ;
7
+ import { cleanInput , displayFormat , getFormattedNumber , getRawValue , parsePhoneNumber , useMask , usePhone } from "../src" ;
5
8
6
9
const usePhoneTester = ( {
7
10
country = "us" ,
@@ -15,8 +18,8 @@ const usePhoneTester = ({
15
18
const [ countryCode , setCountryCode ] = useState < string > ( country ) ;
16
19
17
20
const {
18
- clean,
19
21
value,
22
+ pattern,
20
23
metadata,
21
24
setValue,
22
25
countriesList,
@@ -31,15 +34,14 @@ const usePhoneTester = ({
31
34
} ) ;
32
35
33
36
const update = useCallback ( ( value : string ) => {
34
- const formattedNumber = displayFormat ( clean ( value ) . join ( "" ) ) ;
37
+ const formattedNumber = getFormattedNumber ( value , pattern ) ;
35
38
const phoneMetadata = parsePhoneNumber ( formattedNumber , countriesList ) ;
36
39
setCountryCode ( phoneMetadata . isoCode as any ) ;
37
40
setValue ( formattedNumber ) ;
38
- } , [ clean , countriesList , setValue ] ) ;
41
+ } , [ countriesList , pattern , setValue ] ) ;
39
42
40
43
const backspace = useCallback ( ( ) => {
41
- const rawValue = getRawValue ( value ) ;
42
- const formattedNumber = displayFormat ( rawValue . slice ( 0 , - 1 ) ) ;
44
+ const formattedNumber = displayFormat ( getRawValue ( value ) . slice ( 0 , - 1 ) ) ;
43
45
const phoneMetadata = parsePhoneNumber ( formattedNumber , countriesList ) ;
44
46
setCountryCode ( phoneMetadata . isoCode as any ) ;
45
47
setValue ( formattedNumber ) ;
@@ -60,15 +62,19 @@ const usePhoneTester = ({
60
62
if ( ! initialValue . startsWith ( metadata ?. [ 2 ] as string ) ) {
61
63
initialValue = metadata ?. [ 2 ] as string ;
62
64
}
63
- const formattedNumber = displayFormat ( clean ( initialValue ) . join ( "" ) ) ;
65
+ const formattedNumber = getFormattedNumber ( initialValue , pattern ) ;
64
66
const phoneMetadata = parsePhoneNumber ( formattedNumber , countriesList ) ;
65
67
setCountryCode ( phoneMetadata . isoCode as any ) ;
66
68
setValue ( formattedNumber ) ;
67
- } , [ clean , countriesList , metadata , setValue , value ] )
69
+ } , [ countriesList , pattern , metadata , setValue , value ] )
68
70
69
71
return { update, search, select, value, metadata, backspace, countriesList} ;
70
72
}
71
73
74
+ const UseMaskTester = ( { pattern = "" , ...props } : any ) => {
75
+ return < input data-testid = "input" { ...useMask ( pattern ) } { ...props } /> ;
76
+ }
77
+
72
78
describe ( "Verifying the functionality of hooks" , ( ) => {
73
79
it ( "Check the usePhone hook initiation and updates" , ( ) => {
74
80
const { result} = renderHook ( usePhoneTester , {
@@ -132,4 +138,16 @@ describe("Verifying the functionality of hooks", () => {
132
138
133
139
expect ( ( result . current . metadata as any ) [ 0 ] ) . toBe ( "us" ) ;
134
140
} )
141
+
142
+ it ( "Check useMask for basic use case" , async ( ) => {
143
+ render ( < UseMaskTester
144
+ pattern = "+... (..) ... ....."
145
+ onChange = { ( e : any ) => {
146
+ const isValid = "+380 (11) 222 34567" . startsWith ( e . target . value ) ;
147
+ assert ( isValid || "+380 (1)" === e . target . value ) ;
148
+ } }
149
+ /> ) ;
150
+
151
+ await userEvent . type ( screen . getByTestId ( "input" ) , "3801122234567" ) ;
152
+ } )
135
153
} )
0 commit comments