@@ -113,7 +113,7 @@ describe( 'ContextualToolbar', () => {
113113 } ) ;
114114 } ) ;
115115
116- describe ( '_showPanel ()' , ( ) => {
116+ describe ( 'show ()' , ( ) => {
117117 let balloonAddSpy , forwardSelectionRect , backwardSelectionRect ;
118118
119119 beforeEach ( ( ) => {
@@ -127,9 +127,9 @@ describe( 'ContextualToolbar', () => {
127127 } ;
128128
129129 backwardSelectionRect = {
130- top : 100 ,
130+ top : 200 ,
131131 height : 10 ,
132- bottom : 110 ,
132+ bottom : 210 ,
133133 left : 200 ,
134134 width : 50 ,
135135 right : 250
@@ -146,13 +146,13 @@ describe( 'ContextualToolbar', () => {
146146
147147 const defaultPositions = BalloonPanelView . defaultPositions ;
148148
149- contextualToolbar . _showPanel ( ) ;
149+ contextualToolbar . show ( ) ;
150150
151- sinon . assert . calledWithExactly ( balloonAddSpy , {
151+ sinon . assert . calledWith ( balloonAddSpy , {
152152 view : contextualToolbar . toolbarView ,
153153 balloonClassName : 'ck-toolbar-container ck-editor-toolbar-container' ,
154154 position : {
155- target : sinon . match ( value => value ( ) == backwardSelectionRect ) ,
155+ target : sinon . match . func ,
156156 limiter : editor . ui . view . editable . element ,
157157 positions : [
158158 defaultPositions . southEastArrowNorth ,
@@ -164,20 +164,22 @@ describe( 'ContextualToolbar', () => {
164164 ]
165165 }
166166 } ) ;
167+
168+ expect ( balloonAddSpy . firstCall . args [ 0 ] . position . target ( ) ) . to . deep . equal ( backwardSelectionRect ) ;
167169 } ) ;
168170
169171 it ( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the backward selection' , ( ) => {
170172 setData ( editor . document , '<paragraph>b[a]r</paragraph>' , { lastRangeBackward : true } ) ;
171173
172174 const defaultPositions = BalloonPanelView . defaultPositions ;
173175
174- contextualToolbar . _showPanel ( ) ;
176+ contextualToolbar . show ( ) ;
175177
176178 sinon . assert . calledWithExactly ( balloonAddSpy , {
177179 view : contextualToolbar . toolbarView ,
178180 balloonClassName : 'ck-toolbar-container ck-editor-toolbar-container' ,
179181 position : {
180- target : sinon . match ( value => value ( ) == forwardSelectionRect ) ,
182+ target : sinon . match . func ,
181183 limiter : editor . ui . view . editable . element ,
182184 positions : [
183185 defaultPositions . northWestArrowSouth ,
@@ -189,6 +191,8 @@ describe( 'ContextualToolbar', () => {
189191 ]
190192 }
191193 } ) ;
194+
195+ expect ( balloonAddSpy . firstCall . args [ 0 ] . position . target ( ) ) . to . deep . equal ( forwardSelectionRect ) ;
192196 } ) ;
193197
194198 it ( 'should update balloon position on ViewDocument#render event while balloon is added to the #_balloon' , ( ) => {
@@ -198,7 +202,7 @@ describe( 'ContextualToolbar', () => {
198202
199203 editor . editing . view . fire ( 'render' ) ;
200204
201- contextualToolbar . _showPanel ( ) ;
205+ contextualToolbar . show ( ) ;
202206 sinon . assert . notCalled ( spy ) ;
203207
204208 editor . editing . view . fire ( 'render' ) ;
@@ -208,28 +212,44 @@ describe( 'ContextualToolbar', () => {
208212 it ( 'should not add #toolbarView to the #_balloon more than once' , ( ) => {
209213 setData ( editor . document , '<paragraph>b[a]r</paragraph>' ) ;
210214
211- contextualToolbar . _showPanel ( ) ;
212- contextualToolbar . _showPanel ( ) ;
215+ contextualToolbar . show ( ) ;
216+ contextualToolbar . show ( ) ;
213217 sinon . assert . calledOnce ( balloonAddSpy ) ;
214218 } ) ;
215219
216- it ( 'should not add #toolbarView to the #_balloon when editor is not focused' , ( ) => {
217- setData ( editor . document , '<paragraph>b[a]r</paragraph>' ) ;
218- editor . editing . view . isFocused = false ;
220+ describe ( 'on #_selectionChangeDebounced event' , ( ) => {
221+ let showSpy ;
219222
220- contextualToolbar . _showPanel ( ) ;
221- sinon . assert . notCalled ( balloonAddSpy ) ;
222- } ) ;
223+ beforeEach ( ( ) => {
224+ showSpy = sinon . spy ( contextualToolbar , 'show' ) ;
225+ } ) ;
223226
224- it ( 'should not add #toolbarView to the #_balloon when selection is collapsed' , ( ) => {
225- setData ( editor . document , '<paragraph>b[]ar</paragraph>' ) ;
227+ it ( 'should not be called when the editor is not focused' , ( ) => {
228+ setData ( editor . document , '<paragraph>b[a]r</paragraph>' ) ;
229+ editor . editing . view . isFocused = false ;
226230
227- contextualToolbar . _showPanel ( ) ;
228- sinon . assert . notCalled ( balloonAddSpy ) ;
231+ contextualToolbar . fire ( '_selectionChangeDebounced' ) ;
232+ sinon . assert . notCalled ( showSpy ) ;
233+ } ) ;
234+
235+ it ( 'should not be called when the selection is collapsed' , ( ) => {
236+ setData ( editor . document , '<paragraph>b[]ar</paragraph>' ) ;
237+
238+ contextualToolbar . fire ( '_selectionChangeDebounced' ) ;
239+ sinon . assert . notCalled ( showSpy ) ;
240+ } ) ;
241+
242+ it ( 'should be called when the selection is not collapsed and editor is focused' , ( ) => {
243+ setData ( editor . document , '<paragraph>b[a]r</paragraph>' ) ;
244+ editor . editing . view . isFocused = true ;
245+
246+ contextualToolbar . fire ( '_selectionChangeDebounced' ) ;
247+ sinon . assert . calledOnce ( showSpy ) ;
248+ } ) ;
229249 } ) ;
230250 } ) ;
231251
232- describe ( '_hidePanel ()' , ( ) => {
252+ describe ( 'hide ()' , ( ) => {
233253 let removeBalloonSpy ;
234254
235255 beforeEach ( ( ) => {
@@ -240,9 +260,9 @@ describe( 'ContextualToolbar', () => {
240260 it ( 'should remove #toolbarView from the #_balloon' , ( ) => {
241261 setData ( editor . document , '<paragraph>b[a]r</paragraph>' ) ;
242262
243- contextualToolbar . _showPanel ( ) ;
263+ contextualToolbar . show ( ) ;
244264
245- contextualToolbar . _hidePanel ( ) ;
265+ contextualToolbar . hide ( ) ;
246266 sinon . assert . calledWithExactly ( removeBalloonSpy , contextualToolbar . toolbarView ) ;
247267 } ) ;
248268
@@ -251,15 +271,15 @@ describe( 'ContextualToolbar', () => {
251271
252272 const spy = sandbox . spy ( balloon , 'updatePosition' ) ;
253273
254- contextualToolbar . _showPanel ( ) ;
255- contextualToolbar . _hidePanel ( ) ;
274+ contextualToolbar . show ( ) ;
275+ contextualToolbar . hide ( ) ;
256276
257277 editor . editing . view . fire ( 'render' ) ;
258278 sinon . assert . notCalled ( spy ) ;
259279 } ) ;
260280
261281 it ( 'should not remove #ttolbarView when is not added to the #_balloon' , ( ) => {
262- contextualToolbar . _hidePanel ( ) ;
282+ contextualToolbar . hide ( ) ;
263283
264284 sinon . assert . notCalled ( removeBalloonSpy ) ;
265285 } ) ;
@@ -295,8 +315,8 @@ describe( 'ContextualToolbar', () => {
295315 beforeEach ( ( ) => {
296316 setData ( editor . document , '<paragraph>[bar]</paragraph>' ) ;
297317
298- showPanelSpy = sandbox . spy ( contextualToolbar , '_showPanel ' ) ;
299- hidePanelSpy = sandbox . spy ( contextualToolbar , '_hidePanel ' ) ;
318+ showPanelSpy = sandbox . spy ( contextualToolbar , 'show ' ) ;
319+ hidePanelSpy = sandbox . spy ( contextualToolbar , 'hide ' ) ;
300320 } ) ;
301321
302322 it ( 'should open when selection stops changing' , ( ) => {
@@ -395,7 +415,7 @@ describe( 'ContextualToolbar', () => {
395415 contextualToolbar . on ( 'beforeShow' , spy ) ;
396416 setData ( editor . document , '<paragraph>b[a]r</paragraph>' ) ;
397417
398- contextualToolbar . _showPanel ( ) ;
418+ contextualToolbar . show ( ) ;
399419 sinon . assert . calledOnce ( spy ) ;
400420 } ) ;
401421
@@ -408,7 +428,7 @@ describe( 'ContextualToolbar', () => {
408428 stop ( ) ;
409429 } ) ;
410430
411- contextualToolbar . _showPanel ( ) ;
431+ contextualToolbar . show ( ) ;
412432 sinon . assert . notCalled ( balloonAddSpy ) ;
413433 } ) ;
414434 } ) ;
@@ -421,14 +441,8 @@ describe( 'ContextualToolbar', () => {
421441 sandbox . stub ( editingView . domConverter , 'viewRangeToDom' , ( ...args ) => {
422442 const domRange = originalViewRangeToDom . apply ( editingView . domConverter , args ) ;
423443
424- sandbox . stub ( domRange , 'getClientRects' , ( ) => {
425- return {
426- length : 2 ,
427- item ( id ) {
428- return id === 0 ? forwardSelectionRect : backwardSelectionRect ;
429- }
430- } ;
431- } ) ;
444+ sandbox . stub ( domRange , 'getClientRects' )
445+ . returns ( [ forwardSelectionRect , backwardSelectionRect ] ) ;
432446
433447 return domRange ;
434448 } ) ;
0 commit comments