blob: ec5bfd8f3df19eb1fd997beea2bf0dc13a3aee1a [file] [log] [blame]
Ittay Stern6f900cc2018-08-29 17:01:32 +03001import {ComponentFixture, TestBed} from '@angular/core/testing';
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03002import {Component, DebugElement} from "@angular/core";
3import {By} from "@angular/platform-browser";
Ittay Stern6f900cc2018-08-29 17:01:32 +03004import {InputPreventionPatternDirective} from './inputPreventionPattern.directive';
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03005
6@Component({
Ittay Stern6f900cc2018-08-29 17:01:32 +03007 template: `<input
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03008 patternInput
Ittay Stern6f900cc2018-08-29 17:01:32 +03009 pattern="^[a-zA-Z0-9._-]*$">`
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030010})
11class TestHoverFocusComponent {
12}
13
14
15describe('InputPrevention Pattern Directive', () => {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030016 let component: TestHoverFocusComponent;
17 let fixture: ComponentFixture<TestHoverFocusComponent>;
18 let directiveInstance : InputPreventionPatternDirective;
19 let inputEl: DebugElement;
20
21 beforeEach(() => {
22 TestBed.configureTestingModule({
23 declarations: [TestHoverFocusComponent, InputPreventionPatternDirective]
24 });
25 fixture = TestBed.createComponent(TestHoverFocusComponent);
26 component = fixture.componentInstance;
27 inputEl = fixture.debugElement.query(By.css('input'));
28 directiveInstance = inputEl.injector.get(InputPreventionPatternDirective);
29 });
30
Ittay Stern6f900cc2018-08-29 17:01:32 +030031 test('directive should be defined', () => {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030032 expect(directiveInstance).toBeDefined();
33 });
34
Ittay Stern6f900cc2018-08-29 17:01:32 +030035 test('pattern exists', () => {
36 expect(inputEl.nativeElement.pattern).toEqual('^[a-zA-Z0-9._-]*$');
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030037 });
38
Ittay Stern6f900cc2018-08-29 17:01:32 +030039 test('kepress legal input', () => {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030040 fixture.detectChanges();
41 inputEl.nativeElement.value = "legalInput";
42 expect(new RegExp(inputEl.nativeElement.pattern).test(inputEl.nativeElement.value)).toBeTruthy();
43 });
44
Ittay Stern6f900cc2018-08-29 17:01:32 +030045 test('kepress illegal input', () => {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030046 inputEl.triggerEventHandler('kepress', " ");
47 fixture.detectChanges();
48 expect(inputEl.nativeElement.value).toBe('');
49 });
50
Ittay Stern6f900cc2018-08-29 17:01:32 +030051
52 test('kepress event legal input should return event', () => {
53 const event = <any>{ key: 'A'};
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030054 inputEl.nativeElement.value = "legalInput";
55 let result = directiveInstance.onKeypress(event);
56 expect(result).toBe(event);
57 });
58
Ittay Stern6f900cc2018-08-29 17:01:32 +030059
60 test('kepress event legal(-) input should return event', () => {
61 const event = <any>{ key: '-'};
62 inputEl.nativeElement.value = "legalInput";
63 let result = directiveInstance.onKeypress(event);
64 expect(result).toBe(event);
65 });
66
67 test('kepress event legal (.) input should return event', () => {
68 const event = <any>{ key: '.'};
69 inputEl.nativeElement.value = "legalInput";
70 let result = directiveInstance.onKeypress(event);
71 expect(result).toBe(event);
72 });
73
74
75 test('kepress event illegal input should prevent default', () => {
76 const event = <any>{key: '$', preventDefault : function () {} };
77 jest.spyOn(event, 'preventDefault');
78 inputEl.nativeElement.value = "$";
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030079 let result = directiveInstance.onKeypress(event);
80 expect(event.preventDefault).toHaveBeenCalled();
81 });
82});