[英]How to mock the removeChild or appendChild function call in angular directives testing?
I am facing issue in angular directive testing with mocking the dom manipulation api's removeChild or appendChild functions. 我在角度指令测试中遇到问题,模仿dom操作api的removeChild或appendChild函数。 directive is created to add/remove the items to dropdown menu.
创建指令以添加/删除项目到下拉菜单。
I have tried mocking the DOM manipulation API's removeChild
or appendChild
functions with jasmine.createSpyObj
, but is not mocking the call and the call is going to actual function call and throwing error. 我已经尝试使用
jasmine.createSpyObj
DOM操作API的removeChild
或appendChild
函数,但是没有jasmine.createSpyObj
调用,调用将转到实际的函数调用和抛出错误。
it('TestCase: appMenuDropDown Directive',() => {
var rendererMock;
const debugEl: HTMLElement = fixture.debugElement.nativeElement;
rendererMock = jasmine.createSpyObj('rendererMock',['removeChild']);
rendererMock.removeChild(); /*mocking removeChild call*/
const inputEl: HTMLElement = debugEl.querySelector('.list-item');
inputEl.click();
fixture.detectChanges();
expect(rendererMock.removeChild).toHaveBeenCalled();
});
below is the console error. 以下是控制台错误。
context.js:248 ERROR TypeError: Cannot read property 'parentNode' of undefined at MenuDropDownDirective../src/app/directives/menu-drop-down.directive.ts.MenuDropDownDirective.clickListener
context.js:248 ERROR TypeError:无法在MenuDropDownDirective中读取未定义的属性'parentNode'../ src / app / directives / menu-drop-down.directive.ts.MenuDropDownDirective.clickListener
@Directive({ selector: '[appMenuDropDown]' })
export class MenuDropDownDirective {
@Input() subMenuContainer: ElementRef;
constructor(private el: ElementRef, private renderer: Renderer2) {}
@HostListener('click') clickListener() {
const sourceElement: any = this.el.nativeElement;
const targetElement: any = this.subMenuContainer;
if (sourceElement.children.length > 1) {
this.renderer.removeChild(targetElement.parentNode, targetElement);
} else {
this.renderer.appendChild(sourceElement, targetElement);
}
}
}
@Component({ template: <mat-nav-list> <mat-list-item> <div class='list-item' appMenuDropDown [subMenuContainer]="subMenuList" mat-list-item>test1 <span>test2 </span> </div> </mat-list-item> </mat-nav-list> <mat-nav-list> <ul #subMenuList> <li>child1</li> <li>child2</li> </ul> </mat-nav-list>
, styles: ['mat-list-item { display: block; }'] @Component({template:
<mat-nav-list> <mat-list-item> <div class='list-item' appMenuDropDown [subMenuContainer]="subMenuList" mat-list-item>test1 <span>test2 </span> </div> </mat-list-item> </mat-nav-list> <mat-nav-list> <ul #subMenuList> <li>child1</li> <li>child2</li> </ul> </mat-nav-list>
,styles:['mat-list-item {display:block;}']
}) })
class TestMenuDropDownComponent { class TestMenuDropDownComponent {
} }
it('TestCase: appMenuDropDown Directive appendChild',fakeAsync(() => {
const debugEl: HTMLElement = fixture.debugElement.nativeElement;
const inputEl: HTMLElement = debugEl.querySelector('.list-item');
inputEl.click();
console.log(inputEl.children.length);
expect(inputEl.children.length).toBeGreaterThan(1)
tick();
inputEl.click();
expect(inputEl.children.length).toEqual(1);
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.