Skip to content

Commit

Permalink
feat(cdk): add useHostBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbe812 committed Apr 19, 2023
1 parent ed51684 commit 3187ff3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions libs/cdk/rx-interop/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './lib/use-functions/use-on-changes$';
export * from './lib/use-functions/use-on-changes-state$';
export * from './lib/use-functions/use-host-listener$';
export * from './lib/use-functions/use-from-event$';
export * from './lib/use-functions/use-host-binding';
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {useHostBinding} from "./use-host-binding";
import {Component, ElementRef, inject} from "@angular/core";
import {TestBed} from "@angular/core/testing";

describe('useHostBinding', () => {
it('should create Testcomponent', async () => {
const {component} = await setup(true);
expect(component).toBeTruthy();
});

it('should add css class on init when enabledByDefault is true', async () => {
const {cssClass, fixture} = await setup(true);

expect(containsCssClass(fixture, cssClass)).toBe(true);
});

it('should NOT have css class on init when enabledByDefault is false', async () => {
const { cssClass, fixture} = await setup(false);

expect(containsCssClass(fixture, cssClass)).toBe(false);
});

it('should add and remove css class', async () => {
const {component, cssClass, fixture} = await setup(false);

expect(containsCssClass(fixture, cssClass)).toBe(false);

component.hostBinding$.set(true);
expect(containsCssClass(fixture, cssClass)).toBe(true);

component.hostBinding$.set(false);
expect(containsCssClass(fixture, cssClass)).toBe(false);
});
})


async function setup(enabledByDefault: boolean) {
const {TestComponent, cssClass} = createTestComponent(enabledByDefault);
await TestBed.configureTestingModule({
imports: [TestComponent]
}).compileComponents();

const fixture = TestBed.createComponent(TestComponent);
const component = fixture.componentInstance;
fixture.detectChanges();


return {
component,
cssClass,
fixture
};
}

function containsCssClass(fixture, cssClass: string){
return fixture.componentInstance.classList.contains(cssClass)
}



function createTestComponent(enabledByDefault: boolean){
const cssClass = 'test-css-class';

@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'test',
template: '',
standalone: true,
imports: [],
})
class TestComponent {
hostBinding$ = useHostBinding(cssClass, enabledByDefault);
classList = (inject(ElementRef).nativeElement as HTMLElement).classList;
}

return {TestComponent, cssClass}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {ElementRef, inject, Renderer2} from "@angular/core";
* @param className
* @param enabledByDefault
*/
export function useHostBinding$(className: string, enabledByDefault: boolean) {
export function useHostBinding(className: string, enabledByDefault: boolean) {
const renderer2 = inject(Renderer2);
const {nativeElement} = inject(ElementRef)

Expand Down

0 comments on commit 3187ff3

Please sign in to comment.