Initial commit

This commit is contained in:
zhongjin
2020-06-13 18:34:34 +08:00
commit 52aaa9f15d
655 changed files with 96796 additions and 0 deletions
+240
View File
@@ -0,0 +1,240 @@
var _ = require('underscore');
var CartoDBLayer = require('../../../../src/geo/map/cartodb-layer');
var sharedTestsForInteractiveLayers = require('./shared-for-interactive-layers');
var createEngine = require('../../fixtures/engine.fixture.js');
describe('geo/map/cartodb-layer', function () {
var engineMock;
beforeEach(function () {
engineMock = createEngine();
});
sharedTestsForInteractiveLayers(CartoDBLayer);
it('should be type CartoDB', function () {
var layer = new CartoDBLayer({}, { engine: engineMock });
expect(layer.get('type')).toEqual('CartoDB');
});
it('should expose infowindow and tooltip properties', function () {
var layer = new CartoDBLayer({}, { engine: engineMock });
expect(layer.infowindow).toBeDefined();
expect(layer.tooltip).toBeDefined();
});
describe('vis reloading', function () {
var ATTRIBUTES = ['sql', 'source', 'sql_wrap', 'cartocss'];
_.each(ATTRIBUTES, function (attribute) {
it("should reload the vis when '" + attribute + "' attribute changes", function () {
var layer = new CartoDBLayer({}, { engine: engineMock });
layer.set(attribute, 'new_value');
expect(engineMock.reload).toHaveBeenCalled();
});
});
it('should reload the map just once when multiple attributes change', function () {
var layer = new CartoDBLayer({}, { engine: engineMock });
var newAttributes = {};
_.each(ATTRIBUTES, function (attr, index) {
newAttributes[attr] = 'new_value_' + index;
});
layer.set(newAttributes);
expect(engineMock.reload).toHaveBeenCalled();
expect(engineMock.reload.calls.count()).toEqual(1);
});
describe('popups changes', function () {
var layer;
beforeEach(function () {
layer = new CartoDBLayer({}, { engine: engineMock });
});
describe('infowindow bind', function () {
it('should reload the map when infowindow fields are reset', function () {
layer.infowindow.fields.reset([
{
'name': 'name',
'title': true,
'position': 1
}
]);
expect(engineMock.reload).toHaveBeenCalled();
});
it('should reload the map when infowindow fields are added', function () {
layer.infowindow.fields.add({
'name': 'name',
'title': true,
'position': 1
});
expect(engineMock.reload).toHaveBeenCalled();
});
it('should reload the map when infowindow fields are removed', function () {
layer.infowindow.fields.add({
'name': 'name',
'title': true,
'position': 1
});
engineMock.reload.calls.reset();
layer.infowindow.fields.add({
'name': 'name',
'title': true,
'position': 1
});
layer.infowindow.fields.remove(layer.infowindow.fields.at(0));
expect(engineMock.reload).toHaveBeenCalled();
});
});
describe('tooltip bind', function () {
it('should reload the map when tooltip fields are reset', function () {
layer.tooltip.fields.reset([
{
'name': 'hello',
'title': false,
'position': 1
}
]);
expect(engineMock.reload).toHaveBeenCalled();
});
it('should reload the map when tooltip fields are added', function () {
layer.tooltip.fields.add({
'name': 'pericamen',
'title': false,
'position': 1
});
expect(engineMock.reload).toHaveBeenCalled();
});
it('should reload the map when tooltip fields are removed', function () {
layer.tooltip.fields.add({
'name': 'rename',
'title': true,
'position': 1
});
engineMock.reload.calls.reset();
layer.tooltip.fields.add({
'name': 'name',
'title': true,
'position': 1
});
layer.tooltip.fields.remove(layer.tooltip.fields.at(0));
expect(engineMock.reload).toHaveBeenCalled();
});
});
});
});
describe('.isInteractive', function () {
var layer, infowindowSpy, tooltipSpy;
beforeEach(function () {
layer = new CartoDBLayer({}, { engine: engineMock });
infowindowSpy = spyOn(layer, '_hasInfowindowFields');
tooltipSpy = spyOn(layer, '_hasTooltipFields');
});
it('returns true if there are selected fields', function () {
infowindowSpy.and.returnValue(true);
tooltipSpy.and.returnValue(false);
expect(layer.isInteractive()).toBe(true);
infowindowSpy.and.returnValue(false);
tooltipSpy.and.returnValue(true);
expect(layer.isInteractive()).toBe(true);
});
it('returns true if there are selected fields', function () {
infowindowSpy.and.returnValue(false);
tooltipSpy.and.returnValue(false);
expect(layer.isInteractive()).toBe(false);
});
});
describe('.getInteractiveColumnNames', function () {
it("should return 'cartodb_id' and the names of the fields for infowindows and tooltips with no duplicates", function () {
this.layer = new CartoDBLayer({
infowindow: {
fields: [
{ name: 'a' },
{ name: 'b' }
]
},
tooltip: {
fields: [
{ name: 'b' },
{ name: 'c' }
]
}
}, { engine: engineMock });
expect(this.layer.getInteractiveColumnNames()).toEqual(['cartodb_id', 'a', 'b', 'c']);
});
it("should return the 'cartodb_id' if no fields are present", function () {
this.layer = new CartoDBLayer({
infowindow: {
fields: []
},
tooltip: {
fields: []
}
}, { engine: engineMock });
expect(this.layer.getInteractiveColumnNames()).toEqual(['cartodb_id']);
});
});
describe('.getEstimatedFeatureCount', function () {
var layer;
beforeEach(function () {
layer = new CartoDBLayer({}, { engine: engineMock });
});
it('should return undefined when there is no meta information', function () {
layer.set('meta', {
stats: {}
});
expect(layer.getEstimatedFeatureCount()).toBeUndefined();
});
it('should return the number of features extracted from the meta-information when the layer is visible', function () {
layer.show();
layer.set('meta', {
stats: {
estimatedFeatureCount: 27
}
});
expect(layer.getEstimatedFeatureCount()).toEqual(27);
});
it('should return the number of features extracted from the meta-information when the layer is invisible', function () {
layer.hide();
layer.set('meta', {
stats: {
estimatedFeatureCount: 27
}
});
expect(layer.getEstimatedFeatureCount()).toEqual(27);
});
});
});
@@ -0,0 +1,8 @@
var GMapsBaseLayer = require('../../../../src/geo/map/gmaps-base-layer');
describe('GMapsBaseLayer', function () {
it('should be type GMapsBase', function () {
var layer = new GMapsBaseLayer();
expect(layer.get('type')).toEqual('GMapsBase');
});
});
@@ -0,0 +1,106 @@
var InfowindowTemplate = require('../../../../src/geo/map/infowindow-template');
describe('geo/map/infowindow-template', function () {
describe('.update', function () {
beforeEach(function () {
this.callback = jasmine.createSpy('callback');
this.infowindowTemplate = new InfowindowTemplate({
fields: [
{ name: 'Name', title: true, position: 1 }
]
});
this.infowindowTemplate.fields.bind('reset', this.callback, this);
});
it('should reset the fields when new fields are given', function () {
this.infowindowTemplate.update({
fields: [
{ name: 'Name', title: true, position: 1 },
{ name: 'Description', title: false, position: 2 }
]
});
expect(this.infowindowTemplate.fields.toJSON()).toEqual([
{ name: 'Name', title: true, position: 1 },
{ name: 'Description', title: false, position: 2 }
]);
expect(this.callback).toHaveBeenCalled();
});
it('should NOT reset the fields when given the same fields', function () {
this.infowindowTemplate.update({
fields: [
{ name: 'Name', title: true, position: 1 }
]
});
expect(this.infowindowTemplate.fields.toJSON()).toEqual([
{ name: 'Name', title: true, position: 1 }
]);
expect(this.callback).not.toHaveBeenCalled();
});
it('should NOT reset the fields when given the same fields with different `position`', function () {
this.infowindowTemplate.update({
fields: [
{ name: 'Name', title: true, position: 0 }
]
});
expect(this.infowindowTemplate.fields.toJSON()).toEqual([
{ name: 'Name', title: true, position: 1 }
]);
expect(this.callback).not.toHaveBeenCalled();
});
it('should reset the fields when given the same fields with different `position` and order', function () {
this.infowindowTemplate.update({
fields: [
{ name: 'Name', title: true, position: 0 },
{ name: 'Description', title: true, position: 1 }
]
});
expect(this.callback).toHaveBeenCalled();
this.callback.calls.reset();
this.infowindowTemplate.update({
fields: [
{ name: 'Description', title: true, position: 0 },
{ name: 'Name', title: true, position: 1 }
]
});
expect(this.callback).toHaveBeenCalled();
});
it('should clone alternative names', function () {
var callback = jasmine.createSpy('callback');
this.infowindowTemplate.bind('change', callback);
var alternativeNames = {
'name': 'Nombre'
};
this.infowindowTemplate.update({
alternative_names: alternativeNames
});
expect(callback).toHaveBeenCalled();
callback.calls.reset();
// Original object with names is changed
alternativeNames.description = 'Descripción';
this.infowindowTemplate.update({
alternative_names: alternativeNames
});
// Change event has been triggered
expect(callback).toHaveBeenCalled();
});
});
});
@@ -0,0 +1,85 @@
var LayerModelBase = require('../../../../src/geo/map/layer-model-base');
var MyLayer = LayerModelBase.extend({});
describe('geo/map/layer-model-base.js', function () {
beforeEach(function () {
this.layer = new MyLayer();
});
describe('.remove', function () {
it('should trigger a destroy event with options', function () {
var callback = jasmine.createSpy('callback');
var collection = {};
var option = { option: 'one' };
this.layer.bind('destroy', callback);
this.layer.collection = collection;
this.layer.remove(option);
expect(callback).toHaveBeenCalledWith(this.layer, this.layer.collection, option);
});
});
describe('.update', function () {
it('should set the attributes', function () {
this.layer.update({
a: 1,
b: 2
});
expect(this.layer.get('a')).toEqual(1);
expect(this.layer.get('b')).toEqual(2);
});
});
describe('.show', function () {
it('should set the visible attribute to true', function () {
this.layer.set('visible', false);
this.layer.show();
expect(this.layer.get('visible')).toBeTruthy();
});
});
describe('.hide', function () {
it('should set the visible attribute to false', function () {
this.layer.set('visible', true);
this.layer.hide();
expect(this.layer.get('visible')).toBeFalsy();
});
});
describe('.toggle', function () {
it('should toggel the visible attribute', function () {
this.layer.set('visible', false);
this.layer.toggle();
expect(this.layer.get('visible')).toBeTruthy();
this.layer.toggle();
expect(this.layer.get('visible')).toBeFalsy();
});
});
describe('.setOk', function () {
it('should unset error attribute', function () {
this.layer.set('error', 'error');
this.layer.setOk();
expect(this.layer.get('error')).toBeUndefined();
});
});
describe('.setError', function () {
it('should set error attribute', function () {
this.layer.setError('wadus');
expect(this.layer.get('error')).toEqual('wadus');
});
});
});
+164
View File
@@ -0,0 +1,164 @@
var Layers = require('../../../../src/geo/map/layers');
var PlainLayer = require('../../../../src/geo/map/plain-layer');
var TileLayer = require('../../../../src/geo/map/tile-layer');
var TorqueLayer = require('../../../../src/geo/map/torque-layer');
var CartoDBLayer = require('../../../../src/geo/map/cartodb-layer');
var createEngine = require('../../fixtures/engine.fixture.js');
describe('geo/map/layers', function () {
var layers;
var engineMock;
beforeEach(function () {
engineMock = createEngine();
layers = new Layers();
});
it('should re-assign order when new layers are added to the collection', function () {
var baseLayer = new TileLayer(null, { engine: {} });
var layer1 = new CartoDBLayer({}, { engine: engineMock });
var layer2 = new CartoDBLayer({}, { engine: engineMock });
var layer3 = new CartoDBLayer({}, { engine: engineMock });
// Sets the order to 0
layers.add(baseLayer);
expect(baseLayer.get('order')).toEqual(0);
// Sets the order to 1
layers.add(layer1);
layers.add(layer2);
expect(baseLayer.get('order')).toEqual(0);
expect(layer1.get('order')).toEqual(1);
expect(layer2.get('order')).toEqual(2);
expect(layers.pluck('order')).toEqual([ 0, 1, 2 ]);
// Sets the order to 1 and re-orders the rest of the layers
layers.add(layer3, { at: 1 });
expect(baseLayer.get('order')).toEqual(0);
expect(layer1.get('order')).toEqual(2);
expect(layer2.get('order')).toEqual(3);
expect(layer3.get('order')).toEqual(1);
expect(layers.pluck('order')).toEqual([ 0, 1, 2, 3 ]);
var torqueLayer = new TorqueLayer({}, { engine: engineMock });
// Torque layer should be at the top
layers.add(torqueLayer);
expect(baseLayer.get('order')).toEqual(0);
expect(layer1.get('order')).toEqual(2);
expect(layer2.get('order')).toEqual(3);
expect(layer3.get('order')).toEqual(1);
expect(torqueLayer.get('order')).toEqual(4);
expect(layers.pluck('order')).toEqual([ 0, 1, 2, 3, 4 ]);
var tiledLayer = new TileLayer(null, { engine: {} });
// Tiled layer should be at the top
layers.add(tiledLayer);
expect(baseLayer.get('order')).toEqual(0);
expect(layer1.get('order')).toEqual(2);
expect(layer2.get('order')).toEqual(3);
expect(layer3.get('order')).toEqual(1);
expect(torqueLayer.get('order')).toEqual(4);
expect(tiledLayer.get('order')).toEqual(5);
expect(layers.pluck('order')).toEqual([ 0, 1, 2, 3, 4, 5 ]);
var layer4 = new CartoDBLayer({}, { engine: engineMock });
layers.add(layer4);
expect(baseLayer.get('order')).toEqual(0);
expect(layer1.get('order')).toEqual(2);
expect(layer2.get('order')).toEqual(3);
expect(layer3.get('order')).toEqual(1);
expect(layer4.get('order')).toEqual(4);
expect(torqueLayer.get('order')).toEqual(5);
expect(tiledLayer.get('order')).toEqual(6);
expect(layers.pluck('order')).toEqual([ 0, 1, 2, 3, 4, 5, 6 ]);
});
it('should re-assign order when new layers are removed from the collection', function () {
var baseLayer = new TileLayer(null, { engine: {} });
var layer1 = new CartoDBLayer({}, { engine: engineMock });
var layer2 = new CartoDBLayer({}, { engine: engineMock });
var torqueLayer = new TorqueLayer({}, { engine: engineMock });
var labelsLayer = new TileLayer(null, { engine: {} });
// Sets the order to 0
layers.add(baseLayer);
layers.add(layer1);
layers.add(layer2);
layers.add(torqueLayer);
layers.add(labelsLayer);
expect(baseLayer.get('order')).toEqual(0);
expect(layer1.get('order')).toEqual(1);
expect(layer2.get('order')).toEqual(2);
expect(torqueLayer.get('order')).toEqual(3);
expect(labelsLayer.get('order')).toEqual(4);
expect(layers.pluck('order')).toEqual([ 0, 1, 2, 3, 4 ]);
layers.remove(layer1);
expect(baseLayer.get('order')).toEqual(0);
expect(layer2.get('order')).toEqual(1);
expect(torqueLayer.get('order')).toEqual(2);
expect(labelsLayer.get('order')).toEqual(3);
expect(layers.pluck('order')).toEqual([ 0, 1, 2, 3 ]);
layers.remove(torqueLayer);
expect(baseLayer.get('order')).toEqual(0);
expect(layer2.get('order')).toEqual(1);
expect(labelsLayer.get('order')).toEqual(2);
expect(layers.pluck('order')).toEqual([ 0, 1, 2 ]);
layers.remove(labelsLayer);
expect(baseLayer.get('order')).toEqual(0);
expect(layer2.get('order')).toEqual(1);
expect(layers.pluck('order')).toEqual([ 0, 1 ]);
});
describe('.moveCartoDBLayer', function () {
beforeEach(function () {
layers.add(new PlainLayer({ name: 'Positron' }, { engine: {} }));
layers.add(new CartoDBLayer({ title: 'CARTO' }, { engine: engineMock }));
});
it('should move a layer from one position to other', function () {
var movedLayer = layers.moveCartoDBLayer(1, 0);
expect(layers.indexOf(movedLayer)).toBe(0);
expect(movedLayer.get('title')).toBe('CARTO');
layers.add(new CartoDBLayer({ title: 'CARTO 2' }, { engine: engineMock }));
movedLayer = layers.moveCartoDBLayer(2, 1);
expect(layers.indexOf(movedLayer)).toBe(1);
expect(movedLayer.get('title')).toBe('CARTO 2');
});
it('should move a layer from one position to other', function () {
spyOn(layers, 'trigger');
var movedLayer = layers.moveCartoDBLayer(1, 0);
expect(layers.trigger).toHaveBeenCalledWith('layerMoved', movedLayer);
});
it('should not move anything if the position is the same', function () {
var movedLayer = layers.moveCartoDBLayer(1, 1);
expect(movedLayer).toBeFalsy();
expect(layers.at(1).get('title')).toBe('CARTO');
});
it('should not move the layer if it is not a CartoDB type', function () {
var movedLayer = layers.moveCartoDBLayer(0, 1);
expect(movedLayer).toBeFalsy();
expect(layers.at(1).get('title')).toBe('CARTO');
});
});
});
@@ -0,0 +1,24 @@
var LegendModelBase = require('../../../../../src/geo/map/legends/legend-model-base');
var createEngine = require('../../../fixtures/engine.fixture.js');
var Engine = require('../../../../../src/engine');
var MyLegendModel = LegendModelBase.extend({ TYPE: 'type' });
describe('src/geo/map/legends/legend-model-base', function () {
var engineMock;
var legendModel;
beforeEach(function () {
engineMock = createEngine();
legendModel = new MyLegendModel({}, { engine: engineMock });
});
it('should have a "loading" state', function () {
expect(legendModel.isLoading()).toBeTruthy();
});
it('should change state to "loading" when vis is reloading', function () {
legendModel.set('state', 'something');
expect(legendModel.isLoading()).toBeFalsy();
engineMock._eventEmmitter.trigger(Engine.Events.RELOAD_STARTED);
expect(legendModel.isLoading()).toBeTruthy();
});
});
+121
View File
@@ -0,0 +1,121 @@
var Legends = require('../../../../../src/geo/map/legends/legends');
var createEngine = require('../../../fixtures/engine.fixture.js');
describe('geo/map/legends/legends', function () {
var engineMock;
beforeEach(function () {
engineMock = createEngine();
this.legends = new Legends([
{
type: 'bubble',
title: 'Bubble Legend',
pre_html: '<p>PRE Bubble Legend</p>',
post_html: '<p>POST Bubble Legend</p>',
definition: {
color: '#FABADA'
}
},
{
type: 'category',
title: 'Categories Legend',
pre_html: '<p>PRE Category Legend</p>',
post_html: '<p>POST Category Legend</p>',
definition: {
prefix: 'prefix',
suffix: 'suffix'
}
},
{
type: 'choropleth',
title: 'Choropleth Legend',
pre_html: '<p>PRE Choropleth Legend</p>',
post_html: '<p>POST Choropleth Legend</p>',
definition: {
prefix: 'prefix',
suffix: 'suffix'
}
},
{
type: 'custom',
title: 'Custom Legend',
pre_html: '<p>PRE Custom Legend</p>',
post_html: '<p>POST Custom Legend</p>',
definition: {
categories: [
{ type: 'color', name: 'Item 1', color: '#CACACA;' },
{ type: 'color', name: 'Item 2', color: '#FABADA;' }
]
}
}
], {
engine: engineMock
});
});
it('should initialize bubble legends correctly', function () {
expect(this.legends.bubble.get('title')).toEqual('Bubble Legend');
expect(this.legends.bubble.get('preHTMLSnippet')).toEqual('<p>PRE Bubble Legend</p>');
expect(this.legends.bubble.get('postHTMLSnippet')).toEqual('<p>POST Bubble Legend</p>');
expect(this.legends.bubble.get('fillColor')).toEqual('#FABADA');
});
it('should initialize category legends correctly', function () {
expect(this.legends.category.get('title')).toEqual('Categories Legend');
expect(this.legends.category.get('preHTMLSnippet')).toEqual('<p>PRE Category Legend</p>');
expect(this.legends.category.get('postHTMLSnippet')).toEqual('<p>POST Category Legend</p>');
expect(this.legends.category.get('prefix')).toEqual('prefix');
expect(this.legends.category.get('suffix')).toEqual('suffix');
});
it('should initialize choropleth legends correctly', function () {
expect(this.legends.choropleth.get('title')).toEqual('Choropleth Legend');
expect(this.legends.choropleth.get('preHTMLSnippet')).toEqual('<p>PRE Choropleth Legend</p>');
expect(this.legends.choropleth.get('postHTMLSnippet')).toEqual('<p>POST Choropleth Legend</p>');
expect(this.legends.choropleth.get('prefix')).toEqual('prefix');
expect(this.legends.choropleth.get('suffix')).toEqual('suffix');
});
it('should initialize custom legends correctly', function () {
expect(this.legends.custom.get('title')).toEqual('Custom Legend');
expect(this.legends.custom.get('preHTMLSnippet')).toEqual('<p>PRE Custom Legend</p>');
expect(this.legends.custom.get('postHTMLSnippet')).toEqual('<p>POST Custom Legend</p>');
expect(this.legends.custom.get('items').length).toEqual(2);
});
it("it should 'show' all legends that have data", function () {
this.legends = new Legends([
{
type: 'bubble',
title: 'Bubble Legend',
pre_html: '<p>PRE Bubble Legend</p>',
post_html: '<p>POST Bubble Legend</p>',
definition: {
color: '#FABADA'
}
}
], {
engine: engineMock
});
expect(this.legends.bubble.isVisible()).toBeTruthy();
expect(this.legends.category.isVisible()).toBeFalsy();
this.legends = new Legends([
{
type: 'category',
title: 'Categories Legend',
pre_html: '<p>PRE Category Legend</p>',
post_html: '<p>POST Category Legend</p>',
definition: {
prefix: 'prefix',
suffix: 'suffix'
}
}
], {
engine: engineMock
});
expect(this.legends.bubble.isVisible()).toBeFalsy();
expect(this.legends.category.isVisible()).toBeTruthy();
});
});
@@ -0,0 +1,20 @@
var StaticLegendModelBase = require('../../../../../src/geo/map/legends/static-legend-model-base');
var createEngine = require('../../../fixtures/engine.fixture.js');
var Engine = require('../../../../../src/engine');
var MyLegendModel = StaticLegendModelBase.extend({ TYPE: 'type' });
describe('src/geo/map/legends/static-legend-model-base', function () {
var engineMock;
var legendModel;
beforeEach(function () {
engineMock = createEngine();
legendModel = new MyLegendModel({}, { engine: engineMock });
});
it('should never change state to "loading" when vis is reloading', function () {
legendModel.set('state', 'something');
expect(legendModel.isLoading()).toBeFalsy();
engineMock._eventEmmitter.trigger(Engine.Events.RELOAD_STARTED);
expect(legendModel.isLoading()).toBeFalsy();
});
});
+29
View File
@@ -0,0 +1,29 @@
var _ = require('underscore');
var PlainLayer = require('../../../../src/geo/map/plain-layer');
var createEngine = require('../../fixtures/engine.fixture.js');
describe('PlainLayer', function () {
var engineMock;
beforeEach(function () {
engineMock = createEngine();
});
it('should be type plain', function () {
var layer = new PlainLayer(null, { engine: engineMock });
expect(layer.get('type')).toEqual('Plain');
});
describe('vis reloading', function () {
var ATTRIBUTES = ['color', 'image'];
_.each(ATTRIBUTES, function (attribute) {
it("should reload the vis when '" + attribute + "' attribute changes", function () {
var layer = new PlainLayer(null, { engine: engineMock });
layer.set(attribute, 'new_value');
expect(engineMock.reload).toHaveBeenCalled();
});
});
});
});
@@ -0,0 +1,76 @@
var _ = require('underscore');
var MockFactory = require('../../../helpers/mockFactory');
var createEngine = require('../../fixtures/engine.fixture.js');
module.exports = function (LayerModel) {
var layer;
var source;
var engineMock;
beforeEach(function () {
source = MockFactory.createAnalysisModel({ id: 'a0' });
engineMock = createEngine();
layer = new LayerModel({source: source}, { engine: engineMock });
});
var METHODS = [
'isVisible',
'getName'
];
_.each(METHODS, function (method) {
it('should respond to .' + method, function () {
expect(typeof layer[method] === 'function').toBeTruthy();
});
});
it('should have legends', function () {
var legends = [
{ type: 'bubble', title: 'My Bubble Legend' },
{ type: 'category', title: 'My Category Legend' },
{ type: 'choropleth', title: 'My Choropleth Legend' },
{ type: 'custom', title: 'My Custom Legend' }
];
layer = new LayerModel({ legends: legends }, { engine: engineMock });
expect(layer.get('legends')).toBeUndefined();
expect(layer.legends.bubble.get('title')).toEqual('My Bubble Legend');
expect(layer.legends.category.get('title')).toEqual('My Category Legend');
expect(layer.legends.choropleth.get('title')).toEqual('My Choropleth Legend');
expect(layer.legends.custom.get('title')).toEqual('My Custom Legend');
});
describe('source references', function () {
describe('when layer is initialized', function () {
it('should mark source as referenced', function () {
expect(source.isSourceOf(layer)).toBe(true);
});
});
describe('when layer is updated', function () {
it('should unmark source and mark new source as referenced', function () {
var oldSource = source;
var newSource = MockFactory.createAnalysisModel({ id: 'a1' });
expect(oldSource.isSourceOf(layer)).toBe(true);
expect(newSource.isSourceOf(layer)).toBe(false);
layer.setSource(newSource);
expect(oldSource.isSourceOf(layer)).toBe(false);
expect(newSource.isSourceOf(layer)).toBe(true);
});
});
describe('when layer is removed', function () {
it('should unmark source as referenced', function () {
expect(source.isSourceOf(layer)).toBe(true);
layer.remove();
expect(source.isSourceOf(layer)).toBe(false);
});
});
});
};
+23
View File
@@ -0,0 +1,23 @@
var TileLayer = require('../../../../src/geo/map/tile-layer');
var createEngine = require('../../fixtures/engine.fixture.js');
describe('TileLayer', function () {
var engineMock;
var layer;
beforeEach(function () {
engineMock = createEngine();
layer = new TileLayer(null, { engine: engineMock });
});
it('should be type tiled', function () {
expect(layer.get('type')).toEqual('Tiled');
});
describe('vis reloading', function () {
it("should reload the vis when 'urlTemplate' attribute changes", function () {
layer.set('urlTemplate', 'new_value');
expect(engineMock.reload).toHaveBeenCalled();
});
});
});
+106
View File
@@ -0,0 +1,106 @@
var TooltipTemplate = require('../../../../src/geo/map/tooltip-template');
describe('geo/map/tooltip-template', function () {
describe('.update', function () {
beforeEach(function () {
this.callback = jasmine.createSpy('callback');
this.tooltipTemplate = new TooltipTemplate({
fields: [
{ name: 'Name', title: true, position: 1 }
]
});
this.tooltipTemplate.fields.bind('reset', this.callback, this);
});
it('should reset the fields when new fields are given', function () {
this.tooltipTemplate.update({
fields: [
{ name: 'Name', title: true, position: 1 },
{ name: 'Description', title: false, position: 2 }
]
});
expect(this.tooltipTemplate.fields.toJSON()).toEqual([
{ name: 'Name', title: true, position: 1 },
{ name: 'Description', title: false, position: 2 }
]);
expect(this.callback).toHaveBeenCalled();
});
it('should NOT reset the fields when given the same fields', function () {
this.tooltipTemplate.update({
fields: [
{ name: 'Name', title: true, position: 1 }
]
});
expect(this.tooltipTemplate.fields.toJSON()).toEqual([
{ name: 'Name', title: true, position: 1 }
]);
expect(this.callback).not.toHaveBeenCalled();
});
it('should NOT reset the fields when given the same fields with different `position`', function () {
this.tooltipTemplate.update({
fields: [
{ name: 'Name', title: true, position: 2 }
]
});
expect(this.tooltipTemplate.fields.toJSON()).toEqual([
{ name: 'Name', title: true, position: 1 }
]);
expect(this.callback).not.toHaveBeenCalled();
});
it('should reset the fields when given the same fields with different `position` and order', function () {
this.tooltipTemplate.update({
fields: [
{ name: 'Name', title: true, position: 0 },
{ name: 'Description', title: true, position: 1 }
]
});
expect(this.callback).toHaveBeenCalled();
this.callback.calls.reset();
this.tooltipTemplate.update({
fields: [
{ name: 'Description', title: true, position: 0 },
{ name: 'Name', title: true, position: 1 }
]
});
expect(this.callback).toHaveBeenCalled();
});
it('should clone alternative names', function () {
var callback = jasmine.createSpy('callback');
this.tooltipTemplate.bind('change', callback);
var alternativeNames = {
'name': 'Nombre'
};
this.tooltipTemplate.update({
alternative_names: alternativeNames
});
expect(callback).toHaveBeenCalled();
callback.calls.reset();
// Original object with names is changed
alternativeNames.description = 'Descripción';
this.tooltipTemplate.update({
alternative_names: alternativeNames
});
// Change event has been triggered
expect(callback).toHaveBeenCalled();
});
});
});
+100
View File
@@ -0,0 +1,100 @@
var _ = require('underscore');
var TorqueLayer = require('../../../../src/geo/map/torque-layer');
var sharedTestsForInteractiveLayers = require('./shared-for-interactive-layers');
var createEngine = require('../../fixtures/engine.fixture.js');
describe('geo/map/torque-layer', function () {
var engineMock;
beforeEach(function () {
engineMock = createEngine();
});
sharedTestsForInteractiveLayers(TorqueLayer);
describe('vis reloading', function () {
var ATTRIBUTES = ['sql', 'sql_wrap', 'source'];
_.each(ATTRIBUTES, function (attribute) {
it("should reload the vis when '" + attribute + "' attribute changes", function () {
var layer = new TorqueLayer({}, { engine: engineMock });
layer.set(attribute, 'new_value');
expect(engineMock.reload).toHaveBeenCalled();
});
});
it('should reload the map just once when multiple attributes change', function () {
var layer = new TorqueLayer({}, { engine: engineMock });
var newAttributes = {};
_.each(ATTRIBUTES, function (attr, index) {
newAttributes[attr] = 'new_value_' + index;
});
layer.set(newAttributes);
expect(engineMock.reload).toHaveBeenCalled();
expect(engineMock.reload.calls.count()).toEqual(1);
});
it('should NOT reload the map when cartocss is set and it was previously empty', function () {
var layer = new TorqueLayer({}, { engine: engineMock });
layer.set('cartocss', 'new_value');
expect(engineMock.reload).not.toHaveBeenCalled();
});
it('should NOT reload the map if cartocss property has changed and a reload is not needed', function () {
var layer = new TorqueLayer({
cartocss: 'Map { something: "a"; -torque-time-attribute: "column"; }'
}, { engine: engineMock });
layer.set('cartocss', 'Map { something: "b"; -torque-time-attribute: "column"; }');
expect(engineMock.reload).not.toHaveBeenCalled();
});
_.each([
'-torque-frame-count',
'-torque-time-attribute',
'-torque-aggregation-function',
'-torque-data-aggregation',
'-torque-resolution'
], function (property) {
it('should reload the map if cartocss attribute has changed and "' + property + '"" property has changed', function () {
var layer = new TorqueLayer({
cartocss: 'Map { something: "a"; ' + property + ': "valueA"; }'
}, { engine: engineMock });
layer.set('cartocss', 'Map { something: "b"; ' + property + ': "valueB"; }');
expect(engineMock.reload).toHaveBeenCalled();
});
});
});
describe('.getAnimationDuration', function () {
var layer;
beforeEach(function () {
layer = new TorqueLayer({}, { engine: engineMock });
});
it('should take the animation duration if it is defined', function () {
var cartocss = 'Map { -torque-animation-duration: 22; }';
expect(layer.getAnimationDuration(cartocss)).toBe(22);
cartocss = 'Map { -torque-animation-duration:15; }';
expect(layer.getAnimationDuration(cartocss)).toBe(15);
});
it('should take the defualt animation duration if the attribute isn\'t defined', function () {
var cartocss = 'Map { -torque-animation-aggregation: cartodb_id; }';
expect(layer.getAnimationDuration(cartocss)).toBe(30);
cartocss = 'Map { something: "a"; -torque-time-attribute: "column"; }';
expect(layer.getAnimationDuration(cartocss)).toBe(30);
});
});
});