自己动手开发SAP Spartacus focus Directive的单元测试
生活随笔
收集整理的這篇文章主要介紹了
自己动手开发SAP Spartacus focus Directive的单元测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在已有的單元測試代碼,加了這個div標簽之后就報錯了:
報錯:this.service.findFirstFocusable is not a function:
at FocusDirective.get (http://localhost:9876/karma_webpack/src/layout/a11y/keyboard-focus/autofocus/auto-focus.directive.ts:91:25)
從調用棧能看出,FocusDirective實例調用get時,會將控制轉角給this.service的findFirstFocusable方法:
解決方法:
添加dummy方法:
通過下列代碼拿到Angular Test Bed,ATB注入好的Service:
待測試的Component源代碼:
@Component({selector: 'cx-host',template: ` <div id="a" cxFocus></div><div id="b" [cxFocus]="{autofocus: ':host'}"></div> `, }) class MockComponent {}單元測試代碼:
it('should focus itself', () => {let service: KeyboardFocusService;service = TestBed.inject(KeyboardFocusService);const host = fixture.debugElement.query(By.css('#b'));const el = host.nativeElement;spyOn(service, 'findFirstFocusable').and.returnValue(el);spyOn(el, 'focus').and.callThrough();fixture.detectChanges();const event = {preventDefault: () => {},stopPropagation: () => {},};host.triggerEventHandler('focus', event);expect(el.focus).toHaveBeenCalled();expect(service.findFirstFocusable).toHaveBeenCalled();});測試結果:
更多Jerry的原創文章,盡在:“汪子熙”:
總結
以上是生活随笔為你收集整理的自己动手开发SAP Spartacus focus Directive的单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2023 款极氪 001 车型金融贴息活
- 下一篇: 运算器的功能是什么(core是什么区别)