Skip to content

Commit 50939ac

Browse files
update trigger function
1 parent cf134d1 commit 50939ac

File tree

7 files changed

+165
-48
lines changed

7 files changed

+165
-48
lines changed

package-lock.json

+94-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.test.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,7 @@ describe('onChange callback : ', () => {
541541
currentData.selectedTabID = '6';
542542
previousData.selectedTabID = '6';
543543
});
544-
const onChange2 = jest.fn(({currentData, previousData, closedTabIDs, openedTabIDs}) => {
545-
closedTabIDs.push('7');
546-
openedTabIDs.push('7');
547-
currentData.selectedTabID = '7';
548-
previousData.selectedTabID = '7';
549-
});
544+
const onChange2 = jest.fn(({currentData, previousData, closedTabIDs, openedTabIDs}) => {});
550545
const onOpen = jest.fn(() => {});
551546
const onClose = jest.fn(() => {});
552547
const onSelect = jest.fn(() => {});
@@ -595,6 +590,9 @@ describe('onChange callback : ', () => {
595590
expect(this.hasOwnProperty('getData')).toBe(true);
596591
});
597592
renderApp();
593+
act(() => {
594+
instance.select('2');
595+
});
598596
op.onChange = function () {};
599597
});
600598
});
@@ -620,9 +618,7 @@ describe('onSelect callback : ', () => {
620618
const onSelect1 = jest.fn((param) => {
621619
param.previousSelectedTabId = 19;
622620
});
623-
const onSelect2 = jest.fn((param) => {
624-
param.previousSelectedTabId = 20;
625-
});
621+
const onSelect2 = jest.fn((param) => {});
626622
act(() => {
627623
instance.setOption('onSelect', (param) => {
628624
param.currentSelectedTabId = 10;
@@ -644,6 +640,9 @@ describe('onSelect callback : ', () => {
644640
expect(this.hasOwnProperty('getData')).toBe(true);
645641
});
646642
renderApp();
643+
act(() => {
644+
instance.select('2');
645+
});
647646
op.onSelect = function () {};
648647
});
649648
});
@@ -686,9 +685,7 @@ describe('onFirstSelect callback : ', () => {
686685
const onFirstSelect1 = jest.fn((param) => {
687686
param.previousSelectedTabId = 19;
688687
});
689-
const onFirstSelect2 = jest.fn((param) => {
690-
param.previousSelectedTabId = 20;
691-
});
688+
const onFirstSelect2 = jest.fn((param) => {});
692689
act(() => {
693690
instance.setOption('onFirstSelect', (param) => {
694691
param.currentSelectedTabId = 10;
@@ -709,6 +706,9 @@ describe('onFirstSelect callback : ', () => {
709706
expect(this.hasOwnProperty('getData')).toBe(true);
710707
});
711708
renderApp();
709+
act(() => {
710+
instance.select('2');
711+
});
712712
op.onFirstSelect = function () {};
713713
});
714714
});
@@ -730,9 +730,7 @@ describe('onOpen callback : ', () => {
730730
const onOpen1 = jest.fn((openedTabIDs) => {
731731
openedTabIDs.push('5');
732732
});
733-
const onOpen2 = jest.fn((openedTabIDs) => {
734-
openedTabIDs.push('6');
735-
});
733+
const onOpen2 = jest.fn((openedTabIDs) => {});
736734
act(() => {
737735
instance.setOption('onOpen', (openedTabIDs) => {
738736
openedTabIDs.push('4');
@@ -750,6 +748,9 @@ describe('onOpen callback : ', () => {
750748
expect(this.hasOwnProperty('getData')).toBe(true);
751749
});
752750
renderApp();
751+
act(() => {
752+
instance.open({id:'3'});
753+
});
753754
op.onOpen = function () {};
754755
});
755756
});
@@ -771,9 +772,7 @@ describe('onClose callback : ', () => {
771772
const onClose1 = jest.fn((closedTabIDs) => {
772773
closedTabIDs.push('4');
773774
});
774-
const onClose2 = jest.fn((closedTabIDs) => {
775-
closedTabIDs.push('5');
776-
});
775+
const onClose2 = jest.fn((closedTabIDs) => {});
777776
act(() => {
778777
instance.setOption('onClose', (closedTabIDs) => {
779778
closedTabIDs.push('3');
@@ -791,6 +790,9 @@ describe('onClose callback : ', () => {
791790
expect(this.hasOwnProperty('getData')).toBe(true);
792791
});
793792
renderApp();
793+
act(() => {
794+
instance.close('2');
795+
});
794796
op.onClose = function () {};
795797
});
796798
});

src/useDynamicTabs/useDynamicTabs.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ function useDynamicTabs(getDeps, options = {}) {
3333
api.onChange({newState: state, oldState, closedTabIDs, openedTabIDs, isSwitched});
3434
}, [state]);
3535
useLayoutEffect(() => {
36-
api.trigger('_onFlushEffects', api.userProxy, {currentData: api.getData(), instance: api.userProxy});
36+
api.trigger('_onFlushEffects', api.userProxy, () => {
37+
return [{currentData: api.getData(), instance: api.userProxy}];
38+
});
3739
}, [flushState]);
3840
if (!_ref.TabListComponent)
3941
_ref.TabListComponent = function TabListComponent(props = {}) {

src/utils/api/api.factory.js

+33-15
Original file line numberDiff line numberDiff line change
@@ -176,26 +176,44 @@ const _apiProps = {
176176
Helper.setNoneEnumProps(_apiProps, {
177177
onChange: function ({newState, oldState, closedTabIDs, openedTabIDs, isSwitched}) {
178178
if (isSwitched || openedTabIDs.length || closedTabIDs.length) {
179-
this.trigger('onChange', this.userProxy, {
180-
currentData: {...newState},
181-
previousData: {...oldState},
182-
perviousData: {...oldState},
183-
closedTabIDs,
184-
openedTabIDs,
179+
this.trigger('onChange', this.userProxy, () => {
180+
return [
181+
{
182+
currentData: this.helper.getCopyState(newState),
183+
previousData: this.helper.getCopyState(oldState),
184+
perviousData: this.helper.getCopyState(oldState),
185+
closedTabIDs: closedTabIDs.slice(),
186+
openedTabIDs: openedTabIDs.slice(),
187+
},
188+
];
185189
});
186-
openedTabIDs.length && this.trigger('onOpen', this.userProxy, openedTabIDs);
187-
closedTabIDs.length && this.trigger('onClose', this.userProxy, closedTabIDs);
190+
openedTabIDs.length &&
191+
this.trigger('onOpen', this.userProxy, () => {
192+
return [openedTabIDs.slice()];
193+
});
194+
closedTabIDs.length &&
195+
this.trigger('onClose', this.userProxy, () => {
196+
return [closedTabIDs.slice()];
197+
});
188198
if (isSwitched) {
189199
if (newState.selectedTabID && this.activedTabsHistory.tabsId.indexOf(newState.selectedTabID) === -1) {
190-
this.trigger('onFirstSelect', this.userProxy, {
191-
currentSelectedTabId: newState.selectedTabID,
192-
previousSelectedTabId: oldState.selectedTabID,
200+
this.trigger('onFirstSelect', this.userProxy, () => {
201+
return [
202+
{
203+
currentSelectedTabId: newState.selectedTabID,
204+
previousSelectedTabId: oldState.selectedTabID,
205+
},
206+
];
193207
});
194208
}
195-
this.trigger('onSelect', this.userProxy, {
196-
currentSelectedTabId: newState.selectedTabID,
197-
previousSelectedTabId: oldState.selectedTabID,
198-
perviousSelectedTabId: oldState.selectedTabID,
209+
this.trigger('onSelect', this.userProxy, () => {
210+
return [
211+
{
212+
currentSelectedTabId: newState.selectedTabID,
213+
previousSelectedTabId: oldState.selectedTabID,
214+
perviousSelectedTabId: oldState.selectedTabID,
215+
},
216+
];
199217
});
200218
}
201219
}

src/utils/api/api.factory.test.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ describe('Api.prototype._subscribeSelectedTabsHistory : ', () => {
441441
const currentData = {selectedTabID: '2', openTabIDs: ['2']};
442442
const previousData = {selectedTabID: '2', openTabIDs: ['2']};
443443
const closedTabIDs = ['1', '3'];
444-
obj.trigger('onChange', obj.userProxy, {currentData, previousData, closedTabIDs, openTabIDs: []});
444+
obj.trigger('onChange', obj.userProxy, () => {
445+
return [{currentData, previousData, closedTabIDs, openTabIDs: []}];
446+
});
445447
expect(obj.activedTabsHistory.remove.mock.calls.length).toBe(2);
446448
expect(obj.activedTabsHistory.remove.mock.calls[0][0]).toBe('1');
447449
expect(obj.activedTabsHistory.remove.mock.calls[1][0]).toBe('3');
@@ -459,7 +461,9 @@ describe('Api.prototype._subscribeSelectedTabsHistory : ', () => {
459461
});
460462
const currentData = {selectedTabID: '2', openTabIDs: ['1', '2']};
461463
const previousData = {selectedTabID: '1', openTabIDs: ['1', '2']};
462-
obj.trigger('onChange', obj.userProxy, {currentData, previousData, closedTabIDs: [], openTabIDs: []});
464+
obj.trigger('onChange', obj.userProxy, () => {
465+
return [{currentData, previousData, closedTabIDs: [], openTabIDs: []}];
466+
});
463467
expect(obj.activedTabsHistory.add.mock.calls.length).toBe(1);
464468
expect(obj.activedTabsHistory.add.mock.calls[0][0]).toBe('1');
465469
});
@@ -526,10 +530,12 @@ describe('Api.prorotype.onChange : ', () => {
526530
expect(obj.trigger.mock.calls[1][0]).toBe('onFirstSelect');
527531
expect(obj.trigger.mock.calls[2][0]).toBe('onSelect');
528532
expect(obj.trigger.mock.calls[1][1]).toBe(obj.userProxy);
529-
expect(obj.trigger.mock.calls[1][2]).toEqual({
530-
currentSelectedTabId: '2',
531-
previousSelectedTabId: '1',
532-
});
533+
expect(obj.trigger.mock.calls[1][2]()).toEqual([
534+
{
535+
currentSelectedTabId: '2',
536+
previousSelectedTabId: '1',
537+
},
538+
]);
533539
});
534540
it('it should not trigger onFirstSelect if activedTabsHistory.tabsId includes new selected tab id', () => {
535541
obj.trigger = jest.fn(() => {});

src/utils/api/pub_sub.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ Pub_Sub.prototype.one = function (publisherName, fn) {
4343
return this;
4444
};
4545
helper.setNoneEnumProps(Pub_Sub.prototype, {
46-
trigger: function (publisherName, context, ...param) {
46+
trigger: function (publisherName, context, generateParamsCallback = () => []) {
4747
context = context || null;
4848
const result = [];
4949
const _subscribers = [...this._publishers[publisherName]];
5050
_subscribers.map((subscriber) => {
51-
result.push(subscriber.apply(context, param));
51+
result.push(subscriber.apply(context, generateParamsCallback()));
5252
});
5353
return result;
5454
},

src/utils/helper.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ helper.getInstance = function (Fn) {
55
};
66
helper.resolve = (result) => Promise.resolve(result);
77
helper.getCopyState = function (state) {
8-
if (!Object.prototype.hasOwnProperty.call(state, 'openTabIDs')) state.openTabIDs = [];
98
return {
109
selectedTabID: state.selectedTabID,
11-
openTabIDs: [...state.openTabIDs],
10+
openTabIDs: (state.openTabIDs || []).slice(),
1211
};
1312
};
1413
helper.assingAll = function (targetObj, ...sourcObjs) {

0 commit comments

Comments
 (0)