Skip to content

Commit 38ecf8e

Browse files
committed
refactor(form-floating): host binding, cleanup, tests
1 parent 053afba commit 38ecf8e

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed
Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,55 @@
1-
import { TestBed } from '@angular/core/testing';
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { Component, ComponentRef, DebugElement, input } from '@angular/core';
3+
import { By } from '@angular/platform-browser';
24
import { FormFloatingDirective } from './form-floating.directive';
5+
import { FormControlDirective } from '../form-control/form-control.directive';
6+
import { FormLabelDirective } from '../form-label/form-label.directive';
7+
8+
@Component({
9+
imports: [FormFloatingDirective, FormControlDirective, FormLabelDirective],
10+
template: `
11+
<div [cFormFloating]="floating()">
12+
<textarea cFormControl id="floatingTextarea" placeholder="Leave a comment here"></textarea>
13+
<label cLabel for="floatingTextarea">Comments</label>
14+
</div>
15+
`
16+
})
17+
class TestComponent {
18+
readonly floating = input(false);
19+
}
320

421
describe('FormFloatingDirective', () => {
22+
let component: TestComponent;
23+
let fixture: ComponentFixture<TestComponent>;
24+
let debugElement: DebugElement;
25+
let componentRef: ComponentRef<TestComponent>;
26+
27+
beforeEach(() => {
28+
TestBed.configureTestingModule({
29+
imports: [TestComponent]
30+
}).compileComponents();
31+
32+
fixture = TestBed.createComponent(TestComponent);
33+
component = fixture.componentInstance;
34+
componentRef = fixture.componentRef;
35+
debugElement = fixture.debugElement.query(By.directive(FormFloatingDirective));
36+
fixture.detectChanges();
37+
});
38+
539
it('should create an instance', () => {
640
TestBed.runInInjectionContext(() => {
741
const directive = new FormFloatingDirective();
842
expect(directive).toBeTruthy();
943
});
1044
});
45+
46+
it('should have css classes', () => {
47+
expect(debugElement.nativeElement).not.toHaveClass('form-floating');
48+
componentRef.setInput('floating', true);
49+
fixture.detectChanges();
50+
expect(debugElement.nativeElement).toHaveClass('form-floating');
51+
componentRef.setInput('floating', false);
52+
fixture.detectChanges();
53+
expect(debugElement.nativeElement).not.toHaveClass('form-floating');
54+
});
1155
});
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import { booleanAttribute, computed, Directive, input } from '@angular/core';
1+
import { booleanAttribute, Directive, input } from '@angular/core';
22

33
@Directive({
44
selector: '[cFormFloating]',
5-
host: { '[class]': 'hostClasses()' }
5+
host: { '[class.form-floating]': 'floating()' }
66
})
77
export class FormFloatingDirective {
88
/**
99
* Enable floating labels
1010
* @type boolean
1111
*/
1212
readonly floating = input(true, { transform: booleanAttribute, alias: 'cFormFloating' });
13-
14-
readonly hostClasses = computed(() => {
15-
return {
16-
'form-floating': this.floating()
17-
} as Record<string, boolean>;
18-
});
1913
}

0 commit comments

Comments
 (0)