我需要對此進行測驗,但我是新測驗,我不知道,我正在使用角度,我只想測驗關閉功能,也許它是否呈現。
這是html。
<div class="modal active" *ngIf="active" id="modal">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title">{{tittle}}</h1>
<button type="button" class="close" (click)="close()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<ng-content select="[modal-body]"></ng-content>
<div modal-body>
<h5 class="modal-description">{{description}}</h5>
</div>
</div>
<div class="modal-footer">
<ng-content select="[modal-footer]"></ng-content>
<button type="button" class="btn btn-secondary" (click)="close()">Close</button>
</div>
</div>
</div>
<div class="modal-background" (click)="close()"></div>
</div>
這是 modal.component.ts
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.css']
})
export class ModalComponent {
@Input() tittle: string = ''
@Input() description: string = ''
@Input() active: boolean = false;
@Output() activeChange = new EventEmitter<boolean>();
close() {
this.active = false;
this.activeChange.emit(this.active);
}
}
uj5u.com熱心網友回復:
為了測驗EventEmitter單擊 background 時是否發出事件,您應該使用如下方式div撰寫測驗:spyOn
it('should emit event for closing a modal on click event on background div', () => {
spyOn(component.activeChange, 'emit');
component.close();
expect(component.activeChange.emit).toHaveBeenCalled();
});
component通過將這些行放在beforeEach塊中,確保您可以訪問:
beforeEach(() => {
fixture = TestBed.createComponent(ModalComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/416665.html
標籤:
上一篇:使用react和typescript的狀態型別應該是什么?
下一篇:洗掉資料并獲取按鈕ID
