|
1 |
| -import { TestBed } from '@angular/core/testing'; |
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
2 | 2 | import { BorderDirective } from './border.directive';
|
| 3 | +import { Component, DebugElement, input } from '@angular/core'; |
| 4 | +import { By } from '@angular/platform-browser'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + imports: [BorderDirective], |
| 8 | + template: '<div [cBorder]="border()"></div>' |
| 9 | +}) |
| 10 | +class TestComponent { |
| 11 | + readonly border = input(1); |
| 12 | +} |
3 | 13 |
|
4 | 14 | describe('BorderDirective', () => {
|
| 15 | + let component: TestComponent; |
| 16 | + let fixture: ComponentFixture<TestComponent>; |
| 17 | + let debugElement: 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(BorderDirective)); |
| 27 | + fixture.detectChanges(); |
| 28 | + }); |
| 29 | + |
5 | 30 | it('should create an instance', () => {
|
6 | 31 | TestBed.runInInjectionContext(() => {
|
7 |
| - const directive = new BorderDirective(); |
8 |
| - expect(directive).toBeTruthy(); |
| 32 | + const directive = new BorderDirective(); |
| 33 | + expect(directive).toBeTruthy(); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should have css classes', () => { |
| 38 | + expect(debugElement.nativeElement).toHaveClass('border-1'); |
| 39 | + fixture.componentRef.setInput('border', true); |
| 40 | + fixture.detectChanges(); |
| 41 | + expect(debugElement.nativeElement).toHaveClass('border'); |
| 42 | + fixture.componentRef.setInput('border', { |
| 43 | + top: 1, |
| 44 | + end: true, |
| 45 | + color: 'primary', |
| 46 | + start: { color: 'success', width: 2 } |
9 | 47 | });
|
| 48 | + fixture.detectChanges(); |
| 49 | + expect(debugElement.nativeElement).toHaveClass('border-top-1'); |
| 50 | + expect(debugElement.nativeElement).toHaveClass('border-end'); |
| 51 | + expect(debugElement.nativeElement).toHaveClass('border-color-primary'); |
| 52 | + expect(debugElement.nativeElement).toHaveClass('border-start-2'); |
| 53 | + expect(debugElement.nativeElement).toHaveClass('border-start-success'); |
| 54 | + expect(debugElement.nativeElement.classList.length).toBe(5); |
| 55 | + fixture.componentRef.setInput('border', {}); |
| 56 | + fixture.detectChanges(); |
| 57 | + expect(debugElement.nativeElement.classList.length).toBe(0); |
| 58 | + fixture.componentRef.setInput('border', 1234n); |
| 59 | + fixture.detectChanges(); |
| 60 | + expect(debugElement.nativeElement.classList.length).toBe(0); |
10 | 61 | });
|
11 | 62 | });
|
0 commit comments