Skip to content

Commit ab4eab2

Browse files
fix test
1 parent b89323a commit ab4eab2

File tree

7 files changed

+136
-52
lines changed

7 files changed

+136
-52
lines changed

src/index.test.js

+62-17
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,19 @@ describe('calling some action inside the events options', () => {
272272
});
273273
describe('select method : ', () => {
274274
test('it should fire onInit event then onChange and then onSelect evnets', () => {
275-
const onSelect = jest.fn(() => {});
275+
const executionOrder = [];
276+
op.onInit = jest.fn(() => {
277+
executionOrder.push('onInit');
278+
});
279+
op.onChange = jest.fn(() => {
280+
executionOrder.push('onChange');
281+
});
282+
op.onSelect = jest.fn(() => {
283+
executionOrder.push('onSelect');
284+
});
285+
const onSelect = jest.fn(() => {
286+
executionOrder.push('onSelect2');
287+
});
276288
renderApp();
277289
act(() => {
278290
instance.one('onSelect', onSelect).select('2');
@@ -284,9 +296,7 @@ describe('select method : ', () => {
284296
currentSelectedTabId: '2',
285297
previousSelectedTabId: '1',
286298
});
287-
expect(op.onInit).toHaveBeenCalledBefore(op.onChange);
288-
expect(op.onChange).toHaveBeenCalledBefore(op.onSelect);
289-
expect(op.onSelect).toHaveBeenCalledBefore(onSelect);
299+
expect(executionOrder.toString()).toBe('onInit,onInit,onChange,onSelect,onSelect2');
290300
});
291301
test('select Promise is resolved with {currentData,instance} parameter after onSelect event', () => {
292302
expect.assertions(6);
@@ -327,7 +337,19 @@ describe('select method : ', () => {
327337
});
328338
describe('close method : ', () => {
329339
test('it should fire onInit event then onChange and then onClose evnets', () => {
330-
const onClose = jest.fn(() => {});
340+
const executionOrder = [];
341+
op.onInit = jest.fn(() => {
342+
executionOrder.push('onInit');
343+
});
344+
op.onChange = jest.fn(() => {
345+
executionOrder.push('onChange');
346+
});
347+
op.onClose = jest.fn(() => {
348+
executionOrder.push('onClose');
349+
});
350+
const onClose = jest.fn(() => {
351+
executionOrder.push('onClose2');
352+
});
331353
renderApp();
332354
act(() => {
333355
instance.one('onClose', onClose).close('2');
@@ -337,9 +359,7 @@ describe('close method : ', () => {
337359
expect(op.onClose.mock.calls.length === 1).toBe(true);
338360
expect(op.onClose.mock.calls[0][0]).toEqual(['2']);
339361
expect(onClose.mock.calls[0][0]).toEqual(['2']);
340-
expect(op.onInit).toHaveBeenCalledBefore(op.onChange);
341-
expect(op.onChange).toHaveBeenCalledBefore(op.onClose);
342-
expect(op.onClose).toHaveBeenCalledBefore(onClose);
362+
expect(executionOrder.toString()).toBe('onInit,onInit,onChange,onClose,onClose2');
343363
});
344364
test('close Promise is resolved with {currentData,instance} parameter after onClose event', () => {
345365
expect.assertions(6);
@@ -368,7 +388,19 @@ describe('close method : ', () => {
368388
});
369389
describe('open method : ', () => {
370390
test('it should fire onInit event then onChange and then onOpen evnets', () => {
371-
const onOpen = jest.fn(() => {});
391+
const executionOrder = [];
392+
op.onInit = jest.fn(() => {
393+
executionOrder.push('onInit');
394+
});
395+
op.onChange = jest.fn(() => {
396+
executionOrder.push('onChange');
397+
});
398+
op.onOpen = jest.fn(() => {
399+
executionOrder.push('onOpen');
400+
});
401+
const onOpen = jest.fn(() => {
402+
executionOrder.push('onOpen2');
403+
});
372404
renderApp();
373405
act(() => {
374406
instance.one('onOpen', onOpen).open({
@@ -385,9 +417,7 @@ describe('open method : ', () => {
385417
expect(op.onOpen.mock.calls.length === 1).toBe(true);
386418
expect(op.onOpen.mock.calls[0][0]).toEqual(['3', '4']);
387419
expect(onOpen.mock.calls[0][0]).toEqual(['3', '4']);
388-
expect(op.onInit).toHaveBeenCalledBefore(op.onChange);
389-
expect(op.onChange).toHaveBeenCalledBefore(op.onOpen);
390-
expect(op.onOpen).toHaveBeenCalledBefore(onOpen);
420+
expect(executionOrder.toString()).toBe('onInit,onInit,onChange,onOpen,onOpen2');
391421
});
392422
test('open Promise is resolved with {currentData,instance} parameter after onOpen event', () => {
393423
expect.assertions(5);
@@ -498,15 +528,23 @@ describe('ready function : ', () => {
498528
});
499529
});
500530
test('it calls its function parameter after onLoad event and first execution of onInit event', () => {
501-
expect.assertions(6);
502-
const onReady = jest.fn(() => {});
531+
expect.assertions(5);
532+
const executionOrder = [];
533+
const onReady = jest.fn(() => {
534+
executionOrder.push('onReady');
535+
});
536+
op.onLoad = jest.fn(() => {
537+
executionOrder.push('onLoad');
538+
});
539+
op.onInit = jest.fn(() => {
540+
executionOrder.push('onInit');
541+
});
503542
renderApp();
504543
ready(onReady);
505544
expect(op.onLoad.mock.calls.length === 1).toBe(true);
506545
expect(op.onInit.mock.calls.length === 1).toBe(true);
507546
expect(onReady.mock.calls.length === 1).toBe(true);
508-
expect(op.onLoad).toHaveBeenCalledBefore(op.onInit);
509-
expect(op.onInit).toHaveBeenCalledBefore(onReady);
547+
expect(executionOrder.toString()).toBe('onLoad,onInit,onReady');
510548
expect(op.onChange.mock.calls.length).toBe(0);
511549
});
512550
});
@@ -685,6 +723,13 @@ describe('onFirstSelect callback : ', () => {
685723
expect(op.onFirstSelect.mock.calls.length).toBe(0);
686724
});
687725
test('it is triggered at most once per each tab, before onSelect event. if the tab has not been selected yet', () => {
726+
const executionOrder = [];
727+
op.onFirstSelect = jest.fn(() => {
728+
executionOrder.push('onFirstSelect');
729+
});
730+
op.onSelect = jest.fn(() => {
731+
executionOrder.push('onSelect');
732+
});
688733
renderApp();
689734
expect(op.onFirstSelect.mock.calls.length).toBe(0);
690735
expect(op.onSelect.mock.calls.length).toBe(0);
@@ -693,7 +738,7 @@ describe('onFirstSelect callback : ', () => {
693738
});
694739
expect(op.onFirstSelect.mock.calls.length).toBe(1);
695740
expect(op.onSelect.mock.calls.length).toBe(1);
696-
expect(op.onFirstSelect).toHaveBeenCalledBefore(op.onSelect);
741+
expect(executionOrder.toString()).toBe('onFirstSelect,onSelect');
697742
act(() => {
698743
instance.select('1');
699744
});

src/plugins/moreButtonPlugin/__snapshots__/setComponents.test.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
exports[`showMorePlugin MoreButtonPlugin : 1`] = `
44
<div
5-
ctx={Object {}}
5+
ctx={{}}
66
customProp="customProp"
77
openTabIDs={
8-
Array [
8+
[
99
"1",
1010
"2",
1111
]

src/plugins/moreButtonPlugin/button/__snapshots__/button.test.js.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`DISABLE ACCESIBILITY : DEFAULT: 1`] = `
88
>
99
<div
1010
instance={
11-
Object {
11+
{
1212
"close": [Function],
1313
"getCopyData": [Function],
1414
"getCopyPerviousData": [Function],
@@ -50,7 +50,7 @@ exports[`Enable Accesibility : DEFAULT: 1`] = `
5050
>
5151
<div
5252
instance={
53-
Object {
53+
{
5454
"close": [Function],
5555
"getCopyData": [Function],
5656
"getCopyPerviousData": [Function],
@@ -92,7 +92,7 @@ exports[`Enable Accesibility : MULTIPLE BUTTON SHOULD NOT HAVE SAME ID: 1`] = `
9292
>
9393
<div
9494
instance={
95-
Object {
95+
{
9696
"close": [Function],
9797
"getCopyData": [Function],
9898
"getCopyPerviousData": [Function],
@@ -129,7 +129,7 @@ exports[`Enable Accesibility : MULTIPLE BUTTON SHOULD NOT HAVE SAME ID: 1`] = `
129129
>
130130
<div
131131
instance={
132-
Object {
132+
{
133133
"close": [Function],
134134
"getCopyData": [Function],
135135
"getCopyPerviousData": [Function],

src/plugins/moreButtonPlugin/setComponents.test.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,23 @@ describe('showMorePlugin ', () => {
9595
expect(ctx.optionsManager.setting.showMorePopperClass).toBe('rc-dyn-tabs-popper');
9696
});
9797
test('constructor : ', () => {
98+
const executionOrder = [];
9899
showMorePlugin.ShowMoreButton = jest.fn(() => {});
99-
showMorePlugin.setMoreButtonPlugin = jest.fn(() => {});
100-
showMorePlugin.setTablistOverflow = jest.fn(() => {});
101-
showMorePlugin.setTablistView = jest.fn(() => {});
102-
showMorePlugin.setDefaultOptions = jest.fn(() => {});
103-
showMorePlugin.setSetting = jest.fn(() => {});
100+
showMorePlugin.setMoreButtonPlugin = jest.fn(() => {
101+
executionOrder.push('setMoreButtonPlugin');
102+
});
103+
showMorePlugin.setTablistOverflow = jest.fn(() => {
104+
executionOrder.push('setTablistOverflow');
105+
});
106+
showMorePlugin.setTablistView = jest.fn(() => {
107+
executionOrder.push('setTablistView');
108+
});
109+
showMorePlugin.setDefaultOptions = jest.fn(() => {
110+
executionOrder.push('setDefaultOptions');
111+
});
112+
showMorePlugin.setSetting = jest.fn(() => {
113+
executionOrder.push('setSetting');
114+
});
104115
const deps = {
105116
moreButtonPlugin_buttonComponent: 'moreButtonPlugin_buttonComponent',
106117
ShowMoreTabs: 'ShowMoreTabs',
@@ -114,10 +125,9 @@ describe('showMorePlugin ', () => {
114125
expect(showMorePlugin.setTablistView.mock.calls.length).toBe(1);
115126
expect(showMorePlugin.setMoreButtonPlugin.mock.calls.length).toBe(1);
116127
expect(showMorePlugin.setTablistOverflow.mock.calls.length).toBe(1);
117-
expect(showMorePlugin.setSetting).toHaveBeenCalledBefore(showMorePlugin.setDefaultOptions);
118-
expect(showMorePlugin.setDefaultOptions).toHaveBeenCalledBefore(showMorePlugin.setTablistView);
119-
expect(showMorePlugin.setTablistView).toHaveBeenCalledBefore(showMorePlugin.setMoreButtonPlugin);
120-
expect(showMorePlugin.setMoreButtonPlugin).toHaveBeenCalledBefore(showMorePlugin.setTablistOverflow);
128+
expect(executionOrder.toString()).toBe(
129+
'setSetting,setDefaultOptions,setTablistView,setMoreButtonPlugin,setTablistOverflow',
130+
);
121131
expect(showMorePlugin.setMoreButtonPlugin.mock.calls[0][3]).toEqual(showMorePlugin.ShowMoreButton);
122132
});
123133
});

src/plugins/moreButtonPlugin/show-more-tabs/__snapshots__/show-more-tabs.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exports[`BUTTON CONTAINER STRUCTURE : default button 1`] = `
77
<div
88
hiddenTabIDs=""
99
instance={
10-
Object {
10+
{
1111
"close": [Function],
1212
"getCopyData": [Function],
1313
"getCopyPerviousData": [Function],

src/plugins/moreButtonPlugin/show-more-tabs/show-more-tabs.test.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,16 @@ describe('button component : ', () => {
310310
});
311311
describe('mounting : ', () => {
312312
test('at first the setResizer method should be called then installResizer and then resize method', () => {
313-
const installResizer = jest.fn(() => {});
314-
const resize = jest.fn(() => {});
315-
const setResizer = jest.fn(() => {});
313+
const executionOrder = [];
314+
const installResizer = jest.fn(() => {
315+
executionOrder.push('installResizer');
316+
});
317+
const resize = jest.fn(() => {
318+
executionOrder.push('resize');
319+
});
320+
const setResizer = jest.fn(() => {
321+
executionOrder.push('setResizer');
322+
});
316323
renderApp(
317324
false,
318325
{
@@ -327,8 +334,7 @@ describe('mounting : ', () => {
327334
resize,
328335
},
329336
);
330-
expect(setResizer).toHaveBeenCalledBefore(installResizer);
331-
expect(installResizer).toHaveBeenCalledBefore(resize);
337+
expect(executionOrder.toString()).toBe('setResizer,installResizer,resize');
332338
expect(resize.mock.calls.length).toBe(1);
333339
});
334340
});

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

+37-14
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,23 @@ describe('Api Contructor : ', () => {
5656
});
5757
describe('Api.prototype.open : ', () => {
5858
test('it should call optionsManager.validateTabData internally', () => {
59+
const executionOrder = [];
5960
Object.assign(obj, {
60-
_getFlushEffectsPromise: jest.fn(() => Promise),
61-
_open: jest.fn(() => {}),
61+
_getFlushEffectsPromise: jest.fn(() => {
62+
executionOrder.push('_getFlushEffectsPromise');
63+
return Promise;
64+
}),
65+
_open: jest.fn(() => {
66+
executionOrder.push('_open');
67+
}),
68+
});
69+
obj.optionsManager.validateTabData = jest.fn((data) => {
70+
executionOrder.push('validateTabData');
71+
return data;
6272
});
63-
obj.optionsManager.validateTabData = jest.fn((data) => data);
6473
obj.open({id: '2'});
6574
expect(obj.optionsManager.validateTabData.mock.calls.length === 1).toBe(true);
66-
expect(obj.optionsManager.validateTabData).toHaveBeenCalledBefore(obj._getFlushEffectsPromise);
67-
expect(obj._getFlushEffectsPromise).toHaveBeenCalledBefore(obj._open);
75+
expect(executionOrder.toString()).toBe('validateTabData,_getFlushEffectsPromise,_open');
6876
});
6977
test('it throws an error if is called with falsy value of id parameter', () => {
7078
expect.assertions(3);
@@ -101,11 +109,16 @@ describe('Api.prototype.close : ', () => {
101109
});
102110
test('it will call select function internally if switch parameter was true and tab was already opended and selected', () => {
103111
expect.assertions(3);
112+
const executionOrder = [];
104113
Object.assign(obj, {
105114
__close: jest.fn(() => {
115+
executionOrder.push('__close');
106116
return Promise.resolve({currentData: {}, instance: {}});
107117
}),
108-
select: jest.fn(() => Promise),
118+
select: jest.fn(() => {
119+
executionOrder.push('select');
120+
return Promise;
121+
}),
109122
isOpen: jest.fn(() => true),
110123
stateRef: {openTabIDs: ['1', '2'], selectedTabID: '2'},
111124
_findTabIdForSwitching: jest.fn(() => '1'),
@@ -117,16 +130,21 @@ describe('Api.prototype.close : ', () => {
117130
Object.prototype.hasOwnProperty.call(result, 'instance'),
118131
).toBe(true);
119132
expect(obj.__close.mock.calls.length === 1).toBe(true);
120-
expect(obj.select).toHaveBeenCalledBefore(obj.__close);
133+
expect(executionOrder.toString()).toBe('select,__close');
121134
});
122135
});
123136
test('switch parameter default value is true', () => {
124137
expect.assertions(3);
138+
const executionOrder = [];
125139
Object.assign(obj, {
126140
__close: jest.fn(() => {
141+
executionOrder.push('__close');
127142
return Promise.resolve({currentData: {}, instance: {}});
128143
}),
129-
select: jest.fn(() => Promise),
144+
select: jest.fn(() => {
145+
executionOrder.push('select');
146+
return Promise;
147+
}),
130148
isOpen: jest.fn(() => true),
131149
stateRef: {openTabIDs: ['1', '2'], selectedTabID: '2'},
132150
_findTabIdForSwitching: jest.fn(() => '1'),
@@ -138,7 +156,7 @@ describe('Api.prototype.close : ', () => {
138156
Object.prototype.hasOwnProperty.call(result, 'instance'),
139157
).toBe(true);
140158
expect(obj.__close.mock.calls.length === 1).toBe(true);
141-
expect(obj.select).toHaveBeenCalledBefore(obj.__close);
159+
expect(executionOrder.toString()).toBe('select,__close');
142160
});
143161
});
144162
test('it will not call select function internally if switch parameter was false', () => {
@@ -448,12 +466,17 @@ describe('Api.prototype.getPreviousData and Api.prototype.getData : ', () => {
448466
});
449467
describe('Api.prototype.setTab : ', () => {
450468
test('it should call optionsManager.validateObjectiveTabData and validatePanelComponent internally', () => {
451-
obj.optionsManager.validateObjectiveTabData = jest.fn(() => obj.optionsManager);
452-
obj.optionsManager.validatePanelComponent = jest.fn(() => obj.optionsManager);
469+
const executionOrder = [];
470+
obj.optionsManager.validateObjectiveTabData = jest.fn(() => {
471+
executionOrder.push('validateObjectiveTabData');
472+
return obj.optionsManager;
473+
});
474+
obj.optionsManager.validatePanelComponent = jest.fn(() => {
475+
executionOrder.push('validatePanelComponent');
476+
return obj.optionsManager;
477+
});
453478
obj.setTab('1', {title: 'c'});
454-
expect(obj.optionsManager.validateObjectiveTabData).toHaveBeenCalledBefore(
455-
obj.optionsManager.validatePanelComponent,
456-
);
479+
expect(executionOrder.toString()).toBe('validateObjectiveTabData,validatePanelComponent');
457480
});
458481
test('it should return the context', () => {
459482
obj.optionsManager.validateObjectiveTabData = jest.fn(() => obj.optionsManager);

0 commit comments

Comments
 (0)