-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module:check-list): add Unit Testing
- Loading branch information
OriginRing
committed
Jan 20, 2025
1 parent
7c8889f
commit 64e0fdc
Showing
8 changed files
with
338 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
import { OverlayContainer } from '@angular/cdk/overlay'; | ||
import { provideHttpClientTesting } from '@angular/common/http/testing'; | ||
import { Component, DebugElement, TemplateRef } from '@angular/core'; | ||
import { ComponentFixture, fakeAsync, inject, TestBed, tick } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
import { provideNoopAnimations } from '@angular/platform-browser/animations'; | ||
|
||
import { dispatchMouseEvent } from 'ng-zorro-antd/core/testing'; | ||
|
||
import { NzCheckListComponent } from './check-list.component'; | ||
import { NzCheckListModule } from './check-list.module'; | ||
import { NzItemProps } from './typings'; | ||
|
||
describe('check-list', () => { | ||
let fixture: ComponentFixture<NzTestCheckListBasicComponent>; | ||
let testComponent: NzTestCheckListBasicComponent; | ||
let resultEl: DebugElement; | ||
let overlayContainer: OverlayContainer; | ||
let overlayContainerElement: HTMLElement; | ||
|
||
function waitingForTooltipToggling(): void { | ||
fixture.detectChanges(); | ||
tick(500); | ||
fixture.detectChanges(); | ||
} | ||
|
||
beforeEach(fakeAsync(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [provideNoopAnimations(), provideHttpClientTesting()] | ||
}); | ||
fixture = TestBed.createComponent(NzTestCheckListBasicComponent); | ||
fixture.detectChanges(); | ||
testComponent = fixture.debugElement.componentInstance; | ||
resultEl = fixture.debugElement.query(By.directive(NzCheckListComponent)); | ||
})); | ||
|
||
beforeEach(inject([OverlayContainer], (oc: OverlayContainer) => { | ||
overlayContainer = oc; | ||
overlayContainerElement = oc.getContainerElement(); | ||
})); | ||
|
||
afterEach(() => { | ||
overlayContainer.ngOnDestroy(); | ||
}); | ||
|
||
it('basic', () => { | ||
expect(resultEl.nativeElement.classList).toContain('ant-checklist'); | ||
expect(!!resultEl.nativeElement.querySelector('.ant-checklist-button .ant-checklist-icon')).toBeTrue(); | ||
expect(!!resultEl.nativeElement.querySelector('.ant-checklist-button .ant-checklist-description')).toBeTrue(); | ||
}); | ||
|
||
it('nzShow', () => { | ||
testComponent.show = false; | ||
fixture.detectChanges(); | ||
expect(resultEl.nativeElement.classList).toContain('ant-checklist-hide'); | ||
}); | ||
|
||
it('nzVisible', fakeAsync(() => { | ||
testComponent.visible = true; | ||
fixture.detectChanges(); | ||
waitingForTooltipToggling(); | ||
expect(!!overlayContainerElement.querySelector('.ant-popover-inner-content')).toBeTrue(); | ||
expect(!!overlayContainerElement.querySelector('.ant-popover-inner-content .ant-checklist-header')).toBeTrue(); | ||
expect( | ||
!!overlayContainerElement.querySelector( | ||
'.ant-popover-inner-content .ant-checklist-header .ant-checklist-header-title' | ||
) | ||
).toBeTrue(); | ||
})); | ||
|
||
it('nzItems', fakeAsync(() => { | ||
testComponent.visible = true; | ||
testComponent.items = [ | ||
{ | ||
description: 'Step 1', | ||
onClick: () => {} | ||
} | ||
]; | ||
fixture.detectChanges(); | ||
waitingForTooltipToggling(); | ||
expect(!!overlayContainerElement.querySelector('.ant-checklist-steps')).toBeTrue(); | ||
})); | ||
|
||
it('nzProgress', fakeAsync(() => { | ||
testComponent.visible = true; | ||
fixture.detectChanges(); | ||
waitingForTooltipToggling(); | ||
expect(!!overlayContainerElement.querySelector('.ant-checklist-progressBar')).toBeTrue(); | ||
testComponent.progress = false; | ||
fixture.detectChanges(); | ||
expect(!overlayContainerElement.querySelector('.ant-checklist-progressBar')).toBeTrue(); | ||
})); | ||
|
||
it('nzIndex', fakeAsync(() => { | ||
testComponent.visible = true; | ||
testComponent.items = [ | ||
{ | ||
description: 'Step 1', | ||
onClick: () => {} | ||
}, | ||
{ | ||
description: 'Step 2', | ||
onClick: () => {} | ||
} | ||
]; | ||
fixture.detectChanges(); | ||
waitingForTooltipToggling(); | ||
expect(overlayContainerElement.querySelectorAll('.ant-checklist-steps').length).toBe(2); | ||
expect( | ||
( | ||
overlayContainerElement.querySelector( | ||
'.ant-checklist-highlight .ant-checklist-steps-item-description' | ||
) as HTMLElement | ||
).innerText | ||
).toBe('Step 1'); | ||
expect((overlayContainerElement.querySelector('.ant-progress-text') as HTMLElement).innerText).toBe('0%'); | ||
expect(overlayContainerElement.querySelectorAll('.ant-checklist-steps-item-arrows').length).toBe(1); | ||
|
||
testComponent.index = 2; | ||
fixture.detectChanges(); | ||
expect( | ||
( | ||
overlayContainerElement.querySelector( | ||
'.ant-checklist-checked .ant-checklist-steps-item-description' | ||
) as HTMLElement | ||
).innerText | ||
).toBe('Step 1'); | ||
expect( | ||
( | ||
overlayContainerElement.querySelector( | ||
'.ant-checklist-highlight .ant-checklist-steps-item-description' | ||
) as HTMLElement | ||
).innerText | ||
).toBe('Step 2'); | ||
expect((overlayContainerElement.querySelector('.ant-progress-text') as HTMLElement).innerText).toBe('50%'); | ||
|
||
testComponent.index = 3; | ||
fixture.detectChanges(); | ||
expect(!overlayContainerElement.querySelector('.ant-checklist-progressBar')).toBeTrue(); | ||
expect(!!overlayContainerElement.querySelector('.ant-checklist-header-finish')).toBeTrue(); | ||
})); | ||
|
||
it('lose the list when you are finished', fakeAsync(() => { | ||
testComponent.visible = true; | ||
testComponent.items = [ | ||
{ | ||
description: 'Step 1', | ||
onClick: () => {} | ||
} | ||
]; | ||
testComponent.index = 2; | ||
fixture.detectChanges(); | ||
waitingForTooltipToggling(); | ||
const dom = overlayContainerElement.querySelector('.ant-checklist-header-finish .ant-btn'); | ||
if (!!dom) { | ||
dispatchMouseEvent(dom, 'click'); | ||
waitingForTooltipToggling(); | ||
expect(!overlayContainerElement.querySelector('.ant-popover-inner-content')).toBeTrue(); | ||
} | ||
})); | ||
|
||
it('icon close List', fakeAsync(() => { | ||
testComponent.visible = true; | ||
fixture.detectChanges(); | ||
waitingForTooltipToggling(); | ||
const dom = overlayContainerElement.querySelector('.ant-checklist-header-extra .anticon'); | ||
if (!!dom) { | ||
dispatchMouseEvent(dom, 'click'); | ||
waitingForTooltipToggling(); | ||
expect(!overlayContainerElement.querySelector('.ant-popover-inner-content')).toBeTrue(); | ||
} | ||
})); | ||
|
||
it('actively close the list', fakeAsync(() => { | ||
testComponent.visible = true; | ||
fixture.detectChanges(); | ||
waitingForTooltipToggling(); | ||
const dom = overlayContainerElement.querySelector('.ant-checklist-footer'); | ||
if (!!dom) { | ||
dispatchMouseEvent(dom, 'click'); | ||
waitingForTooltipToggling(); | ||
expect(!!overlayContainerElement.querySelector('.ant-checklist-close-check')).toBeTrue(); | ||
const btnDom = overlayContainerElement.querySelector('.ant-checklist-close-check-action .ant-btn'); | ||
if (btnDom) { | ||
dispatchMouseEvent(btnDom, 'click'); | ||
waitingForTooltipToggling(); | ||
expect(!overlayContainerElement.querySelector('.ant-popover-inner-content')).toBeTrue(); | ||
} | ||
} | ||
})); | ||
|
||
it('actively close hidden lists', fakeAsync(() => { | ||
testComponent.visible = true; | ||
fixture.detectChanges(); | ||
waitingForTooltipToggling(); | ||
const dom = overlayContainerElement.querySelector('.ant-checklist-footer'); | ||
if (!!dom) { | ||
dispatchMouseEvent(dom, 'click'); | ||
waitingForTooltipToggling(); | ||
expect(!!overlayContainerElement.querySelector('.ant-checklist-close-check')).toBeTrue(); | ||
expect( | ||
!!overlayContainerElement.querySelector('.ant-checklist-close-check .ant-checklist-close-check-title') | ||
).toBeTrue(); | ||
expect( | ||
!!overlayContainerElement.querySelector('.ant-checklist-close-check .ant-checklist-close-check-action') | ||
).toBeTrue(); | ||
expect( | ||
!!overlayContainerElement.querySelector('.ant-checklist-close-check .ant-checklist-close-check-other') | ||
).toBeTrue(); | ||
const btnDom = overlayContainerElement.querySelector('.ant-checklist-close-check-action .ant-btn'); | ||
const labelDom = overlayContainerElement.querySelector('.ant-checklist-close-check-other .ant-checkbox-wrapper'); | ||
if (btnDom && labelDom) { | ||
dispatchMouseEvent(labelDom, 'click'); | ||
waitingForTooltipToggling(); | ||
dispatchMouseEvent(btnDom, 'click'); | ||
waitingForTooltipToggling(); | ||
expect(!overlayContainerElement.querySelector('.ant-popover-inner-content')).toBeTrue(); | ||
expect(!overlayContainerElement.querySelector('.ant-checklist')).toBeTrue(); | ||
} | ||
} | ||
})); | ||
|
||
it('nzTriggerRender ', () => { | ||
testComponent.show = true; | ||
testComponent.triggerRender = 'Check List'; | ||
fixture.detectChanges(); | ||
expect((resultEl.nativeElement.querySelector('.ant-checklist-button') as HTMLElement).innerText).toBe('Check List'); | ||
}); | ||
|
||
it('nzTitle', fakeAsync(() => { | ||
testComponent.title = 'Check List'; | ||
testComponent.visible = true; | ||
fixture.detectChanges(); | ||
waitingForTooltipToggling(); | ||
expect((overlayContainerElement.querySelector('.ant-checklist-header-title') as HTMLElement).innerText).toBe( | ||
'Check List' | ||
); | ||
})); | ||
|
||
it('nzFooter', fakeAsync(() => { | ||
testComponent.footer = 'Check List'; | ||
testComponent.visible = true; | ||
fixture.detectChanges(); | ||
waitingForTooltipToggling(); | ||
expect((overlayContainerElement.querySelector('.ant-checklist-footer') as HTMLElement).innerText).toBe( | ||
'Check List' | ||
); | ||
})); | ||
}); | ||
|
||
@Component({ | ||
selector: 'nz-test-check-list-basic', | ||
imports: [NzCheckListModule], | ||
template: ` | ||
<nz-check-list | ||
[nzShow]="show" | ||
[nzVisible]="visible" | ||
[nzItems]="items" | ||
[nzIndex]="index" | ||
[nzProgress]="progress" | ||
[nzTriggerRender]="triggerRender" | ||
[nzTitle]="title" | ||
[nzFooter]="footer" | ||
> | ||
</nz-check-list> | ||
` | ||
}) | ||
export class NzTestCheckListBasicComponent { | ||
show: boolean = true; | ||
visible: boolean = false; | ||
items: NzItemProps[] = []; | ||
index: number = 1; | ||
progress: boolean = true; | ||
triggerRender: TemplateRef<void> | string | null = null; | ||
title: TemplateRef<void> | string | null = null; | ||
footer: TemplateRef<void> | string | null = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.