|
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'; |
2 | 4 | 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 | +} |
3 | 20 |
|
4 | 21 | 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 | + |
5 | 39 | it('should create an instance', () => {
|
6 | 40 | TestBed.runInInjectionContext(() => {
|
7 | 41 | const directive = new FormFloatingDirective();
|
8 | 42 | expect(directive).toBeTruthy();
|
9 | 43 | });
|
10 | 44 | });
|
| 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 | + }); |
11 | 55 | });
|
0 commit comments