|
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { Component, ComponentRef, DebugElement, input } from '@angular/core'; |
| 3 | +import { By } from '@angular/platform-browser'; |
1 | 4 | import { FormDirective } from './form.directive';
|
2 |
| -import { TestBed } from '@angular/core/testing'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + imports: [FormDirective], |
| 8 | + template: '<form cForm [validated]="validated()"></form>' |
| 9 | +}) |
| 10 | +class TestComponent { |
| 11 | + readonly validated = input(false); |
| 12 | +} |
3 | 13 |
|
4 | 14 | describe('FormDirective', () => {
|
| 15 | + let component: TestComponent; |
| 16 | + let fixture: ComponentFixture<TestComponent>; |
| 17 | + let debugElement: DebugElement; |
| 18 | + let componentRef: ComponentRef<TestComponent>; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + TestBed.configureTestingModule({ |
| 22 | + imports: [TestComponent] |
| 23 | + }).compileComponents(); |
| 24 | + |
| 25 | + fixture = TestBed.createComponent(TestComponent); |
| 26 | + component = fixture.componentInstance; |
| 27 | + componentRef = fixture.componentRef; |
| 28 | + debugElement = fixture.debugElement.query(By.directive(FormDirective)); |
| 29 | + fixture.detectChanges(); |
| 30 | + }); |
| 31 | + |
5 | 32 | it('should create an instance', () => {
|
6 | 33 | TestBed.runInInjectionContext(() => {
|
7 | 34 | const directive = new FormDirective();
|
8 | 35 | expect(directive).toBeTruthy();
|
9 | 36 | });
|
10 | 37 | });
|
| 38 | + |
| 39 | + it('should have css classes', () => { |
| 40 | + expect(debugElement.nativeElement).not.toHaveClass('was-validated'); |
| 41 | + componentRef.setInput('validated', true); |
| 42 | + fixture.detectChanges(); |
| 43 | + expect(debugElement.nativeElement).toHaveClass('was-validated'); |
| 44 | + componentRef.setInput('validated', false); |
| 45 | + fixture.detectChanges(); |
| 46 | + expect(debugElement.nativeElement).not.toHaveClass('was-validated'); |
| 47 | + }); |
11 | 48 | });
|
0 commit comments