|
| 1 | +import { Component, DebugElement } from '@angular/core'; |
| 2 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | +import { By } from '@angular/platform-browser'; |
1 | 4 | import { TableActiveDirective } from './table-active.directive';
|
2 | 5 |
|
| 6 | +@Component({ |
| 7 | + imports: [TableActiveDirective], |
| 8 | + template: ` <tr [cTableActive]="active"></tr>` |
| 9 | +}) |
| 10 | +class TestComponent { |
| 11 | + active = false; |
| 12 | +} |
| 13 | + |
3 | 14 | describe('TableActiveDirective', () => {
|
| 15 | + let fixture: ComponentFixture<TestComponent>; |
| 16 | + let debugElement: DebugElement; |
| 17 | + let directive: TableActiveDirective; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + TestBed.configureTestingModule({ |
| 21 | + imports: [TestComponent] |
| 22 | + }); |
| 23 | + fixture = TestBed.createComponent(TestComponent); |
| 24 | + debugElement = fixture.debugElement.query(By.directive(TableActiveDirective)); |
| 25 | + directive = debugElement.injector.get(TableActiveDirective); |
| 26 | + fixture.detectChanges(); |
| 27 | + }); |
| 28 | + |
4 | 29 | it('should create an instance', () => {
|
5 |
| - const directive = new TableActiveDirective(); |
6 | 30 | expect(directive).toBeTruthy();
|
| 31 | + TestBed.runInInjectionContext(() => { |
| 32 | + const directive = new TableActiveDirective(); |
| 33 | + expect(directive).toBeTruthy(); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should add class "table-active" when active is true', () => { |
| 38 | + fixture.componentInstance.active = true; |
| 39 | + fixture.detectChanges(); |
| 40 | + expect(debugElement.nativeElement.classList).toContain('table-active'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should not add class "table-active" when active is false', () => { |
| 44 | + fixture.componentInstance.active = false; |
| 45 | + fixture.detectChanges(); |
| 46 | + expect(debugElement.nativeElement.classList).not.toContain('table-active'); |
7 | 47 | });
|
8 | 48 | });
|
0 commit comments