-
-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |