Я сел этим прекрасным субботним утром с целью перевести мой angular 9 проект в шутку. Пока что провал. Помимо JSDOM, не поддерживающего ClipboardEvent of DragDropEvent (для которого у меня есть обходные пути), тест, который проходит в 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 и в конфигурациях жасмина вызывается директива, которую я хочу проверить, но «цель» никогда не устанавливается в jest, поэтому тест не пройден. evAutoTab = "target".
Я считаю, что Jest настроен правильно (точнее angular настроен правильно для Jest)
Тест:
@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');
});
});
Любые предложения