1
1
import ElManagement from '../src/distanceFromFactory.js' ;
2
- import { Api } from '../src/api.factory.js' ;
2
+ import { Api } from '../src/api.factory.js' ;
3
3
let container = document . createElement ( 'div' ) ;
4
4
let getElManagementIns , ins , buttonElement , containerElement , tablistElement ;
5
5
beforeAll ( ( ) => {
@@ -29,7 +29,7 @@ beforeAll(() => {
29
29
} ) ;
30
30
beforeEach ( ( ) => {
31
31
getElManagementIns = ( params ) => new ElManagement ( params ) ;
32
- ins = new Api ( ( ) => ( { getElManagementIns} ) , {
32
+ ins = new Api ( ( ) => ( { getElManagementIns } ) , {
33
33
buttonElement,
34
34
containerElement,
35
35
tablistElement,
@@ -53,14 +53,14 @@ describe('_setEls method : ', () => {
53
53
ins . _options = {
54
54
buttonElement : {
55
55
previousElementSibling : {
56
- style : { overflow : 'unset' } ,
56
+ style : { overflow : 'unset' } ,
57
57
} ,
58
58
} ,
59
59
containerElement : {
60
- style : { overflow : 'unset' } ,
60
+ style : { overflow : 'unset' } ,
61
61
} ,
62
62
tablistElement : {
63
- style : { overflow : 'unset' } ,
63
+ style : { overflow : 'unset' } ,
64
64
} ,
65
65
} ;
66
66
ins . _setEls ( ) ;
@@ -71,14 +71,14 @@ describe('_setEls method : ', () => {
71
71
ins . _options = {
72
72
buttonElement : {
73
73
previousElementSibling : {
74
- style : { overflow : 'unset' } ,
74
+ style : { overflow : 'unset' } ,
75
75
} ,
76
76
} ,
77
77
containerElement : {
78
- style : { overflow : 'unset' } ,
78
+ style : { overflow : 'unset' } ,
79
79
} ,
80
80
tablistElement : {
81
- style : { overflow : 'unset' } ,
81
+ style : { overflow : 'unset' } ,
82
82
} ,
83
83
} ;
84
84
ins . _setEls ( ) ;
@@ -87,7 +87,7 @@ describe('_setEls method : ', () => {
87
87
} ) ;
88
88
describe ( 'resize method : ' , ( ) => {
89
89
test ( 'it should call showAll method at frist if validateTabsCount method returns true, regardless of return value from checkOverflow method' , ( ) => {
90
- ins . _showAll = jest . fn ( ( ) => { } ) ;
90
+ ins . _showAll = jest . fn ( ( ) => { } ) ;
91
91
ins . _validateTabsCount = jest . fn ( ( ) => {
92
92
ins . _tabs = ins . _tablistEl . children ;
93
93
ins . _tabsCount = ins . _tabs . length ;
@@ -98,7 +98,7 @@ describe('resize method : ', () => {
98
98
expect ( ins . _validateTabsCount . mock . calls . length ) . toBe ( 1 ) ;
99
99
expect ( ins . _showAll . mock . calls . length ) . toBe ( 1 ) ;
100
100
101
- ins . _showAll = jest . fn ( ( ) => { } ) ;
101
+ ins . _showAll = jest . fn ( ( ) => { } ) ;
102
102
ins . _validateTabsCount = jest . fn ( ( ) => {
103
103
ins . _tabs = ins . _tablistEl . children ;
104
104
ins . _tabsCount = ins . _tabs . length ;
@@ -155,9 +155,9 @@ describe('resize method : ', () => {
155
155
describe ( 'hideTabs method : ' , ( ) => {
156
156
test ( 'it should hide tabs from firstHiddenTabIndex' , ( ) => {
157
157
ins . _validateTabsCount ( ) ;
158
- const selectedTabInfo = { index : 4 } ;
159
- ins . _options . containerElement = { style : { display : '' } } ;
160
- ins . _showBtn = ( ) => { } ;
158
+ const selectedTabInfo = { index : 4 } ;
159
+ ins . _options . containerElement = { style : { display : '' } } ;
160
+ ins . _showBtn = ( ) => { } ;
161
161
ins . _hideTabs ( 5 , selectedTabInfo ) ;
162
162
expect ( ins . _tabs [ 0 ] . style . display === 'none' ) . toBe ( false ) ;
163
163
expect ( ins . _tabs [ 1 ] . style . display === 'none' ) . toBe ( false ) ;
@@ -169,9 +169,9 @@ describe('hideTabs method : ', () => {
169
169
} ) ;
170
170
test ( 'it should return the array of objects including el and index properties' , ( ) => {
171
171
ins . _validateTabsCount ( ) ;
172
- const selectedTabInfo = { index : 4 } ;
173
- ins . _options . containerElement = { style : { display : '' } } ;
174
- ins . _showBtn = ( ) => { } ;
172
+ const selectedTabInfo = { index : 4 } ;
173
+ ins . _options . containerElement = { style : { display : '' } } ;
174
+ ins . _showBtn = ( ) => { } ;
175
175
const result = ins . _hideTabs ( 5 , selectedTabInfo ) ;
176
176
expect ( result . length ) . toBe ( 2 ) ;
177
177
expect ( result [ 0 ] . el ) . toEqual ( ins . _tabs [ 5 ] ) ;
@@ -181,9 +181,9 @@ describe('hideTabs method : ', () => {
181
181
} ) ;
182
182
test ( 'it should not consider selected tab if third parameter is not true' , ( ) => {
183
183
ins . _validateTabsCount ( ) ;
184
- const selectedTabInfo = { index : 4 } ;
185
- ins . _options . containerElement = { style : { display : '' } } ;
186
- ins . _showBtn = ( ) => { } ;
184
+ const selectedTabInfo = { index : 4 } ;
185
+ ins . _options . containerElement = { style : { display : '' } } ;
186
+ ins . _showBtn = ( ) => { } ;
187
187
const result = ins . _hideTabs ( 3 , selectedTabInfo ) ;
188
188
expect ( ins . _tabs [ 0 ] . style . display === 'none' ) . toBe ( false ) ;
189
189
expect ( ins . _tabs [ 1 ] . style . display === 'none' ) . toBe ( false ) ;
@@ -202,9 +202,9 @@ describe('hideTabs method : ', () => {
202
202
} ) ;
203
203
test ( 'it should consider selected tab if third parameter is true' , ( ) => {
204
204
ins . _validateTabsCount ( ) ;
205
- const selectedTabInfo = { index : 4 } ;
206
- ins . _options . containerElement = { style : { display : '' } } ;
207
- ins . _showBtn = ( ) => { } ;
205
+ const selectedTabInfo = { index : 4 } ;
206
+ ins . _options . containerElement = { style : { display : '' } } ;
207
+ ins . _showBtn = ( ) => { } ;
208
208
const result = ins . _hideTabs ( 3 , selectedTabInfo , true ) ;
209
209
expect ( ins . _tabs [ 0 ] . style . display === 'none' ) . toBe ( false ) ;
210
210
expect ( ins . _tabs [ 1 ] . style . display === 'none' ) . toBe ( false ) ;
@@ -225,33 +225,33 @@ describe('hideTabs method : ', () => {
225
225
} ) ;
226
226
test ( 'it should call _showBtn method' , ( ) => {
227
227
ins . _validateTabsCount ( ) ;
228
- const selectedTabInfo = { index : 4 } ;
229
- ins . _options . containerElement = { style : { display : '' } } ;
230
- ins . _showBtn = jest . fn ( ( ) => { } ) ;
228
+ const selectedTabInfo = { index : 4 } ;
229
+ ins . _options . containerElement = { style : { display : '' } } ;
230
+ ins . _showBtn = jest . fn ( ( ) => { } ) ;
231
231
ins . _hideTabs ( 3 , selectedTabInfo , true ) ;
232
232
expect ( ins . _showBtn . mock . calls . length ) . toBe ( 1 ) ;
233
233
} ) ;
234
234
} ) ;
235
235
describe ( 'validateTabsCount method : ' , ( ) => {
236
236
test ( 'it should set tabs and tabsCount properties' , ( ) => {
237
- ins . _tablistEl = { children : { length : 2 } } ;
237
+ ins . _tablistEl = { children : { length : 2 } } ;
238
238
ins . _validateTabsCount ( ) ;
239
239
expect ( ins . _tabs ) . toEqual ( ins . _tablistEl . children ) ;
240
240
expect ( ins . _tabsCount ) . toEqual ( ins . _tablistEl . children . length ) ;
241
241
} ) ;
242
242
test ( 'it should returns true if tabsCount is greater than zero' , ( ) => {
243
- ins . _tablistEl = { children : { length : 2 } } ;
243
+ ins . _tablistEl = { children : { length : 2 } } ;
244
244
expect ( ins . _validateTabsCount ( ) ) . toBe ( true ) ;
245
- ins . _tablistEl = { children : { length : 0 } } ;
245
+ ins . _tablistEl = { children : { length : 0 } } ;
246
246
expect ( ins . _validateTabsCount ( ) ) . toBe ( false ) ;
247
247
} ) ;
248
248
} ) ;
249
249
describe ( 'showAll method : ' , ( ) => {
250
250
test ( 'it should set display of all tabs into tabDisplay option' , ( ) => {
251
251
ins . _validateTabsCount ( ) ;
252
- ins . _options . containerElement = { style : { display : '' } } ;
252
+ ins . _options . containerElement = { style : { display : '' } } ;
253
253
ins . _options . tabDisplay = 'flex' ;
254
- ins . _hideBtn = ( ) => { } ;
254
+ ins . _hideBtn = ( ) => { } ;
255
255
ins . _showAll ( ) ;
256
256
expect ( ins . _tabs [ 0 ] . style . display === 'flex' ) . toBe ( true ) ;
257
257
expect ( ins . _tabs [ 1 ] . style . display === 'flex' ) . toBe ( true ) ;
@@ -263,9 +263,9 @@ describe('showAll method : ', () => {
263
263
} ) ;
264
264
test ( 'it should call _hideBtn method' , ( ) => {
265
265
ins . _validateTabsCount ( ) ;
266
- ins . _options . containerElement = { style : { display : '' } } ;
266
+ ins . _options . containerElement = { style : { display : '' } } ;
267
267
ins . _options . tabDisplay = 'flex' ;
268
- ins . _hideBtn = jest . fn ( ( ) => { } ) ;
268
+ ins . _hideBtn = jest . fn ( ( ) => { } ) ;
269
269
ins . _showAll ( ) ;
270
270
expect ( ins . _hideBtn . mock . calls . length ) . toBe ( 1 ) ;
271
271
} ) ;
@@ -305,66 +305,78 @@ describe('findFirstHiddenTabIndexFactory mehtod : ', () => {
305
305
test ( 'findFirstHiddenTabIndexDSCE should loop through the tabs from the end and return the last tab index which its distance is negative' , ( ) => {
306
306
ins . _tablistEl = tablistElement ;
307
307
ins . _validateTabsCount ( ) ;
308
- const selectedTabInfo = { index : 5 , overflowFullSize : 6 } ;
308
+ const selectedTabInfo = { index : 5 , overflowFullSize : 6 } ;
309
309
const tabsArray = [ ...ins . _tabs ] ;
310
310
ins . _getTabDis = ( selectedTabInfo , tabEl ) => {
311
311
const tabIndex = tabsArray . indexOf ( tabEl ) ;
312
312
if ( selectedTabInfo . index == tabIndex || selectedTabInfo . overflowFullSize == tabIndex ) {
313
- return { value : - 1 } ;
313
+ return { value : - 1 } ;
314
314
}
315
- return { value : 1 } ;
315
+ return { value : 1 } ;
316
316
} ;
317
317
expect ( ins . _findFirstHiddenTabIndexDSCE ( selectedTabInfo , 0 , 6 ) ) . toBe ( 5 ) ;
318
318
} ) ;
319
319
test ( '_findFirstHiddenTabIndexASC should loop through the tabs and return the first tab index which its distance is negative' , ( ) => {
320
320
ins . _tablistEl = tablistElement ;
321
321
ins . _validateTabsCount ( ) ;
322
- const selectedTabInfo = { index : 4 , overflowFullSize : 3 } ;
322
+ const selectedTabInfo = { index : 4 , overflowFullSize : 3 } ;
323
323
const tabsArray = [ ...ins . _tabs ] ;
324
324
ins . _getTabDis = ( selectedTabInfo , tabEl ) => {
325
325
const tabIndex = tabsArray . indexOf ( tabEl ) ;
326
326
if ( selectedTabInfo . index == tabIndex || selectedTabInfo . overflowFullSize == tabIndex ) {
327
- return { value : - 1 } ;
327
+ return { value : - 1 } ;
328
328
}
329
- return { value : 1 } ;
329
+ return { value : 1 } ;
330
330
} ;
331
331
expect ( ins . _findFirstHiddenTabIndexASC ( selectedTabInfo , 0 , 6 ) ) . toBe ( 3 ) ;
332
332
} ) ;
333
333
} ) ;
334
334
describe ( 'getOrder method : ' , ( ) => {
335
- test ( 'it should return asc if : last tab child distance from end-edge of tablist container would greater then tablist container size ' , ( ) => {
335
+ test ( 'it should return asc if : first tab distance is more less than last tab distance ' , ( ) => {
336
336
ins . _validateTabsCount ( ) ;
337
337
ins . els = {
338
- getDistance : ( ) => ( { value : 100 } ) ,
339
- getVisibleSize : ( ) => ( { value : 99 } ) ,
338
+ getDistance : ( el ) => {
339
+ if ( el === ins . _tabs [ 0 ] ) {
340
+ return { value : 50 } ;
341
+ }
342
+ if ( el === ins . _tabs [ ins . _tabs . length - 1 ] ) {
343
+ return { value : 100 } ;
344
+ }
345
+ } ,
340
346
} ;
341
- expect ( ins . _getOrder ( ) ) . toBe ( 'asc' ) ;
347
+ expect ( ins . _getOrder ( ins . _tabs [ 0 ] , ins . _tabs [ ins . _tabs . length - 1 ] ) ) . toBe ( 'asc' ) ;
342
348
} ) ;
343
- it ( 'it should return desc if : last tab child distance from end-edge of tablist container would equal or less then tablist container size ' , ( ) => {
349
+ it ( 'it should return desc if : last tab distance is more less than first tab distance ' , ( ) => {
344
350
ins . _validateTabsCount ( ) ;
345
351
ins . els = {
346
- getDistance : ( ) => ( { value : 100 } ) ,
347
- getVisibleSize : ( ) => ( { value : 101 } ) ,
352
+ getDistance : ( el ) => {
353
+ if ( el === ins . _tabs [ 0 ] ) {
354
+ return { value : 150 } ;
355
+ }
356
+ if ( el === ins . _tabs [ ins . _tabs . length - 1 ] ) {
357
+ return { value : 100 } ;
358
+ }
359
+ } ,
348
360
} ;
349
- expect ( ins . _getOrder ( ) ) . toBe ( 'desc' ) ;
361
+ expect ( ins . _getOrder ( ins . _tabs [ 0 ] , ins . _tabs [ ins . _tabs . length - 1 ] ) ) . toBe ( 'desc' ) ;
350
362
} ) ;
351
363
} ) ;
352
364
describe ( 'getSearchBoundries method : ' , ( ) => {
353
365
test ( 'it should return [0, tabsCount - 2] if there is not any tab selected' , ( ) => {
354
366
ins . _validateTabsCount ( ) ;
355
- const boundry = ins . _getSearchBoundries ( { index : - 1 } ) ;
367
+ const boundry = ins . _getSearchBoundries ( { index : - 1 } ) ;
356
368
expect ( boundry [ 0 ] ) . toBe ( 0 ) ; //start value
357
369
expect ( boundry [ 1 ] ) . toBe ( ins . _tabsCount - 2 ) ; //stop value
358
370
} ) ;
359
371
test ( 'it should return [0, selectedTabIndex - 1] if selected tab is overflow' , ( ) => {
360
372
ins . _validateTabsCount ( ) ;
361
- const boundry = ins . _getSearchBoundries ( { index : 3 , overflowFullSize : 1 } ) ;
373
+ const boundry = ins . _getSearchBoundries ( { index : 3 , overflowFullSize : 1 } ) ;
362
374
expect ( boundry [ 0 ] ) . toBe ( 0 ) ; //start value
363
375
expect ( boundry [ 1 ] ) . toBe ( 3 - 1 ) ; //stop value
364
376
} ) ;
365
377
test ( 'it should return [selectedTabIndex + 1, tabsCount - 2] if selected tab is not overflow' , ( ) => {
366
378
ins . _validateTabsCount ( ) ;
367
- const boundry = ins . _getSearchBoundries ( { index : 3 , overflowFullSize : 0 } ) ;
379
+ const boundry = ins . _getSearchBoundries ( { index : 3 , overflowFullSize : 0 } ) ;
368
380
expect ( boundry [ 0 ] ) . toBe ( 3 + 1 ) ; //start value
369
381
expect ( boundry [ 1 ] ) . toBe ( ins . _tabsCount - 2 ) ; //stop value
370
382
} ) ;
@@ -373,7 +385,7 @@ describe('hideBtn method : ', () => {
373
385
test ( `it should not hide the button, because the button size is needed when all tabs are visible
374
386
but the button should not be visible` , ( ) => {
375
387
ins . _options . buttonElement = {
376
- style : { display : 'flex' , position : 'relative' , opacity : 1 , pointerEvents : 'all' } ,
388
+ style : { display : 'flex' , position : 'relative' , opacity : 1 , pointerEvents : 'all' } ,
377
389
} ;
378
390
ins . _hideBtn ( ) ;
379
391
expect ( ins . _options . buttonElement . style . display ) . toBe ( 'flex' ) ;
@@ -396,7 +408,7 @@ describe('showBtn method : ', () => {
396
408
describe ( '_checkOverflow method : ' , ( ) => {
397
409
test ( '_checkOverflow should check distance of last tab' , ( ) => {
398
410
ins . els = {
399
- getDistance : ( lastTab ) => ( { value : lastTab } ) ,
411
+ getDistance : ( lastTab ) => ( { value : lastTab } ) ,
400
412
} ;
401
413
expect ( ins . _checkOverflow ( 1 ) ) . toBe ( false ) ;
402
414
expect ( ins . _checkOverflow ( - 1 ) ) . toBe ( true ) ;
0 commit comments