-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathplotly.service.spec.ts
More file actions
111 lines (84 loc) · 4.72 KB
/
Copy pathplotly.service.spec.ts
File metadata and controls
111 lines (84 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { TestBed, inject } from '@angular/core/testing';
import PlotlyJS from 'plotly.js-dist';
import { PlotlyService } from './plotly.service';
import { Plotly } from './plotly.interface';
describe('PlotlyService', () => {
beforeEach(() => {
PlotlyService.setPlotly(PlotlyJS);
TestBed.configureTestingModule({
providers: [PlotlyService]
});
});
it('should get a plotly instance by id', inject([PlotlyService], (service: PlotlyService) => {
const instance = { id: 'aidi' } as Plotly.PlotlyHTMLElement;
PlotlyService.insert(instance);
expect(service.getInstanceByDivId('aidi')).toBe(instance);
PlotlyService.remove(instance);
expect(service.getInstanceByDivId('aidi')).toBeUndefined();
}));
it('should check the plotly dependency', inject([PlotlyService], (service: PlotlyService) => {
(PlotlyService as any).plotly = undefined;
return service.getPlotly().catch(err => {
expect(err.message).toBe(`Peer dependency plotly.js isn't installed`);
PlotlyService.setPlotly(PlotlyJS);
});
}));
it('should be created', inject([PlotlyService], (service: PlotlyService) => {
expect(service).toBeTruthy();
}));
it('should return the plotly object', inject([PlotlyService], async (service: PlotlyService) => {
expect(await service.getPlotly()).toBe(PlotlyJS);
}));
it('should reject a Plotly loading error', inject([PlotlyService], async (service: PlotlyService) => {
const error = new Error('CDN failed');
PlotlyService.setPlotly('waiting');
PlotlyService.setPlotlyError(error);
await expectAsync(service.getPlotly()).toBeRejectedWith(error);
PlotlyService.setPlotly(PlotlyJS);
}));
it('should set the module name', () => {
expect(PlotlyService.getModuleName()).toBe('PlotlyJS');
PlotlyService.setModuleName('ViaCDN');
expect(PlotlyService.getModuleName()).toBe('ViaCDN');
// cleaning up
PlotlyService.setModuleName(undefined);
});
it('should call plotly.newPlot method', inject([PlotlyService], async (service: PlotlyService) => {
return new Promise<void>(async (resolve) => {
const plotly = await (service as any).getPlotly();
PlotlyService.setPlotly('waiting');
setTimeout(() => PlotlyService.setPlotly(PlotlyJS), 100);
spyOn(plotly, 'newPlot').and.returnValue(Promise.resolve());
await service.newPlot('one' as any, 'two' as any, 'three' as any, 'four' as any);
expect(plotly.newPlot).toHaveBeenCalledWith('one', 'two', 'three', 'four');
await service.newPlot('one' as any, 'two' as any, 'three' as any, 'four' as any, 'five' as any);
expect(plotly.newPlot).toHaveBeenCalledWith('one', {data: 'two', layout: 'three', config: 'four', frames: 'five'});
resolve();
});
}));
it('should call plotly.plot method', inject([PlotlyService], async (service: PlotlyService) => {
const plotly = await service.getPlotly();
spyOn(plotly, 'newPlot').and.returnValue(new Promise(() => {}));
service.plot('one' as any, 'two' as any, 'three' as any, 'four' as any);
expect(plotly.newPlot).toHaveBeenCalledWith('one', 'two', 'three', 'four');
service.plot('one' as any, 'two' as any, 'three' as any, 'four' as any, 'five' as any);
expect(plotly.newPlot).toHaveBeenCalledWith('one', {data: 'two', layout: 'three', config: 'four', frames: 'five'});
}));
it('should call plotly.update method', inject([PlotlyService], async (service: PlotlyService) => {
const plotly = await service.getPlotly();
spyOn(plotly, 'react').and.returnValue(new Promise(() => {}));
service.update('one' as any, 'two' as any, 'three' as any, 'four' as any);
expect(plotly.react).toHaveBeenCalledWith('one', 'two', 'three', 'four');
service.update('one' as any, 'two' as any, 'three' as any, 'four' as any, 'five' as any);
expect(plotly.react).toHaveBeenCalledWith('one', {data: 'two', layout: 'three', config: 'four', frames: 'five'});
}));
it('should call plotly.Plots.resize method', inject([PlotlyService], async (service: PlotlyService) => {
const plotly = await service.getPlotly();
spyOn(plotly.Plots, 'resize').and.returnValue(new Promise(() => {}));
service.resize('one' as any);
expect(plotly.Plots.resize).toHaveBeenCalledWith('one');
}));
it('should fail if is using an unsupported version of plotly.js', inject([PlotlyService], () => {
expect(() => PlotlyService.setPlotly({})).toThrowError(`Invalid plotly.js version. Please, use any version above 1.40.0`);
}));
});