Skip to content

Commit

Permalink
Improving code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
andrefarzat committed Jan 8, 2019
1 parent 9ec343a commit 319415b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
30 changes: 30 additions & 0 deletions src/app/plotly/plotly.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,41 @@ describe('PlotlyService', () => {
});
});

it('should check the plotly dependency', () => {
class LocalPlotlyService extends PlotlyService {
protected get plotly() {
return undefined;
}

public getPlotly() {
return undefined;
}
}

expect(() => new LocalPlotlyService()).toThrowError(`Peer dependency plotly.js isn't installed`);
});

it('should be created', inject([PlotlyService], (service: PlotlyService) => {
expect(service).toBeTruthy();
}));

it('should return the plotly object', inject([PlotlyService], (service: PlotlyService) => {
expect(service.getPlotly()).toBe(Plotlyjs);
}));

it('should call plotly methods', inject([PlotlyService], (service: PlotlyService) => {
const methods: (keyof PlotlyService)[] = ['plot', 'update', 'newPlot'];
methods.forEach(methodName => {
spyOn(service, methodName);

(service as any)[methodName]('one' as any, 'two' as any, 'three' as any, 'four' as any);
expect(service[methodName]).toHaveBeenCalledWith('one', 'two', 'three', 'four');
});

spyOn(service.getPlotly().Plots, 'resize');
service.resize('one' as any);
expect(service.getPlotly().Plots.resize).toHaveBeenCalledWith('one');
}));


});
29 changes: 9 additions & 20 deletions src/app/plotly/plotly.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ export namespace Plotly {
@Injectable()
export class PlotlyService {
protected static instances: Plotly.PlotlyHTMLElement[] = [];
protected plotly = Plotlyjs;

constructor() {
if (typeof this.plotly === 'undefined') {
if (typeof this.getPlotly() === 'undefined') {
throw new Error(`Peer dependency plotly.js isn't installed`);
}
}
Expand All @@ -47,33 +46,23 @@ export class PlotlyService {
}
}

public getPlotly() {
return Plotlyjs;
}

public newPlot(div: HTMLDivElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>) {
return this.plotly.newPlot(div, data, layout, config).then(instance => PlotlyService.insert(instance));
return this.getPlotly().newPlot(div, data, layout, config).then(instance => PlotlyService.insert(instance));
}

public plot(div: Plotly.PlotlyHTMLElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>) {
return this.plotly.plot(div, data, layout, config);
return this.getPlotly().plot(div, data, layout, config);
}

public update(div: Plotly.PlotlyHTMLElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>) {
return this.plotly.update(div, data, layout, config);
return this.getPlotly().update(div, data, layout, config);
}

public resize(div: Plotly.PlotlyHTMLElement): void {
return this.plotly.Plots.resize(div);
}

public getPlotly() {
return this.plotly;
return this.getPlotly().Plots.resize(div);
}

public getInstanceByDivId(id: string): Plotly.PlotlyHTMLElement | undefined {
for (const instance of PlotlyService.instances) {
if (instance.id === id) {
return instance;
}
}
return undefined;
}

}

0 comments on commit 319415b

Please sign in to comment.