Upgrade to Mocha 2.4 to fix iffy "stealing back" focus test Recommend -w to view diff. That test requires the document to be focused for the browser to exhibit the "stealing back" focus behavior, so we had a check to skip the test if the browser isn't focused. Unfortunately that check was useless a large fraction of the time due to a race condition: if you Refresh unit.html, while tests are all being defined but none are being run the check happens, and then while the whole test suite is actually running (slow), you switch to another browser tab, then when the test actually runs the document is unfocused and the test fails. (Note that this only happens if you have the Developer Tools/Web Inspector closed; when they're open and focused the document will be unfocused anyway, even when you Refresh. So to reproduce that above behavior make sure Dev Tools aren't open on unit.html when you Refresh.)
diff --git a/package.json b/package.json index 703e3ec..b34bf9b 100644 --- a/package.json +++ b/package.json
@@ -11,7 +11,7 @@ "pjs": "3.x" }, "devDependencies": { - "mocha": "*", + "mocha": ">=2.4.1", "uglify-js": "2.x", "less": ">=1.5.1" }
diff --git a/test/unit/saneKeyboardEvents.test.js b/test/unit/saneKeyboardEvents.test.js index 9247803..158d425 100644 --- a/test/unit/saneKeyboardEvents.test.js +++ b/test/unit/saneKeyboardEvents.test.js
@@ -146,42 +146,42 @@ assert.ok(document.activeElement !== el[0], 'textarea remains blurred'); }); - if (!document.hasFocus()) { - test('blur in keystroke handler: DOCUMENT NEEDS FOCUS, SEE CONSOLE '); - console.warn( - 'The test "blur in keystroke handler" needs the document to have ' + - 'focus. Only when the document has focus does .select() on an ' + - 'element also focus it, which is part of the problematic behavior ' + - 'we are testing robustness against. (Specifically, erroneously ' + - 'calling .select() in a timeout after the textarea has blurred, ' + - '"stealing back" focus.)\n' + - 'Normally, the page being open and focused is enough to have focus, ' + - 'but with the Developer Tools open, it depends on whether you last ' + - 'clicked on something in the Developer Tools or on the page itself. ' + - 'Click the page, or close the Developer Tools, and Refresh.' - ); - } - else { - test('blur in keystroke handler', function(done) { - var shim = saneKeyboardEvents(el, { - keystroke: function(key) { - assert.equal(key, 'Left'); - el[0].blur(); - } - }); + test('blur in keystroke handler', function(done) { + if (!document.hasFocus()) { + console.warn( + 'The test "blur in keystroke handler" needs the document to have ' + + 'focus. Only when the document has focus does .select() on an ' + + 'element also focus it, which is part of the problematic behavior ' + + 'we are testing robustness against. (Specifically, erroneously ' + + 'calling .select() in a timeout after the textarea has blurred, ' + + '"stealing back" focus.)\n' + + 'Normally, the page being open and focused is enough to have focus, ' + + 'but with the Developer Tools open, it depends on whether you last ' + + 'clicked on something in the Developer Tools or on the page itself. ' + + 'Click the page, or close the Developer Tools, and Refresh.' + ); + el.remove(); // LOL next line skips teardown https://git.io/vaUWq + this.skip(); + } - shim.select('foobar'); - assert.ok(document.activeElement === el[0], 'textarea focused'); - - el.trigger(Event('keydown', { which: 37 })); - assert.ok(document.activeElement !== el[0], 'textarea blurred'); - - setTimeout(function() { - assert.ok(document.activeElement !== el[0], 'textarea remains blurred'); - done(); - }); + var shim = saneKeyboardEvents(el, { + keystroke: function(key) { + assert.equal(key, 'Left'); + el[0].blur(); + } }); - } + + shim.select('foobar'); + assert.ok(document.activeElement === el[0], 'textarea focused'); + + el.trigger(Event('keydown', { which: 37 })); + assert.ok(document.activeElement !== el[0], 'textarea blurred'); + + setTimeout(function() { + assert.ok(document.activeElement !== el[0], 'textarea remains blurred'); + done(); + }); + }); suite('selected text after keypress or paste doesn\'t get mistaken' + ' for inputted text', function() {