|
| 1 | +import { OverlayContainer } from '@angular/cdk/overlay'; |
| 2 | +import { provideHttpClientTesting } from '@angular/common/http/testing'; |
| 3 | +import { Component, DebugElement, TemplateRef } from '@angular/core'; |
| 4 | +import { ComponentFixture, fakeAsync, inject, TestBed, tick } from '@angular/core/testing'; |
| 5 | +import { By } from '@angular/platform-browser'; |
| 6 | +import { provideNoopAnimations } from '@angular/platform-browser/animations'; |
| 7 | + |
| 8 | +import { NzItemProps } from 'ng-zorro-antd/check-list/typings'; |
| 9 | +import { dispatchMouseEvent } from 'ng-zorro-antd/core/testing'; |
| 10 | + |
| 11 | +import { NzCheckListComponent } from './check-list.component'; |
| 12 | +import { NzCheckListModule } from './check-list.module'; |
| 13 | + |
| 14 | +describe('check-list', () => { |
| 15 | + let fixture: ComponentFixture<NzTestCheckListBasicComponent>; |
| 16 | + let testComponent: NzTestCheckListBasicComponent; |
| 17 | + let resultEl: DebugElement; |
| 18 | + let overlayContainer: OverlayContainer; |
| 19 | + let overlayContainerElement: HTMLElement; |
| 20 | + |
| 21 | + function waitingForTooltipToggling(): void { |
| 22 | + fixture.detectChanges(); |
| 23 | + tick(500); |
| 24 | + fixture.detectChanges(); |
| 25 | + } |
| 26 | + |
| 27 | + beforeEach(fakeAsync(() => { |
| 28 | + TestBed.configureTestingModule({ |
| 29 | + providers: [provideNoopAnimations(), provideHttpClientTesting()] |
| 30 | + }); |
| 31 | + fixture = TestBed.createComponent(NzTestCheckListBasicComponent); |
| 32 | + fixture.detectChanges(); |
| 33 | + testComponent = fixture.debugElement.componentInstance; |
| 34 | + resultEl = fixture.debugElement.query(By.directive(NzCheckListComponent)); |
| 35 | + })); |
| 36 | + |
| 37 | + beforeEach(inject([OverlayContainer], (oc: OverlayContainer) => { |
| 38 | + overlayContainer = oc; |
| 39 | + overlayContainerElement = oc.getContainerElement(); |
| 40 | + })); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + overlayContainer.ngOnDestroy(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('basic', () => { |
| 47 | + expect(resultEl.nativeElement.classList).toContain('ant-checklist'); |
| 48 | + expect(!!resultEl.nativeElement.querySelector('.ant-checklist-button .ant-checklist-icon')).toBeTrue(); |
| 49 | + expect(!!resultEl.nativeElement.querySelector('.ant-checklist-button .ant-checklist-description')).toBeTrue(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('nzShow', () => { |
| 53 | + testComponent.show = false; |
| 54 | + fixture.detectChanges(); |
| 55 | + expect(resultEl.nativeElement.classList).toContain('ant-checklist-hide'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('nzVisible', fakeAsync(() => { |
| 59 | + testComponent.visible = true; |
| 60 | + fixture.detectChanges(); |
| 61 | + waitingForTooltipToggling(); |
| 62 | + expect(!!overlayContainerElement.querySelector('.ant-popover-inner-content')).toBeTrue(); |
| 63 | + expect(!!overlayContainerElement.querySelector('.ant-popover-inner-content .ant-checklist-header')).toBeTrue(); |
| 64 | + expect( |
| 65 | + !!overlayContainerElement.querySelector( |
| 66 | + '.ant-popover-inner-content .ant-checklist-header .ant-checklist-header-title' |
| 67 | + ) |
| 68 | + ).toBeTrue(); |
| 69 | + })); |
| 70 | + |
| 71 | + it('nzItems', fakeAsync(() => { |
| 72 | + testComponent.visible = true; |
| 73 | + testComponent.items = [ |
| 74 | + { |
| 75 | + description: 'Step 1', |
| 76 | + onClick: () => {} |
| 77 | + } |
| 78 | + ]; |
| 79 | + fixture.detectChanges(); |
| 80 | + waitingForTooltipToggling(); |
| 81 | + expect(!!overlayContainerElement.querySelector('.ant-checklist-steps')).toBeTrue(); |
| 82 | + })); |
| 83 | + |
| 84 | + it('nzProgress', fakeAsync(() => { |
| 85 | + testComponent.visible = true; |
| 86 | + fixture.detectChanges(); |
| 87 | + waitingForTooltipToggling(); |
| 88 | + expect(!!overlayContainerElement.querySelector('.ant-checklist-progressBar')).toBeTrue(); |
| 89 | + testComponent.progress = false; |
| 90 | + fixture.detectChanges(); |
| 91 | + expect(!overlayContainerElement.querySelector('.ant-checklist-progressBar')).toBeTrue(); |
| 92 | + })); |
| 93 | + |
| 94 | + it('nzIndex', fakeAsync(() => { |
| 95 | + testComponent.visible = true; |
| 96 | + testComponent.items = [ |
| 97 | + { |
| 98 | + description: 'Step 1', |
| 99 | + onClick: () => {} |
| 100 | + }, |
| 101 | + { |
| 102 | + description: 'Step 2', |
| 103 | + onClick: () => {} |
| 104 | + } |
| 105 | + ]; |
| 106 | + fixture.detectChanges(); |
| 107 | + waitingForTooltipToggling(); |
| 108 | + expect(overlayContainerElement.querySelectorAll('.ant-checklist-steps').length).toBe(2); |
| 109 | + expect( |
| 110 | + ( |
| 111 | + overlayContainerElement.querySelector( |
| 112 | + '.ant-checklist-highlight .ant-checklist-steps-item-description' |
| 113 | + ) as HTMLElement |
| 114 | + ).innerText |
| 115 | + ).toBe('Step 1'); |
| 116 | + expect((overlayContainerElement.querySelector('.ant-progress-text') as HTMLElement).innerText).toBe('0%'); |
| 117 | + expect(overlayContainerElement.querySelectorAll('.ant-checklist-steps-item-arrows').length).toBe(1); |
| 118 | + |
| 119 | + testComponent.index = 2; |
| 120 | + fixture.detectChanges(); |
| 121 | + expect( |
| 122 | + ( |
| 123 | + overlayContainerElement.querySelector( |
| 124 | + '.ant-checklist-checked .ant-checklist-steps-item-description' |
| 125 | + ) as HTMLElement |
| 126 | + ).innerText |
| 127 | + ).toBe('Step 1'); |
| 128 | + expect( |
| 129 | + ( |
| 130 | + overlayContainerElement.querySelector( |
| 131 | + '.ant-checklist-highlight .ant-checklist-steps-item-description' |
| 132 | + ) as HTMLElement |
| 133 | + ).innerText |
| 134 | + ).toBe('Step 2'); |
| 135 | + expect((overlayContainerElement.querySelector('.ant-progress-text') as HTMLElement).innerText).toBe('50%'); |
| 136 | + |
| 137 | + testComponent.index = 3; |
| 138 | + fixture.detectChanges(); |
| 139 | + expect(!overlayContainerElement.querySelector('.ant-checklist-progressBar')).toBeTrue(); |
| 140 | + expect(!!overlayContainerElement.querySelector('.ant-checklist-header-finish')).toBeTrue(); |
| 141 | + })); |
| 142 | + |
| 143 | + it('lose the list when you are finished', fakeAsync(() => { |
| 144 | + testComponent.visible = true; |
| 145 | + testComponent.items = [ |
| 146 | + { |
| 147 | + description: 'Step 1', |
| 148 | + onClick: () => {} |
| 149 | + } |
| 150 | + ]; |
| 151 | + testComponent.index = 2; |
| 152 | + fixture.detectChanges(); |
| 153 | + waitingForTooltipToggling(); |
| 154 | + const dom = overlayContainerElement.querySelector('.ant-checklist-header-finish .ant-btn'); |
| 155 | + if (!!dom) { |
| 156 | + dispatchMouseEvent(dom, 'click'); |
| 157 | + waitingForTooltipToggling(); |
| 158 | + expect(!overlayContainerElement.querySelector('.ant-popover-inner-content')).toBeTrue(); |
| 159 | + } |
| 160 | + })); |
| 161 | + |
| 162 | + it('icon close List', fakeAsync(() => { |
| 163 | + testComponent.visible = true; |
| 164 | + fixture.detectChanges(); |
| 165 | + waitingForTooltipToggling(); |
| 166 | + const dom = overlayContainerElement.querySelector('.ant-checklist-header-extra .anticon'); |
| 167 | + if (!!dom) { |
| 168 | + dispatchMouseEvent(dom, 'click'); |
| 169 | + waitingForTooltipToggling(); |
| 170 | + expect(!overlayContainerElement.querySelector('.ant-popover-inner-content')).toBeTrue(); |
| 171 | + } |
| 172 | + })); |
| 173 | + |
| 174 | + it('actively close the list', fakeAsync(() => { |
| 175 | + testComponent.visible = true; |
| 176 | + fixture.detectChanges(); |
| 177 | + waitingForTooltipToggling(); |
| 178 | + const dom = overlayContainerElement.querySelector('.ant-checklist-footer'); |
| 179 | + if (!!dom) { |
| 180 | + dispatchMouseEvent(dom, 'click'); |
| 181 | + waitingForTooltipToggling(); |
| 182 | + expect(!!overlayContainerElement.querySelector('.ant-checklist-close-check')).toBeTrue(); |
| 183 | + const btnDom = overlayContainerElement.querySelector('.ant-checklist-close-check-action .ant-btn'); |
| 184 | + if (btnDom) { |
| 185 | + dispatchMouseEvent(btnDom, 'click'); |
| 186 | + waitingForTooltipToggling(); |
| 187 | + expect(!overlayContainerElement.querySelector('.ant-popover-inner-content')).toBeTrue(); |
| 188 | + } |
| 189 | + } |
| 190 | + })); |
| 191 | + |
| 192 | + it('actively close hidden lists', fakeAsync(() => { |
| 193 | + testComponent.visible = true; |
| 194 | + fixture.detectChanges(); |
| 195 | + waitingForTooltipToggling(); |
| 196 | + const dom = overlayContainerElement.querySelector('.ant-checklist-footer'); |
| 197 | + if (!!dom) { |
| 198 | + dispatchMouseEvent(dom, 'click'); |
| 199 | + waitingForTooltipToggling(); |
| 200 | + expect(!!overlayContainerElement.querySelector('.ant-checklist-close-check')).toBeTrue(); |
| 201 | + expect( |
| 202 | + !!overlayContainerElement.querySelector('.ant-checklist-close-check .ant-checklist-close-check-title') |
| 203 | + ).toBeTrue(); |
| 204 | + expect( |
| 205 | + !!overlayContainerElement.querySelector('.ant-checklist-close-check .ant-checklist-close-check-action') |
| 206 | + ).toBeTrue(); |
| 207 | + expect( |
| 208 | + !!overlayContainerElement.querySelector('.ant-checklist-close-check .ant-checklist-close-check-other') |
| 209 | + ).toBeTrue(); |
| 210 | + const btnDom = overlayContainerElement.querySelector('.ant-checklist-close-check-action .ant-btn'); |
| 211 | + const labelDom = overlayContainerElement.querySelector('.ant-checklist-close-check-other .ant-checkbox-wrapper'); |
| 212 | + if (btnDom && labelDom) { |
| 213 | + dispatchMouseEvent(labelDom, 'click'); |
| 214 | + waitingForTooltipToggling(); |
| 215 | + dispatchMouseEvent(btnDom, 'click'); |
| 216 | + waitingForTooltipToggling(); |
| 217 | + expect(!overlayContainerElement.querySelector('.ant-popover-inner-content')).toBeTrue(); |
| 218 | + expect(!overlayContainerElement.querySelector('.ant-checklist')).toBeTrue(); |
| 219 | + } |
| 220 | + } |
| 221 | + })); |
| 222 | + |
| 223 | + it('nzTriggerRender ', () => { |
| 224 | + testComponent.show = true; |
| 225 | + testComponent.triggerRender = 'Check List'; |
| 226 | + fixture.detectChanges(); |
| 227 | + expect((resultEl.nativeElement.querySelector('.ant-checklist-button') as HTMLElement).innerText).toBe('Check List'); |
| 228 | + }); |
| 229 | + |
| 230 | + it('nzTitle', fakeAsync(() => { |
| 231 | + testComponent.title = 'Check List'; |
| 232 | + testComponent.visible = true; |
| 233 | + fixture.detectChanges(); |
| 234 | + waitingForTooltipToggling(); |
| 235 | + expect((overlayContainerElement.querySelector('.ant-checklist-header-title') as HTMLElement).innerText).toBe( |
| 236 | + 'Check List' |
| 237 | + ); |
| 238 | + })); |
| 239 | + |
| 240 | + it('nzFooter', fakeAsync(() => { |
| 241 | + testComponent.footer = 'Check List'; |
| 242 | + testComponent.visible = true; |
| 243 | + fixture.detectChanges(); |
| 244 | + waitingForTooltipToggling(); |
| 245 | + expect((overlayContainerElement.querySelector('.ant-checklist-footer') as HTMLElement).innerText).toBe( |
| 246 | + 'Check List' |
| 247 | + ); |
| 248 | + })); |
| 249 | +}); |
| 250 | + |
| 251 | +@Component({ |
| 252 | + selector: 'nz-test-check-list-basic', |
| 253 | + imports: [NzCheckListModule], |
| 254 | + template: ` |
| 255 | + <nz-check-list |
| 256 | + [nzShow]="show" |
| 257 | + [nzVisible]="visible" |
| 258 | + [nzItems]="items" |
| 259 | + [nzIndex]="index" |
| 260 | + [nzProgress]="progress" |
| 261 | + [nzTriggerRender]="triggerRender" |
| 262 | + [nzTitle]="title" |
| 263 | + [nzFooter]="footer" |
| 264 | + > |
| 265 | + </nz-check-list> |
| 266 | + ` |
| 267 | +}) |
| 268 | +export class NzTestCheckListBasicComponent { |
| 269 | + show: boolean = true; |
| 270 | + visible: boolean = false; |
| 271 | + items: NzItemProps[] = []; |
| 272 | + index: number = 1; |
| 273 | + progress: boolean = true; |
| 274 | + triggerRender: TemplateRef<void> | string | null = null; |
| 275 | + title: TemplateRef<void> | string | null = null; |
| 276 | + footer: TemplateRef<void> | string | null = null; |
| 277 | +} |
0 commit comments