Skip to content

Commit ab3df89

Browse files
feat(chart): implementa testes unitarios e ajustes
1 parent 55741c8 commit ab3df89

File tree

2 files changed

+103
-31
lines changed

2 files changed

+103
-31
lines changed

projects/ui/src/lib/components/po-chart-new/po-chart-new.component.spec.ts

Lines changed: 99 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,54 @@ describe('PoChartNewComponent', () => {
376376
});
377377
});
378378

379+
describe('checkShowCEcharts', () => {
380+
it('should call initECharts and disconnect observer when element is visible and hideDomEchartsDiv is true', () => {
381+
const chartElement = document.createElement('div');
382+
chartElement.id = 'chart-id';
383+
384+
(component['el'].nativeElement.querySelector as jasmine.Spy).and.returnValue(chartElement);
385+
component['hideDomEchartsDiv'] = true;
386+
387+
spyOn(component as any, 'initECharts');
388+
389+
const observeSpy = jasmine.createSpy('observe');
390+
const disconnectSpy = jasmine.createSpy('disconnect');
391+
392+
let callback: (entries: Array<IntersectionObserverEntry>) => void;
393+
394+
(window as any).IntersectionObserver = function (cb: any) {
395+
callback = cb;
396+
return {
397+
observe: observeSpy,
398+
disconnect: disconnectSpy
399+
};
400+
};
401+
402+
(component as any).checkShowCEcharts();
403+
404+
callback([{ isIntersecting: true }] as any);
405+
406+
expect((component as any).initECharts).toHaveBeenCalled();
407+
expect(observeSpy).toHaveBeenCalledWith(chartElement);
408+
expect(disconnectSpy).toHaveBeenCalled();
409+
});
410+
});
411+
412+
it('should set showPopup to false when hideExportCsv, hideExportImage are true and there are no customActions', () => {
413+
component['options'] = {
414+
header: {
415+
hideExportCsv: true,
416+
hideExportImage: true
417+
}
418+
};
419+
420+
component['customActions'] = [];
421+
422+
(component as any).setInitialPopupActions();
423+
424+
expect(component['showPopup']).toBeFalse();
425+
});
426+
379427
describe('initECHarts:', () => {
380428
it('should not initialize the chart if #chart-id is not found', () => {
381429
const originalQuerySelector = component['el'].nativeElement.querySelector;
@@ -394,24 +442,24 @@ describe('PoChartNewComponent', () => {
394442

395443
it('should emit seriesClick event when clicking on the chart', () => {
396444
const onSpy = jasmine.createSpy('on');
445+
397446
component['chartInstance'] = {
398447
on: onSpy
399-
} as Partial<EChartsType> as EChartsType;
448+
} as any;
400449

401450
spyOn(component.seriesClick, 'emit');
402451
spyOn(component.seriesHover, 'emit');
403452

404453
component['initEChartsEvents']();
405454

406-
expect(component['chartInstance'].on).toHaveBeenCalledWith('click', jasmine.any(Function));
455+
expect(onSpy).toHaveBeenCalledWith('click', jasmine.any(Function));
407456

408457
const clickCallback = onSpy.calls.argsFor(0)[1];
409458

410459
const mockParams = { seriesName: 'Exemplo', value: 100, name: 'Categoria X' };
411460
clickCallback(mockParams);
412461

413462
const mouseoverCallback = onSpy.calls.argsFor(1)[1];
414-
415463
const mockParamsMouse = {};
416464
mouseoverCallback(mockParamsMouse);
417465

@@ -420,6 +468,7 @@ describe('PoChartNewComponent', () => {
420468
data: 100,
421469
category: 'Categoria X'
422470
});
471+
423472
expect(component.seriesHover.emit).not.toHaveBeenCalled();
424473
});
425474

@@ -701,29 +750,31 @@ describe('PoChartNewComponent', () => {
701750
expect(result.yAxis.splitLine.show).toBeTrue();
702751
});
703752

704-
it('should apply correct axis configurations if type is Bar', () => {
705-
const categories = ['Mon', 'Tue', 'Wed'];
753+
it('should configure axes correctly when isTypeBar is true', () => {
706754
component.isTypeBar = true;
707-
component.options.axis = {
708-
minRange: 10,
709-
maxRange: 100,
710-
gridLines: 7,
711-
showXAxis: false,
712-
showYAxis: true,
713-
paddingLeft: 60
755+
756+
component.options = {
757+
axis: {
758+
showXAxis: undefined,
759+
showYAxis: false
760+
}
714761
};
715-
component.categories = categories;
716762

717-
const result = component['setOptions']() as any;
718-
expect(result.yAxis.min).toBe(10);
719-
expect(result.yAxis.max).toBe(100);
720-
expect(result.yAxis.splitNumber).toBe(7);
721-
expect(result.yAxis.type).toBe('category');
722-
expect(result.yAxis.data).toEqual(categories);
723-
expect(result.grid.left).toBe(60);
724-
expect(result.xAxis.type).toBe('value');
725-
expect(result.xAxis.splitLine.show).toBeFalse();
726-
expect(result.yAxis.splitLine.show).toBeTrue();
763+
component.categories = ['Jan', 'Feb', 'Mar'];
764+
765+
const options: any = {};
766+
767+
component['chartGridUtils'].setOptionsAxis(options);
768+
769+
expect(options.xAxis.type).toBe('value');
770+
771+
expect(options.xAxis.splitLine.show).toBeTrue();
772+
773+
expect(options.yAxis.type).toBe('category');
774+
775+
expect(options.yAxis.splitLine.show).toBeFalse();
776+
777+
expect(options.yAxis.data).toEqual(['Jan', 'Feb', 'Mar']);
727778
});
728779

729780
it('should apply number formatting when labelType is Number', () => {
@@ -784,12 +835,13 @@ describe('PoChartNewComponent', () => {
784835
});
785836

786837
describe('setShowAxisDetails: ', () => {
787-
it('deve adicionar tooltip com axisPointer quando showAxisDetails for true', () => {
838+
it('should add tooltip with axisPointer when showAxisDetails is true', () => {
788839
component['options'] = {
789840
axis: {
790841
showAxisDetails: true
791842
}
792843
};
844+
spyOn(component, 'getCSSVariable').and.returnValue('#4a5c60');
793845

794846
const options: any = {};
795847

@@ -800,13 +852,34 @@ describe('PoChartNewComponent', () => {
800852
axisPointer: {
801853
type: 'cross',
802854
label: {
803-
backgroundColor: '#6a7985'
855+
backgroundColor: '#4a5c60'
804856
}
805857
}
806858
});
807859
});
808860
});
809861

862+
it('should set splitNumber on xAxis when isTypeBar is true', () => {
863+
component.isTypeBar = true;
864+
component.options = {
865+
axis: {
866+
gridLines: 7,
867+
minRange: 0,
868+
maxRange: 100
869+
}
870+
};
871+
872+
const options: any = {
873+
xAxis: {
874+
axisLabel: {}
875+
}
876+
};
877+
878+
(component as any).formatLabelOption(options);
879+
880+
expect(options.xAxis.splitNumber).toBe(7);
881+
});
882+
810883
describe('setSeries:', () => {
811884
let mockSeriesWithColor: Array<PoChartSerie>;
812885

@@ -1371,7 +1444,7 @@ describe('PoChartNewComponent', () => {
13711444
spyOn(document, 'createElement').and.callFake((tag: string) => {
13721445
if (tag === 'canvas') return canvas;
13731446
if (tag === 'a') return link;
1374-
return originalCreateElement.call(document, tag); // aqui corrigido
1447+
return originalCreateElement.call(document, tag);
13751448
});
13761449

13771450
spyOn(canvas, 'toDataURL').and.returnValue('data:image/png;base64,fakeImageData');

projects/ui/src/lib/components/po-chart-new/po-chart-new.component.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ use([
5858
]);
5959

6060
/**
61-
* @docsExtends PoChartBaseComponent
61+
* @docsPrivate
62+
*
63+
* Componente de uso interno.
6264
*/
6365

6466
/* @ToDo - Foi retirado o exemplo do sample pois causava problema no build:package da pipe */
@@ -229,10 +231,7 @@ export class PoChartNewComponent
229231
const hideExportCsv = this.options?.header?.hideExportCsv;
230232
const hideExportImage = this.options?.header?.hideExportImage;
231233

232-
if (hideExportCsv && hideExportImage && !this.customActions?.length) {
233-
this.showPopup = false;
234-
this.cdr.detectChanges();
235-
}
234+
this.showPopup = !(hideExportCsv && hideExportImage && !this.customActions?.length);
236235

237236
this.popupActions = [
238237
{

0 commit comments

Comments
 (0)