@@ -71,7 +71,11 @@ describe('markdown-toolbar-element', function() {
71
71
<md-bold>bold</md-bold>
72
72
<md-header>header</md-header>
73
73
<md-header level="1">h1</md-header>
74
+ <div hidden>
75
+ <md-header level="5">h5</md-header>
76
+ </div>
74
77
<md-header level="10">h1</md-header>
78
+ <div data-md-button>Other button</div>
75
79
<md-italic>italic</md-italic>
76
80
<md-quote>quote</md-quote>
77
81
<md-code>code</md-code>
@@ -92,6 +96,87 @@ describe('markdown-toolbar-element', function() {
92
96
document . body . innerHTML = ''
93
97
} )
94
98
99
+ describe ( 'focus management' , function ( ) {
100
+ function focusFirstButton ( ) {
101
+ const button = document . querySelector ( 'md-bold' )
102
+ button . focus ( )
103
+ }
104
+
105
+ function pushKeyOnFocussedButton ( key ) {
106
+ const event = document . createEvent ( 'Event' )
107
+ event . initEvent ( 'keydown' , true , true )
108
+ event . key = key
109
+ document . activeElement . dispatchEvent ( event )
110
+ }
111
+
112
+ function getElementsWithTabindex ( index ) {
113
+ return [ ...document . querySelectorAll ( `markdown-toolbar [tabindex="${ index } "]` ) ]
114
+ }
115
+
116
+ beforeEach ( ( ) => {
117
+ document . querySelector ( 'markdown-toolbar' ) . focus ( )
118
+ } )
119
+
120
+ it ( 'moves focus to next button when ArrowRight is pressed' , function ( ) {
121
+ focusFirstButton ( )
122
+ pushKeyOnFocussedButton ( 'ArrowRight' )
123
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 14 )
124
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-header' ) ] )
125
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
126
+ pushKeyOnFocussedButton ( 'ArrowRight' )
127
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 14 )
128
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-header[level="1"]' ) ] )
129
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
130
+ pushKeyOnFocussedButton ( 'ArrowRight' )
131
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 14 )
132
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-header[level="10"]' ) ] )
133
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
134
+ } )
135
+
136
+ it ( 'cycles focus round to last element from first when ArrowLeft is pressed' , function ( ) {
137
+ focusFirstButton ( )
138
+ pushKeyOnFocussedButton ( 'ArrowLeft' )
139
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 14 )
140
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-ref' ) ] )
141
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
142
+ pushKeyOnFocussedButton ( 'ArrowLeft' )
143
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 14 )
144
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-mention' ) ] )
145
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
146
+ } )
147
+
148
+ it ( 'focussed first/last button when Home/End key is pressed' , function ( ) {
149
+ focusFirstButton ( )
150
+ pushKeyOnFocussedButton ( 'End' )
151
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 14 )
152
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-ref' ) ] )
153
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
154
+ pushKeyOnFocussedButton ( 'End' )
155
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 14 )
156
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-ref' ) ] )
157
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
158
+ pushKeyOnFocussedButton ( 'Home' )
159
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 14 )
160
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-bold' ) ] )
161
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
162
+ pushKeyOnFocussedButton ( 'Home' )
163
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 14 )
164
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'md-bold' ) ] )
165
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
166
+ } )
167
+
168
+ it ( 'counts `data-md-button` elements in the focussable set' , function ( ) {
169
+ focusFirstButton ( )
170
+ pushKeyOnFocussedButton ( 'ArrowRight' )
171
+ pushKeyOnFocussedButton ( 'ArrowRight' )
172
+ pushKeyOnFocussedButton ( 'ArrowRight' )
173
+ pushKeyOnFocussedButton ( 'ArrowRight' )
174
+ assert . equal ( getElementsWithTabindex ( - 1 ) . length , 14 )
175
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . querySelector ( 'div[data-md-button]' ) ] )
176
+ assert . deepEqual ( getElementsWithTabindex ( 0 ) , [ document . activeElement ] )
177
+ } )
178
+ } )
179
+
95
180
describe ( 'bold' , function ( ) {
96
181
it ( 'bold selected text when you click the bold icon' , function ( ) {
97
182
setVisualValue ( 'The |quick| brown fox jumps over the lazy dog' )
0 commit comments