diff --git a/package.json b/package.json index 7b22ab7..1493cff 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "dependencies": { "@types/geojson": "7946.0.7", "@types/jest": "24.0.11", - "@types/json-schema": "7.0.3", "@types/node": "11.13.2", "@types/shpjs": "3.4.0", "geostyler-data": "1.0.0", diff --git a/src/ShapefileDataParser.spec.ts b/src/ShapefileDataParser.spec.ts index 52afd95..f73b0ba 100644 --- a/src/ShapefileDataParser.spec.ts +++ b/src/ShapefileDataParser.spec.ts @@ -69,24 +69,55 @@ beforeEach(() => { parser = new ShapefileDataParser(); }); -it('ShapefileDataParser is defined', () => { - expect(ShapefileDataParser).toBeDefined(); -}); +describe('ShapefileDataParser', () => { + + it('…is defined', () => { + expect(ShapefileDataParser).toBeDefined(); + }); -describe('ShapefileDataParser implements DataParser', () => { + describe('implements DataParser', () => { + + it('…readData is defined', () => { + expect(parser.readData).toBeDefined(); + }); - it('readData is defined', () => { - expect(parser.readData).toBeDefined(); }); -}); + describe('constructor', () => { -describe('readData implementation', () => { + it('…creates a GeoJsonDataParser with the passed projections', () => { + const parserWithProjection = new ShapefileDataParser('EPSG:3857', 'EPSG:4326'); + expect(parserWithProjection._geoJsonParser.sourceProjection).toBe('EPSG:3857'); + expect(parserWithProjection._geoJsonParser.targetProjection).toBe('EPSG:4326'); + }); - it('can read a Buffer', async () => { - const buffer = readFileSync(path.resolve(__dirname, '../data/point.zip')); - const data = await parser.readData(buffer); - expect(data).toEqual(expectedData); }); + describe('readData implementation', () => { + + it('…can read a Buffer', async () => { + const buffer = readFileSync(path.resolve(__dirname, '../data/point.zip')); + const data = await parser.readData(buffer); + expect(data).toEqual(expectedData); + }); + + it('…rejects the promise if called with invalid (Array)Buffer', (done) => { + expect.assertions(1); + const buffer = new ArrayBuffer(0); + parser.readData(buffer) + .catch((e) => { + expect(e).toBeDefined(); + }).finally(done); + }); + + it('…rejects the promise if called with invalid Argument', (done) => { + expect.assertions(1); + const buffer = undefined as unknown as ArrayBuffer; + parser.readData(buffer) + .catch((e) => { + expect(e).toBeDefined(); + }).finally(done); + }); + + }); }); diff --git a/src/ShapefileDataParser.ts b/src/ShapefileDataParser.ts index 2eee50a..46d8e3c 100644 --- a/src/ShapefileDataParser.ts +++ b/src/ShapefileDataParser.ts @@ -1,4 +1,5 @@ import { DataParser, VectorData } from 'geostyler-data'; +import { GeoJSON } from 'geojson'; import GeoJsonDataParser from 'geostyler-geojson-parser'; // @ts-ignore // Typing is currently wrong. See PR: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/34623 import shpjs from 'shpjs'; @@ -15,39 +16,25 @@ export class ShapefileDataParser implements DataParser { title = 'Shapefile Data Parser'; - sourceProjection: string; - - targetProjection: string; - _geoJsonParser: any; constructor(sourceProj?: string, targetProj?: string) { - this._geoJsonParser = new GeoJsonDataParser(); - if (sourceProj && targetProj) { - this.sourceProjection = sourceProj; - this.targetProjection = targetProj; - } + this._geoJsonParser = new GeoJsonDataParser(sourceProj, targetProj); } /** * * @param inputData */ - async readData(array: string | Buffer | ArrayBuffer): Promise { + readData(array: Buffer | ArrayBuffer): Promise { return new Promise((resolve, reject) => { - try { - shpjs(array) - .then((geojson: any) => { - if (geojson.type === 'FeatureCollection') { - resolve(this._geoJsonParser.readData(geojson)); - } - }) - .catch((e: any) => { - reject(e); - }); - } catch (e) { - reject(e); - } + shpjs(array) + .then((geojson: GeoJSON) => { + resolve(this._geoJsonParser.readData(geojson)); + }) + .catch((e: any) => { + reject(e); + }); }); }