|
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { Component, ComponentRef, DebugElement, input } from '@angular/core'; |
1 | 3 | import { FormSelectDirective } from './form-select.directive';
|
2 |
| -import { TestBed } from '@angular/core/testing'; |
| 4 | +import { By } from '@angular/platform-browser'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + imports: [FormSelectDirective], |
| 8 | + template: ` <select cSelect [sizing]="sizing()" [valid]="valid()"></select> ` |
| 9 | +}) |
| 10 | +class TestComponent { |
| 11 | + readonly sizing = input<'' | 'sm' | 'lg' | string>(); |
| 12 | + readonly valid = input<boolean>(); |
| 13 | +} |
3 | 14 |
|
4 | 15 | describe('FormSelectDirective', () => {
|
| 16 | + let component: TestComponent; |
| 17 | + let fixture: ComponentFixture<TestComponent>; |
| 18 | + let debugElement: DebugElement; |
| 19 | + let componentRef: ComponentRef<TestComponent>; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + TestBed.configureTestingModule({ |
| 23 | + imports: [TestComponent] |
| 24 | + }).compileComponents(); |
| 25 | + |
| 26 | + fixture = TestBed.createComponent(TestComponent); |
| 27 | + component = fixture.componentInstance; |
| 28 | + componentRef = fixture.componentRef; |
| 29 | + debugElement = fixture.debugElement.query(By.directive(FormSelectDirective)); |
| 30 | + fixture.detectChanges(); |
| 31 | + }); |
| 32 | + |
5 | 33 | it('should create an instance', () => {
|
6 | 34 | TestBed.runInInjectionContext(() => {
|
7 | 35 | const directive = new FormSelectDirective();
|
8 | 36 | expect(directive).toBeTruthy();
|
9 | 37 | });
|
10 | 38 | });
|
| 39 | + |
| 40 | + it('should have css classes', () => { |
| 41 | + expect(debugElement.nativeElement).toHaveClass('form-select'); |
| 42 | + componentRef.setInput('sizing', 'sm'); |
| 43 | + fixture.detectChanges(); |
| 44 | + expect(debugElement.nativeElement).toHaveClass('form-select-sm'); |
| 45 | + componentRef.setInput('sizing', 'lg'); |
| 46 | + fixture.detectChanges(); |
| 47 | + expect(debugElement.nativeElement).toHaveClass('form-select-lg'); |
| 48 | + componentRef.setInput('sizing', ''); |
| 49 | + fixture.detectChanges(); |
| 50 | + expect(debugElement.nativeElement).not.toHaveClass('form-select-sm'); |
| 51 | + expect(debugElement.nativeElement).not.toHaveClass('form-select-lg'); |
| 52 | + expect(debugElement.nativeElement).not.toHaveClass('is-invalid'); |
| 53 | + expect(debugElement.nativeElement).not.toHaveClass('is-valid'); |
| 54 | + componentRef.setInput('valid', true); |
| 55 | + fixture.detectChanges(); |
| 56 | + expect(debugElement.nativeElement).not.toHaveClass('is-invalid'); |
| 57 | + expect(debugElement.nativeElement).toHaveClass('is-valid'); |
| 58 | + componentRef.setInput('valid', false); |
| 59 | + fixture.detectChanges(); |
| 60 | + expect(debugElement.nativeElement).toHaveClass('is-invalid'); |
| 61 | + expect(debugElement.nativeElement).not.toHaveClass('is-valid'); |
| 62 | + componentRef.setInput('valid', undefined); |
| 63 | + fixture.detectChanges(); |
| 64 | + expect(debugElement.nativeElement).not.toHaveClass('is-invalid'); |
| 65 | + expect(debugElement.nativeElement).not.toHaveClass('is-valid'); |
| 66 | + }); |
11 | 67 | });
|
0 commit comments