Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 1 | import {ComponentFixture, TestBed} from '@angular/core/testing'; |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 2 | import {Component, DebugElement} from "@angular/core"; |
| 3 | import {By} from "@angular/platform-browser"; |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 4 | import {InputPreventionPatternDirective} from './inputPreventionPattern.directive'; |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 5 | |
| 6 | @Component({ |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 7 | template: `<input |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 8 | patternInput |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 9 | pattern="^[a-zA-Z0-9._-]*$">` |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 10 | }) |
| 11 | class TestHoverFocusComponent { |
| 12 | } |
| 13 | |
| 14 | |
| 15 | describe('InputPrevention Pattern Directive', () => { |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 16 | 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 Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 31 | test('directive should be defined', () => { |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 32 | expect(directiveInstance).toBeDefined(); |
| 33 | }); |
| 34 | |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 35 | test('pattern exists', () => { |
| 36 | expect(inputEl.nativeElement.pattern).toEqual('^[a-zA-Z0-9._-]*$'); |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 37 | }); |
| 38 | |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 39 | test('kepress legal input', () => { |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 40 | fixture.detectChanges(); |
| 41 | inputEl.nativeElement.value = "legalInput"; |
| 42 | expect(new RegExp(inputEl.nativeElement.pattern).test(inputEl.nativeElement.value)).toBeTruthy(); |
| 43 | }); |
| 44 | |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 45 | test('kepress illegal input', () => { |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 46 | inputEl.triggerEventHandler('kepress', " "); |
| 47 | fixture.detectChanges(); |
| 48 | expect(inputEl.nativeElement.value).toBe(''); |
| 49 | }); |
| 50 | |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 51 | |
| 52 | test('kepress event legal input should return event', () => { |
| 53 | const event = <any>{ key: 'A'}; |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 54 | inputEl.nativeElement.value = "legalInput"; |
| 55 | let result = directiveInstance.onKeypress(event); |
| 56 | expect(result).toBe(event); |
| 57 | }); |
| 58 | |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 59 | |
| 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) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 79 | let result = directiveInstance.onKeypress(event); |
| 80 | expect(event.preventDefault).toHaveBeenCalled(); |
| 81 | }); |
| 82 | }); |