@@ -10,115 +10,48 @@ const MockEl = (setSelectionRange?: (from: number, to: number) => void): any =>
1010 } ) ( ) ;
1111
1212describe ( "MaskedInput" , ( ) => {
13- it ( "is empty before interaction" , ( ) => {
14- const maskedInput = new MaskedInput ( "" , MockEl ( ) ) ;
15- expect ( maskedInput . value ) . toEqual ( "" ) ;
13+ describe ( "WHEN uncontrolled" , ( ) => {
14+ it ( "retains initial value before interaction" , ( ) => {
15+ const maskedInput = new MaskedInput ( "00:00:00" , MockEl ( ) ) ;
16+ expect ( maskedInput . value ) . toEqual ( "00:00:00" ) ;
17+ } ) ;
1618 } ) ;
1719
18- describe ( "WHEN showTemplateWhileEditing is true" , ( ) => {
19- describe ( "AND no value set and focus received" , ( ) => {
20- it ( "THEN value = template and cursor position = 0" , ( ) => {
21- vi . useFakeTimers ( ) ;
22-
23- const maskedInput = new MaskedInput ( "" , MockEl ( ) ) ;
24- maskedInput . focus ( ) ;
25- expect ( maskedInput . value ) . toEqual ( "hh:mm:ss" ) ;
26-
27- vi . advanceTimersToNextTimer ( ) ;
20+ describe ( "WHEN focus received" , ( ) => {
21+ it ( "THEN value = '00:00:00' and cursor position = 0" , ( ) => {
22+ vi . useFakeTimers ( ) ;
2823
29- expect ( maskedInput . cursorPos ) . toEqual ( 0 ) ;
30- expect ( maskedInput . selectionStart ) . toEqual ( 0 ) ;
31- expect ( maskedInput . selectionEnd ) . toEqual ( 2 ) ;
32- } ) ;
24+ const maskedInput = new MaskedInput ( "00:00:00" , MockEl ( ) ) ;
25+ maskedInput . focus ( ) ;
26+ vi . advanceTimersToNextTimer ( ) ;
27+ expect ( maskedInput . cursorPos ) . toEqual ( 0 ) ;
28+ expect ( maskedInput . selectionStart ) . toEqual ( 0 ) ;
29+ expect ( maskedInput . selectionEnd ) . toEqual ( 2 ) ;
3330 } ) ;
3431
35- describe ( "AND focus received and zero pressed 6 times " , ( ) => {
36- it ( "THEN value is edited correctly, cursor positions are correct and final value = '00:00:00 '" , ( ) => {
37- const maskedInput = new MaskedInput ( "" , MockEl ( ) ) ;
32+ describe ( "AND 1-6 key pressed" , ( ) => {
33+ it ( "THEN value is edited correctly, cursor positions are correct and final value = '12:34:56 '" , ( ) => {
34+ const maskedInput = new MaskedInput ( "00:00:00 " , MockEl ( ) ) ;
3835 vi . useFakeTimers ( ) ;
39-
4036 maskedInput . focus ( ) ;
41-
4237 vi . advanceTimersToNextTimer ( ) ;
43-
4438 // prettier-ignore
45- const expectedValues = [ "0h:mm:ss " , "00:mm:ss " , "00:0m:ss " , "00:00:ss " , "00:00:0s " , "00:00:00 " ]
39+ const expectedValues = [ "10:00:00 " , "12: 00:00 " , "12:30:00 " , "12:34:00 " , "12:34:50 " , "12:34:56 " ]
4640 const expectedCursorPositions = [ 1 , 3 , 4 , 6 , 7 , 6 ] ;
4741 for ( let i = 0 ; i < 6 ; i ++ ) {
48- maskedInput . update ( "0" ) ;
42+ maskedInput . update ( String ( i + 1 ) as Digit ) ;
4943 expect ( maskedInput . value ) . toEqual ( expectedValues [ i ] ) ;
5044 expect ( maskedInput . cursorPos ) . toEqual ( expectedCursorPositions [ i ] ) ;
5145 }
5246 } ) ;
53- describe ( "AND backspace pressed 3 times" , ( ) => {
54- it ( "THEN value = template and cursor position = 0" , ( ) => {
55- const maskedInput = new MaskedInput ( "" , MockEl ( ) ) ;
56- vi . useFakeTimers ( ) ;
57-
58- maskedInput . focus ( ) ;
59-
60- vi . advanceTimersToNextTimer ( ) ;
61-
62- // prettier-ignore
63- const expectedValues = [ "00:00:ss" , "00:mm:ss" , "hh:mm:ss" ]
64- const expectedCursorPositions = [ 3 , 0 , 0 ] ;
65- for ( let i = 0 ; i < 6 ; i ++ ) {
66- maskedInput . update ( "0" ) ;
67- }
68- for ( let i = 0 ; i < 3 ; i ++ ) {
69- maskedInput . backspace ( ) ;
70- expect ( maskedInput . cursorPos ) . toEqual ( expectedCursorPositions [ i ] ) ;
71- expect ( maskedInput . value ) . toEqual ( expectedValues [ i ] ) ;
72- }
73- } ) ;
74- } ) ;
7547 } ) ;
76- } ) ;
77- } ) ;
78- describe ( "WHEN showTemplateWhileEditing is false" , ( ) => {
79- describe ( "AND no value set and focus received" , ( ) => {
80- it ( "THEN value = '00:00:00' and cursor position = 0" , ( ) => {
81- vi . useFakeTimers ( ) ;
82-
83- const maskedInput = new MaskedInput ( "" , MockEl ( ) , false ) ;
84- maskedInput . focus ( ) ;
85- expect ( maskedInput . value ) . toEqual ( "00:00:00" ) ;
86-
87- vi . advanceTimersToNextTimer ( ) ;
88-
89- expect ( maskedInput . cursorPos ) . toEqual ( 0 ) ;
90- expect ( maskedInput . selectionStart ) . toEqual ( 0 ) ;
91- expect ( maskedInput . selectionEnd ) . toEqual ( 2 ) ;
92- } ) ;
93- } ) ;
94-
95- describe ( "AND focus received and 1-6 key pressed" , ( ) => {
96- it ( "THEN value is edited correctly, cursor positions are correct and final value = '12:34:56'" , ( ) => {
97- const maskedInput = new MaskedInput ( "" , MockEl ( ) , false ) ;
98- vi . useFakeTimers ( ) ;
99-
100- maskedInput . focus ( ) ;
10148
102- vi . advanceTimersToNextTimer ( ) ;
103-
104- // prettier-ignore
105- const expectedValues = [ "10:00:00" , "12:00:00" , "12:30:00" , "12:34:00" , "12:34:50" , "12:34:56" ]
106- const expectedCursorPositions = [ 1 , 3 , 4 , 6 , 7 , 6 ] ;
107- for ( let i = 0 ; i < 6 ; i ++ ) {
108- maskedInput . update ( String ( i + 1 ) as Digit ) ;
109- expect ( maskedInput . value ) . toEqual ( expectedValues [ i ] ) ;
110- expect ( maskedInput . cursorPos ) . toEqual ( expectedCursorPositions [ i ] ) ;
111- }
112- } ) ;
11349 describe ( "AND backspace pressed 3 times" , ( ) => {
11450 it ( "THEN value = template and cursor position = 0" , ( ) => {
115- const maskedInput = new MaskedInput ( "" , MockEl ( ) , false ) ;
51+ const maskedInput = new MaskedInput ( "00:00:00 " , MockEl ( ) ) ;
11652 vi . useFakeTimers ( ) ;
117-
11853 maskedInput . focus ( ) ;
119-
12054 vi . advanceTimersToNextTimer ( ) ;
121-
12255 // prettier-ignore
12356 const expectedValues = [ "12:34:00" , "12:00:00" , "00:00:00" ]
12457 const expectedCursorPositions = [ 3 , 0 , 0 ] ;
0 commit comments