mercredi 18 décembre 2019

How to spy on focus and select function with Jasmine?

How can I spy on the focus() and select() functions within Jasmine (Unit testing)?

My function:

     nextFieldOnAlt(e) {
        if (e.keyCode === 18) {
            const focusableFields = Array.prototype.slice.call(document.querySelectorAll('input, textarea'));
            const indexFocus = (focusableFields.indexOf(document.activeElement) + 1) % focusableFields.length;
            const input = focusableFields[indexFocus];

            input.focus();
            input.select();
        }
    }

The unit test:

    describe('nextFieldOnAlt function', function() {
        it('check', function() {
            const event = {
                type: 'keypress',
                keyCode: 18
            };
            const focusableFields = angular.element(['<input type="number">22</input>', '<textarea>Test</textarea>']);
            spyOn(document, 'querySelectorAll').and.returnValue(focusableFields);

            Utilities.nextFieldOnAlt(event);

            expect(event.keyCode).toEqual(13);
        });
    });

I get the error

focus is not a function

Tried to add spy like the querySelectorAll, but that didn't work.

Any ideas, suggestions?

Aucun commentaire:

Enregistrer un commentaire