Skip to content

Commit 61bd85c

Browse files
create api.factory.test file
1 parent a40243b commit 61bd85c

File tree

1 file changed

+342
-0
lines changed

1 file changed

+342
-0
lines changed

src/api.factory.test.js

Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
1+
import ElManagement from './distanceFromFactory.js';
2+
import {Api} from './api.factory.js';
3+
let container = document.createElement('div');
4+
let getElManagementIns, ins, buttonElement, containerElement;
5+
beforeAll(() => {
6+
const d = document;
7+
document.body.appendChild(container);
8+
container.innerHTML = `
9+
<div id='view'>
10+
<div id='container'>
11+
<div id='overflow'>
12+
<ul id="ul">
13+
<li>Tab 0</li>
14+
<li>Tab 1</li>
15+
<li>Tab 2</li>
16+
<li>Tab 3</li>
17+
<li style='color:red;'>Tab 4</li>
18+
<li>Tab 5</li>
19+
<li>Tab 6</li>
20+
</ul>
21+
<button id='more-button'>more</button>
22+
</div>
23+
</div>
24+
</div>
25+
`;
26+
(buttonElement = d.getElementById('more-button')), (containerElement = d.getElementById('container'));
27+
});
28+
beforeEach(() => {
29+
getElManagementIns = (params) => new ElManagement(params);
30+
ins = new Api(() => ({getElManagementIns}), {buttonElement, containerElement, containerDisplay: 'block'});
31+
});
32+
afterEach(() => {
33+
ins = null;
34+
getElManagementIns = null;
35+
});
36+
afterAll(() => {
37+
buttonElement = null;
38+
containerElement = null;
39+
container.innerHTML = '';
40+
document.body.removeChild(container);
41+
container = null;
42+
});
43+
describe('_setEls method : ', () => {
44+
test('it should set _tablistEl and make its overflow, visible', () => {
45+
ins._options = {
46+
buttonElement: {
47+
previousElementSibling: {
48+
style: {overflow: 'unset'},
49+
},
50+
},
51+
containerElement: {
52+
style: {overflow: 'unset'},
53+
},
54+
};
55+
ins._setEls();
56+
expect(ins._tablistEl).toEqual(ins._options.buttonElement.previousElementSibling);
57+
expect(ins._tablistEl.style.overflow).toBe('visible');
58+
});
59+
test('it should set overflow of containerElement element, hidden', () => {
60+
ins._options = {
61+
buttonElement: {
62+
previousElementSibling: {
63+
style: {overflow: 'unset'},
64+
},
65+
},
66+
containerElement: {
67+
style: {overflow: 'unset'},
68+
},
69+
};
70+
ins._setEls();
71+
expect(ins._options.containerElement.style.overflow).toBe('hidden');
72+
});
73+
});
74+
describe('resize method : ', () => {
75+
test('it should call showAll method at frist if validateTabsCount method returns true, regardless of return value from checkOverflow method', () => {
76+
ins._showAll = jest.fn(() => {});
77+
ins._validateTabsCount = jest.fn(() => {
78+
ins._tabs = ins._tablistEl.children;
79+
ins._tabsCount = ins._tabs.length;
80+
return true;
81+
});
82+
ins._checkOverflow = () => false;
83+
ins.resize(4);
84+
expect(ins._validateTabsCount.mock.calls.length).toBe(1);
85+
expect(ins._showAll.mock.calls.length).toBe(1);
86+
87+
ins._showAll = jest.fn(() => {});
88+
ins._validateTabsCount = jest.fn(() => {
89+
ins._tabs = ins._tablistEl.children;
90+
ins._tabsCount = ins._tabs.length;
91+
return false;
92+
});
93+
ins._checkOverflow = () => false;
94+
ins.resize(4);
95+
expect(ins._validateTabsCount.mock.calls.length).toBe(1);
96+
expect(ins._showAll.mock.calls.length).toBe(0);
97+
});
98+
test('it should set els prop and then calls checkOverflow method', () => {
99+
expect.assertions(1);
100+
ins._checkOverflow = jest.fn(() => {
101+
expect(!!ins.els).toBe(true);
102+
return false;
103+
});
104+
ins.resize(4);
105+
});
106+
test('if checkOverflow method returns false then it should returns a empty array', () => {
107+
ins._checkOverflow = jest.fn(() => false);
108+
expect(ins.resize()).toEqual([]);
109+
});
110+
test('it should call hideTabs method if checkOverflow method returns true', () => {
111+
ins._hideTabs = jest.fn(() => []);
112+
ins._checkOverflow = () => true;
113+
ins.resize(4);
114+
expect(ins._hideTabs.mock.calls.length).toBe(1);
115+
});
116+
test('it should call hideTabs method based on returned value of validateSliderMinSize method', () => {
117+
ins._checkOverflow = () => true;
118+
ins._getSelectedTabInfo = () => 'selectedTabInfo';
119+
ins._findFirstHiddenTabIndexFactory = () => '_findFirstHiddenTabIndexFactory';
120+
ins._getSearchBoundries = () => [0, 6];
121+
ins._getOrder = () => 'asc';
122+
ins._hideTabs = jest.fn(() => []);
123+
// if _validateSliderMinSize return true
124+
ins._validateSliderMinSize = () => true;
125+
ins.resize(4);
126+
expect(ins._hideTabs.mock.calls[0][0]).toBe('_findFirstHiddenTabIndexFactory');
127+
expect(ins._hideTabs.mock.calls[0][1]).toBe('selectedTabInfo');
128+
expect(ins._hideTabs.mock.calls[0][2]).toBe(undefined);
129+
// if _validateSliderMinSize return false
130+
ins._validateSliderMinSize = () => false;
131+
ins.resize(4);
132+
expect(ins._hideTabs.mock.calls[1][0]).toBe(0);
133+
expect(ins._hideTabs.mock.calls[1][1]).toBe('selectedTabInfo');
134+
expect(ins._hideTabs.mock.calls[1][2]).toBe(true);
135+
});
136+
test('if _validateTabsCount method returns false then it should returns a empty array', () => {
137+
ins._validateTabsCount = jest.fn(() => false);
138+
expect(ins.resize()).toEqual([]);
139+
});
140+
});
141+
describe('hideTabs method : ', () => {
142+
test('it should hide tabs from firstHiddenTabIndex', () => {
143+
ins._validateTabsCount();
144+
const selectedTabInfo = {index: 4};
145+
ins._options.containerElement = {style: {display: ''}};
146+
ins._showBtn = () => {};
147+
ins._hideTabs(5, selectedTabInfo);
148+
expect(ins._tabs[0].style.display === 'none').toBe(false);
149+
expect(ins._tabs[1].style.display === 'none').toBe(false);
150+
expect(ins._tabs[2].style.display === 'none').toBe(false);
151+
expect(ins._tabs[3].style.display === 'none').toBe(false);
152+
expect(ins._tabs[4].style.display === 'none').toBe(false);
153+
expect(ins._tabs[5].style.display === 'none').toBe(true);
154+
expect(ins._tabs[6].style.display === 'none').toBe(true);
155+
});
156+
test('it should return the array of objects including el and index properties', () => {
157+
ins._validateTabsCount();
158+
const selectedTabInfo = {index: 4};
159+
ins._options.containerElement = {style: {display: ''}};
160+
ins._showBtn = () => {};
161+
const result = ins._hideTabs(5, selectedTabInfo);
162+
expect(result.length).toBe(2);
163+
expect(result[0].el).toEqual(ins._tabs[5]);
164+
expect(result[0].index).toBe(5);
165+
expect(result[1].el).toEqual(ins._tabs[6]);
166+
expect(result[1].index).toBe(6);
167+
});
168+
test('it should not consider selected tab if third parameter is not true', () => {
169+
ins._validateTabsCount();
170+
const selectedTabInfo = {index: 4};
171+
ins._options.containerElement = {style: {display: ''}};
172+
ins._showBtn = () => {};
173+
const result = ins._hideTabs(3, selectedTabInfo);
174+
expect(ins._tabs[0].style.display === 'none').toBe(false);
175+
expect(ins._tabs[1].style.display === 'none').toBe(false);
176+
expect(ins._tabs[2].style.display === 'none').toBe(false);
177+
expect(ins._tabs[3].style.display === 'none').toBe(true);
178+
expect(ins._tabs[4].style.display === 'none').toBe(false);
179+
expect(ins._tabs[5].style.display === 'none').toBe(true);
180+
expect(ins._tabs[6].style.display === 'none').toBe(true);
181+
expect(result.length).toBe(3);
182+
expect(result[0].el).toEqual(ins._tabs[3]);
183+
expect(result[0].index).toBe(3);
184+
expect(result[1].el).toEqual(ins._tabs[5]);
185+
expect(result[1].index).toBe(5);
186+
expect(result[2].el).toEqual(ins._tabs[6]);
187+
expect(result[2].index).toBe(6);
188+
});
189+
test('it should consider selected tab if third parameter is true', () => {
190+
ins._validateTabsCount();
191+
const selectedTabInfo = {index: 4};
192+
ins._options.containerElement = {style: {display: ''}};
193+
ins._showBtn = () => {};
194+
const result = ins._hideTabs(3, selectedTabInfo, true);
195+
expect(ins._tabs[0].style.display === 'none').toBe(false);
196+
expect(ins._tabs[1].style.display === 'none').toBe(false);
197+
expect(ins._tabs[2].style.display === 'none').toBe(false);
198+
expect(ins._tabs[3].style.display === 'none').toBe(true);
199+
expect(ins._tabs[4].style.display === 'none').toBe(true);
200+
expect(ins._tabs[5].style.display === 'none').toBe(true);
201+
expect(ins._tabs[6].style.display === 'none').toBe(true);
202+
expect(result.length).toBe(4);
203+
expect(result[0].el).toEqual(ins._tabs[3]);
204+
expect(result[0].index).toBe(3);
205+
expect(result[1].el).toEqual(ins._tabs[4]);
206+
expect(result[1].index).toBe(4);
207+
expect(result[2].el).toEqual(ins._tabs[5]);
208+
expect(result[2].index).toBe(5);
209+
expect(result[3].el).toEqual(ins._tabs[6]);
210+
expect(result[3].index).toBe(6);
211+
});
212+
test('it should call _showBtn method', () => {
213+
ins._validateTabsCount();
214+
const selectedTabInfo = {index: 4};
215+
ins._options.containerElement = {style: {display: ''}};
216+
ins._showBtn = jest.fn(() => {});
217+
ins._hideTabs(3, selectedTabInfo, true);
218+
expect(ins._showBtn.mock.calls.length).toBe(1);
219+
});
220+
});
221+
describe('validateTabsCount method : ', () => {
222+
test('it should set tabs and tabsCount properties', () => {
223+
ins._tablistEl = {children: {length: 2}};
224+
ins._validateTabsCount();
225+
expect(ins._tabs).toEqual(ins._tablistEl.children);
226+
expect(ins._tabsCount).toEqual(ins._tablistEl.children.length);
227+
});
228+
test('it should returns true if tabsCount is greater than zero', () => {
229+
ins._tablistEl = {children: {length: 2}};
230+
expect(ins._validateTabsCount()).toBe(true);
231+
ins._tablistEl = {children: {length: 0}};
232+
expect(ins._validateTabsCount()).toBe(false);
233+
});
234+
});
235+
describe('showAll method : ', () => {
236+
test('it should set display of all tabs into tabDisplay option', () => {
237+
ins._validateTabsCount();
238+
ins._options.containerElement = {style: {display: ''}};
239+
ins._options.tabDisplay = 'flex';
240+
ins._hideBtn = () => {};
241+
ins._showAll();
242+
expect(ins._tabs[0].style.display === 'flex').toBe(true);
243+
expect(ins._tabs[1].style.display === 'flex').toBe(true);
244+
expect(ins._tabs[2].style.display === 'flex').toBe(true);
245+
expect(ins._tabs[3].style.display === 'flex').toBe(true);
246+
expect(ins._tabs[4].style.display === 'flex').toBe(true);
247+
expect(ins._tabs[5].style.display === 'flex').toBe(true);
248+
expect(ins._tabs[6].style.display === 'flex').toBe(true);
249+
});
250+
test('it should call _hideBtn method', () => {
251+
ins._validateTabsCount();
252+
ins._options.containerElement = {style: {display: ''}};
253+
ins._options.tabDisplay = 'flex';
254+
ins._hideBtn = jest.fn(() => {});
255+
ins._showAll();
256+
expect(ins._hideBtn.mock.calls.length).toBe(1);
257+
});
258+
});
259+
describe('findFirstHiddenTabIndexFactory mehtod : ', () => {
260+
test('its default value for returning is the last tab if it wont find it', () => {
261+
ins._validateTabsCount();
262+
expect(ins._findFirstHiddenTabIndexFactory({}, [1, 0], 'asc')).toBe(6);
263+
expect(ins._findFirstHiddenTabIndexFactory({}, [1, 0], 'desc')).toBe(6);
264+
});
265+
test('it should call findFirstHiddenTabIndexASC method if order is asc', () => {
266+
ins._validateTabsCount();
267+
ins._findFirstHiddenTabIndexASC = jest.fn(() => 1);
268+
ins._findFirstHiddenTabIndexDSCE = jest.fn(() => 1);
269+
ins._findFirstHiddenTabIndexFactory({}, [1, 0], 'asc');
270+
expect(ins._findFirstHiddenTabIndexASC.mock.calls.length).toBe(1);
271+
expect(ins._findFirstHiddenTabIndexDSCE.mock.calls.length).toBe(0);
272+
});
273+
test('it should call findFirstHiddenTabIndexDSCE method if order is desc', () => {
274+
ins._validateTabsCount();
275+
ins._findFirstHiddenTabIndexASC = jest.fn(() => 1);
276+
ins._findFirstHiddenTabIndexDSCE = jest.fn(() => 1);
277+
ins._findFirstHiddenTabIndexFactory({}, [1, 0], 'desc');
278+
expect(ins._findFirstHiddenTabIndexASC.mock.calls.length).toBe(0);
279+
expect(ins._findFirstHiddenTabIndexDSCE.mock.calls.length).toBe(1);
280+
});
281+
test('the findFirstHiddenTabIndexASC method should return last tab index if the getTabDis method always returns a positive value', () => {
282+
ins._validateTabsCount();
283+
ins._getTabDis = () => 1;
284+
expect(ins._findFirstHiddenTabIndexASC({}, 0, 10)).toBe(6);
285+
});
286+
test('the findFirstHiddenTabIndexDSCE method should return last tab index if the getTabDis method always returns a positive value', () => {
287+
ins._validateTabsCount();
288+
ins._getTabDis = () => 1;
289+
expect(ins._findFirstHiddenTabIndexDSCE({}, 0, 10)).toBe(6);
290+
});
291+
});
292+
describe('getOrder method : ', () => {
293+
test('it should return asc if : last tab child distance from end-edge of tablist container would greater then tablist container size', () => {
294+
ins._validateTabsCount();
295+
ins.els = {
296+
getDistance: () => ({value: 100}),
297+
getEl: () => ({getSize: () => 99}),
298+
};
299+
expect(ins._getOrder()).toBe('asc');
300+
});
301+
it('it should return desc if : last tab child distance from end-edge of tablist container would equal or less then tablist container size', () => {
302+
ins._validateTabsCount();
303+
ins.els = {
304+
getDistance: () => ({value: 100}),
305+
getEl: () => ({getSize: () => 101}),
306+
};
307+
expect(ins._getOrder()).toBe('desc');
308+
});
309+
});
310+
describe('getSearchBoundries method : ', () => {
311+
test('it should return [0, tabsCount - 2] if there is not any tab selected', () => {
312+
ins._validateTabsCount();
313+
const boundry = ins._getSearchBoundries({index: -1});
314+
expect(boundry[0]).toBe(0); //start value
315+
expect(boundry[1]).toBe(ins._tabsCount - 2); //stop value
316+
});
317+
test('it should return [0, selectedTabIndex - 1] if selected tab is overflow', () => {
318+
ins._validateTabsCount();
319+
const boundry = ins._getSearchBoundries({index: 3, overflowFullSize: 1});
320+
expect(boundry[0]).toBe(0); //start value
321+
expect(boundry[1]).toBe(3 - 1); //stop value
322+
});
323+
test('it should return [selectedTabIndex + 1, tabsCount - 2] if selected tab is not overflow', () => {
324+
ins._validateTabsCount();
325+
const boundry = ins._getSearchBoundries({index: 3, overflowFullSize: 0});
326+
expect(boundry[0]).toBe(3 + 1); //start value
327+
expect(boundry[1]).toBe(ins._tabsCount - 2); //stop value
328+
});
329+
});
330+
describe('hideBtn method : ', () => {
331+
test(`it should not hide the button, because the button size is needed when all tabs are visible
332+
but the button should not be visible`, () => {
333+
ins._options.buttonElement = {
334+
style: {display: 'flex', position: 'relative', opacity: 1, pointerEvents: 'all'},
335+
};
336+
ins._hideBtn();
337+
expect(ins._options.buttonElement.style.display).toBe('flex');
338+
expect(ins._options.buttonElement.style.opacity).toBe(0);
339+
expect(ins._options.buttonElement.style.position).toBe('absolute');
340+
expect(ins._options.buttonElement.style.pointerEvents).toBe('none');
341+
});
342+
});

0 commit comments

Comments
 (0)