@@ -10,7 +10,6 @@ import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
10
10
import ButtonView from '../../src/button/buttonview' ;
11
11
import DropdownPanelView from '../../src/dropdown/dropdownpanelview' ;
12
12
import global from '@ckeditor/ckeditor5-utils/src/dom/global' ;
13
- import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect' ;
14
13
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils' ;
15
14
16
15
describe ( 'DropdownView' , ( ) => {
@@ -19,7 +18,10 @@ describe( 'DropdownView', () => {
19
18
testUtils . createSinonSandbox ( ) ;
20
19
21
20
beforeEach ( ( ) => {
22
- locale = { t ( ) { } } ;
21
+ locale = {
22
+ uiLanguageDirection : 'ltr' ,
23
+ t ( ) { }
24
+ } ;
23
25
24
26
buttonView = new ButtonView ( locale ) ;
25
27
panelView = new DropdownPanelView ( locale ) ;
@@ -116,7 +118,7 @@ describe( 'DropdownView', () => {
116
118
} ) ;
117
119
118
120
describe ( 'view.panelView#position to view#panelPosition' , ( ) => {
119
- it ( 'does not update until the dropdown is opened ' , ( ) => {
121
+ it ( 'does not update until the dropdown is open ' , ( ) => {
120
122
view . isOpen = false ;
121
123
view . panelPosition = 'nw' ;
122
124
@@ -128,86 +130,37 @@ describe( 'DropdownView', () => {
128
130
} ) ;
129
131
130
132
describe ( 'in "auto" mode' , ( ) => {
131
- beforeEach ( ( ) => {
132
- // Bloat the panel a little to give the positioning algorithm something to
133
- // work with. If the panel was empty, any smart positioning is pointless.
134
- // Placing an empty element in the viewport isn't that hard, right?
135
- panelView . element . style . width = '200px' ;
136
- panelView . element . style . height = '200px' ;
137
- } ) ;
138
-
139
- it ( 'defaults to "south-east" when there is a plenty of space around' , ( ) => {
140
- const windowRect = new Rect ( global . window ) ;
141
-
142
- // "Put" the dropdown in the middle of the viewport.
143
- stubElementClientRect ( view . buttonView . element , {
144
- top : windowRect . height / 2 ,
145
- left : windowRect . width / 2 ,
146
- width : 10 ,
147
- height : 10
148
- } ) ;
149
-
150
- view . isOpen = true ;
151
-
152
- expect ( panelView . position ) . to . equal ( 'se' ) ;
153
- } ) ;
154
-
155
- it ( 'when the dropdown in the north-west corner of the viewport' , ( ) => {
156
- stubElementClientRect ( view . buttonView . element , {
157
- top : 0 ,
158
- left : 0 ,
159
- width : 100 ,
160
- height : 10
161
- } ) ;
133
+ it ( 'uses _getOptimalPosition() and a dedicated set of positions (LTR)' , ( ) => {
134
+ const spy = testUtils . sinon . spy ( DropdownView , '_getOptimalPosition' ) ;
135
+ const { southEast, southWest, northEast, northWest } = DropdownView . defaultPanelPositions ;
162
136
163
137
view . isOpen = true ;
164
138
165
- expect ( panelView . position ) . to . equal ( 'se' ) ;
139
+ sinon . assert . calledWithExactly ( spy , sinon . match ( {
140
+ element : panelView . element ,
141
+ target : buttonView . element ,
142
+ positions : [
143
+ southEast , southWest , northEast , northWest
144
+ ] ,
145
+ fitInViewport : true
146
+ } ) ) ;
166
147
} ) ;
167
148
168
- it ( 'when the dropdown in the north-east corner of the viewport' , ( ) => {
169
- const windowRect = new Rect ( global . window ) ;
170
-
171
- stubElementClientRect ( view . buttonView . element , {
172
- top : 0 ,
173
- left : windowRect . right - 100 ,
174
- width : 100 ,
175
- height : 10
176
- } ) ;
149
+ it ( 'uses _getOptimalPosition() and a dedicated set of positions (RTL)' , ( ) => {
150
+ const spy = testUtils . sinon . spy ( DropdownView , '_getOptimalPosition' ) ;
151
+ const { southEast, southWest, northEast, northWest } = DropdownView . defaultPanelPositions ;
177
152
153
+ view . locale . uiLanguageDirection = 'rtl' ;
178
154
view . isOpen = true ;
179
155
180
- expect ( panelView . position ) . to . equal ( 'sw' ) ;
181
- } ) ;
182
-
183
- it ( 'when the dropdown in the south-west corner of the viewport' , ( ) => {
184
- const windowRect = new Rect ( global . window ) ;
185
-
186
- stubElementClientRect ( view . buttonView . element , {
187
- top : windowRect . bottom - 10 ,
188
- left : 0 ,
189
- width : 100 ,
190
- height : 10
191
- } ) ;
192
-
193
- view . isOpen = true ;
194
-
195
- expect ( panelView . position ) . to . equal ( 'ne' ) ;
196
- } ) ;
197
-
198
- it ( 'when the dropdown in the south-east corner of the viewport' , ( ) => {
199
- const windowRect = new Rect ( global . window ) ;
200
-
201
- stubElementClientRect ( view . buttonView . element , {
202
- top : windowRect . bottom - 10 ,
203
- left : windowRect . right - 100 ,
204
- width : 100 ,
205
- height : 10
206
- } ) ;
207
-
208
- view . isOpen = true ;
209
-
210
- expect ( panelView . position ) . to . equal ( 'nw' ) ;
156
+ sinon . assert . calledWithExactly ( spy , sinon . match ( {
157
+ element : panelView . element ,
158
+ target : buttonView . element ,
159
+ positions : [
160
+ southWest , southEast , northWest , northEast
161
+ ] ,
162
+ fitInViewport : true
163
+ } ) ) ;
211
164
} ) ;
212
165
} ) ;
213
166
} ) ;
@@ -372,13 +325,66 @@ describe( 'DropdownView', () => {
372
325
sinon . assert . calledOnce ( spy ) ;
373
326
} ) ;
374
327
} ) ;
375
- } ) ;
376
328
377
- function stubElementClientRect ( element , data ) {
378
- const clientRect = Object . assign ( { } , data ) ;
329
+ describe ( 'DropdownView.defaultPanelPositions' , ( ) => {
330
+ let positions , buttonRect , panelRect ;
331
+
332
+ beforeEach ( ( ) => {
333
+ positions = DropdownView . defaultPanelPositions ;
334
+
335
+ buttonRect = {
336
+ top : 100 ,
337
+ bottom : 200 ,
338
+ left : 100 ,
339
+ right : 200 ,
340
+ width : 100 ,
341
+ height : 100
342
+ } ;
343
+
344
+ panelRect = {
345
+ top : 0 ,
346
+ bottom : 0 ,
347
+ left : 0 ,
348
+ right : 0 ,
349
+ width : 50 ,
350
+ height : 50
351
+ } ;
352
+ } ) ;
353
+
354
+ it ( 'should have a proper length' , ( ) => {
355
+ expect ( Object . keys ( positions ) ) . to . have . length ( 4 ) ;
356
+ } ) ;
357
+
358
+ it ( 'should define the "southEast" position' , ( ) => {
359
+ expect ( positions . southEast ( buttonRect , panelRect ) ) . to . deep . equal ( {
360
+ top : 200 ,
361
+ left : 100 ,
362
+ name : 'se'
363
+ } ) ;
364
+ } ) ;
365
+
366
+ it ( 'should define the "southWest" position' , ( ) => {
367
+ expect ( positions . southWest ( buttonRect , panelRect ) ) . to . deep . equal ( {
368
+ top : 200 ,
369
+ left : 150 ,
370
+ name : 'sw'
371
+ } ) ;
372
+ } ) ;
379
373
380
- clientRect . right = clientRect . left + clientRect . width ;
381
- clientRect . bottom = clientRect . top + clientRect . height ;
374
+ it ( 'should define the "northEast" position' , ( ) => {
375
+ expect ( positions . northEast ( buttonRect , panelRect ) ) . to . deep . equal ( {
376
+ top : 50 ,
377
+ left : 100 ,
378
+ name : 'ne'
379
+ } ) ;
380
+ } ) ;
382
381
383
- testUtils . sinon . stub ( element , 'getBoundingClientRect' ) . returns ( clientRect ) ;
384
- }
382
+ it ( 'should define the "northWest" position' , ( ) => {
383
+ expect ( positions . northWest ( buttonRect , panelRect ) ) . to . deep . equal ( {
384
+ top : 150 ,
385
+ left : 150 ,
386
+ name : 'nw'
387
+ } ) ;
388
+ } ) ;
389
+ } ) ;
390
+ } ) ;
0 commit comments