|
| 1 | +import { Component, DebugElement, TemplateRef, ViewChild } from "@angular/core"; |
| 2 | +import { ComponentFixture, TestBed } from "@angular/core/testing"; |
| 3 | +import { By } from "@angular/platform-browser"; |
| 4 | +import { IconGroupComponent } from "./icon-group.component"; |
| 5 | +import { IconComponent } from "./icon.component"; |
| 6 | +import { IconModule } from "./icon.module"; |
| 7 | + |
| 8 | +@Component({ |
| 9 | + template: ` |
| 10 | + <d-icon-group> |
| 11 | + <d-icon |
| 12 | + [icon]="icon" |
| 13 | + [operable]="operable" |
| 14 | + [disabled]="disabled" |
| 15 | + [rotate]="rotate" |
| 16 | + [color]="color" |
| 17 | + ></d-icon> |
| 18 | + </d-icon-group> |
| 19 | + <ng-template #iconTemplate> |
| 20 | + <svg></svg> |
| 21 | + </ng-template>`, |
| 22 | + standalone: true, |
| 23 | + imports: [IconModule] |
| 24 | +}) |
| 25 | +class TestIconComponent { |
| 26 | + icon: string | TemplateRef<any> = ''; |
| 27 | + operable: boolean = false; |
| 28 | + disabled: boolean = false; |
| 29 | + rotate?: number | 'infinite'; |
| 30 | + color?: string; |
| 31 | + @ViewChild('iconTemplate', { static: true }) iconTemplate: TemplateRef<any>; |
| 32 | +} |
| 33 | + |
| 34 | +describe('dIcon', () => { |
| 35 | + let component: TestIconComponent; |
| 36 | + let fixture: ComponentFixture<TestIconComponent>; |
| 37 | + let iconGroupHostComponent: DebugElement; |
| 38 | + let iconHostComponent: DebugElement; |
| 39 | + let iconContainerElement: DebugElement; |
| 40 | + beforeEach(async () => { |
| 41 | + await TestBed.configureTestingModule({ |
| 42 | + imports: [TestIconComponent] |
| 43 | + }) |
| 44 | + .compileComponents(); |
| 45 | + |
| 46 | + fixture = TestBed.createComponent(TestIconComponent); |
| 47 | + component = fixture.componentInstance; |
| 48 | + iconGroupHostComponent = fixture.debugElement.query(By.directive(IconGroupComponent)); |
| 49 | + iconHostComponent = fixture.debugElement.query(By.directive(IconComponent)); |
| 50 | + iconContainerElement = iconHostComponent.children[0]; |
| 51 | + fixture.detectChanges(); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('icon default behaviour', () => { |
| 55 | + it('icon should be created successfully', () => { |
| 56 | + expect(component).toBeTruthy(); |
| 57 | + expect(iconGroupHostComponent).toBeTruthy(); |
| 58 | + expect(iconHostComponent).toBeTruthy(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('icon should have correct default classList', () => { |
| 62 | + expect(iconContainerElement.classes['devui-icon-container']).toBeTrue(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('icon disabled', () => { |
| 67 | + beforeEach(() => { |
| 68 | + component.disabled = true; |
| 69 | + fixture.detectChanges(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('icon should have apply class', () => { |
| 73 | + expect(iconContainerElement.classes['disabled']).toBeTrue(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('icon operable', () => { |
| 78 | + beforeEach(() => { |
| 79 | + component.operable = true; |
| 80 | + fixture.detectChanges(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('icon should have apply class', () => { |
| 84 | + expect(iconContainerElement.classes['devui-icon-can-interactive']).toBeTrue(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('icon input is string', () => { |
| 89 | + let iElement: DebugElement; |
| 90 | + beforeEach(() => { |
| 91 | + component.icon = 'icon-setting'; |
| 92 | + fixture.detectChanges(); |
| 93 | + iElement = iconContainerElement.children[0]; |
| 94 | + }); |
| 95 | + |
| 96 | + it('icon should have correct class', () => { |
| 97 | + expect(iElement.classes['devui-icon']).toBeTrue(); |
| 98 | + expect(iElement.classes['icon-setting']).toBeTrue(); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('icon rotate', () => { |
| 102 | + it('icon should have infinite class when rotate is infinite', () => { |
| 103 | + component.rotate = 'infinite'; |
| 104 | + fixture.detectChanges(); |
| 105 | + expect(iElement.classes['devui-icon-spin']).toBeTrue(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('icon should have correct rotate style when rotate is number', () => { |
| 109 | + component.rotate = 45; |
| 110 | + fixture.detectChanges(); |
| 111 | + expect(iElement.styles.transform).toEqual('rotate(45deg)'); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('icon color', () => { |
| 116 | + beforeEach(() => { |
| 117 | + component.color = 'blue'; |
| 118 | + fixture.detectChanges(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('icon should have correct color style', () => { |
| 122 | + expect(iElement.styles.color).toEqual('blue'); |
| 123 | + }); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('icon customTemplate', () => { |
| 128 | + beforeEach(() => { |
| 129 | + component.icon = component.iconTemplate; |
| 130 | + fixture.detectChanges(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('icon customTemplate should equal to input TemplateRef', () => { |
| 134 | + expect(iconContainerElement.children[0].nativeElement.outerHtml).toEqual(component.iconTemplate.elementRef.nativeElement.outerHtml); |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments