846ff70f2852e3824b94874d0a06796eb8d3f01a
[vid.git] /
1 import {TestBed, ComponentFixture} from '@angular/core/testing';
2 import {Component, DebugElement} from "@angular/core";
3 import {By} from "@angular/platform-browser";
4 import { InputPreventionPatternDirective } from './inputPreventionPattern.directive';
5
6 @Component({
7   template: `<input 
8     patternInput
9     pattern="^[a-zA-Z0-9_]*$">`
10 })
11 class TestHoverFocusComponent {
12 }
13
14
15 describe('InputPrevention Pattern Directive', () => {
16
17   let component: TestHoverFocusComponent;
18   let fixture: ComponentFixture<TestHoverFocusComponent>;
19   let directiveInstance : InputPreventionPatternDirective;
20   let inputEl: DebugElement;
21
22   beforeEach(() => {
23     TestBed.configureTestingModule({
24       declarations: [TestHoverFocusComponent, InputPreventionPatternDirective]
25     });
26     fixture = TestBed.createComponent(TestHoverFocusComponent);
27     component = fixture.componentInstance;
28     inputEl = fixture.debugElement.query(By.css('input'));
29     directiveInstance = inputEl.injector.get(InputPreventionPatternDirective);
30   });
31
32   it('directive should be defined', () => {
33     expect(directiveInstance).toBeDefined();
34   });
35
36   it('pattern exists', () => {
37     expect(inputEl.nativeElement.pattern).toEqual('^[a-zA-Z0-9_]*$');
38   });
39
40   it('kepress legal input', () => {
41     fixture.detectChanges();
42     inputEl.nativeElement.value = "legalInput";
43     expect(new RegExp(inputEl.nativeElement.pattern).test(inputEl.nativeElement.value)).toBeTruthy();
44   });
45
46   it('kepress illegal input', () => {
47     inputEl.triggerEventHandler('kepress', " ");
48     fixture.detectChanges();
49     expect(inputEl.nativeElement.value).toBe('');
50   });
51
52   it('kepress event legal input should return event', () => {
53     const event = <any>{ key: 'A' };
54     inputEl.nativeElement.value = "legalInput";
55     let result = directiveInstance.onKeypress(event);
56     expect(result).toBe(event);
57   });
58
59   it('kepress event illegal input should prevent default', () => {
60     const event = <any>{key: '-', preventDefault : function () {} };
61     spyOn(event, 'preventDefault');
62     inputEl.nativeElement.value = "-";
63     let result = directiveInstance.onKeypress(event);
64     expect(event.preventDefault).toHaveBeenCalled();
65   });
66 });