1
1
import { module , test } from 'qunit' ;
2
2
import { focus , setupContext , teardownContext , _registerHook } from '@ember/test-helpers' ;
3
- import { buildInstrumentedElement , insertElement } from '../../helpers/events' ;
4
- import { isIE11 } from '../../helpers/browser-detect' ;
3
+ import { buildInstrumentedElement , insertElement , instrumentElement } from '../../helpers/events' ;
4
+ import { isIE11 , isEdge } from '../../helpers/browser-detect' ;
5
5
import hasEmberVersion from '@ember/test-helpers/has-ember-version' ;
6
6
7
7
let focusSteps = [ 'focus' , 'focusin' ] ;
8
+ let blurSteps = [ 'blur' , 'focusout' ] ;
8
9
9
10
if ( isIE11 ) {
10
11
focusSteps = [ 'focusin' , 'focus' ] ;
12
+ blurSteps = [ 'focusout' , 'blur' ] ;
13
+ } else if ( isEdge ) {
14
+ blurSteps = [ 'focusout' , 'blur' ] ;
11
15
}
12
16
13
17
module ( 'DOM Helper: focus' , function ( hooks ) {
@@ -95,6 +99,43 @@ module('DOM Helper: focus', function (hooks) {
95
99
assert . verifySteps ( focusSteps ) ;
96
100
} ) ;
97
101
102
+ test ( 'blurs the previous active element' , async function ( assert ) {
103
+ element = buildInstrumentedElement ( 'input' , [ 'target' ] ) ;
104
+
105
+ const focusedElement = document . createElement ( 'textarea' ) ;
106
+ insertElement ( focusedElement ) ;
107
+ focusedElement . focus ( ) ;
108
+ instrumentElement ( focusedElement , [ 'target' ] ) ;
109
+
110
+ await focus ( element ) ;
111
+
112
+ assert . verifySteps ( [
113
+ ...blurSteps . map ( s => {
114
+ return `${ s } [object HTMLTextAreaElement]` ;
115
+ } ) ,
116
+ ...focusSteps . map ( s => {
117
+ return `${ s } [object HTMLInputElement]` ;
118
+ } ) ,
119
+ ] ) ;
120
+ } ) ;
121
+
122
+ test ( 'does not attempt to blur the previous element if it is not focusable' , async function ( assert ) {
123
+ element = buildInstrumentedElement ( 'input' , [ 'target' ] ) ;
124
+
125
+ const focusedElement = document . createElement ( 'div' ) ;
126
+ insertElement ( focusedElement ) ;
127
+ focusedElement . focus ( ) ;
128
+ instrumentElement ( focusedElement , [ 'target' ] ) ;
129
+
130
+ await focus ( element ) ;
131
+
132
+ assert . verifySteps ( [
133
+ ...focusSteps . map ( s => {
134
+ return `${ s } [object HTMLInputElement]` ;
135
+ } ) ,
136
+ ] ) ;
137
+ } ) ;
138
+
98
139
test ( 'rejects if selector is not found' , async function ( assert ) {
99
140
element = buildInstrumentedElement ( 'div' ) ;
100
141
@@ -106,7 +147,7 @@ module('DOM Helper: focus', function (hooks) {
106
147
) ;
107
148
} ) ;
108
149
109
- test ( 'focusing a input via selector with context set' , async function ( assert ) {
150
+ test ( 'focusing an input via selector with context set' , async function ( assert ) {
110
151
element = buildInstrumentedElement ( 'input' ) ;
111
152
112
153
await setupContext ( context ) ;
@@ -116,7 +157,7 @@ module('DOM Helper: focus', function (hooks) {
116
157
assert . strictEqual ( document . activeElement , element , 'activeElement updated' ) ;
117
158
} ) ;
118
159
119
- test ( 'focusing a input via element with context set' , async function ( assert ) {
160
+ test ( 'focusing an input via element with context set' , async function ( assert ) {
120
161
element = buildInstrumentedElement ( 'input' ) ;
121
162
122
163
await setupContext ( context ) ;
@@ -126,7 +167,7 @@ module('DOM Helper: focus', function (hooks) {
126
167
assert . strictEqual ( document . activeElement , element , 'activeElement updated' ) ;
127
168
} ) ;
128
169
129
- test ( 'focusing a input via element without context set' , async function ( assert ) {
170
+ test ( 'focusing an input via element without context set' , async function ( assert ) {
130
171
element = buildInstrumentedElement ( 'input' ) ;
131
172
132
173
await focus ( element ) ;
@@ -135,7 +176,7 @@ module('DOM Helper: focus', function (hooks) {
135
176
assert . strictEqual ( document . activeElement , element , 'activeElement updated' ) ;
136
177
} ) ;
137
178
138
- test ( 'focusing a input via selector without context set' , async function ( assert ) {
179
+ test ( 'focusing an input via selector without context set' , async function ( assert ) {
139
180
element = buildInstrumentedElement ( 'input' ) ;
140
181
141
182
assert . rejects (
0 commit comments