自己动手开发SAP Spartacus focus Directive的单元测试
生活随笔
收集整理的這篇文章主要介紹了
自己动手开发SAP Spartacus focus Directive的单元测试
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在已有的單元測(cè)試代碼,加了這個(gè)div標(biāo)簽之后就報(bào)錯(cuò)了:
報(bào)錯(cuò):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)
從調(diào)用棧能看出,FocusDirective實(shí)例調(diào)用get時(shí),會(huì)將控制轉(zhuǎn)角給this.service的findFirstFocusable方法:
解決方法:
添加dummy方法:
通過(guò)下列代碼拿到Angular Test Bed,ATB注入好的Service:
待測(cè)試的Component源代碼:
@Component({selector: 'cx-host',template: ` <div id="a" cxFocus></div><div id="b" [cxFocus]="{autofocus: ':host'}"></div> `, }) class MockComponent {}單元測(cè)試代碼:
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();});測(cè)試結(jié)果:
更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:
總結(jié)
以上是生活随笔為你收集整理的自己动手开发SAP Spartacus focus Directive的单元测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2023 款极氪 001 车型金融贴息活
- 下一篇: Angular Jasmine单元测试用