Skip to content

Commit 4f3bc02

Browse files
committed
fix(testSelDir): Added test for all input types
1 parent 11c93bc commit 4f3bc02

File tree

2 files changed

+101
-37
lines changed

2 files changed

+101
-37
lines changed

projects/igniteui-angular/src/lib/directives/text-selection/text-selection.directive.spec.ts

Lines changed: 101 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,122 @@ describe('IgxSelection', () => {
1212
declarations: [
1313
TriggerTextSelectionComponent,
1414
TriggerTextSelectionOnClickComponent,
15-
TriggerTextSelectionFalseOnClickComponent
1615
],
1716
imports: [IgxTextSelectionModule]
1817
});
1918
}));
2019

21-
it('Should select the text which is into the input', async () => {
20+
21+
it('Should select the text which is into the input', () => {
2222
const fix = TestBed.createComponent(TriggerTextSelectionComponent);
2323
fix.detectChanges();
2424

2525
const input = fix.debugElement.query(By.css('input')).nativeElement;
2626
input.focus();
2727

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);
3330
});
3431

35-
it('Should select the text when the input is clicked', async () => {
32+
it('Should select the text when the input is clicked', ()=> {
3633
const fix = TestBed.createComponent(TriggerTextSelectionOnClickComponent);
3734
fix.detectChanges();
3835

3936
const input: DebugElement = fix.debugElement.query(By.css('input'));
4037
const inputNativeElem = input.nativeElement;
4138
const inputElem: HTMLElement = input.nativeElement;
4239

43-
inputElem.click();
40+
inputElem.click(); // might need to change to .focus
41+
fix.detectChanges();
4442

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);
5146
});
5247

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', () => {
5449
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+
{'email': '[email protected]'},
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']
5670

5771
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+
});
61112
});
62113

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";
65121
fix.detectChanges();
66122

67123
const input = fix.debugElement.query(By.css('input'));
68124
const inputNativeElem = input.nativeElement;
69125
const inputElem: HTMLElement = input.nativeElement;
70126

71-
inputElem.click();
72-
fix.detectChanges();
73127

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);
76131
});
77132
});
78133

@@ -87,14 +142,25 @@ class TriggerTextSelectionComponent { }
87142
@Component({
88143
template:
89144
`
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" />
91146
`
92147
})
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+
}

projects/igniteui-angular/src/lib/directives/text-selection/text-selection.directive.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ export class IgxTextSelectionDirective {
108108

109109
public trigger() {
110110
if (this.selected && this.nativeElement.value.length) {
111-
requestAnimationFrame(() => {
112111
this.nativeElement.select();
113-
})
114112
}
115113
}
116114
}

0 commit comments

Comments
 (0)