Skip to content

Commit 45c8171

Browse files
committed
refactor(text-color): host binding, cleanup, tests
1 parent a09ec7a commit 45c8171

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1-
import { TestBed } from '@angular/core/testing';
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
22
import { TextColorDirective } from './text-color.directive';
3+
import { Component, DebugElement } from '@angular/core';
4+
import { By } from '@angular/platform-browser';
5+
6+
@Component({
7+
imports: [TextColorDirective],
8+
template: '<div cTextColor="primary"></div>'
9+
})
10+
class TestComponent {}
311

412
describe('TextColorDirective', () => {
13+
let component: TestComponent;
14+
let fixture: ComponentFixture<TestComponent>;
15+
let debugElement: DebugElement;
16+
17+
beforeEach(() => {
18+
TestBed.configureTestingModule({
19+
imports: [TestComponent]
20+
}).compileComponents();
21+
22+
fixture = TestBed.createComponent(TestComponent);
23+
component = fixture.componentInstance;
24+
debugElement = fixture.debugElement.query(By.directive(TextColorDirective));
25+
fixture.detectChanges();
26+
});
27+
528
it('should create an instance', () => {
629
TestBed.runInInjectionContext(() => {
730
const directive = new TextColorDirective();
831
expect(directive).toBeTruthy();
932
});
1033
});
34+
35+
it('should have css classes', () => {
36+
expect(debugElement.nativeElement).toHaveClass('text-primary');
37+
});
1138
});
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { Directive, HostBinding, input, InputSignal } from '@angular/core';
1+
import { computed, Directive, input, InputSignal } from '@angular/core';
22
import { TextColors } from '../coreui.types';
33

44
@Directive({
5-
selector: '[cTextColor]'
5+
selector: '[cTextColor]',
6+
host: {
7+
'[class]': 'hostClasses()'
8+
}
69
})
710
export class TextColorDirective {
811
/**
@@ -11,11 +14,10 @@ export class TextColorDirective {
1114
*/
1215
readonly color: InputSignal<TextColors> = input('', { alias: 'cTextColor' });
1316

14-
@HostBinding('class')
15-
get hostClasses(): any {
17+
readonly hostClasses = computed(() => {
1618
const color = this.color();
1719
return {
1820
[`text-${color}`]: !!color
1921
};
20-
}
22+
});
2123
}

0 commit comments

Comments
 (0)