Skip to content

Commit 08d849a

Browse files
update optionManager : getOption should return
immutable tabs prop + setOption can not set tabs prop
1 parent fb0c28d commit 08d849a

File tree

2 files changed

+121
-4
lines changed

2 files changed

+121
-4
lines changed

src/utils/api/optionManager/optionManager.factory.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ function OptionManager(getDeps, { options }) {
1111
this.initialTabs = [];
1212
this._setSetting()._setInitialData();
1313
};
14-
OptionManager.prototype.getOption = function (OptionName) {
15-
return this.options[OptionName];
14+
OptionManager.prototype.getOption = function (optionName) {
15+
if (optionName === 'tabs') { // returned result should be immutable
16+
let arr = [];
17+
for (let i = 0, tabs = this.options.tabs, l = tabs.length; i < l; i++) {
18+
arr.push({ ...tabs[i] });
19+
}
20+
return arr;
21+
}
22+
return this.options[optionName];
1623
};
1724
OptionManager.prototype.setOption = function (name = missingParamEr('setOption'), value = missingParamEr('setOption')) {
18-
if (name.toUpperCase() === ('SELECTEDTABID' || 'OPENTABIDS' || 'DATA'))
25+
if (['SELECTEDTABID', 'TABS'].indexOf(name.toUpperCase()) >= 0)
1926
return this;
2027
if (this._defaultOptions.hasOwnProperty(name))
2128
this.options[name] = value;

src/utils/api/optionManager/optionManager.test.js

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,93 @@
11
import OptionManager from './optionManager.js';
22
import React from 'react';
3+
import DefaultOptions from './defaultOptions.js';
4+
import DefaultTabInnerComponent from '../../../tab/defaulTabInner.js';
35
let obj;
46
beforeEach(() => {
5-
const options = {};
7+
const options = {
8+
tabs: [{ id: '1', title: 'a' }, { id: '2', title: 'b' }]
9+
};
610
obj = new (OptionManager)({ options });
711
});
812
afterEach(() => {
913
obj = null;
1014
});
15+
describe('OptionManager constructor : ', () => {
16+
test('it should throw an error when is called with undefined parameter', () => {
17+
expect.assertions(2);
18+
try {
19+
const obj = new (OptionManager)();
20+
} catch (er) {
21+
expect(1).toBe(1);
22+
}
23+
try {
24+
const obj = new (OptionManager)(undefined);
25+
} catch (er) {
26+
expect(1).toBe(1);
27+
}
28+
});
29+
test('object parameter should have options property', () => {
30+
expect.assertions(1);
31+
try {
32+
const obj = new (OptionManager)({});
33+
} catch (er) {
34+
expect(1).toBe(1);
35+
}
36+
});
37+
test('parameter should be type of an object', () => {
38+
expect.assertions(1);
39+
try {
40+
const obj = new (OptionManager)([]);
41+
} catch (er) {
42+
expect(1).toBe(1);
43+
}
44+
});
45+
test('structure of setting property', () => {
46+
const _setting = {
47+
tabClass: 'rc-dyn-tabs-tab',
48+
titleClass: 'rc-dyn-tabs-title',
49+
iconClass: 'rc-dyn-tabs-icon',
50+
selectedClass: 'rc-dyn-tabs-selected',
51+
hoverClass: 'rc-dyn-tabs-hover',
52+
tablistClass: 'rc-dyn-tabs-tablist',
53+
closeClass: 'rc-dyn-tabs-close',
54+
panelClass: 'rc-dyn-tabs-panel',
55+
panellistClass: 'rc-dyn-tabs-panellist',
56+
disableClass: 'rc-dyn-tabs-disable',
57+
ltrClass: 'rc-dyn-tabs-ltr',
58+
rtlClass: 'rc-dyn-tabs-rtl',
59+
panelIdTemplate: expect.any(Function),
60+
ariaLabelledbyIdTemplate: expect.any(Function),
61+
getDefaultTabData: expect.any(Function)
62+
};
63+
expect(obj.setting).toEqual(_setting);
64+
});
65+
test('setting.panelIdTemplate should return result correctly', () => {
66+
expect(obj.setting.panelIdTemplate('2')).toBe('rc-dyn-tabs-p-2');
67+
});
68+
test('setting.ariaLabelledbyIdTemplate should return result correctly', () => {
69+
expect(obj.setting.ariaLabelledbyIdTemplate('2')).toBe('rc-dyn-tabs-l-2');
70+
});
71+
test('setting.getDefaultTabData should return result correctly', () => {
72+
const defaultTabData = obj.setting.getDefaultTabData();
73+
expect(defaultTabData).toEqual({
74+
title: "",
75+
tooltip: "",
76+
panelComponent: obj.options.defaultPanelComponent,
77+
closable: true,
78+
iconClass: "",
79+
disable: false,
80+
id: defaultTabData.id
81+
});
82+
expect(defaultTabData.id.includes('tab_')).toBe(true);
83+
});
84+
});
85+
describe('OptionManager options prop : ', () => {
86+
test('it should be equal to defaultOptions if options parameter is an empty object', () => {
87+
const obj = new (OptionManager)({ options: {} });
88+
expect(obj.options).toEqual(obj._defaultOptions);
89+
});
90+
});
1191
describe('OptionManager.prototype.validateTabData : ', () => {
1292
test('validateTabData method should work correctly with empty object as a parameter', () => {
1393
const tabData = obj.validateTabData({});
@@ -57,5 +137,35 @@ describe('OptionManager.prototype.validateTabData : ', () => {
57137
expect(typeof tabData.id === 'string').toBe(true);
58138
});
59139
});
140+
describe('OptionManager.prototype.setOption : ', () => {
141+
it('it can not set tabs option', () => {
142+
const tabs = obj.getOption('tabs');
143+
obj.setOption('tabs', [{ id: '3', title: 'c' }]);
144+
const newTabs = obj.getOption('tabs');
145+
expect(tabs).toBe(newTabs);
146+
});
147+
it('it can not set selectedTabID option', () => {
148+
const selectedTabID = obj.getOption('selectedTabID');
149+
obj.setOption('selectedTabID', '2');
150+
const newSelectedTabID = obj.getOption('selectedTabID');
151+
expect(selectedTabID).toBe(newSelectedTabID);
152+
});
153+
});
154+
describe('OptionManager.prototype.getOption : ', () => {
155+
it('it returns tabs prop as an immutable array', () => {
156+
const newTitle = 'newTitle';
157+
let oldTitle;
158+
const tabs = obj.getOption('tabs');
159+
oldTitle = tabs[0].title;
160+
tabs[0].title = newTitle;
161+
const newTabs = obj.getOption('tabs');
162+
expect(newTabs[0].title).toBe(oldTitle);
163+
});
164+
it('it returns undefined when it is called with wrong option name', () => {
165+
const result = obj.getOption('Tabs');
166+
expect(result).toBe(undefined);
167+
});
168+
});
169+
60170

61171

0 commit comments

Comments
 (0)