|
| 1 | +import { Component, DebugElement } from '@angular/core'; |
| 2 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | +import { By } from '@angular/platform-browser'; |
| 4 | +import { DisabledDirective } from './disabled-directive'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + template: ` |
| 8 | + <button [dsDisabled]="isDisabled">Test Button</button> |
| 9 | + ` |
| 10 | +}) |
| 11 | +class TestComponent { |
| 12 | + isDisabled = false; |
| 13 | +} |
| 14 | + |
| 15 | +describe('DisabledDirective', () => { |
| 16 | + let component: TestComponent; |
| 17 | + let fixture: ComponentFixture<TestComponent>; |
| 18 | + let button: DebugElement; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + TestBed.configureTestingModule({ |
| 22 | + declarations: [TestComponent, DisabledDirective] |
| 23 | + }); |
| 24 | + fixture = TestBed.createComponent(TestComponent); |
| 25 | + component = fixture.componentInstance; |
| 26 | + button = fixture.debugElement.query(By.css('button')); |
| 27 | + fixture.detectChanges(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should bind aria-disabled to false initially', () => { |
| 31 | + expect(button.nativeElement.getAttribute('aria-disabled')).toBe('false'); |
| 32 | + expect(button.nativeElement.classList.contains('disabled')).toBeFalse(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should bind aria-disabled to true and add disabled class when isDisabled is true', () => { |
| 36 | + component.isDisabled = true; |
| 37 | + fixture.detectChanges(); |
| 38 | + |
| 39 | + expect(button.nativeElement.getAttribute('aria-disabled')).toBe('true'); |
| 40 | + expect(button.nativeElement.classList.contains('disabled')).toBeTrue(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should prevent click events when disabled', () => { |
| 44 | + component.isDisabled = true; |
| 45 | + fixture.detectChanges(); |
| 46 | + |
| 47 | + let clickHandled = false; |
| 48 | + button.nativeElement.addEventListener('click', () => clickHandled = true); |
| 49 | + |
| 50 | + button.nativeElement.click(); |
| 51 | + |
| 52 | + expect(clickHandled).toBeFalse(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should prevent Enter or Space keydown events when disabled', () => { |
| 56 | + component.isDisabled = true; |
| 57 | + fixture.detectChanges(); |
| 58 | + |
| 59 | + let keydownHandled = false; |
| 60 | + button.nativeElement.addEventListener('keydown', () => keydownHandled = true); |
| 61 | + |
| 62 | + const enterEvent = new KeyboardEvent('keydown', { key: 'Enter' }); |
| 63 | + const spaceEvent = new KeyboardEvent('keydown', { key: 'Space' }); |
| 64 | + |
| 65 | + button.nativeElement.dispatchEvent(enterEvent); |
| 66 | + button.nativeElement.dispatchEvent(spaceEvent); |
| 67 | + |
| 68 | + expect(keydownHandled).toBeFalse(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should allow click and keydown events when not disabled', () => { |
| 72 | + let clickHandled = false; |
| 73 | + let keydownHandled = false; |
| 74 | + |
| 75 | + button.nativeElement.addEventListener('click', () => clickHandled = true); |
| 76 | + button.nativeElement.addEventListener('keydown', () => keydownHandled = true); |
| 77 | + |
| 78 | + button.nativeElement.click(); |
| 79 | + |
| 80 | + const enterEvent = new KeyboardEvent('keydown', { key: 'Enter' }); |
| 81 | + const spaceEvent = new KeyboardEvent('keydown', { key: 'Space' }); |
| 82 | + |
| 83 | + button.nativeElement.dispatchEvent(enterEvent); |
| 84 | + button.nativeElement.dispatchEvent(spaceEvent); |
| 85 | + |
| 86 | + expect(clickHandled).toBeTrue(); |
| 87 | + expect(keydownHandled).toBeTrue(); |
| 88 | + }); |
| 89 | +}); |
0 commit comments