Skip to content

Commit 1eb2085

Browse files
fix(context-tabs): ajusta adição dinâmica de tabs
Ajusta adição de tabs dinamicamente para ser exibida fora do dropdown enquanto houver espaço em tela. Fixes: DTHFUI-11863
1 parent 8b721ce commit 1eb2085

File tree

2 files changed

+108
-39
lines changed

2 files changed

+108
-39
lines changed

projects/ui/src/lib/components/po-context-tabs/po-context-tabs.component.spec.ts

Lines changed: 106 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ describe('PoContextTabsComponent:', () => {
1212
declarations: [PoContextTabsComponent]
1313
}).compileComponents();
1414

15-
tabElements = [document.createElement('div'), document.createElement('div'), document.createElement('div')];
16-
1715
fixture = TestBed.createComponent(PoContextTabsComponent);
1816
component = fixture.componentInstance;
1917
fixture.detectChanges();
@@ -386,10 +384,10 @@ describe('PoContextTabsComponent:', () => {
386384
expect(afterMethod).toHaveBeenCalledWith(200);
387385
});
388386

389-
it('updateTabsState: should update tabs state and call calculateTabs and executeTabsState', () => {
387+
it('updateTabsState: should correctly update tabs state considering checkChangesTabs logic', () => {
390388
component.initCheckChangesTab = true;
391389
component.quantityTabsButton = 2;
392-
component.byQuantityFunction = undefined; // ou um número qualquer, se quiser simular
390+
component.byQuantityFunction = undefined;
393391
component.initialTabsWidth = [
394392
{ id: 'tab-1', width: 100 },
395393
{ id: 'tab-2-hide', width: 120 },
@@ -402,10 +400,74 @@ describe('PoContextTabsComponent:', () => {
402400
{ id: 'tab-other', removed: true, hide: true },
403401
{ id: 'tab-2-hide', hide: true },
404402
{ id: 'tab-2', hide: false, showTooltip: true },
405-
{ id: 'tab-3' }
403+
{ id: 'tab-3', hide: false }
404+
]
405+
} as any;
406+
407+
const mockTab1 = { nativeElement: { id: 'tab-1', style: { display: '' }, hidden: false } };
408+
const mockTab2Hidden = { nativeElement: { id: 'tab-2-hide', style: { display: '' }, hidden: true } };
409+
const mockTab2 = { nativeElement: { id: 'tab-2', style: { display: '' }, hidden: false } };
410+
const mockTab3 = { nativeElement: { id: 'tab-3', style: { display: 'none' }, hidden: true } };
411+
412+
component.tabButton = [mockTab1, mockTab2Hidden, mockTab2, mockTab3] as any;
413+
414+
spyOn(component as any, 'calculateTabs');
415+
spyOn(component as any, 'executeTabsState');
416+
417+
component.updateTabsState(false, { id: 'tab-2', hide: true, widthButton: undefined } as any);
418+
419+
expect(mockTab3.nativeElement.hidden).toBeFalse();
420+
expect(mockTab3.nativeElement.style.display).toBe('inline-block');
421+
422+
expect(mockTab2Hidden.nativeElement.hidden).toBeTrue();
423+
expect(mockTab2Hidden.nativeElement.style.display).toBe('');
424+
425+
expect(mockTab1.nativeElement.hidden).toBeFalse();
426+
expect(mockTab2.nativeElement.hidden).toBeFalse();
427+
428+
expect((component as any).calculateTabs).toHaveBeenCalled();
429+
expect((component as any).executeTabsState).not.toHaveBeenCalled();
430+
expect(component.initialTabsWidth.length).toBe(4);
431+
});
432+
433+
it('checkChangesTabs: should hide tabs exceeding quantityTabs', () => {
434+
component.quantityTabsButton = 2;
435+
436+
const mockTab1 = { nativeElement: { id: 'tab-1', style: { display: '' }, hidden: false, offsetWidth: 100 } };
437+
const mockTab2 = { nativeElement: { id: 'tab-2', style: { display: '' }, hidden: false, offsetWidth: 100 } };
438+
const mockTab3 = { nativeElement: { id: 'tab-3', style: { display: '' }, hidden: false, offsetWidth: 100 } };
439+
const mockTab4 = { nativeElement: { id: 'tab-4', style: { display: '' }, hidden: false, offsetWidth: 100 } };
440+
441+
component.tabButton = [mockTab1, mockTab2, mockTab3, mockTab4] as any;
442+
443+
component['tabsChildren'] = {
444+
_results: [
445+
{ id: 'tab-1', hide: false },
446+
{ id: 'tab-2', hide: false },
447+
{ id: 'tab-3', hide: false },
448+
{ id: 'tab-4', hide: false }
406449
]
407450
} as any;
408451

452+
spyOn(component as any, 'calculateTabs');
453+
454+
component['checkChangesTabs']();
455+
456+
expect(mockTab4.nativeElement.hidden).toBeTrue();
457+
expect(mockTab4.nativeElement.style.display).toBe('none');
458+
459+
expect(mockTab1.nativeElement.hidden).toBeFalse();
460+
expect(mockTab2.nativeElement.hidden).toBeFalse();
461+
462+
expect((component as any).calculateTabs).toHaveBeenCalled();
463+
});
464+
465+
it('updateTabsState: should return defaultLastTabWidth is valid', () => {
466+
component.quantityTabsButton = 2;
467+
component.initialTabsWidth = [
468+
{ id: 'tab-1', width: 100 },
469+
{ id: 'tab-2', width: 120 }
470+
];
409471
const mockTab1 = {
410472
nativeElement: {
411473
id: 'tab-1',
@@ -414,14 +476,6 @@ describe('PoContextTabsComponent:', () => {
414476
}
415477
};
416478

417-
const mockTab2Hidden = {
418-
nativeElement: {
419-
id: 'tab-2-hide',
420-
style: { display: '' },
421-
hidden: true
422-
}
423-
};
424-
425479
const mockTab2 = {
426480
nativeElement: {
427481
id: 'tab-2',
@@ -434,48 +488,58 @@ describe('PoContextTabsComponent:', () => {
434488
nativeElement: {
435489
id: 'tab-3',
436490
style: { display: '' },
437-
hidden: true
491+
hidden: false,
492+
offsetWidth: 120
438493
}
439494
};
440495

441-
component.tabButton = [mockTab1, mockTab2Hidden, mockTab2, mockTab3] as any;
496+
component.tabButton = [mockTab1, mockTab2, mockTab3] as any;
442497

443498
spyOn(component as any, 'calculateTabs');
444499
spyOn(component as any, 'executeTabsState');
445500

446-
component.updateTabsState(false, {
447-
id: 'tab-2',
448-
hide: true,
449-
widthButton: undefined
450-
} as any);
451-
452-
expect(mockTab3.nativeElement.style.display).toBe('none');
453-
expect(mockTab3.nativeElement.hidden).toBeTrue();
501+
component.updateTabsState(false);
454502

455-
expect((component as any).calculateTabs).toHaveBeenCalled();
456-
expect(component.initialTabsWidth.length).toBe(4);
457-
expect((component as any).executeTabsState).not.toHaveBeenCalled();
503+
expect((component as any).calculateTabs).not.toHaveBeenCalled();
504+
expect(component.defaultLastTabWidth).toBe(120);
505+
expect((component as any).executeTabsState).toHaveBeenCalled();
458506
});
459507

460-
it('updateTabsState: should return defaultLastTabWidth is valid', () => {
461-
component.quantityTabsButton = 2;
508+
it('updateTabsState: should keep newly added tab visible when there is space available (no dropdown)', () => {
509+
component.initCheckChangesTab = true;
510+
component.quantityTabsButton = 3;
511+
component.byQuantityFunction = undefined;
462512
component.initialTabsWidth = [
463513
{ id: 'tab-1', width: 100 },
464514
{ id: 'tab-2', width: 120 }
465515
];
516+
517+
const mockTabsChildren = [
518+
{ id: 'tab-1', hide: false, showTooltip: true },
519+
{ id: 'tab-2', hide: false, showTooltip: true },
520+
{ id: 'tab-3', hide: false, showTooltip: true }
521+
];
522+
523+
component['tabsChildren'] = {
524+
_results: mockTabsChildren,
525+
toArray: () => mockTabsChildren
526+
} as any;
527+
466528
const mockTab1 = {
467529
nativeElement: {
468530
id: 'tab-1',
469531
style: { display: '' },
470-
hidden: false
532+
hidden: false,
533+
offsetWidth: 100
471534
}
472535
};
473536

474537
const mockTab2 = {
475538
nativeElement: {
476539
id: 'tab-2',
477540
style: { display: '' },
478-
hidden: false
541+
hidden: false,
542+
offsetWidth: 120
479543
}
480544
};
481545

@@ -484,20 +548,25 @@ describe('PoContextTabsComponent:', () => {
484548
id: 'tab-3',
485549
style: { display: '' },
486550
hidden: false,
487-
offsetWidth: 120
551+
offsetWidth: 110
488552
}
489553
};
490554

555+
component['containerTabs'] = {
556+
nativeElement: { offsetWidth: 600 }
557+
} as any;
558+
491559
component.tabButton = [mockTab1, mockTab2, mockTab3] as any;
492560

493-
spyOn(component as any, 'calculateTabs');
494-
spyOn(component as any, 'executeTabsState');
561+
spyOn(component as any, 'calculateTabs').and.callThrough();
495562

496-
component.updateTabsState(false);
563+
component.updateTabsState(false, { id: 'tab-3', hide: false } as any);
497564

498-
expect((component as any).calculateTabs).not.toHaveBeenCalled();
499-
expect(component.defaultLastTabWidth).toBe(120);
500-
expect((component as any).executeTabsState).toHaveBeenCalled();
565+
expect(mockTab3.nativeElement.hidden).toBeFalse();
566+
expect(component.tabsDefault.some(t => t.id === 'tab-3')).toBeTrue();
567+
expect(component.overflowedTabs.some(t => t.id === 'tab-3')).toBeFalse();
568+
569+
expect((component as any).calculateTabs).toHaveBeenCalled();
501570
});
502571

503572
it('setQuantityTabsButton: should values valid', () => {

projects/ui/src/lib/components/po-context-tabs/po-context-tabs.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ export class PoContextTabsComponent extends PoTabsComponent {
207207

208208
if (!currentTab) {
209209
this.initialTabsWidth.push({ id: tab.nativeElement.id, width: tab.nativeElement.offsetWidth });
210-
if (index > quantityTabs - 1) {
210+
if (index > quantityTabs) {
211211
tab.nativeElement.style.display = 'none';
212212
tab.nativeElement.hidden = true;
213213
}
214214
this.tabsChildren['_results'] = this.tabsChildren['_results'].filter(item => !item.removed);
215215
}
216-
if (tab.nativeElement.hidden && index <= quantityTabs - 1) return;
216+
if (tab.nativeElement.hidden && index <= quantityTabs) return;
217217
index++;
218218
});
219219
this.calculateTabs(true);

0 commit comments

Comments
 (0)