我在这个周六早上好好的时候坐下来,目的是将我的9号角项目推开玩笑。到目前为止失败。除了不支持DragDropEvent的ClipboardEvent的JSDOM(我有解决方法)之外,在Jasmine中通过的测试在Jest中失败。
这是我正在测试的内容:
@Directive({
selector: '[evAutoTab]'
})
export class EvAutoTabDirective {
@Input('evAutoTab') target: string;
@HostListener('keyup') onKeyup() {
this.moveFocus();
}
constructor(private el: ElementRef) {
}
private moveFocus() {
const maxLen = this.el.nativeElement.getAttribute('maxlength');
const len = this.el.nativeElement.value.length;
// console.log(`len ${len} maxLen ${maxLen} target ${this.target}`);
if (len === parseInt(maxLen, 10)) {
const next: HTMLElement = document.querySelector('#' + this.target);
next.focus();
}
}
}
在jest和jasmine配置中,都会调用我要测试的指令,但是从来没有在jest中设置“目标”,因此测试失败。 evAutoTab =“ target”。
我相信我已正确配置了玩笑(或为玩笑正确配置了角度)
考试:
@Component({
template: `
<div>
<input evAutoTab="AutoTab1" id="AutoTab0" maxlength="4" value=""/>
<input evAutoTab id="AutoTab1" value=""/>
<input evAutoTab="AutoTab4" id="AutoTab2" maxlength="2" value=""/>
</div>
<div>
<input evAutoTab id="AutoTab3" value=""/>
<input evAutoTab id="AutoTab4" value=""/>
<input evAutoTab id="AutoTab5" value=""/>
</div>
`
})
class TestComponent {
constructor() {
}
}
describe('EvAutoTabDirective', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
TestComponent,
EvAutoTabDirective
]
});
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
});
it('should move focus from third element skipping to fifth', () => {
const debugEl: HTMLElement = fixture.debugElement.nativeElement;
const autoTab2: HTMLInputElement = debugEl.querySelector('#AutoTab2');
const autoTab4: HTMLInputElement = debugEl.querySelector('#AutoTab4');
const focusSpy = spyOn({
here: () => {
}
}, 'here');
// verify setup
autoTab2.focus();
expect(document.activeElement.id).toEqual('AutoTab2');
// act
autoTab2.value = '19';
autoTab2.dispatchEvent(new Event('keyup'));
fixture.detectChanges();
expect(document.activeElement.id).toEqual('AutoTab4');
});
});
有什么建议?
最佳答案
因此,以开玩笑的方式测试,我需要第二个Fixture.detectChanges();。
fixture.detectChanges();
fixture.detectChanges();
expect(document.activeElement.id).toEqual('AutoTab4');
现在可以了。
给玩笑第二次机会