cartodb-4.42/lib/assets/test/spec/deep-insights/widgets-service.spec.js
2024-04-06 05:25:13 +00:00

329 lines
10 KiB
JavaScript

var specHelper = require('./spec-helper');
var WidgetsService = require('../../../javascripts/deep-insights/widgets-service');
var WidgetsCollection = require('../../../javascripts/deep-insights/widgets/widgets-collection');
describe('widgets-service', function () {
beforeEach(function () {
this.vis = specHelper.createDefaultVis();
this.widgetsCollection = new WidgetsCollection();
this.widgetsService = new WidgetsService(this.widgetsCollection, this.vis.dataviews);
this.a0 = this.vis.analysis.findNodeById('a0');
});
it('should return the WidgetsService instance', function () {
expect(this.widgetsService).toBeDefined();
});
describe('.get', function () {
it('should return the corresponding widgetModel for given id', function () {
expect(this.widgetsService.get('some-id')).toBeUndefined();
var aWidgetModel = this.widgetsCollection.add({
id: 'some-id'
});
expect(this.widgetsService.get('some-id')).toBe(aWidgetModel);
});
});
describe('.createCategoryModel', function () {
describe('when given valid input', function () {
beforeEach(function () {
var attrs = {
id: 'abc-123',
title: 'some_title',
column: 'my_column',
aggregation: 'avg',
prefix: '$',
suffix: ' people',
source: this.a0
};
this.widgetModel = this.widgetsService.createCategoryModel(attrs, this.vis.map.layers.first());
});
it('should return a category widget model', function () {
expect(this.widgetModel).toBeDefined();
});
it('should have id', function () {
expect(this.widgetModel.id).toEqual('abc-123');
});
it('should have a title', function () {
expect(this.widgetModel.get('title')).toEqual('some_title');
});
it('should have a column', function () {
expect(this.widgetModel.dataviewModel.get('column')).toEqual('my_column');
});
it('should have an aggregation operation', function () {
expect(this.widgetModel.dataviewModel.get('aggregation')).toEqual('avg');
});
it('should have a suffix text', function () {
expect(this.widgetModel.get('suffix')).toEqual(' people');
});
it('should have a prefix text', function () {
expect(this.widgetModel.get('prefix')).toEqual('$');
});
it('should enable dataview by default', function () {
expect(this.widgetModel.dataviewModel.get('sync_on_bbox_change')).toBe(true);
expect(this.widgetModel.dataviewModel.get('enabled')).toBe(true);
});
});
describe('when given custom sync options and layer visibility', function () {
beforeEach(function () {
var attrs = {
id: 'abc-123',
title: 'some_title',
column: 'my_column',
aggregation: 'avg',
suffix: ' people',
sync_on_bbox_change: false,
source: this.a0
};
var layer = this.vis.map.layers.first();
layer.set('visible', false);
this.widgetModel = this.widgetsService.createCategoryModel(attrs, layer);
});
it('should take them into account', function () {
expect(this.widgetModel.dataviewModel.get('sync_on_bbox_change')).toBe(false);
expect(this.widgetModel.dataviewModel.get('enabled')).toBe(true);
});
});
it('when no aggregation specified should use the default operation', function () {
this.widgetModel = this.widgetsService.createCategoryModel({
title: 'some_title',
column: 'my_column',
source: this.a0
}, this.vis.map.layers.first());
expect(this.widgetModel.dataviewModel.get('aggregation')).toEqual('count');
});
describe('fails when the input has no', function () {
it('title', function () {
expect(function () {
this.widgetModel = this.widgetsService.createCategoryModel({
column: 'my_column'
}, this.vis.map.layers.first());
}).toThrowError();
});
it('column', function () {
expect(function () {
this.widgetModel = this.widgetsService.createCategoryModel({
title: 'some_title'
}, this.vis.map.layers.first());
}).toThrowError();
});
});
});
describe('.createHistogramModel', function () {
describe('when given valid input', function () {
beforeEach(function () {
var attrs = {
id: 'abc-123',
title: 'my histogram',
column: 'a_column',
source: this.a0,
bins: 20
};
this.widgetModel = this.widgetsService.createHistogramModel(attrs, this.vis.map.layers.first());
});
it('should return a widget model', function () {
expect(this.widgetModel).toBeDefined();
});
it('should have id', function () {
expect(this.widgetModel.id).toEqual('abc-123');
});
it('should set title', function () {
expect(this.widgetModel.get('title')).toEqual('my histogram');
});
it('should set column', function () {
expect(this.widgetModel.dataviewModel.get('column')).toEqual('a_column');
});
it('should set bins', function () {
expect(this.widgetModel.dataviewModel.get('bins')).toEqual(20);
});
it('should enable dataview by default', function () {
expect(this.widgetModel.dataviewModel.get('sync_on_bbox_change')).toBe(true);
expect(this.widgetModel.dataviewModel.get('enabled')).toBe(true);
});
});
it('when no bins specified should use the default value', function () {
this.widgetModel = this.widgetsService.createHistogramModel({
title: 'some_title',
column: 'my_column',
source: this.a0
}, this.vis.map.layers.first());
expect(this.widgetModel.dataviewModel.get('bins')).toEqual(10);
});
describe('fails when the input has no', function () {
it('title', function () {
expect(function () {
this.widgetModel = this.widgetsService.createHistogramModel({
column: 'my_column'
}, this.vis.map.layers.first());
}).toThrowError();
});
it('column', function () {
expect(function () {
this.widgetModel = this.widgetsService.createHistogramModel({
title: 'some_title'
}, this.vis.map.layers.first());
}).toThrowError();
});
});
});
describe('.createFormulaModel', function () {
describe('when given valid input', function () {
beforeEach(function () {
var attrs = {
id: 'abc-123',
title: 'my formula',
column: 'a_column',
source: this.a0,
operation: 'sum',
prefix: '$',
suffix: '¢'
};
this.widgetModel = this.widgetsService.createFormulaModel(attrs, this.vis.map.layers.first());
});
it('should return a widget model', function () {
expect(this.widgetModel).toBeDefined();
});
it('should have id', function () {
expect(this.widgetModel.id).toEqual('abc-123');
});
it('should set title', function () {
expect(this.widgetModel.get('title')).toEqual('my formula');
});
it('should set column', function () {
expect(this.widgetModel.dataviewModel.get('column')).toEqual('a_column');
});
it('should set operation', function () {
expect(this.widgetModel.dataviewModel.get('operation')).toEqual('sum');
});
it('should have a default suffix text', function () {
expect(this.widgetModel.get('suffix')).toEqual('¢');
});
it('should have a prefix text', function () {
expect(this.widgetModel.get('prefix')).toEqual('$');
});
it('should enable dataview by default', function () {
expect(this.widgetModel.dataviewModel.get('sync_on_bbox_change')).toBe(true);
expect(this.widgetModel.dataviewModel.get('enabled')).toBe(true);
});
});
describe('fails when the input has no', function () {
it('title', function () {
expect(function () {
this.widgetModel = this.widgetsService.createFormulaModel({
id: 'abc-123',
column: 'my_column',
operation: 'sum'
}, this.vis.map.layers.first());
}).toThrowError();
});
it('column', function () {
expect(function () {
this.widgetModel = this.widgetsService.createFormulaModel({
title: 'some_title',
operation: 'sum'
}, this.vis.map.layers.first());
}).toThrowError();
});
it('operation', function () {
expect(function () {
this.widgetModel = this.widgetsService.createFormulaModel({
title: 'some_title',
column: 'my_column'
}, this.vis.map.layers.first());
}).toThrowError();
});
});
});
describe('.createTimeSeriesModel', function () {
describe('when given valid input', function () {
beforeEach(function () {
var attrs = {
id: 'abc-123',
column: 'dates',
bins: 50,
start: 0,
end: 10,
source: this.a0
};
this.widgetModel = this.widgetsService.createTimeSeriesModel(attrs, this.vis.map.layers.first());
});
it('should return a widget model', function () {
expect(this.widgetModel).toBeDefined();
});
it('should have id', function () {
expect(this.widgetModel.id).toEqual('abc-123');
});
it('should set column', function () {
expect(this.widgetModel.dataviewModel.get('column')).toEqual('dates');
});
it('should be backed up by a histogram dataview model', function () {
expect(this.widgetModel.dataviewModel.get('type')).toEqual('histogram');
});
it('should have normalized attribute', function () {
expect(this.widgetModel.get('normalized')).toBeDefined();
});
it('should have animated attribute', function () {
expect(this.widgetModel.get('animated')).toBeDefined();
});
it('should enable dataview by default', function () {
expect(this.widgetModel.dataviewModel.get('sync_on_bbox_change')).toBe(true);
expect(this.widgetModel.dataviewModel.get('enabled')).toBe(true);
});
});
});
describe('fails when the input has no', function () {
it('column', function () {
expect(function () {
this.widgetModel = this.widgetsService.createTimeSeriesModel({
title: 'some_title'
}, this.vis.map.layers.first());
}).toThrowError();
});
});
});