Skip to content

Commit

Permalink
protected null inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
mlazar committed Nov 14, 2023
1 parent 70c6566 commit 0267228
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
29 changes: 27 additions & 2 deletions libs/mapjet-core/src/lib/mapjet-core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ import { ContainerResizeObserverPlugin } from './util-plugins/container-resize-o
import { Log } from './utils/log';
import { ResourceLoader } from './utils/resource-loader/resource-loader';

jest.mock('./util-plugins/container-resize-observer/container-resize-observer.plugin');
jest.mock('./util-plugins/container-resize-observer/container-resize-observer.plugin', () => {
const originalModuleMock: any = jest.genMockFromModule('./util-plugins/container-resize-observer/container-resize-observer.plugin');

return {
...originalModuleMock,
ContainerResizeObserverPlugin: class ContainerResizeObserverPlugin extends originalModuleMock.ContainerResizeObserverPlugin {
id = 'containerResizePlugin';
},
};
});

jest.mock('./utils/log');
jest.mock('maplibre-gl', () => {
const originalModuleMock: any = jest.genMockFromModule('maplibre-gl');
Expand All @@ -19,6 +29,7 @@ jest.mock('maplibre-gl', () => {
};
});


const FakePlugin: MapJetPlugin = {
id: '1234',
onAdd(_mapjet) {},
Expand Down Expand Up @@ -46,7 +57,7 @@ describe('MapJet', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should create MapLibre with correct default Options', () => {
const { map } = createCore();

Expand Down Expand Up @@ -107,6 +118,13 @@ describe('MapJet', () => {
expect(() => mapCore.addPlugin(plugin)).toThrowError(`Plugin with id ${plugin.id} already exists`);
});

it('should throw error when provided incorrect plugin id', () => {
mapCore = createCore({ debug: true });
const plugin = { ...FakePlugin, id: undefined };

expect(() => mapCore.addPlugin(plugin)).toThrowError(`Invalid plugin id`);
});

it('should call onAdd callback on plugin', () => {
const plugin = { ...FakePlugin };
jest.spyOn(plugin, 'onAdd');
Expand Down Expand Up @@ -174,6 +192,13 @@ describe('MapJet', () => {
expect(() => mapCore.removePlugin(plugin)).toThrowError(`Plugin with id ${plugin.id} not exists`);
});

it('should throw error when provided incorrect plugin id', () => {
mapCore = createCore({ debug: true });

expect(() => mapCore.removePlugin(null)).toThrowError(`Invalid input`);
expect(() => mapCore.removePlugin(undefined)).toThrowError(`Invalid input`);
});

it('should call onRemove callback on plugin', () => {
const plugin = { ...FakePlugin };
jest.spyOn(plugin, 'onRemove');
Expand Down
12 changes: 12 additions & 0 deletions libs/mapjet-core/src/lib/mapjet-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class MapJet {
return;
}

if (typeof plugin.id !== 'string' && typeof plugin.id !== 'number') {
throw new Error(`Invalid plugin id`);
}

if (this.hasPlugin(plugin)) {
throw new Error(`Plugin with id ${plugin.id} already exists`);
}
Expand All @@ -64,6 +68,10 @@ export class MapJet {
return;
}

if (plugin == null) {
throw new Error(`Invalid input`);
}

const pluginId: string = typeof plugin === 'object' ? plugin.id : plugin;
const pluginInstance: MapJetPlugin | undefined = this.plugins.get(pluginId);

Expand Down Expand Up @@ -121,6 +129,10 @@ export class MapJet {
}

public hasPlugin(plugin: string | MapJetPlugin): boolean {
if (plugin == null) {
return false;
}

const pluginId: string = typeof plugin === 'object' ? plugin.id : plugin;

return this.plugins.has(pluginId);
Expand Down

0 comments on commit 0267228

Please sign in to comment.