@@ -92,6 +92,72 @@ describe('markdown-toolbar-element', function() {
92
92
document . body . innerHTML = ''
93
93
} )
94
94
95
+ describe ( 'focus management' , function ( ) {
96
+ function focusFirstButton ( ) {
97
+ const button = document . querySelector ( 'md-bold' )
98
+ button . focus ( )
99
+ }
100
+
101
+ function pushKeyOnFocussedButton ( key ) {
102
+ const event = document . createEvent ( 'Event' )
103
+ event . initEvent ( 'keydown' , true , true )
104
+ event . key = key
105
+ document . activeElement . dispatchEvent ( event )
106
+ }
107
+
108
+ function getElementsWithTabindex ( index ) {
109
+ return [ ...document . querySelectorAll ( `markdown-toolbar [tabindex="${ index } "]` ) ]
110
+ }
111
+
112
+ it ( 'moves focus to next button when ArrowRight is pressed' , function ( ) {
113
+ focusFirstButton ( )
114
+ pushKeyOnFocussedButton ( 'ArrowRight' )
115
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 13 )
116
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-header' ) ] )
117
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
118
+ pushKeyOnFocussedButton ( 'ArrowRight' )
119
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 13 )
120
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-header[level="1"]' ) ] )
121
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
122
+ pushKeyOnFocussedButton ( 'ArrowRight' )
123
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 13 )
124
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-header[level="10"]' ) ] )
125
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
126
+ } )
127
+
128
+ it ( 'cycles focus round to last element from first when ArrowLeft is pressed' , function ( ) {
129
+ focusFirstButton ( )
130
+ pushKeyOnFocussedButton ( 'ArrowLeft' )
131
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 13 )
132
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-ref' ) ] )
133
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
134
+ pushKeyOnFocussedButton ( 'ArrowLeft' )
135
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 13 )
136
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-mention' ) ] )
137
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
138
+ } )
139
+
140
+ it ( 'focussed first/last button when Home/End key is pressed' , function ( ) {
141
+ focusFirstButton ( )
142
+ pushKeyOnFocussedButton ( 'End' )
143
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 13 )
144
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-ref' ) ] )
145
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
146
+ pushKeyOnFocussedButton ( 'End' )
147
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 13 )
148
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-ref' ) ] )
149
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
150
+ pushKeyOnFocussedButton ( 'Home' )
151
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 13 )
152
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-bold' ) ] )
153
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
154
+ pushKeyOnFocussedButton ( 'Home' )
155
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 13 )
156
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-bold' ) ] )
157
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
158
+ } )
159
+ } )
160
+
95
161
describe ( 'bold' , function ( ) {
96
162
it ( 'bold selected text when you click the bold icon' , function ( ) {
97
163
setVisualValue ( 'The |quick| brown fox jumps over the lazy dog' )
0 commit comments