Skip to content

Commit

Permalink
Add unit tests for focus.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Dec 6, 2017
1 parent a587f73 commit 1f5b629
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions tests/unit/dom/focus-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { module, test } from 'qunit';
import { focus, setContext, unsetContext } from '@ember/test-helpers';
import { buildInstrumentedElement } from '../../helpers/events';

module('DOM Helper: focus', function(hooks) {
let element;

hooks.beforeEach(function() {
// used to simulate how `setupRenderingTest` (and soon `setupApplicationTest`)
// set context.element to the rootElement
this.element = document.querySelector('#qunit-fixture');
});

hooks.afterEach(function() {
if (element) {
element.parentNode.removeChild(element);
}
unsetContext();
});

test('focusing a div via selector with context set', async function(assert) {
element = buildInstrumentedElement('div');

setContext(this);
assert.throws(() => {
focus(`#${element.id}`);
}, /is not focusable/);
});

test('focusing a div via element with context set', async function(assert) {
element = buildInstrumentedElement('div');

setContext(this);
assert.throws(() => {
focus(element);
}, /is not focusable/);
});

test('does not run sync', async function(assert) {
element = buildInstrumentedElement('input');

let promise = focus(element);

assert.verifySteps([]);

await promise;

assert.verifySteps(['focus', 'focusin']);
});

test('throws an error if selector is not found', async function(assert) {
element = buildInstrumentedElement('div');

assert.throws(() => {
setContext(this);
focus(`#foo-bar-baz-not-here-ever-bye-bye`);
}, /Element not found when calling `focus\('#foo-bar-baz-not-here-ever-bye-bye'\)`/);
});

test('focusing a input via selector with context set', async function(assert) {
element = buildInstrumentedElement('input');

setContext(this);
await focus(`#${element.id}`);

assert.verifySteps(['focus', 'focusin']);
assert.strictEqual(document.activeElement, element, 'activeElement updated');
});

test('focusing a input via element with context set', async function(assert) {
element = buildInstrumentedElement('input');

setContext(this);
await focus(element);

assert.verifySteps(['focus', 'focusin']);
assert.strictEqual(document.activeElement, element, 'activeElement updated');
});

test('focusing a input via element without context set', async function(assert) {
element = buildInstrumentedElement('input');

await focus(element);

assert.verifySteps(['focus', 'focusin']);
assert.strictEqual(document.activeElement, element, 'activeElement updated');
});
});

0 comments on commit 1f5b629

Please sign in to comment.