|
| 1 | +import { ChangeDetectionStrategy, Component, Provider, Type } from '@angular/core'; |
| 2 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | +import { By } from '@angular/platform-browser'; |
| 4 | +import { KbqHovered } from './hovered'; |
| 5 | + |
| 6 | +const createComponent = <T>(component: Type<T>, providers: Provider[] = []): ComponentFixture<T> => { |
| 7 | + TestBed.configureTestingModule({ |
| 8 | + imports: [component], |
| 9 | + providers |
| 10 | + }); |
| 11 | + const fixture = TestBed.createComponent<T>(component); |
| 12 | + |
| 13 | + fixture.autoDetectChanges(); |
| 14 | + |
| 15 | + return fixture; |
| 16 | +}; |
| 17 | + |
| 18 | +@Component({ |
| 19 | + standalone: true, |
| 20 | + selector: 'test-by-attr-directive', |
| 21 | + template: '<div kbqHovered>test</div>', |
| 22 | + imports: [KbqHovered], |
| 23 | + changeDetection: ChangeDetectionStrategy.OnPush |
| 24 | +}) |
| 25 | +class TestByAttrDirective {} |
| 26 | + |
| 27 | +@Component({ |
| 28 | + standalone: true, |
| 29 | + selector: 'test-by-host-directive', |
| 30 | + hostDirectives: [KbqHovered], |
| 31 | + template: '<div>test</div>', |
| 32 | + changeDetection: ChangeDetectionStrategy.OnPush |
| 33 | +}) |
| 34 | +class TestByHostDirective {} |
| 35 | + |
| 36 | +describe(KbqHovered.name, () => { |
| 37 | + it('should toggle selector by attribute directive', () => { |
| 38 | + const { debugElement } = createComponent(TestByAttrDirective); |
| 39 | + const element = debugElement.query(By.css('div')).nativeElement as HTMLElement; |
| 40 | + |
| 41 | + expect(element.classList).not.toContain('kbq-hovered'); |
| 42 | + |
| 43 | + element.dispatchEvent(new MouseEvent('mouseenter')); |
| 44 | + |
| 45 | + expect(element.classList).toContain('kbq-hovered'); |
| 46 | + |
| 47 | + element.dispatchEvent(new MouseEvent('mouseleave')); |
| 48 | + |
| 49 | + expect(element.classList).not.toContain('kbq-hovered'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should toggle selector by host directive', () => { |
| 53 | + const { debugElement } = createComponent(TestByHostDirective); |
| 54 | + const element = debugElement.nativeElement as HTMLElement; |
| 55 | + |
| 56 | + expect(element.classList).not.toContain('kbq-hovered'); |
| 57 | + |
| 58 | + element.dispatchEvent(new MouseEvent('mouseenter')); |
| 59 | + |
| 60 | + expect(element.classList).toContain('kbq-hovered'); |
| 61 | + |
| 62 | + element.dispatchEvent(new MouseEvent('mouseleave')); |
| 63 | + |
| 64 | + expect(element.classList).not.toContain('kbq-hovered'); |
| 65 | + }); |
| 66 | +}); |
0 commit comments