Skip to content

Commit 0c5ec0d

Browse files
update test
1 parent 0d5b59a commit 0c5ec0d

File tree

1 file changed

+63
-51
lines changed

1 file changed

+63
-51
lines changed

__tests__/api.factory.test.js

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ElManagement from '../src/distanceFromFactory.js';
2-
import {Api} from '../src/api.factory.js';
2+
import { Api } from '../src/api.factory.js';
33
let container = document.createElement('div');
44
let getElManagementIns, ins, buttonElement, containerElement, tablistElement;
55
beforeAll(() => {
@@ -29,7 +29,7 @@ beforeAll(() => {
2929
});
3030
beforeEach(() => {
3131
getElManagementIns = (params) => new ElManagement(params);
32-
ins = new Api(() => ({getElManagementIns}), {
32+
ins = new Api(() => ({ getElManagementIns }), {
3333
buttonElement,
3434
containerElement,
3535
tablistElement,
@@ -53,14 +53,14 @@ describe('_setEls method : ', () => {
5353
ins._options = {
5454
buttonElement: {
5555
previousElementSibling: {
56-
style: {overflow: 'unset'},
56+
style: { overflow: 'unset' },
5757
},
5858
},
5959
containerElement: {
60-
style: {overflow: 'unset'},
60+
style: { overflow: 'unset' },
6161
},
6262
tablistElement: {
63-
style: {overflow: 'unset'},
63+
style: { overflow: 'unset' },
6464
},
6565
};
6666
ins._setEls();
@@ -71,14 +71,14 @@ describe('_setEls method : ', () => {
7171
ins._options = {
7272
buttonElement: {
7373
previousElementSibling: {
74-
style: {overflow: 'unset'},
74+
style: { overflow: 'unset' },
7575
},
7676
},
7777
containerElement: {
78-
style: {overflow: 'unset'},
78+
style: { overflow: 'unset' },
7979
},
8080
tablistElement: {
81-
style: {overflow: 'unset'},
81+
style: { overflow: 'unset' },
8282
},
8383
};
8484
ins._setEls();
@@ -87,7 +87,7 @@ describe('_setEls method : ', () => {
8787
});
8888
describe('resize method : ', () => {
8989
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(() => { });
9191
ins._validateTabsCount = jest.fn(() => {
9292
ins._tabs = ins._tablistEl.children;
9393
ins._tabsCount = ins._tabs.length;
@@ -98,7 +98,7 @@ describe('resize method : ', () => {
9898
expect(ins._validateTabsCount.mock.calls.length).toBe(1);
9999
expect(ins._showAll.mock.calls.length).toBe(1);
100100

101-
ins._showAll = jest.fn(() => {});
101+
ins._showAll = jest.fn(() => { });
102102
ins._validateTabsCount = jest.fn(() => {
103103
ins._tabs = ins._tablistEl.children;
104104
ins._tabsCount = ins._tabs.length;
@@ -155,9 +155,9 @@ describe('resize method : ', () => {
155155
describe('hideTabs method : ', () => {
156156
test('it should hide tabs from firstHiddenTabIndex', () => {
157157
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 = () => { };
161161
ins._hideTabs(5, selectedTabInfo);
162162
expect(ins._tabs[0].style.display === 'none').toBe(false);
163163
expect(ins._tabs[1].style.display === 'none').toBe(false);
@@ -169,9 +169,9 @@ describe('hideTabs method : ', () => {
169169
});
170170
test('it should return the array of objects including el and index properties', () => {
171171
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 = () => { };
175175
const result = ins._hideTabs(5, selectedTabInfo);
176176
expect(result.length).toBe(2);
177177
expect(result[0].el).toEqual(ins._tabs[5]);
@@ -181,9 +181,9 @@ describe('hideTabs method : ', () => {
181181
});
182182
test('it should not consider selected tab if third parameter is not true', () => {
183183
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 = () => { };
187187
const result = ins._hideTabs(3, selectedTabInfo);
188188
expect(ins._tabs[0].style.display === 'none').toBe(false);
189189
expect(ins._tabs[1].style.display === 'none').toBe(false);
@@ -202,9 +202,9 @@ describe('hideTabs method : ', () => {
202202
});
203203
test('it should consider selected tab if third parameter is true', () => {
204204
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 = () => { };
208208
const result = ins._hideTabs(3, selectedTabInfo, true);
209209
expect(ins._tabs[0].style.display === 'none').toBe(false);
210210
expect(ins._tabs[1].style.display === 'none').toBe(false);
@@ -225,33 +225,33 @@ describe('hideTabs method : ', () => {
225225
});
226226
test('it should call _showBtn method', () => {
227227
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(() => { });
231231
ins._hideTabs(3, selectedTabInfo, true);
232232
expect(ins._showBtn.mock.calls.length).toBe(1);
233233
});
234234
});
235235
describe('validateTabsCount method : ', () => {
236236
test('it should set tabs and tabsCount properties', () => {
237-
ins._tablistEl = {children: {length: 2}};
237+
ins._tablistEl = { children: { length: 2 } };
238238
ins._validateTabsCount();
239239
expect(ins._tabs).toEqual(ins._tablistEl.children);
240240
expect(ins._tabsCount).toEqual(ins._tablistEl.children.length);
241241
});
242242
test('it should returns true if tabsCount is greater than zero', () => {
243-
ins._tablistEl = {children: {length: 2}};
243+
ins._tablistEl = { children: { length: 2 } };
244244
expect(ins._validateTabsCount()).toBe(true);
245-
ins._tablistEl = {children: {length: 0}};
245+
ins._tablistEl = { children: { length: 0 } };
246246
expect(ins._validateTabsCount()).toBe(false);
247247
});
248248
});
249249
describe('showAll method : ', () => {
250250
test('it should set display of all tabs into tabDisplay option', () => {
251251
ins._validateTabsCount();
252-
ins._options.containerElement = {style: {display: ''}};
252+
ins._options.containerElement = { style: { display: '' } };
253253
ins._options.tabDisplay = 'flex';
254-
ins._hideBtn = () => {};
254+
ins._hideBtn = () => { };
255255
ins._showAll();
256256
expect(ins._tabs[0].style.display === 'flex').toBe(true);
257257
expect(ins._tabs[1].style.display === 'flex').toBe(true);
@@ -263,9 +263,9 @@ describe('showAll method : ', () => {
263263
});
264264
test('it should call _hideBtn method', () => {
265265
ins._validateTabsCount();
266-
ins._options.containerElement = {style: {display: ''}};
266+
ins._options.containerElement = { style: { display: '' } };
267267
ins._options.tabDisplay = 'flex';
268-
ins._hideBtn = jest.fn(() => {});
268+
ins._hideBtn = jest.fn(() => { });
269269
ins._showAll();
270270
expect(ins._hideBtn.mock.calls.length).toBe(1);
271271
});
@@ -305,66 +305,78 @@ describe('findFirstHiddenTabIndexFactory mehtod : ', () => {
305305
test('findFirstHiddenTabIndexDSCE should loop through the tabs from the end and return the last tab index which its distance is negative', () => {
306306
ins._tablistEl = tablistElement;
307307
ins._validateTabsCount();
308-
const selectedTabInfo = {index: 5, overflowFullSize: 6};
308+
const selectedTabInfo = { index: 5, overflowFullSize: 6 };
309309
const tabsArray = [...ins._tabs];
310310
ins._getTabDis = (selectedTabInfo, tabEl) => {
311311
const tabIndex = tabsArray.indexOf(tabEl);
312312
if (selectedTabInfo.index == tabIndex || selectedTabInfo.overflowFullSize == tabIndex) {
313-
return {value: -1};
313+
return { value: -1 };
314314
}
315-
return {value: 1};
315+
return { value: 1 };
316316
};
317317
expect(ins._findFirstHiddenTabIndexDSCE(selectedTabInfo, 0, 6)).toBe(5);
318318
});
319319
test('_findFirstHiddenTabIndexASC should loop through the tabs and return the first tab index which its distance is negative', () => {
320320
ins._tablistEl = tablistElement;
321321
ins._validateTabsCount();
322-
const selectedTabInfo = {index: 4, overflowFullSize: 3};
322+
const selectedTabInfo = { index: 4, overflowFullSize: 3 };
323323
const tabsArray = [...ins._tabs];
324324
ins._getTabDis = (selectedTabInfo, tabEl) => {
325325
const tabIndex = tabsArray.indexOf(tabEl);
326326
if (selectedTabInfo.index == tabIndex || selectedTabInfo.overflowFullSize == tabIndex) {
327-
return {value: -1};
327+
return { value: -1 };
328328
}
329-
return {value: 1};
329+
return { value: 1 };
330330
};
331331
expect(ins._findFirstHiddenTabIndexASC(selectedTabInfo, 0, 6)).toBe(3);
332332
});
333333
});
334334
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', () => {
336336
ins._validateTabsCount();
337337
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+
},
340346
};
341-
expect(ins._getOrder()).toBe('asc');
347+
expect(ins._getOrder(ins._tabs[0], ins._tabs[ins._tabs.length - 1])).toBe('asc');
342348
});
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', () => {
344350
ins._validateTabsCount();
345351
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+
},
348360
};
349-
expect(ins._getOrder()).toBe('desc');
361+
expect(ins._getOrder(ins._tabs[0], ins._tabs[ins._tabs.length - 1])).toBe('desc');
350362
});
351363
});
352364
describe('getSearchBoundries method : ', () => {
353365
test('it should return [0, tabsCount - 2] if there is not any tab selected', () => {
354366
ins._validateTabsCount();
355-
const boundry = ins._getSearchBoundries({index: -1});
367+
const boundry = ins._getSearchBoundries({ index: -1 });
356368
expect(boundry[0]).toBe(0); //start value
357369
expect(boundry[1]).toBe(ins._tabsCount - 2); //stop value
358370
});
359371
test('it should return [0, selectedTabIndex - 1] if selected tab is overflow', () => {
360372
ins._validateTabsCount();
361-
const boundry = ins._getSearchBoundries({index: 3, overflowFullSize: 1});
373+
const boundry = ins._getSearchBoundries({ index: 3, overflowFullSize: 1 });
362374
expect(boundry[0]).toBe(0); //start value
363375
expect(boundry[1]).toBe(3 - 1); //stop value
364376
});
365377
test('it should return [selectedTabIndex + 1, tabsCount - 2] if selected tab is not overflow', () => {
366378
ins._validateTabsCount();
367-
const boundry = ins._getSearchBoundries({index: 3, overflowFullSize: 0});
379+
const boundry = ins._getSearchBoundries({ index: 3, overflowFullSize: 0 });
368380
expect(boundry[0]).toBe(3 + 1); //start value
369381
expect(boundry[1]).toBe(ins._tabsCount - 2); //stop value
370382
});
@@ -373,7 +385,7 @@ describe('hideBtn method : ', () => {
373385
test(`it should not hide the button, because the button size is needed when all tabs are visible
374386
but the button should not be visible`, () => {
375387
ins._options.buttonElement = {
376-
style: {display: 'flex', position: 'relative', opacity: 1, pointerEvents: 'all'},
388+
style: { display: 'flex', position: 'relative', opacity: 1, pointerEvents: 'all' },
377389
};
378390
ins._hideBtn();
379391
expect(ins._options.buttonElement.style.display).toBe('flex');
@@ -396,7 +408,7 @@ describe('showBtn method : ', () => {
396408
describe('_checkOverflow method : ', () => {
397409
test('_checkOverflow should check distance of last tab', () => {
398410
ins.els = {
399-
getDistance: (lastTab) => ({value: lastTab}),
411+
getDistance: (lastTab) => ({ value: lastTab }),
400412
};
401413
expect(ins._checkOverflow(1)).toBe(false);
402414
expect(ins._checkOverflow(-1)).toBe(true);

0 commit comments

Comments
 (0)