我想為模態彈出組件撰寫單元測驗用例,你能幫我為建構式撰寫測驗用例嗎?
Popup.component.ts
Class PopupComponent{
val;
constructor
(@Inject(MAT_DIALOG_DATA)
public popupValue,
public ref:MatDialogRef)
{
this.val =
this.popupValue;
}
}
引數的輸入值popValue:
data: {
title:'popup',
body:'modalpopup desc'
}
uj5u.com熱心網友回復:
popup.component.spec.ts
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { PopupComponent } from './popup.component';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
describe('src/popup/popup.component.ts', () => {
let popupComponent: PopupComponent;
let componentFixture: ComponentFixture<PopupComponent>;
beforeEach(waitForAsync(() => {
const popupBody = 'modalpopup desc';
const popupTitle = 'popup';
TestBed.configureTestingModule({
declarations: [PopupComponent],
providers: [
{ provide: MatDialogRef, useValue: {} },
{
provide: MAT_DIALOG_DATA, useValue: {
data: {
title: popupTitle,
body: popupBody
}
}
}
]
}).compileComponents();
}));
beforeEach(() => {
componentFixture = TestBed.createComponent(PopupComponent);
popupComponent = componentFixture.componentInstance;
});
it('popup component created', () => {
expect(popupComponent.val).toBeTruthy();
});
it('popup component val data test', () => {
const expectedTitle = 'popup';
const expectedBody = 'modalpopup desc';
expect(popupComponent.val.data.title).toEqual(expectedTitle);
expect(popupComponent.val.data.body).toEqual(expectedBody);
});
})
popup.component.ts
import { Inject, Component } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material/dialog";
export interface IPopupData {
title: string;
body: string;
}
export interface IPopupInput {
data: IPopupData
}
@Component({})
export class PopupComponent {
val: IPopupInput;
constructor(
@Inject(MAT_DIALOG_DATA)
public popupValue: IPopupInput,
public ref: MatDialogRef<PopupComponent>) {
this.val = this.popupValue;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/416713.html
標籤:
