Skip to content

Commit 540d1a3

Browse files
committed
refactor(table-active): signal inputs, host bindings, cleanup, tests
1 parent a40efed commit 540d1a3

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed
Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
1+
import { Component, DebugElement } from '@angular/core';
2+
import { ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { By } from '@angular/platform-browser';
14
import { TableActiveDirective } from './table-active.directive';
25

6+
@Component({
7+
imports: [TableActiveDirective],
8+
template: ` <tr [cTableActive]="active"></tr>`
9+
})
10+
class TestComponent {
11+
active = false;
12+
}
13+
314
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+
429
it('should create an instance', () => {
5-
const directive = new TableActiveDirective();
630
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');
747
});
848
});
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import { booleanAttribute, Directive, HostBinding, Input } from '@angular/core';
1+
import { booleanAttribute, Directive, input } from '@angular/core';
22

33
@Directive({
4-
selector: '[cTableActive]'
4+
selector: '[cTableActive]',
5+
exportAs: 'cTableActive',
6+
host: {
7+
'[class.table-active]': 'active()'
8+
}
59
})
610
export class TableActiveDirective {
711
/**
812
* Highlight a table row or cell
9-
* @type boolean
13+
* @return boolean
1014
*/
11-
@Input({ alias: 'cTableActive', transform: booleanAttribute }) active: string | boolean = false;
12-
13-
@HostBinding('class')
14-
get hostClasses(): any {
15-
return {
16-
'table-active': this.active
17-
};
18-
}
15+
readonly active = input(false, { alias: "cTableActive", transform: booleanAttribute });
1916
}

0 commit comments

Comments
 (0)