Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e60d39a

Browse files
committed
Writing unit tests
1 parent b7c4392 commit e60d39a

File tree

5 files changed

+113
-31
lines changed

5 files changed

+113
-31
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { PlotlyViaCDNModule, PlotlyModuleConfig, PlotlyCDNProvider, PlotlyBundleName } from './plotly-via-cdn.module';
2+
import { PlotlyService } from './plotly.service';
3+
4+
describe('PlotlyViaCDNModule', () => {
5+
beforeEach(() => {
6+
spyOn(PlotlyViaCDNModule, 'loadViaCDN');
7+
});
8+
9+
it('should set the module name to ViaCDN', () => {
10+
spyOn(PlotlyService, 'setModuleName');
11+
const fakeService = {} as PlotlyService;
12+
const module = new PlotlyViaCDNModule(fakeService);
13+
expect(PlotlyService.setModuleName).toHaveBeenCalledWith('ViaCDN');
14+
expect(module.plotlyService).toBe(fakeService);
15+
});
16+
17+
describe('forRoot', () => {
18+
it('should call loadViaCDN with default config', () => {
19+
const result = PlotlyViaCDNModule.forRoot({} as PlotlyModuleConfig);
20+
expect(PlotlyViaCDNModule.loadViaCDN).toHaveBeenCalledWith({
21+
bundleName: null,
22+
cdnProvider: 'plotly',
23+
version: 'latest',
24+
customUrl: ''
25+
});
26+
expect(result.ngModule).toBe(PlotlyViaCDNModule);
27+
expect(result.providers).toEqual([PlotlyService]);
28+
});
29+
30+
it('should validate version', () => {
31+
expect(() => PlotlyViaCDNModule.forRoot({ version: 'invalid' } as PlotlyModuleConfig))
32+
.toThrowError(
33+
'Invalid plotly version. Please set \'latest\' or version number (i.e.: 1.4.3) or strict version number (i.e.: strict-1.4.3)'
34+
);
35+
});
36+
37+
it('should validate bundleName', () => {
38+
expect(() => PlotlyViaCDNModule.forRoot({ bundleName: 'unknown' as any } as PlotlyModuleConfig))
39+
.toThrowError(
40+
'Invalid plotly bundle. Please set to null for full or "basic", "cartesian", "geo", "gl3d", "gl2d", "mapbox", "finance" for a partial bundle.'
41+
);
42+
});
43+
44+
it('should validate cdnProvider', () => {
45+
expect(() => PlotlyViaCDNModule.forRoot({ cdnProvider: 'unknown' as any } as PlotlyModuleConfig))
46+
.toThrowError(
47+
'Invalid CDN provider. Please set to \'plotly\', \'cloudflare\' or \'custom\'.'
48+
);
49+
});
50+
51+
it('should require customUrl when cdnProvider is custom', () => {
52+
expect(() => PlotlyViaCDNModule.forRoot({ cdnProvider: 'custom', customUrl: '' } as PlotlyModuleConfig))
53+
.toThrowError(
54+
'Invalid or missing CDN URL. Please provide a CDN URL in case of custom provider.'
55+
);
56+
});
57+
58+
it('should validate cloudflare latest version unsupported', () => {
59+
expect(() => PlotlyViaCDNModule.forRoot({ cdnProvider: 'cloudflare', version: 'latest' } as PlotlyModuleConfig))
60+
.toThrowError(
61+
'As cloudflare hosts version specific files, \'latest\' as a version is not supported. Please specify a version or you can choose \'plotly\' as a CDN provider.'
62+
);
63+
});
64+
65+
it('should call loadViaCDN and return module for custom config', () => {
66+
const config: PlotlyModuleConfig = {
67+
version: '1.2.3',
68+
bundleName: 'basic' as PlotlyBundleName,
69+
cdnProvider: 'custom' as PlotlyCDNProvider,
70+
customUrl: 'http://cdn'
71+
};
72+
const result = PlotlyViaCDNModule.forRoot(config);
73+
expect(PlotlyViaCDNModule.loadViaCDN).toHaveBeenCalledWith(jasmine.objectContaining({
74+
version: '1.2.3',
75+
bundleName: 'basic',
76+
cdnProvider: 'custom',
77+
customUrl: 'http://cdn'
78+
}));
79+
expect(result.ngModule).toBe(PlotlyViaCDNModule);
80+
expect(result.providers).toEqual([PlotlyService]);
81+
});
82+
});
83+
});

projects/plotly/src/lib/plotly-via-cdn.module.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class PlotlyViaCDNModule {
4040
}
4141

4242
const plotlyBundleNames: PlotlyBundleName[] = ['basic', 'cartesian', 'geo', 'gl3d', 'gl2d', 'mapbox', 'finance']
43-
isOk = config.bundleName === null || !plotlyBundleNames.includes(config.bundleName);
43+
isOk = config.bundleName === null || plotlyBundleNames.includes(config.bundleName);
4444
if (!isOk) {
4545
const names = plotlyBundleNames.map(n => `"${n}"`).join(', ');
4646
throw new Error(`Invalid plotly bundle. Please set to null for full or ${names} for a partial bundle.`);
@@ -51,6 +51,10 @@ export class PlotlyViaCDNModule {
5151
throw new Error(`Invalid CDN provider. Please set to 'plotly', 'cloudflare' or 'custom'.`);
5252
}
5353

54+
if (config.cdnProvider === 'cloudflare' && config.version == 'latest') {
55+
throw new Error(`As cloudflare hosts version specific files, 'latest' as a version is not supported. Please specify a version or you can choose 'plotly' as a CDN provider.`);
56+
}
57+
5458
if (config.cdnProvider === 'custom' && !config.customUrl) {
5559
throw new Error(`Invalid or missing CDN URL. Please provide a CDN URL in case of custom provider.`);
5660
}
@@ -70,9 +74,6 @@ export class PlotlyViaCDNModule {
7074
let src: string = '';
7175
switch (config.cdnProvider) {
7276
case 'cloudflare':
73-
if (config.version == 'latest') {
74-
throw new Error(`As cloudflare hosts version specific files, 'latest' as a version is not supported. Please specify a version or you can choose 'plotly' as a CDN provider.`);
75-
}
7677
src = config.bundleName == null
7778
? `https://cdnjs.cloudflare.com/ajax/libs/plotly.js/${config.version}/plotly.min.js`
7879
: `https://cdnjs.cloudflare.com/ajax/libs/plotly.js/${config.version}/plotly-${config.bundleName}.min.js`;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { PlotlyViaWindowModule } from './plotly-via-window.module';
2+
import { PlotlyService } from './plotly.service';
3+
4+
describe('PlotlyViaWindowModule', () => {
5+
const originalPlotly = (window as any).Plotly;
6+
7+
afterEach(() => {
8+
(window as any).Plotly = originalPlotly;
9+
});
10+
11+
it('should throw error when window.Plotly is undefined', () => {
12+
delete (window as any).Plotly;
13+
expect(() => new PlotlyViaWindowModule())
14+
.toThrowError('Plotly object not found on window.');
15+
});
16+
17+
it('should set PlotlyService.setPlotly with window.Plotly', () => {
18+
const fakePlotly = { react(): void { } };
19+
(window as any).Plotly = fakePlotly;
20+
spyOn(PlotlyService, 'setPlotly');
21+
new PlotlyViaWindowModule();
22+
expect(PlotlyService.setPlotly).toHaveBeenCalledWith(fakePlotly);
23+
});
24+
});

projects/plotly/src/lib/plotly.module.spec.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

projects/plotly/src/lib/plotly.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('PlotlyService', () => {
4040
}));
4141

4242
it('should set the module name', () => {
43-
expect(PlotlyService.getModuleName()).toBeUndefined();
43+
expect(PlotlyService.getModuleName()).toBe('PlotlyJS');
4444
PlotlyService.setModuleName('ViaCDN');
4545
expect(PlotlyService.getModuleName()).toBe('ViaCDN');
4646

0 commit comments

Comments
 (0)