Skip to content

Commit

Permalink
feat(error-dash-events): add error and ready listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcoutinho committed Sep 28, 2020
1 parent 769a0c3 commit 853dac4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,31 @@ describe('DashboardComponent', () => {
expect(dashboardMock.bind).toHaveBeenCalledWith(controlTwo.controlWrapper, [chartOne.chartWrapper, chartTwo.chartWrapper]);
});

it('should register dashboard wrapper event handlers', () => {
const scriptLoaderService = TestBed.inject(ScriptLoaderService) as jest.Mocked<ScriptLoaderService>;
scriptLoaderService.loadChartPackages.mockReturnValueOnce(of(null));

const dashboardMock = { bind: jest.fn(), draw: jest.fn() };
visualizationMock.Dashboard.mockReturnValueOnce(dashboardMock);

const dataTableMock = {};
visualizationMock.arrayToDataTable.mockReturnValueOnce(dataTableMock);

globalThis.google = { visualization: visualizationMock } as any;

// At least one control wrapper is needed to start the drawing
const chart = { wrapperReady$: of(null), chartWrapper: {} };
const control = { wrapperReady$: of(null), for: chart, controlWrapper: {} };
component['controlWrappers'] = [control] as any;

component.data = [];

component.ngOnInit();

expect(visualizationMock.events.removeAllListeners).toHaveBeenCalled();
expect(visualizationMock.events.addListener).toHaveBeenCalledWith(dashboardMock, 'ready', expect.any(Function));
expect(visualizationMock.events.addListener).toHaveBeenCalledWith(dashboardMock, 'error', expect.any(Function));
});
it('should draw the dashboard using the provided data', () => {
const scriptLoaderService = TestBed.inject(ScriptLoaderService) as jest.Mocked<ScriptLoaderService>;
scriptLoaderService.loadChartPackages.mockReturnValueOnce(of(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,22 @@ export class DashboardComponent implements OnInit, OnChanges {
combineLatest([...controlWrappersReady$, ...chartsReady$]).subscribe(() => {
this.dashboard = new google.visualization.Dashboard(this.element.nativeElement);
this.initializeBindings();
this.registerDashboardEvents();
this.dashboard.draw(this.dataTable);
});
}

private registerDashboardEvents() {
google.visualization.events.removeAllListeners(this.dashboard);

const registerDashEvent = (object: any, eventName: string, callback: Function) => {
google.visualization.events.addListener(object, eventName, callback);
};

registerDashEvent(this.dashboard, 'ready', () => this.ready.emit());
registerDashEvent(this.dashboard, 'error', (error: ChartErrorEvent) => this.error.emit(error));
}

private initializeBindings() {
this.controlWrappers.forEach(control => {
if (Array.isArray(control.for)) {
Expand Down

0 comments on commit 853dac4

Please sign in to comment.