Skip to content

Commit cf134d1

Browse files
update tests : code refactoring + make them clean
1 parent bfb1306 commit cf134d1

File tree

3 files changed

+196
-161
lines changed

3 files changed

+196
-161
lines changed

src/index.test.js

+190-81
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,43 @@ describe('ready function : ', () => {
472472
expect(op.onChange.mock.calls.length).toBe(0);
473473
});
474474
});
475-
describe('initial events : ', () => {
476-
test('onLoad and then onInit are called initially but onChange is not called', () => {
475+
describe('onLoad callback : ', () => {
476+
test('onLoad callback should be called once time initially without any parameters', () => {
477477
renderApp();
478478
expect(op.onLoad.mock.calls.length).toBe(1);
479+
expect(op.onLoad.mock.calls[0].length).toBe(0);
480+
act(() => {
481+
instance.select('3');
482+
instance.close('1');
483+
instance.open({id: '3'});
484+
instance.refresh();
485+
});
486+
expect(op.onLoad.mock.calls.length).toBe(1);
487+
});
488+
test('checking context of onLoad callback', () => {
489+
expect.assertions(2);
490+
op.onLoad = jest.fn(function () {
491+
expect(Object.prototype.toString.call(this)).toBe('[object Object]');
492+
expect(this.hasOwnProperty('getData')).toBe(true);
493+
});
494+
renderApp();
495+
op.onLoad = function () {};
496+
});
497+
});
498+
describe('onInit callback : ', () => {
499+
test('onInit callback should be called initially without any parameters', () => {
500+
renderApp();
479501
expect(op.onInit.mock.calls.length).toBe(1);
480-
expect(op.onChange.mock.calls.length).toBe(0);
481-
expect(op.onLoad).toHaveBeenCalledBefore(op.onInit);
502+
expect(op.onInit.mock.calls[0].length).toBe(0);
503+
});
504+
test('checking context of onInit callback', () => {
505+
expect.assertions(2);
506+
op.onInit = jest.fn(function () {
507+
expect(Object.prototype.toString.call(this)).toBe('[object Object]');
508+
expect(this.hasOwnProperty('getData')).toBe(true);
509+
});
510+
renderApp();
511+
op.onInit = function () {};
482512
});
483513
});
484514
describe('onChange callback : ', () => {
@@ -499,6 +529,74 @@ describe('onChange callback : ', () => {
499529
expect(op.onChange.mock.calls[0][0].currentData).toEqual(instance.getData());
500530
expect(op.onChange.mock.calls[0][0].previousData).toEqual(instance.getPreviousData());
501531
});
532+
test('it is not called initially', () => {
533+
renderApp();
534+
expect(op.onChange.mock.calls.length).toBe(0);
535+
});
536+
test('onChange parameters should be immutable', () => {
537+
renderApp();
538+
const onChange1 = jest.fn(({currentData, previousData, closedTabIDs, openedTabIDs}) => {
539+
closedTabIDs.push('6');
540+
openedTabIDs.push('6');
541+
currentData.selectedTabID = '6';
542+
previousData.selectedTabID = '6';
543+
});
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+
});
550+
const onOpen = jest.fn(() => {});
551+
const onClose = jest.fn(() => {});
552+
const onSelect = jest.fn(() => {});
553+
const onFirstSelect = jest.fn(() => {});
554+
act(() => {
555+
instance.setOption('onChange', ({currentData, previousData, closedTabIDs, openedTabIDs}) => {
556+
closedTabIDs.push('5');
557+
openedTabIDs.push('5');
558+
currentData.selectedTabID = '5';
559+
previousData.selectedTabID = '5';
560+
});
561+
instance.one('onChange', onChange1);
562+
instance.on('onChange', onChange2);
563+
instance.on('onOpen', onOpen);
564+
instance.on('onClose', onClose);
565+
instance.on('onSelect', onSelect);
566+
instance.on('onFirstSelect', onFirstSelect);
567+
instance.close('2');
568+
instance.open({id: '3'});
569+
instance.select('3');
570+
});
571+
572+
expect(onSelect.mock.calls[0][0]).toEqual({
573+
currentSelectedTabId: '3',
574+
perviousSelectedTabId: '1',
575+
previousSelectedTabId: '1',
576+
});
577+
expect(onFirstSelect.mock.calls[0][0]).toEqual({
578+
currentSelectedTabId: '3',
579+
previousSelectedTabId: '1',
580+
});
581+
expect(onClose.mock.calls[0][0]).toEqual(['2']);
582+
expect(onOpen.mock.calls[0][0]).toEqual(['3']);
583+
expect(onChange2.mock.calls[0][0]).toEqual({
584+
currentData: {selectedTabID: '3', openTabIDs: ['1', '3']},
585+
previousData: {selectedTabID: '1', openTabIDs: ['1', '2']},
586+
perviousData: {selectedTabID: '1', openTabIDs: ['1', '2']},
587+
closedTabIDs: ['2'],
588+
openedTabIDs: ['3'],
589+
});
590+
});
591+
test('checking context of onChange callback', () => {
592+
expect.assertions(2);
593+
op.onChange = jest.fn(function () {
594+
expect(Object.prototype.toString.call(this)).toBe('[object Object]');
595+
expect(this.hasOwnProperty('getData')).toBe(true);
596+
});
597+
renderApp();
598+
op.onChange = function () {};
599+
});
502600
});
503601
describe('onSelect callback : ', () => {
504602
test('onSelect is called with {currentSelectedTabId,previousSelectedTabId,perviousSelectedTabId} object as a parameter', () => {
@@ -513,6 +611,41 @@ describe('onSelect callback : ', () => {
513611
previousSelectedTabId: '1',
514612
});
515613
});
614+
test('it is not called initially', () => {
615+
renderApp();
616+
expect(op.onSelect.mock.calls.length).toBe(0);
617+
});
618+
test('onSelect parameters should be immutable', () => {
619+
renderApp();
620+
const onSelect1 = jest.fn((param) => {
621+
param.previousSelectedTabId = 19;
622+
});
623+
const onSelect2 = jest.fn((param) => {
624+
param.previousSelectedTabId = 20;
625+
});
626+
act(() => {
627+
instance.setOption('onSelect', (param) => {
628+
param.currentSelectedTabId = 10;
629+
});
630+
instance.on('onSelect', onSelect1);
631+
instance.on('onSelect', onSelect2);
632+
instance.select('2');
633+
});
634+
expect(onSelect2.mock.calls[0][0]).toEqual({
635+
currentSelectedTabId: '2',
636+
perviousSelectedTabId: '1',
637+
previousSelectedTabId: '1',
638+
});
639+
});
640+
test('checking context of onSelect callback', () => {
641+
expect.assertions(2);
642+
op.onSelect = jest.fn(function () {
643+
expect(Object.prototype.toString.call(this)).toBe('[object Object]');
644+
expect(this.hasOwnProperty('getData')).toBe(true);
645+
});
646+
renderApp();
647+
op.onSelect = function () {};
648+
});
516649
});
517650
describe('onFirstSelect callback : ', () => {
518651
test('it is not triggered initially', () => {
@@ -548,30 +681,6 @@ describe('onFirstSelect callback : ', () => {
548681
previousSelectedTabId: '1',
549682
});
550683
});
551-
});
552-
describe('callbacks should be called with immutable parameters : ', () => {
553-
test('onSelect parameters should be immutable', () => {
554-
renderApp();
555-
const onSelect1 = jest.fn((param) => {
556-
param.previousSelectedTabId = 19;
557-
});
558-
const onSelect2 = jest.fn((param) => {
559-
param.previousSelectedTabId = 20;
560-
});
561-
act(() => {
562-
instance.setOption('onSelect', (param) => {
563-
param.currentSelectedTabId = 10;
564-
});
565-
instance.on('onSelect', onSelect1);
566-
instance.on('onSelect', onSelect2);
567-
instance.select('2');
568-
});
569-
expect(onSelect2.mock.calls[0][0]).toEqual({
570-
currentSelectedTabId: '2',
571-
perviousSelectedTabId: '1',
572-
previousSelectedTabId: '1',
573-
});
574-
});
575684
test('onFirstSelect parameters should be immutable', () => {
576685
renderApp();
577686
const onFirstSelect1 = jest.fn((param) => {
@@ -593,6 +702,29 @@ describe('callbacks should be called with immutable parameters : ', () => {
593702
previousSelectedTabId: '1',
594703
});
595704
});
705+
test('checking context of onFirstSelect callback', () => {
706+
expect.assertions(2);
707+
op.onFirstSelect = jest.fn(function () {
708+
expect(Object.prototype.toString.call(this)).toBe('[object Object]');
709+
expect(this.hasOwnProperty('getData')).toBe(true);
710+
});
711+
renderApp();
712+
op.onFirstSelect = function () {};
713+
});
714+
});
715+
describe('onOpen callback : ', () => {
716+
test('checking onOpen parameters', () => {
717+
renderApp();
718+
act(() => {
719+
instance.open({id: '3'});
720+
});
721+
expect(op.onOpen.mock.calls.length).toBe(1);
722+
expect(op.onOpen.mock.calls[0][0]).toEqual(['3']);
723+
});
724+
test('it is not called initially', () => {
725+
renderApp();
726+
expect(op.onOpen.mock.calls.length).toBe(0);
727+
});
596728
test('onOpen parameters should be immutable', () => {
597729
renderApp();
598730
const onOpen1 = jest.fn((openedTabIDs) => {
@@ -611,6 +743,29 @@ describe('callbacks should be called with immutable parameters : ', () => {
611743
});
612744
expect(onOpen2.mock.calls[0][0]).toEqual(['3']);
613745
});
746+
test('checking context of onOpen callback', () => {
747+
expect.assertions(2);
748+
op.onOpen = jest.fn(function () {
749+
expect(Object.prototype.toString.call(this)).toBe('[object Object]');
750+
expect(this.hasOwnProperty('getData')).toBe(true);
751+
});
752+
renderApp();
753+
op.onOpen = function () {};
754+
});
755+
});
756+
describe('onClose callback : ', () => {
757+
test('checking onClose parameters', () => {
758+
renderApp();
759+
act(() => {
760+
instance.close('2');
761+
});
762+
expect(op.onClose.mock.calls.length).toBe(1);
763+
expect(op.onClose.mock.calls[0][0]).toEqual(['2']);
764+
});
765+
test('it is not called initially', () => {
766+
renderApp();
767+
expect(op.onClose.mock.calls.length).toBe(0);
768+
});
614769
test('onClose parameters should be immutable', () => {
615770
renderApp();
616771
const onClose1 = jest.fn((closedTabIDs) => {
@@ -629,59 +784,13 @@ describe('callbacks should be called with immutable parameters : ', () => {
629784
});
630785
expect(onClose2.mock.calls[0][0]).toEqual(['2']);
631786
});
632-
test('onChange parameters should be immutable', () => {
633-
renderApp();
634-
const onChange1 = jest.fn(({currentData, previousData, closedTabIDs, openedTabIDs}) => {
635-
closedTabIDs.push('6');
636-
openedTabIDs.push('6');
637-
currentData.selectedTabID = '6';
638-
previousData.selectedTabID = '6';
639-
});
640-
const onChange2 = jest.fn(({currentData, previousData, closedTabIDs, openedTabIDs}) => {
641-
closedTabIDs.push('7');
642-
openedTabIDs.push('7');
643-
currentData.selectedTabID = '7';
644-
previousData.selectedTabID = '7';
645-
});
646-
const onOpen = jest.fn(() => {});
647-
const onClose = jest.fn(() => {});
648-
const onSelect = jest.fn(() => {});
649-
const onFirstSelect = jest.fn(() => {});
650-
act(() => {
651-
instance.setOption('onChange', ({currentData, previousData, closedTabIDs, openedTabIDs}) => {
652-
closedTabIDs.push('5');
653-
openedTabIDs.push('5');
654-
currentData.selectedTabID = '5';
655-
previousData.selectedTabID = '5';
656-
});
657-
instance.one('onChange', onChange1);
658-
instance.on('onChange', onChange2);
659-
instance.on('onOpen', onOpen);
660-
instance.on('onClose', onClose);
661-
instance.on('onSelect', onSelect);
662-
instance.on('onFirstSelect', onFirstSelect);
663-
instance.close('2');
664-
instance.open({id: '3'});
665-
instance.select('3');
666-
});
667-
668-
expect(onSelect.mock.calls[0][0]).toEqual({
669-
currentSelectedTabId: '3',
670-
perviousSelectedTabId: '1',
671-
previousSelectedTabId: '1',
672-
});
673-
expect(onFirstSelect.mock.calls[0][0]).toEqual({
674-
currentSelectedTabId: '3',
675-
previousSelectedTabId: '1',
676-
});
677-
expect(onClose.mock.calls[0][0]).toEqual(['2']);
678-
expect(onOpen.mock.calls[0][0]).toEqual(['3']);
679-
expect(onChange2.mock.calls[0][0]).toEqual({
680-
currentData: {selectedTabID: '3', openTabIDs: ['1', '3']},
681-
previousData: {selectedTabID: '1', openTabIDs: ['1', '2']},
682-
perviousData: {selectedTabID: '1', openTabIDs: ['1', '2']},
683-
closedTabIDs: ['2'],
684-
openedTabIDs: ['3'],
787+
test('checking context of onClose callback', () => {
788+
expect.assertions(2);
789+
op.onClose = jest.fn(function () {
790+
expect(Object.prototype.toString.call(this)).toBe('[object Object]');
791+
expect(this.hasOwnProperty('getData')).toBe(true);
685792
});
793+
renderApp();
794+
op.onClose = function () {};
686795
});
687796
});

src/useDynamicTabs/useDynamicTabs.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ afterAll(() => {
7676
document.body.removeChild(container);
7777
container = null;
7878
});
79-
describe('checking number of tabs renders : ', () => {
79+
describe('counting tabs renders : ', () => {
8080
test('each tab should be rendered once initially', () => {
8181
let _api;
8282
const getApiInstance = function (op) {
@@ -288,7 +288,7 @@ describe('checking number of tabs renders : ', () => {
288288
expect(_api.optionsManager.setting.panelIdTemplate.mock.calls.length).toBe(3);
289289
});
290290
});
291-
describe('checking number of panels renders : ', () => {
291+
describe('counting panels renders : ', () => {
292292
test('each panel should be rendered once initially', () => {
293293
let _api;
294294
const getApiInstance = function (op) {

0 commit comments

Comments
 (0)