Jasmine test simulates tab hiding and detects a newly focused element

I have a jasmine test where I have 2 input fields. I focus on the first input, then model the keyword on the tab key and expect the focus to be on the second input. Unfortunately, this is not the case. The focus does not change from the first, and my test does not work. How can this be fixed in order to pass a failure test?

The script of what I'm trying to check is: http://jsfiddle.net/G2Qz3/1/

Jasmine test failed scenario: http://jsfiddle.net/mFUhK/4/

HTML:

<input id="first"></input>
<input id="second"></input>

JavaScript:

function simulateTab() {
    var TAB_KEY = 9;
    var keyboardEvent = document.createEvent("KeyboardEvent");
    var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
    keyboardEvent[initMethod]("keydown", true, true, window, 0, 0, 0, 0, 0, TAB_KEY);
    document.dispatchEvent(keyboardEvent);
}

describe('input tabbing test', function() {    
    beforeEach(function() {
        document.getElementById('first').focus();
    });

    //this passes
    it('input with id "first" should be focussed', function() {
        expect(document.activeElement.getAttribute('id')).toBe('first');
    });

    //this fails
    it('input with id "second" should be focussed after tabbing', function() {
        simulateTab(); 
        expect(document.activeElement.getAttribute('id')).toBe('second');   
    });
});
+4
source share
1 answer

. . SO, , :

, , . , , ( ), ( ), .. , , .

+4

All Articles