无法触发仅在确认使用主体可观察对象的确认服务"yes"时运行的参数函数



所以我目前正在使用Jasmine和Karma为Angular应用程序进行一些单元测试。我遇到了一个单元测试的问题,它必须打开一个模态,并从树节点中删除一个项。

在到达removeItem()函数之前,一切都很好。此删除过程使用confirmationDialogService,如下所示,以便用户可以确认项目的删除:

import { Injectable } from '@angular/core';
import { Subject, Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ConfirmDialogService {
subject = new Subject<any>();
confirmThis(message: string, yesFn: () => void): any {
this.setConfirmation(message, yesFn);
}
setConfirmation(message: string, yesFn: () => void): any {
const that = this;
this.subject.next({
type: 'confirm',
text: message,
yesFn(): any {
that.subject.next(); // This will close the modal
yesFn();
}
});
}
getMessage(): Observable<any> {
return this.subject.asObservable();
}
}

这是我的.ts函数:

openModalInternal(itemRootNode: SiteTreeNode, detectChanges: Function) {
this.title = this.editMode ? 'Edit Item' : 'Add Item';
const ngbModalOptions: NgbModalOptions = {
ariaLabelledBy: this.title,
backdrop: 'static',
scrollable: true,
size: 'xl',
centered: true,
keyboard: false
};
this.modalService.open(this.requestTypeItemTypeModal, ngbModalOptions).result.then(result => {
if (result === 'Save') {
this.saveItem(itemRootNode, detectChanges);
} else if (result === 'Remove') {
this.removeItem(itemRootNode, detectChanges);
} else {
this.clearFields();
}
});
}
removeItem(itemRootNode: SiteTreeNode, detectChanges: Function) {
const removeMessage = this.defaultDropdownVisible
? 'Okay to permanetly delete the item and all its Drop Down Options?'
: 'Okay to permanetly delete the item?';
const requestTypeItemTypeId = this.requestTypeItemId;
this.confirmDialogService.confirmThis(removeMessage, () => {
const index = itemRootNode.childs.findIndex(x => x.id === requestTypeItemTypeId);
this.siteService.deleteRequestTypeItem(requestTypeItemTypeId).subscribe(() => {
// Can't reach the following code
itemRootNode.childs.splice(index, 1);
detectChanges();
this.alertMessageService.setSuccessMessage([SuccessMessage.ItemRemovedMessage]);
});
});
this.clearFields();
}

问题是,当测试达到this.confirmDialogService.confirmThis(...)时,它的第二个参数是一个将在确认为true后执行的函数(用户希望删除项目(。此函数是confirmationDialogService所具有的yesFn()。在这个函数中,还有一些其他函数是我想要监视和期待的(比如deleteRequestTypeItem()(,但我找不到触发这个确认和这个参数函数(yesFn()(的方法。

这是我的.规格测试

it('should remove item from tree node and show success message', fakeAsync(() => {
const mockOpenModalResult = {
result: new Promise((resolve, reject) => resolve('Remove'))
};

spyOn(ngbModal, 'open').and.returnValue(mockOpenModalResult);
spyOn(component, 'removeItem').and.callThrough();
spyOn(confirmDialogService, 'confirmThis').and.callThrough();
spyOn(confirmDialogService, 'setConfirmation').and.callThrough();
spyOn(confirmDialogService.subject, 'next').and.returnValue(confirmDialogService.subject.asObservable());
spyOn(siteService, 'deleteRequestTypeItem');
confirmDialogService.subject.next();
component.openModalInternal(mockItemRootNode, mockDetectChanges);
flush();
expect(component.removeItem).toHaveBeenCalledWith(mockItemRootNode, mockDetectChanges);
expect(confirmDialogService.confirmThis).toHaveBeenCalled();
expect(siteService.deleteRequestTypeItem).toHaveBeenCalled();
}));

注意:注意,这使用了确认服务中可观察到的Subject,所以为了实现我想要的目标,必须有时间来处理它。

我认为您无法到达那里,因为您正在监视deleteRequestTypeItem并且没有返回值。你把它当作未定义。

试试这个:

import { of } from 'rxjs';
....
it('should remove item from tree node and show success message', fakeAsync(() => {
const mockOpenModalResult = {
result: new Promise((resolve, reject) => resolve('Remove'))
};

spyOn(ngbModal, 'open').and.returnValue(mockOpenModalResult);
spyOn(component, 'removeItem').and.callThrough();
spyOn(confirmDialogService, 'confirmThis').and.callThrough();
spyOn(confirmDialogService, 'setConfirmation').and.callThrough();
spyOn(confirmDialogService.subject, 'next').and.returnValue(confirmDialogService.subject.asObservable());
// !! - change this line - !!
spyOn(siteMaintenanceService, 'deleteRequestTypeItem').and.returnValue(of({}));
confirmDialogService.subject.next();
component.openModalInternal(mockItemRootNode, mockDetectChanges);
flush();
expect(component.removeItem).toHaveBeenCalledWith(mockItemRootNode, mockDetectChanges);
expect(confirmDialogService.confirmThis).toHaveBeenCalled();
expect(siteService.deleteRequestTypeItem).toHaveBeenCalled();
}));

相关内容

  • 没有找到相关文章

最新更新