Skip to content

Commit

Permalink
Merge pull request #664 from OpenGeoscience/map-coverage
Browse files Browse the repository at this point in the history
Improve some errors and some coverage of map.js.
  • Loading branch information
manthey authored Feb 1, 2017
2 parents 5360086 + 8447eac commit edebe10
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 23 deletions.
24 changes: 20 additions & 4 deletions karma-cov.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,35 @@ var path = require('path');
* Return URL friendly browser string
*/
function browser(b) {
/* The browser string starts with the basic browser name (Firefox, PhantomJS,
* etc. Split on the first space or dash to isolate this name. */
return b.toLowerCase().split(/[ /-]/)[0];
}

function subdir_name(b) {
var subdir = browser(b);
if (process.env.GEOJS_TEST_CASE) {
/* Use thes test case and as part of the name so that different tests end
* up in different coverage directories. Get the last element of the path
* without the extension from the test case for this purpose. */
var parts = /^(.+\/)*(([^\/]+)\.[^\/.]*|[^\/.]+)$/.exec(process.env.GEOJS_TEST_CASE);
if (parts) {
subdir += '_' + (parts[3] || parts[2]);
}
}
return subdir;
}

module.exports = function (config) {
var karma_config = require('./karma-base')(config);

karma_config.reporters = ['progress', 'coverage'];
karma_config.coverageReporter = {
reporters: [
{type: 'html', dir: 'dist/coverage/', subdir: browser},
{type: 'cobertura', dir: 'dist/cobertura/', file: 'coverage.xml', subdir: browser},
{type: 'json', dir: 'dist/coverage/json/', subdir: browser},
{type: 'lcovonly', dir: 'lcov', subdir: browser},
{type: 'html', dir: 'dist/coverage/', subdir: subdir_name},
{type: 'cobertura', dir: 'dist/cobertura/', file: 'coverage.xml', subdir: subdir_name},
{type: 'json', dir: 'dist/coverage/json/', subdir: subdir_name},
{type: 'lcovonly', dir: 'lcov', subdir: subdir_name},
{type: 'text'}
]
};
Expand Down
2 changes: 1 addition & 1 deletion src/d3/d3Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ var d3Renderer = function (arg) {
m_width = width;
m_height = height;
if (!m_width || !m_height) {
throw 'Map layer has size 0';
throw new Error('Map layer has size 0');
}
m_diagonal = Math.pow(width * width + height * height, 0.5);
m_corners = {
Expand Down
2 changes: 1 addition & 1 deletion src/fileReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var fileReader = function (arg) {
arg = arg || {};

if (!(arg.layer instanceof featureLayer)) {
throw 'fileReader must be given a feature layer';
throw new Error('fileReader must be given a feature layer');
}

var m_layer = arg.layer;
Expand Down
35 changes: 20 additions & 15 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,26 +860,31 @@ var map = function (arg) {

////////////////////////////////////////////////////////////////////////////
/**
* Attach a file reader to a layer in the map to be used as a drop target.
* Get, set, or create and set a file reader to a layer in the map to be used
* as a drop target.
*
* @param {string|object|undefined} readerOrName: undefined to get the
* current reader, an instance of a file reader to set the reader, or a
* name to create a file reader (see utils.createFileReader for options).
* @param {object} opts: options for creating a file reader. If this
* includes layer, use that layer, otherwise create a layer using these
* options.
*/
////////////////////////////////////////////////////////////////////////////
this.fileReader = function (readerType, opts) {
var layer, renderer;
opts = opts || {};
if (!readerType) {
this.fileReader = function (readerOrName, opts) {
if (readerOrName === undefined) {
return m_fileReader;
}
layer = opts.layer;
if (!layer) {
renderer = opts.renderer;
if (!renderer) {
renderer = 'd3';
if (typeof readerOrName === 'string') {
opts = opts || {};
if (!opts.layer) {
opts.layer = m_this.createLayer('feature', $.extend({}, opts));
}
layer = m_this.createLayer('feature', {renderer: renderer});
opts.renderer = opts.layer.renderer().api();
m_fileReader = registry.createFileReader(readerOrName, opts);
} else {
m_fileReader = readerOrName;
}
opts.layer = layer;
opts.renderer = renderer;
m_fileReader = registry.createFileReader(readerType, opts);
return m_this;
};

Expand All @@ -891,7 +896,7 @@ var map = function (arg) {
this._init = function () {

if (m_node === undefined || m_node === null) {
throw 'Map require DIV node';
throw new Error('Map require DIV node');
}

m_node.addClass('geojs-map');
Expand Down
4 changes: 2 additions & 2 deletions src/ui/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var widget = function (arg) {
arg.position = arg.position === undefined ? { left: 0, top: 0 } : arg.position;

if (arg.parent !== undefined && !(arg.parent instanceof widget)) {
throw 'Parent must be of type geo.gui.widget';
throw new Error('Parent must be of type geo.gui.widget');
}

this._init = function () {
Expand Down Expand Up @@ -88,7 +88,7 @@ var widget = function (arg) {
*/
////////////////////////////////////////////////////////////////////////////
this._createCanvas = function () {
throw 'Must be defined in derived classes';
throw new Error('Must be defined in derived classes');
};

////////////////////////////////////////////////////////////////////////////
Expand Down
39 changes: 39 additions & 0 deletions tests/cases/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ describe('geo.core.map', function () {
m.rotation(17);
expect(m.rotation()).toBeCloseTo(17 - Math.PI * 4);
});
it('fileReader', function () {
var m = create_map();
expect(m.fileReader()).toBe(null);
var layerCount = m.layers().length;
expect(m.fileReader('jsonReader')).toBe(m);
expect(m.fileReader()).not.toBe(null);
expect(m.layers().length).toBe(layerCount + 1);
expect(m.layers()[m.layers().length - 1].renderer().api()).not.toBe('d3');
expect(m.fileReader('jsonReader', {renderer: 'd3'})).toBe(m);
expect(m.layers()[m.layers().length - 1].renderer().api()).toBe('d3');
var r = geo.createFileReader('jsonReader', {layer: m.layers()[m.layers().length - 1]});
expect(m.fileReader(r)).toBe(m);
expect(m.fileReader()).toBe(r);
});
});

describe('Public utility methods', function () {
Expand Down Expand Up @@ -584,5 +598,30 @@ describe('geo.core.map', function () {
$(window).trigger('resize');
expect(m.size()).toEqual({width: 400, height: 400});
});
it('dragover', function () {
var m = create_map();
var evt = $.Event('dragover');
evt.originalEvent = new window.Event('dragover');
evt.originalEvent.dataTransfer = {};
$(m.node()).trigger(evt);
expect(evt.originalEvent.dataTransfer.dropEffect).not.toBe('copy');
m.fileReader('jsonReader');
evt = $.Event('dragover');
evt.originalEvent = new window.Event('dragover');
evt.originalEvent.dataTransfer = {};
$(m.node()).trigger(evt);
expect(evt.originalEvent.dataTransfer.dropEffect).toBe('copy');
});
it('drop', function () {
var m = create_map();
m.fileReader('jsonReader', {renderer: 'd3'});
var evt = $.Event('drop');
evt.originalEvent = new window.Event('drop');
evt.originalEvent.dataTransfer = {files: [{
geometry: {coordinates: [1, 2], type: 'Point'}, type: 'Feature'
}]};
$(m.node()).trigger(evt);
expect(m.layers()[0].features().length).toBe(1);
});
});
});

0 comments on commit edebe10

Please sign in to comment.