1+ /**
2+ * @jest -environment jsdom
3+ */
4+
5+ import { applyMaskedInput } from './index' ;
6+ import { fireEvent } from '@testing-library/dom' ;
7+
8+ describe ( "index as applyMaskedInput" , ( ) => {
9+
10+ let inputElement : HTMLInputElement ;
11+
12+ beforeEach ( ( ) => {
13+ document . body . innerHTML = '<input type="text" id="test-input" />' ;
14+ inputElement = document . getElementById ( "test-input" ) as HTMLInputElement ;
15+ } ) ;
16+
17+ it ( "should mask input value" , ( ) => {
18+ const maskedInput = applyMaskedInput ( inputElement , { character : "*" } ) ;
19+
20+ // simulate user typing hello
21+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "h" } ) ) ;
22+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "e" } ) ) ;
23+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "l" } ) ) ;
24+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "l" } ) ) ;
25+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "o" } ) ) ;
26+
27+ expect ( inputElement . value ) . toBe ( "*****" ) ;
28+ expect ( maskedInput . getOriginalValue ( ) ) . toBe ( "hello" ) ;
29+ expect ( inputElement . selectionStart ) . toBe ( 5 ) ;
30+ } ) ;
31+
32+ it ( "should mask input value with the specified character" , ( ) => {
33+ const maskedInput = applyMaskedInput ( inputElement , { character : "*" } ) ;
34+
35+ // simulate user typing
36+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "h" } ) ) ;
37+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "e" } ) ) ;
38+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "l" } ) ) ;
39+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "l" } ) ) ;
40+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "o" } ) ) ;
41+
42+ // simulate user typing caret position 5
43+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "1" } ) ) ;
44+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "2" } ) ) ;
45+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "3" } ) ) ;
46+
47+ expect ( inputElement . value ) . toBe ( "********" ) ;
48+ expect ( maskedInput . getOriginalValue ( ) ) . toBe ( "hello123" ) ;
49+ expect ( inputElement . selectionStart ) . toBe ( 8 ) ;
50+ } ) ;
51+
52+ it ( "should mask input value with selected character" , ( ) => {
53+ const maskedInput = applyMaskedInput ( inputElement , { character : "*" } ) ;
54+
55+ // simulate user typing
56+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "hello world" } ) ) ;
57+
58+ // simulate user typing caret position 5 and selected 5 chars
59+ inputElement . setSelectionRange ( 5 , 11 ) ;
60+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "1" } ) ) ;
61+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "2" } ) ) ;
62+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "3" } ) ) ;
63+
64+ expect ( inputElement . value ) . toBe ( "********" ) ;
65+ expect ( maskedInput . getOriginalValue ( ) ) . toBe ( "hello123" ) ;
66+ expect ( inputElement . selectionStart ) . toBe ( 8 ) ;
67+ } ) ;
68+
69+ it ( "should mask input value with delete character : backspace" , ( ) => {
70+ const maskedInput = applyMaskedInput ( inputElement , { character : "*" } ) ;
71+
72+ // simulate user typing
73+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "hello world" } ) ) ;
74+
75+ // simulate user typing caret position 10
76+ inputElement . setSelectionRange ( 10 , 10 ) ;
77+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "deleteContentBackward" } ) ) ;
78+
79+ expect ( inputElement . value ) . toBe ( "**********" ) ;
80+ expect ( maskedInput . getOriginalValue ( ) ) . toBe ( "hello word" ) ;
81+ expect ( inputElement . selectionStart ) . toBe ( 9 ) ;
82+ } ) ;
83+
84+ it ( "should mask input value with delete selected character : backspace" , ( ) => {
85+ const maskedInput = applyMaskedInput ( inputElement , { character : "*" } ) ;
86+
87+ // simulate user typing
88+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "hello world" } ) ) ;
89+
90+ // simulate user typing caret position 5
91+ inputElement . setSelectionRange ( 5 , 10 ) ;
92+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "deleteContentBackward" } ) ) ;
93+
94+ expect ( inputElement . value ) . toBe ( "******" ) ;
95+ expect ( maskedInput . getOriginalValue ( ) ) . toBe ( "hellod" ) ;
96+ expect ( inputElement . selectionStart ) . toBe ( 5 ) ;
97+ } ) ;
98+
99+ it ( "should mask input value with delete character : delete" , ( ) => {
100+ const maskedInput = applyMaskedInput ( inputElement , { character : "*" } ) ;
101+
102+ // simulate user typing
103+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "hello world" } ) ) ;
104+
105+ // simulate user typing caret position 10
106+ inputElement . setSelectionRange ( 10 , 10 ) ;
107+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "deleteContentForward" } ) ) ;
108+
109+ expect ( inputElement . value ) . toBe ( "**********" ) ;
110+ expect ( maskedInput . getOriginalValue ( ) ) . toBe ( "hello worl" ) ;
111+ expect ( inputElement . selectionStart ) . toBe ( 10 ) ;
112+ } ) ;
113+
114+ it ( "should mask input value with delete selected character : delete" , ( ) => {
115+ const maskedInput = applyMaskedInput ( inputElement , { character : "*" } ) ;
116+
117+ // simulate user typing
118+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "hello world" } ) ) ;
119+
120+ // simulate user typing caret position 5
121+ inputElement . setSelectionRange ( 5 , 10 ) ;
122+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "deleteContentForward" } ) ) ;
123+
124+ expect ( inputElement . value ) . toBe ( "******" ) ;
125+ expect ( maskedInput . getOriginalValue ( ) ) . toBe ( "hellod" ) ;
126+ expect ( inputElement . selectionStart ) . toBe ( 5 ) ;
127+ } ) ;
128+
129+ it ( "should mask input value with paste" , ( ) => {
130+ const maskedInput = applyMaskedInput ( inputElement , { character : "*" } ) ;
131+
132+ // create paste event with clipboard data
133+ fireEvent . paste ( inputElement , {
134+ clipboardData : { getData : ( ) => "test123" }
135+ } ) ;
136+
137+ expect ( inputElement . value ) . toBe ( "*******" ) ;
138+ expect ( maskedInput . getOriginalValue ( ) ) . toBe ( "test123" ) ;
139+ expect ( inputElement . selectionStart ) . toBe ( 7 ) ;
140+ } ) ;
141+
142+ it ( "should mask input value with paste block character" , ( ) => {
143+ const maskedInput = applyMaskedInput ( inputElement , { character : "*" } ) ;
144+
145+ inputElement . dispatchEvent ( new InputEvent ( "beforeinput" , { inputType : "insertText" , data : "hello world" } ) ) ;
146+ inputElement . setSelectionRange ( 5 , 10 ) ;
147+
148+ // create paste event with clipboard data
149+ fireEvent . paste ( inputElement , {
150+ clipboardData : { getData : ( ) => "test123" }
151+ } ) ;
152+
153+ expect ( inputElement . value ) . toBe ( "*************" ) ;
154+ expect ( maskedInput . getOriginalValue ( ) ) . toBe ( "hellotest123d" ) ;
155+ expect ( inputElement . selectionStart ) . toBe ( 12 ) ;
156+ } ) ;
157+
158+ } ) ;
0 commit comments