@@ -12,67 +12,122 @@ describe('IgxSelection', () => {
12
12
declarations : [
13
13
TriggerTextSelectionComponent ,
14
14
TriggerTextSelectionOnClickComponent ,
15
- TriggerTextSelectionFalseOnClickComponent
16
15
] ,
17
16
imports : [ IgxTextSelectionModule ]
18
17
} ) ;
19
18
} ) ) ;
20
19
21
- it ( 'Should select the text which is into the input' , async ( ) => {
20
+
21
+ it ( 'Should select the text which is into the input' , ( ) => {
22
22
const fix = TestBed . createComponent ( TriggerTextSelectionComponent ) ;
23
23
fix . detectChanges ( ) ;
24
24
25
25
const input = fix . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
26
26
input . focus ( ) ;
27
27
28
- fix . whenStable ( ) . then ( ( ) => {
29
- fix . detectChanges ( ) ;
30
- expect ( input . selectionEnd ) . toEqual ( input . value . length ) ;
31
- expect ( input . value . substring ( input . selectionStart , input . selectionEnd ) ) . toEqual ( input . value ) ;
32
- } ) ;
28
+ expect ( input . selectionEnd ) . toEqual ( input . value . length ) ;
29
+ expect ( input . value . substring ( input . selectionStart , input . selectionEnd ) ) . toEqual ( input . value ) ;
33
30
} ) ;
34
31
35
- it ( 'Should select the text when the input is clicked' , async ( ) => {
32
+ it ( 'Should select the text when the input is clicked' , ( ) => {
36
33
const fix = TestBed . createComponent ( TriggerTextSelectionOnClickComponent ) ;
37
34
fix . detectChanges ( ) ;
38
35
39
36
const input : DebugElement = fix . debugElement . query ( By . css ( 'input' ) ) ;
40
37
const inputNativeElem = input . nativeElement ;
41
38
const inputElem : HTMLElement = input . nativeElement ;
42
39
43
- inputElem . click ( ) ;
40
+ inputElem . click ( ) ; // might need to change to .focus
41
+ fix . detectChanges ( ) ;
44
42
45
- fix . whenStable ( ) . then ( ( ) => {
46
- fix . detectChanges ( ) ;
47
- expect ( inputNativeElem . selectionEnd ) . toEqual ( inputNativeElem . value . length ) ;
48
- expect ( inputNativeElem . value . substring ( inputNativeElem . selectionStart , inputNativeElem . selectionEnd ) )
49
- . toEqual ( inputNativeElem . value ) ;
50
- } ) ;
43
+ expect ( inputNativeElem . selectionEnd ) . toEqual ( inputNativeElem . value . length ) ;
44
+ expect ( inputNativeElem . value . substring ( inputNativeElem . selectionStart , inputNativeElem . selectionEnd ) )
45
+ . toEqual ( inputNativeElem . value ) ;
51
46
} ) ;
52
47
53
- it ( 'Shouldn\'t make a selection when the state is set to false' , async ( ) => {
48
+ fit ( 'Should check if the value is selected if based on input type' , ( ) => {
54
49
const fix = TestBed . createComponent ( TriggerTextSelectionOnClickComponent ) ;
55
- fix . detectChanges ( ) ;
50
+ const selectableTypes : Types [ ] = [
51
+ { "text" : "Some Values!" } ,
52
+ { "search" : "Search!" } ,
53
+ { "password" : "********" } ,
54
+ { "tel" : '+(359)554-587-415' } ,
55
+ { "url" : "www.infragistics.com" } ,
56
+ { "number" : 2136512312 }
57
+ ] ;
58
+
59
+ const nonSelectableTypes : Types [ ] = [
60
+ { 'date' : new Date ( ) } ,
61
+ { 'datetime-local' : "2018-06-12T19:30" } ,
62
+
63
+ { 'month' : "2018-05" } ,
64
+ { 'time' : "13:30" } ,
65
+ { 'week' : "2017-W01" }
66
+ ] ;
67
+
68
+ //skipped on purpose, if needed feel free to add to any of the above categories
69
+ //const irrelevantTypes = ['button', 'checkbox', 'color', 'file', 'hidden', 'image', 'radio', 'range', 'reset', 'submit']
56
70
57
71
const input = fix . debugElement . query ( By . css ( 'input' ) ) ;
58
- const inputType = ( input . nativeElement as HTMLInputElement ) . type ;
59
- let inputList = [ "email" , "number" , "password" , "tel" , "text" , "url" ] ;
60
- expect ( inputList . indexOf ( inputType ) ) ;
72
+ const inputNativeElem = input . nativeElement ;
73
+ const inputElem : HTMLElement = input . nativeElement ;
74
+
75
+ selectableTypes . forEach ( el => {
76
+ let type = Object . keys ( el ) [ 0 ] ;
77
+ let val = el [ type ] ;
78
+ fix . componentInstance . inputType = type ;
79
+ fix . componentInstance . inputValue = val ;
80
+ fix . detectChanges ( ) ;
81
+
82
+ inputElem . click ( ) ;
83
+ fix . detectChanges ( ) ;
84
+
85
+ if ( type !== 'number' && type !== 'tel' ) {
86
+ expect ( inputNativeElem . selectionEnd ) . toEqual ( inputNativeElem . value . length ) ;
87
+ expect ( inputNativeElem . value . substring ( inputNativeElem . selectionStart , inputNativeElem . selectionEnd ) )
88
+ . toEqual ( val ) ;
89
+ }
90
+
91
+ if ( type === 'number' ) {
92
+ let selection = document . getSelection ( ) . toString ( ) ;
93
+ console . log ( selection ) ;
94
+ console . log ( val )
95
+ fix . componentInstance . waitForOneSecond ( ) . then ( ( ) => {
96
+ expect ( val . toString ( ) . lenght ) . toBe ( selection . length ) ;
97
+ } ) ;
98
+ }
99
+ } ) ;
100
+
101
+ nonSelectableTypes . forEach ( el => {
102
+ let type = Object . keys ( el ) [ 0 ] ;
103
+ let val = el [ type ] ;
104
+ fix . componentInstance . inputType = type ;
105
+ fix . componentInstance . inputValue = val ;
106
+ fix . detectChanges ( ) ;
107
+
108
+ inputElem . focus ( ) ;
109
+ fix . detectChanges ( ) ;
110
+ expect ( inputNativeElem . selectionStart ) . toEqual ( inputNativeElem . selectionEnd ) ;
111
+ } ) ;
61
112
} ) ;
62
113
63
- it ( 'Should check if the input type is adequate for text selection' , async ( ) => {
64
- const fix = TestBed . createComponent ( TriggerTextSelectionFalseOnClickComponent ) ;
114
+
115
+
116
+ it ( 'Shouldn\'t make a selection when the state is set to false' , ( ) => {
117
+ const fix = TestBed . createComponent ( TriggerTextSelectionOnClickComponent ) ;
118
+ fix . componentInstance . selectValue = false ;
119
+ fix . componentInstance . inputType = "text" ;
120
+ fix . componentInstance . inputValue = "4444444" ;
65
121
fix . detectChanges ( ) ;
66
122
67
123
const input = fix . debugElement . query ( By . css ( 'input' ) ) ;
68
124
const inputNativeElem = input . nativeElement ;
69
125
const inputElem : HTMLElement = input . nativeElement ;
70
126
71
- inputElem . click ( ) ;
72
- fix . detectChanges ( ) ;
73
127
74
- expect ( inputNativeElem . selectionEnd ) . toEqual ( 0 ) ;
75
- expect ( inputNativeElem . value . substring ( inputNativeElem . selectionStart , inputNativeElem . selectionEnd ) ) . toEqual ( '' ) ;
128
+ inputElem . focus ( ) ;
129
+ fix . detectChanges ( ) ;
130
+ expect ( inputNativeElem . selectionStart ) . toEqual ( inputNativeElem . selectionEnd ) ;
76
131
} ) ;
77
132
} ) ;
78
133
@@ -87,14 +142,25 @@ class TriggerTextSelectionComponent { }
87
142
@Component ( {
88
143
template :
89
144
`
90
- <input type="text " [igxTextSelection] #select="igxTextSelection" (click)="select.trigger()" value="Some custom value! " />
145
+ <input #input [ type]="inputType " [igxTextSelection]="selectValue" #select="igxTextSelection" (click)="select.trigger()" [ value]="inputValue " />
91
146
`
92
147
} )
93
- class TriggerTextSelectionOnClickComponent { }
94
- @Component ( {
95
- template :
96
- `
97
- <input type="text" [igxTextSelection]="false" #select="igxTextSelection" (click)="select.trigger()" value="Some custom value!" />
98
- `
99
- } )
100
- class TriggerTextSelectionFalseOnClickComponent { }
148
+ class TriggerTextSelectionOnClickComponent {
149
+ public selectValue = true ;
150
+ public inputType : any = "text" ;
151
+ public inputValue : any = "Some custom V!"
152
+
153
+ @ViewChild ( 'input' , { read : HTMLInputElement , static :true } ) public input : HTMLInputElement ;
154
+
155
+ public waitForOneSecond ( ) {
156
+ return new Promise ( resolve => {
157
+ setTimeout ( ( ) => {
158
+ resolve ( "I promise to return after one second!" ) ;
159
+ } , 1000 ) ;
160
+ } ) ;
161
+ }
162
+ }
163
+
164
+ interface Types {
165
+ [ key : string ] : any ;
166
+ }
0 commit comments