|
1 | | -import{TestBed}from'@angular/core/testing'; |
| 1 | +import{ComponentFixture,TestBed}from'@angular/core/testing'; |
2 | 2 | import{RoundedDirective}from'./rounded.directive'; |
| 3 | +import{Component,DebugElement,input}from'@angular/core'; |
| 4 | +import{By}from'@angular/platform-browser'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | +imports:[RoundedDirective], |
| 8 | +template:'<div [cRounded]="rounded()"></div>' |
| 9 | +}) |
| 10 | +classTestComponent{ |
| 11 | +readonlyrounded=input(1); |
| 12 | +} |
3 | 13 |
|
4 | 14 | describe('RoundedDirective',()=>{ |
| 15 | +letcomponent:TestComponent; |
| 16 | +letfixture:ComponentFixture<TestComponent>; |
| 17 | +letdebugElement:DebugElement; |
| 18 | + |
| 19 | +beforeEach(()=>{ |
| 20 | +TestBed.configureTestingModule({ |
| 21 | +imports:[TestComponent] |
| 22 | +}).compileComponents(); |
| 23 | + |
| 24 | +fixture=TestBed.createComponent(TestComponent); |
| 25 | +component=fixture.componentInstance; |
| 26 | +debugElement=fixture.debugElement.query(By.directive(RoundedDirective)); |
| 27 | +fixture.detectChanges(); |
| 28 | +}); |
| 29 | + |
5 | 30 | it('should create an instance',()=>{ |
6 | 31 | TestBed.runInInjectionContext(()=>{ |
7 | | -constdirective=newRoundedDirective(); |
8 | | -expect(directive).toBeTruthy(); |
| 32 | +constdirective=newRoundedDirective(); |
| 33 | +expect(directive).toBeTruthy(); |
| 34 | +}); |
| 35 | +}); |
| 36 | + |
| 37 | +it('should have css classes',()=>{ |
| 38 | +expect(debugElement.nativeElement).toHaveClass('rounded-1'); |
| 39 | +fixture.componentRef.setInput('rounded',true); |
| 40 | +fixture.detectChanges(); |
| 41 | +expect(debugElement.nativeElement).toHaveClass('rounded'); |
| 42 | +fixture.componentRef.setInput('rounded',{ |
| 43 | +top:false, |
| 44 | +end:true, |
| 45 | +circle:true, |
| 46 | +pill:true, |
| 47 | +size:3 |
9 | 48 | }); |
| 49 | +fixture.detectChanges(); |
| 50 | +expect(debugElement.nativeElement).toHaveClass('rounded-3'); |
| 51 | +expect(debugElement.nativeElement).toHaveClass('rounded-end'); |
| 52 | +expect(debugElement.nativeElement).toHaveClass('rounded-circle'); |
| 53 | +expect(debugElement.nativeElement).toHaveClass('rounded-pill'); |
| 54 | +expect(debugElement.nativeElement.classList.length).toBe(4); |
| 55 | +fixture.componentRef.setInput('rounded',{}); |
| 56 | +fixture.detectChanges(); |
| 57 | +expect(debugElement.nativeElement.classList.length).toBe(0); |
| 58 | +fixture.componentRef.setInput('rounded',1234n); |
| 59 | +fixture.detectChanges(); |
| 60 | +expect(debugElement.nativeElement.classList.length).toBe(0); |
10 | 61 | }); |
11 | 62 | }); |