Skip to content

Commit a09ec7a

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

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed
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';
2+
import { Component, DebugElement } from '@angular/core';
3+
import { By } from '@angular/platform-browser';
24
import { TextBgColorDirective } from './text-bg-color.directive';
35

6+
@Component({
7+
imports: [TextBgColorDirective],
8+
template: '<div cTextBgColor="primary"></div>'
9+
})
10+
class TestComponent {}
11+
412
describe('TextBgColorDirective', () => {
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(TextBgColorDirective));
25+
fixture.detectChanges();
26+
});
27+
528
it('should create an instance', () => {
629
TestBed.runInInjectionContext(() => {
730
const directive = new TextBgColorDirective();
831
expect(directive).toBeTruthy();
932
});
1033
});
34+
35+
it('should have css classes', () => {
36+
expect(debugElement.nativeElement).toHaveClass('text-bg-primary');
37+
});
1138
});
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 { Colors } from '../coreui.types';
33

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

14-
@HostBinding('class')
15-
get hostClasses(): any {
17+
readonly hostClasses = computed(() => {
1618
const color = this.textBgColor();
1719
return {
1820
[`text-bg-${color}`]: !!color
19-
};
20-
}
21+
} as Record<string, boolean>;
22+
});
2123
}

0 commit comments

Comments
 (0)