Initial commit

This commit is contained in:
zhongjin
2020-06-15 10:58:47 +08:00
commit 4f1dfe7564
8590 changed files with 1516878 additions and 0 deletions
@@ -0,0 +1,31 @@
var Backbone = require('backbone');
var EditorDateView = require('builder/components/table/editors/types/editor-date-view');
describe('components/table/editors/types/editor-date-view', function () {
beforeEach(function () {
this.model = new Backbone.Model({
value: '2016-12-01T00:00:00+02:00'
});
this.editorModel = jasmine.createSpyObj('editorModel', ['confirm']);
this.view = new EditorDateView({
model: this.model,
editorModel: this.editorModel
});
this.view.render();
});
it('should render properly', function () {
expect(this.view.$('.Editor-fieldset').length).toBe(1);
expect(this.view.$('.Editor-formSelect').length).toBe(1);
expect(this.view.$('.CDB-InputText').length).toBe(4);
});
it('should not have leaks', function () {
expect(this.view).toHaveNoLeaks();
});
afterEach(function () {
this.view.clean();
});
});
@@ -0,0 +1,290 @@
var Backbone = require('backbone');
var $ = require('jquery');
var TableHeadItemView = require('builder/components/table/head/table-head-item-view');
var TableViewModel = require('builder/components/table/table-view-model');
var UserModel = require('builder/data/user-model');
var AnalysisDefinitionNodeSourceModel = require('builder/data/analysis-definition-node-source-model');
var QueryColumnsCollection = require('builder/data/query-columns-collection');
var FactoryModals = require('../../../factories/modals');
var simulateESCKeyPress = function () {
var e = $.Event('keydown');
e.keyCode = e.which = $.ui.keyCode.ESCAPE;
$(document).trigger(e);
};
var simulateENTERKeyPress = function () {
var e = $.Event('keydown');
e.keyCode = e.which = $.ui.keyCode.ENTER;
$(document).trigger(e);
};
describe('components/table/head/table-head-item-view', function () {
beforeEach(function () {
this.configModel = new Backbone.Model();
var userModel = new UserModel({
name: 'hello'
}, {
configModel: this.configModel
});
this.configModel.getSqlApiUrl = function () { return ''; };
this.analysisDefinitionNodeModel = new AnalysisDefinitionNodeSourceModel({
query: 'select * from pepito',
table_name: 'pepito',
id: 'dummy-id'
}, {
configModel: this.configModel,
userModel: userModel
});
this.columnsCollection = new QueryColumnsCollection([
{
name: 'cartodb_id',
type: 'number'
}, {
name: 'description',
type: 'string'
}
], {
querySchemaModel: this.analysisDefinitionNodeModel.querySchemaModel,
configModel: this.configModel
});
spyOn(this.columnsCollection, 'create');
this.tableViewModel = new TableViewModel({
tableName: 'pepito'
}, {
analysisDefinitionNodeModel: this.analysisDefinitionNodeModel,
columnsCollection: this.columnsCollection
});
spyOn(this.analysisDefinitionNodeModel, 'isCustomQueryApplied').and.returnValue(false);
spyOn(this.analysisDefinitionNodeModel, 'isReadOnly').and.returnValue(false);
this.model = this.columnsCollection.at(1);
spyOn(this.model, 'save');
this.modals = FactoryModals.createModalService();
this.view = new TableHeadItemView({
model: this.model,
columnsCollection: this.columnsCollection,
tableViewModel: this.tableViewModel,
simpleGeometry: 'point',
modals: this.modals
});
});
describe('column-options', function () {
beforeEach(function () {
this._makeClick = function () {
this.view.$('.js-columnOptions').click();
}.bind(this);
this.view.render();
});
it('should open options when options is clicked', function () {
this._makeClick();
expect(this.view._menuView).toBeDefined();
});
it('should highlight/unhighlight when options is opened/closed', function () {
this._makeClick();
expect(this.view.$el.hasClass('is-highlighted')).toBeTruthy();
simulateESCKeyPress();
expect(this.view.$el.hasClass('is-highlighted')).toBeFalsy();
});
describe('table-binds', function () {
beforeEach(function () {
spyOn(this.view, '_initScrollBinding');
spyOn(this.view, '_destroyScrollBinding');
});
it('should bind / unbind table scroll when it is opened / hidden', function () {
this._makeClick();
expect(this.view._initScrollBinding).toHaveBeenCalled();
simulateESCKeyPress();
expect(this.view._destroyScrollBinding).toHaveBeenCalled();
});
});
describe('number of options', function () {
it('should show all options', function () {
this._makeClick();
expect(this.view._menuView.collection.size()).toBe(5);
});
it('should not show all options when table view model is disabled', function () {
spyOn(this.tableViewModel, 'isDisabled').and.returnValue(true);
this._makeClick();
expect(this.view._menuView.collection.size()).toBe(1);
});
it('should not show create/update/delete options if column is geometry', function () {
spyOn(this.model, 'isGeometryColumn').and.returnValue(true);
this.view.render();
this._makeClick();
var items = this.view._menuView.collection;
expect(items.size()).toBe(2);
expect(items.at(0).get('val')).toBe('order');
expect(items.at(1).get('val')).toBe('create');
});
it('should not show create/update/delete options if column is cartodb_id', function () {
spyOn(this.model, 'isCartoDBIDColumn').and.returnValue(true);
this.view.render();
this._makeClick();
var items = this.view._menuView.collection;
expect(items.size()).toBe(2);
expect(items.at(0).get('val')).toBe('order');
expect(items.at(1).get('val')).toBe('create');
});
});
describe('actions', function () {
beforeEach(function () {
spyOn(this.tableViewModel, 'isDisabled').and.returnValue(false);
spyOn(this.model, 'isEditable').and.returnValue(true);
this._makeClick();
this._menuView = this.view._menuView;
});
it('should change table-view-model when order is changed', function () {
spyOn(this.tableViewModel, 'set');
this._menuView.$('.js-asc').click();
expect(this.tableViewModel.set).toHaveBeenCalledWith({ sort_order: 'asc', order_by: 'description' });
});
it('should start editing when rename option is clicked', function () {
spyOn(this.view, '_startEditing');
this._menuView.$('[data-val="rename"]').click();
expect(this.view._startEditing).toHaveBeenCalled();
});
it('should change column type when change option and type is clicked', function () {
spyOn(this.view, '_changeColumnType');
this._menuView.$('[data-val="change"]').click();
this._menuView.$('.Table-columnTypeMenu .js-listItem:eq(0)').click();
expect(this.view._changeColumnType).toHaveBeenCalled();
});
it('should add a new column when change create is clicked', function () {
spyOn(this.view, '_addColumn');
this._menuView.$('[data-val="create"]').click();
expect(this.view._addColumn).toHaveBeenCalled();
});
it('should remove column when remove option is clicked', function () {
spyOn(this.view, '_removeColumn');
this._menuView.$('[data-val="delete"]').click();
expect(this.view._removeColumn).toHaveBeenCalled();
});
});
});
describe('rename', function () {
beforeEach(function () {
this.view.render();
this.$input = this.view.$('.js-attribute');
this._makeDblClick = function () {
this.view.$('.js-attribute').dblclick();
}.bind(this);
});
it('should let edit attribute name if double clicked over it', function () {
spyOn(this.model, 'isEditable').and.returnValue(true);
spyOn(this.tableViewModel, 'isDisabled').and.returnValue(true);
spyOn(this.view, '_startEditing');
this._makeDblClick();
expect(this.view._startEditing).not.toHaveBeenCalled();
this.tableViewModel.isDisabled.and.returnValue(false);
this.model.isEditable.and.returnValue(false);
this._makeDblClick();
expect(this.view._startEditing).not.toHaveBeenCalled();
this.model.isEditable.and.returnValue(true);
this.tableViewModel.isDisabled.and.returnValue(false);
this._makeDblClick();
expect(this.view._startEditing).toHaveBeenCalled();
});
it('should add is-active class, remove readonly and bind changes when it is edited', function () {
spyOn(this.view, '_initRenameBinds');
this._makeDblClick();
expect(this.$input.hasClass('is-active')).toBeTruthy();
expect(this.$input.get(0).hasAttribute('readonly')).toBeFalsy();
expect(this.view._initRenameBinds).toHaveBeenCalled();
});
it('should remove is-active class, add readonly and unbind changes when it is finished/closed', function () {
this._makeDblClick();
spyOn(this.view, '_destroyRenameBinds');
simulateESCKeyPress();
expect(this.$input.val()).toBe(this.model.get('name'));
expect(this.$input.hasClass('is-active')).toBeFalsy();
expect(this.$input.get(0).hasAttribute('readonly')).toBeTruthy();
expect(this.view._destroyRenameBinds).toHaveBeenCalled();
});
it('should save name change when ENTER is pressed', function () {
spyOn(this.view, '_saveNewName');
this._makeDblClick();
simulateENTERKeyPress();
expect(this.view._saveNewName).toHaveBeenCalled();
});
it('should discard name change when ESC is pressed', function () {
spyOn(this.view, '_saveNewName');
this._makeDblClick();
simulateESCKeyPress();
expect(this.view._saveNewName).not.toHaveBeenCalled();
});
it('should open confirmation modal when name is changed', function () {
spyOn(this.model, 'isEditable').and.returnValue(true);
spyOn(this.modals, 'create');
this._makeDblClick();
this.$input.val('helloooo');
simulateENTERKeyPress();
expect(this.modals.create).toHaveBeenCalled();
});
});
describe('.focusInput', function () {
beforeEach(function () {
this.view.render();
this.$input = this.view.$('.js-attribute');
this.$input.val('title');
});
it('should start editing', function () {
spyOn(this.view, '_startEditing');
this.view.focusInput();
expect(this.view._startEditing).toHaveBeenCalled();
});
it('should select the input', function () {
spyOn(this.$input[0], 'select');
this.view.focusInput();
expect(this.$input[0].select).toHaveBeenCalled();
});
});
it('should disable scroll binding and document keypress when view is cleaned', function () {
spyOn(this.view, '_destroyScrollBinding');
spyOn(this.view, '_destroyRenameBinds');
this.view.clean();
expect(this.view._destroyScrollBinding).toHaveBeenCalled();
expect(this.view._destroyRenameBinds).toHaveBeenCalled();
});
it('should have no leaks', function () {
expect(this.view).toHaveNoLeaks();
});
afterEach(function () {
this.view.clean();
});
});
@@ -0,0 +1,60 @@
var TableHeadOptionsItemView = require('builder/components/table/head/table-head-options-item-view');
var tableHeadOptionsItemTemplate = require('builder/components/table/head/table-head-options-item.tpl');
var CustomListItemModel = require('builder/components/custom-list/custom-list-item-model');
describe('components/table/head/table-head-options-item-view', function () {
beforeEach(function () {
this.model = new CustomListItemModel({
label: 'cartodb_id',
type: 'number',
isLastColumns: false,
val: 'change'
});
this.view = new TableHeadOptionsItemView({
model: this.model,
template: tableHeadOptionsItemTemplate
});
});
describe('render', function () {
beforeEach(function () {
this.view.render();
});
it('should be rendered properly', function () {
expect(this.view.$('.CDB-ListDecoration-itemLink').length).toBe(1);
});
it('should render order with both order ways, asc and desc', function () {
this.model.set('val', 'order');
this.view.render();
expect(this.view.$('.js-asc').length).toBe(1);
expect(this.view.$('.js-desc').length).toBe(1);
});
it('should render change an arrow at the end', function () {
this.model.set('val', 'change');
this.view.render();
expect(this.view.$('.CDB-IconFont-rArrowLight').length).toBe(1);
});
});
describe('bind', function () {
it('should open sub menu if option is change', function () {
this.model.set('val', 'change');
spyOn(this.view, '_openSubContextMenu');
this.view.render();
this.view.$el.click();
expect(this.view._openSubContextMenu).toHaveBeenCalled();
expect(this.model.get('selected')).toBeFalsy();
});
});
it('should have no leaks', function () {
expect(this.view).toHaveNoLeaks();
});
afterEach(function () {
this.view.clean();
});
});
@@ -0,0 +1,192 @@
var Backbone = require('backbone');
var TableHeadView = require('builder/components/table/head/table-head-view');
var TableViewModel = require('builder/components/table/table-view-model');
var QuerySchemaModel = require('builder/data/query-schema-model');
var QueryGeometryModel = require('builder/data/query-geometry-model');
var QueryColumnModel = require('builder/data/query-column-model');
var configModelFixture = require('fixtures/builder/config-model.fixture');
var FactoryModals = require('../../../factories/modals');
describe('components/table/head/table-head-view', function () {
beforeEach(function () {
var configModel = this.configModel = configModelFixture();
this.querySchemaModel = new QuerySchemaModel({
status: 'fetched',
query: 'SELECT * FROM pepito'
}, {
configModel: configModel
});
this.queryGeometryModel = new QueryGeometryModel({
status: 'fetched',
simple_geom: 'point',
query: 'SELECT * FROM pepito'
}, {
configModel: configModel
});
this.analysisDefinitionNodeModel = new Backbone.Model();
this.analysisDefinitionNodeModel.isReadOnly = function () {
return false;
};
this.analysisDefinitionNodeModel.isCustomQueryApplied = function () { return false; };
this.tableViewModel = new TableViewModel({
tableName: 'pepito'
}, {
columnsCollection: new Backbone.Collection(),
analysisDefinitionNodeModel: this.analysisDefinitionNodeModel
});
this.columnsCollection = new Backbone.Collection([
{
name: 'cartodb_id',
type: 'number'
}, {
name: 'description',
type: 'string'
}, {
name: 'the_geom_webmercator',
type: 'geometry'
}, {
name: 'center',
type: 'geometry'
}
].map(
function (model) { return new QueryColumnModel(model, { configModel: configModel }); })
);
this.view = new TableHeadView({
columnsCollection: this.columnsCollection,
tableViewModel: this.tableViewModel,
querySchemaModel: this.querySchemaModel,
queryGeometryModel: this.queryGeometryModel,
canHideColumns: false,
modals: FactoryModals.createModalService()
});
});
describe('render', function () {
beforeEach(function () {
spyOn(this.view, '_renderColumnHead').and.callThrough();
this.view.render();
});
it('should render as many columns as column collection items has, except mercator', function () {
expect(this.view._renderColumnHead).toHaveBeenCalled();
expect(this.view._renderColumnHead.calls.count()).toBe(this.columnsCollection.length);
expect(this.view.$('.Table-headItem').length).toBe(this.columnsCollection.length - 1); // all but the_geom_webmercator
expect(this.view.$('.js-attribute[value="the_geom_webmercator"]').length).toBe(0);
});
it('should render the_geom_webmercator when a custom query is applied', function () {
spyOn(this.tableViewModel, 'isCustomQueryApplied').and.returnValue(true);
this.view.render();
expect(this.view.$('.Table-headItem').length).toBe(this.columnsCollection.length);
expect(this.view.$('.js-attribute[value="the_geom_webmercator"]').length).toBe(1);
});
describe('when can hide columns flag is true', function () {
it('should not render center geometry columns', function () {
spyOn(this.tableViewModel, 'isCustomQueryApplied').and.returnValue(true);
this.view = new TableHeadView({
columnsCollection: this.columnsCollection,
tableViewModel: this.tableViewModel,
querySchemaModel: this.querySchemaModel,
queryGeometryModel: this.queryGeometryModel,
canHideColumns: true,
modals: FactoryModals.createModalService()
});
this.view.render();
expect(this.view.$('.Table-headItem').length).toBe(this.columnsCollection.length - 1);
expect(this.view.$('.js-attribute[value="center"]').length).toBe(0);
});
});
});
describe('binds', function () {
beforeEach(function () {
spyOn(this.view, 'render').and.callThrough();
spyOn(this.view, '_scrollToLeft');
spyOn(this.view, '_focusLastHeadItem');
this.view.render();
});
it('should render when table view model changes its sort', function () {
this.tableViewModel.set('sort_order', 'desc');
expect(this.view.render).toHaveBeenCalled();
});
it('should render when table view model changes its order', function () {
this.tableViewModel.set('order_by', 'description');
expect(this.view.render).toHaveBeenCalled();
});
it('should render when simple geometry has changed', function () {
this.queryGeometryModel.set('simple_geom', 'polygon');
expect(this.view.render).toHaveBeenCalled();
});
it('should scroll to left when a new element is added', function () {
jasmine.clock().install();
this.columnsCollection.add(new QueryColumnModel({
name: 'hey',
type: 'date'
}, { configModel: this.configModel }));
// When a new column is added, query schema modal is fetched,
// then columnsCollection is reseted again.
this.columnsCollection.trigger('reset');
jasmine.clock().tick(501);
expect(this.view._scrollToLeft).toHaveBeenCalled();
this.view._scrollToLeft.calls.reset();
this.columnsCollection.remove(this.columnsCollection.at(2));
this.columnsCollection.trigger('reset');
jasmine.clock().tick(501);
expect(this.view._scrollToLeft).not.toHaveBeenCalled();
jasmine.clock().uninstall();
});
it('should focus new element input when a new element is added', function () {
jasmine.clock().install();
this.columnsCollection.add(new QueryColumnModel({
name: 'hey',
type: 'date'
}, { configModel: this.configModel }));
// When a new column is added, query schema modal is fetched,
// then columnsCollection is reseted again.
this.columnsCollection.trigger('reset');
jasmine.clock().tick(501);
expect(this.view._focusLastHeadItem).toHaveBeenCalled();
this.view._focusLastHeadItem.calls.reset();
this.columnsCollection.remove(this.columnsCollection.at(2));
this.columnsCollection.trigger('reset');
jasmine.clock().tick(501);
expect(this.view._focusLastHeadItem).not.toHaveBeenCalled();
jasmine.clock().uninstall();
});
});
describe('._focusLastHeadItem', function () {
it('should call `_lastHeadItemView.focusInput` if _lastHeadItemView is not null', function () {
this.view._lastHeadItemView = {
focusInput: function () { }
};
spyOn(this.view._lastHeadItemView, 'focusInput');
this.view._focusLastHeadItem();
expect(this.view._lastHeadItemView.focusInput).toHaveBeenCalled();
});
});
it('should have no leaks', function () {
expect(this.view).toHaveNoLeaks();
});
afterEach(function () {
this.view.clean();
});
});
@@ -0,0 +1,145 @@
var Backbone = require('backbone');
var TablePaginatorView = require('builder/components/table/paginator/table-paginator-view');
var TableViewModel = require('builder/components/table/table-view-model');
var QueryRowsCollection = require('builder/data/query-rows-collection');
describe('components/table/paginator/table-paginator-view', function () {
beforeEach(function () {
this.configModel = new Backbone.Model();
this.configModel.getSqlApiUrl = function () { return ''; };
this.tableViewModel = new TableViewModel({
tableName: 'pepito'
}, {
columnsCollection: new Backbone.Collection(),
analysisDefinitionNodeModel: {}
});
spyOn(this.tableViewModel, 'isCustomQueryApplied').and.returnValue(false);
spyOn(this.tableViewModel, 'isDisabled').and.returnValue(false);
this.rowsCollection = new QueryRowsCollection([{
cartodb_id: 1,
description: 'hello guyis'
}], {
querySchemaModel: this.querySchemaModel,
configModel: this.configModel
});
spyOn(this.rowsCollection, 'fetch');
spyOn(TablePaginatorView.prototype, 'render').and.callThrough();
this.view = new TablePaginatorView({
rowsCollection: this.rowsCollection,
tableViewModel: this.tableViewModel,
queryUtilsModel: {
fetchCount: function () {}
},
scrollToBottom: function () {}
});
this.view.render();
});
describe('render', function () {
it('should render properly', function () {
expect(this.view.$('.Table-paginatorButton').length).toBe(2);
// With only 2 results and in the first page, prev and next buttons will not appear
expect(this.view.$('.js-next').length).toBe(0);
expect(this.view.$('.js-prev').length).toBe(0);
spyOn(this.rowsCollection, 'size').and.returnValue(40);
this.tableViewModel.set('page', 5);
// Mock internal number of rows
this.view._numRows = (5 + 1) * 40 + 3;
this.view.render();
expect(this.view.$('.js-next').length).toBe(1);
expect(this.view.$('.js-prev').length).toBe(1);
expect(this.view.$el.hasClass('Table-paginator--relative')).toBeFalsy();
});
it('should add relative class if table view model has it', function () {
this.tableViewModel.set('relativePositionated', true);
this.view.render();
expect(this.view.$el.hasClass('Table-paginator--relative')).toBeTruthy();
});
});
describe('bind', function () {
it('should set loading flag to true when a loading event is sent', function () {
expect(this.view._isLoading).toBeFalsy();
this.rowsCollection.trigger('loading', this.rowsCollection);
expect(this.view._isLoading).toBeTruthy();
});
it('should not set loading flag to true if loading event is sent from a model', function () {
expect(this.view._isLoading).toBeFalsy();
var mdl = this.rowsCollection.at(0);
mdl.trigger('loading', mdl);
expect(this.view._isLoading).toBeFalsy();
});
it('should move to the last page when a new row is added', function (done) {
var PAGE = 2;
var NUM_ROWS = 81;
// Mock fetchCount
this.view._queryUtilsModel.fetchCount = function (_callback) {
_callback(NUM_ROWS);
};
this.tableViewModel.on('change:page', function () {
expect(this.tableViewModel.get('page')).toBe(PAGE);
done();
}, this);
this.rowsCollection.add({ cartodb_id: 2 });
});
it('should fetch the rowsCollection when an old row is removed', function () {
this.rowsCollection.remove(this.rowsCollection.at(0));
expect(this.rowsCollection.fetch).toHaveBeenCalled();
});
});
describe('onClick', function () {
beforeEach(function () {
spyOn(this.rowsCollection, 'size').and.returnValue(40);
this.tableViewModel.set('page', 2);
// Mock internal number of rows
this.view._numRows = (2 + 1) * 40 + 3;
this.view.render();
});
it('should change page from table-view-model when it is clicked (and enabled)', function () {
expect(this.tableViewModel.get('page')).toBe(2);
this.view.$('.js-prev').click();
expect(this.tableViewModel.get('page')).toBe(1);
});
it('should add loader when button is active and is clicked', function () {
this.view._isLoading = false;
this.view.$('.js-next').click();
expect(this.view.$('.js-next').html()).toContain('CDB-LoaderIcon');
});
it('should not change the page if there is an ongoing request', function () {
expect(this.tableViewModel.get('page')).toBe(2);
this.view._isLoading = true;
this.view.$('.js-next').click();
expect(this.view.$('.js-next').html()).not.toContain('CDB-LoaderIcon');
expect(this.tableViewModel.get('page')).toBe(2);
});
it('should exclude the_geom_webmercator is query is not custom', function () {
this.rowsCollection.size.and.returnValue(200);
this.tableViewModel.isCustomQueryApplied.and.returnValue(false);
this.view._isLoading = false;
this.view.$('.js-next').click();
expect(this.rowsCollection.fetch).toHaveBeenCalled();
var data = this.rowsCollection.fetch.calls.argsFor(0)[0].data;
expect(data.exclude).toEqual(['the_geom_webmercator']);
});
});
it('should have no leaks', function () {
expect(this.view).toHaveNoLeaks();
});
afterEach(function () {
this.view.clean();
});
});
@@ -0,0 +1,53 @@
var ConfigModel = require('builder/data/config-model');
var CoreView = require('backbone/core-view');
var UserModel = require('builder/data/user-model');
var AnalysisDefinitionNodeSourceModel = require('builder/data/analysis-definition-node-source-model');
var TableManager = require('builder/components/table/table-manager');
var FactoryModals = require('../../factories/modals');
describe('components/table/table-manager', function () {
beforeEach(function () {
this.configModel = new ConfigModel();
this.userModel = new UserModel({
name: 'pepito'
}, {
configModel: this.configModel
});
this.modals = FactoryModals.createModalService();
this.analysisDefinitionNodeModel = new AnalysisDefinitionNodeSourceModel({
query: 'select * from pepito',
table_name: 'pepito',
id: 'dummy-id'
}, {
configModel: this.configModel,
userModel: this.userModel
});
spyOn(this.analysisDefinitionNodeModel.querySchemaModel, 'fetch');
spyOn(this.analysisDefinitionNodeModel.queryGeometryModel, 'fetch');
});
describe('create', function () {
it('should generate a TableView with columns collection', function () {
var tableView = TableManager.create({
modals: this.modals,
configModel: this.configModel,
analysisDefinitionNodeModel: this.analysisDefinitionNodeModel,
userModel: this.userModel
});
expect(tableView).toBeDefined();
expect(tableView._columnsCollection).toBeDefined();
});
});
describe('destroy', function () {
it('should clean the table view', function () {
var view = new CoreView();
spyOn(view, 'clean').and.callThrough();
TableManager.destroy(view);
expect(view.clean).toHaveBeenCalled();
});
});
});
@@ -0,0 +1,86 @@
var Backbone = require('backbone');
var ConfigModel = require('builder/data/config-model');
var UserModel = require('builder/data/user-model');
var AnalysisDefinitionNodeSourceModel = require('builder/data/analysis-definition-node-source-model');
var TableViewModel = require('builder/components/table/table-view-model');
describe('components/table/table-view-model', function () {
beforeEach(function () {
this.configModel = new ConfigModel();
this.userModel = new UserModel({
name: 'pepito'
}, {
configModel: this.configModel
});
this.analysisDefinitionNodeModel = new AnalysisDefinitionNodeSourceModel({
query: 'select * from pepito',
table_name: 'pepito',
id: 'dummy-id'
}, {
configModel: this.configModel,
userModel: this.userModel
});
this.columnsCollection = new Backbone.Collection();
spyOn(TableViewModel.prototype, '_setOrderAndSort').and.callThrough();
this.model = new TableViewModel({}, {
columnsCollection: this.columnsCollection,
analysisDefinitionNodeModel: this.analysisDefinitionNodeModel
});
});
describe('_setOrderAndSort', function () {
it('should set order and sort when it is initialized', function () {
expect(TableViewModel.prototype._setOrderAndSort).toHaveBeenCalled();
});
it('should set/change order when columnsCollection is reseted', function () {
TableViewModel.prototype._setOrderAndSort.calls.reset();
expect(TableViewModel.prototype._setOrderAndSort.calls.count()).toBe(0);
this.columnsCollection.reset([]);
expect(TableViewModel.prototype._setOrderAndSort).toHaveBeenCalled();
expect(TableViewModel.prototype._setOrderAndSort.calls.count()).toBe(1);
});
it('should use cartodb_id as order_by and asc as sort_by if columnsCollection is reseted, and query is not custom', function () {
spyOn(this.model, 'isCustomQueryApplied').and.returnValue(false);
this.model.set('order_by', 'hello');
this.columnsCollection.reset([{
name: 'cartodb_id',
type: 'number'
}]);
expect(this.model.get('order_by')).toBe('cartodb_id');
});
it('should not use cartodb_id as order_by and asc as sort_by if columnsCollection is reseted, it is available and query is custom', function () {
spyOn(this.model, 'isCustomQueryApplied').and.returnValue(true);
this.model.set('order_by', 'hello');
this.columnsCollection.reset([{
name: 'cartodb_id',
type: 'number'
}]);
expect(this.model.get('order_by')).not.toBe('cartodb_id');
});
});
describe('isDisabled', function () {
it('should return that if analysisDefinitionNodeModel says so', function () {
spyOn(this.analysisDefinitionNodeModel, 'isReadOnly').and.returnValue(false);
expect(this.model.isDisabled()).toBeFalsy();
this.analysisDefinitionNodeModel.isReadOnly.and.returnValue(true);
expect(this.model.isDisabled()).toBeTruthy();
});
});
describe('isCustomQueryApplied', function () {
it('should return that if analysisDefinitionNodeModel says so', function () {
spyOn(this.analysisDefinitionNodeModel, 'isCustomQueryApplied').and.returnValue(false);
expect(this.model.isCustomQueryApplied()).toBeFalsy();
this.analysisDefinitionNodeModel.isCustomQueryApplied.and.returnValue(true);
expect(this.model.isCustomQueryApplied()).toBeTruthy();
});
});
});
@@ -0,0 +1,337 @@
var _ = require('underscore');
var Backbone = require('backbone');
var QueryGeometryModel = require('builder/data/query-geometry-model');
var QuerySchemaModel = require('builder/data/query-schema-model');
var QueryRowsCollection = require('builder/data/query-rows-collection');
var QueryColumnsCollection = require('builder/data/query-columns-collection');
var UserModel = require('builder/data/user-model');
var AnalysisDefinitionNodeModel = require('builder/data/analysis-definition-node-model');
var AnalysisDefinitionNodeSourceModel = require('builder/data/analysis-definition-node-source-model');
var AnalysisDefinitionNodesCollection = require('builder/data/analysis-definition-nodes-collection');
var TableView = require('builder/components/table/table-view');
var TableHeadView = require('builder/components/table/head/table-head-view');
var TableBodyView = require('builder/components/table/body/table-body-view');
var ConfigModel = require('builder/data/config-model');
var FactoryModals = require('../../factories/modals');
describe('components/table/table-view', function () {
beforeEach(function () {
spyOn(QueryGeometryModel.prototype, 'fetch');
spyOn(QueryRowsCollection.prototype, 'fetch');
spyOn(QueryColumnsCollection.prototype, 'fetch');
spyOn(QuerySchemaModel.prototype, 'fetch');
spyOn(_, 'debounce').and.callFake(function (func) {
return function () {
func.apply(this, arguments);
};
});
this.configModel = new ConfigModel({
user_name: 'pepito'
});
this.userModel = new UserModel({
name: 'pepito'
}, {
configModel: this.configModel
});
this.modals = FactoryModals.createModalService();
this.analysisDefinitionNodeModel = new AnalysisDefinitionNodeSourceModel({
query: 'select * from pepito',
table_name: 'pepito',
id: 'dummy-id',
type: 'source'
}, {
configModel: this.configModel,
userModel: this.userModel
});
this.analysisDefinitionNodesCollection = new AnalysisDefinitionNodesCollection([this.analysisDefinitionNodeModel], {
configModel: this.configModel,
userModel: this.userModel
});
this.querySchemaModel = this.analysisDefinitionNodeModel.querySchemaModel;
this.queryGeometryModel = this.analysisDefinitionNodeModel.queryGeometryModel;
this.querySchemaModel.attributes.status = 'unfetched';
this.columnsCollection = new QueryColumnsCollection([], {
configModel: this.configModel,
querySchemaModel: this.querySchemaModel
});
spyOn(TableView.prototype, '_setDisableState').and.callThrough();
spyOn(TableView.prototype, 'render').and.callThrough();
spyOn(TableHeadView.prototype, 'initialize').and.callThrough();
spyOn(TableBodyView.prototype, 'initialize').and.callThrough();
this.view = new TableView({
modals: this.modals,
columnsCollection: this.columnsCollection,
analysisDefinitionNodeModel: this.analysisDefinitionNodeModel
});
});
it('should create table-view-model', function () {
expect(this.view._tableViewModel).toBeDefined();
});
describe('fetch', function () {
it('should fetch query-schema-model if it is unfetched', function () {
expect(QuerySchemaModel.prototype.fetch).toHaveBeenCalled();
});
it('should fetch rows data if query-schema-model is already fetched', function () {
spyOn(TableView.prototype, '_fetchRowsData');
this.querySchemaModel.set('status', 'fetched');
var view = new TableView({
modals: this.modals,
columnsCollection: this.columnsCollection,
analysisDefinitionNodeModel: this.analysisDefinitionNodeModel
});
expect(TableView.prototype._fetchRowsData).toHaveBeenCalled();
expect(view).toBeDefined();
});
it('should not fetch rows data if query-schema-model query modifies the schema', function () {
spyOn(TableView.prototype, '_fetchRowsData');
this.querySchemaModel.set({
status: 'fetched',
query: 'DELETE FROM whatever WHERE perico="PACO"'
});
var view = new TableView({
modals: this.modals,
columnsCollection: this.columnsCollection,
analysisDefinitionNodeModel: this.analysisDefinitionNodeModel
});
expect(TableView.prototype._fetchRowsData).not.toHaveBeenCalled();
expect(view).toBeDefined();
});
});
describe('render', function () {
beforeEach(function () {
this.view.render();
});
it('should render head and body', function () {
expect(_.size(this.view._subviews)).toBe(2);
});
it('should add relative class if table view model has it', function () {
this.view._tableViewModel.set('relativePositionated', true);
this.view.render();
expect(this.view.$('.js-table').hasClass('Table--relative')).toBeTruthy();
});
});
describe('canHideColumns on TableBodyView/TableHeadView constructor', function () {
it('should be false if simply a source node is provided', function () {
this.view.render();
expect(TableBodyView.prototype.initialize).toHaveBeenCalledWith(jasmine.objectContaining({
canHideColumns: false
}));
expect(TableHeadView.prototype.initialize).toHaveBeenCalledWith(jasmine.objectContaining({
canHideColumns: false
}));
});
it('should be true if a trade-area analysis is somewhere in the analysis chain but no SQL analysis', function () {
const sourceNode = new AnalysisDefinitionNodeSourceModel({
query: 'select * from pepito',
table_name: 'pepito',
id: 'a0',
type: 'source'
}, {
configModel: this.configModel,
userModel: this.userModel
});
const node = new AnalysisDefinitionNodeModel({
id: 'a1',
type: 'trade-area',
source: 'a0'
}, {
configModel: this.configModel
});
new AnalysisDefinitionNodesCollection([sourceNode, node], { // eslint-disable-line no-new
configModel: this.configModel,
userModel: this.userModel
});
this.view = new TableView({
modals: this.modals,
columnsCollection: this.columnsCollection,
analysisDefinitionNodeModel: node
});
this.view.render();
expect(TableBodyView.prototype.initialize).toHaveBeenCalledWith(jasmine.objectContaining({
canHideColumns: true
}));
expect(TableHeadView.prototype.initialize).toHaveBeenCalledWith(jasmine.objectContaining({
canHideColumns: true
}));
});
it('should be false if a trade-area analysis is somewhere in the analysis chain but also a SQL analysis', function () {
const sourceNode = new AnalysisDefinitionNodeSourceModel({
query: 'select * from pepito',
table_name: 'pepito',
id: 'a0',
type: 'source'
}, {
configModel: this.configModel,
userModel: this.userModel
});
const node = new AnalysisDefinitionNodeModel({
id: 'a1',
type: 'trade-area',
source: 'a0'
}, {
configModel: this.configModel
});
const sqlNode = new AnalysisDefinitionNodeModel({
id: 'a2',
type: 'deprecated-sql-function',
source: 'a1'
}, {
configModel: this.configModel
});
new AnalysisDefinitionNodesCollection([sourceNode, node, sqlNode], { // eslint-disable-line no-new
configModel: this.configModel,
userModel: this.userModel
});
this.view = new TableView({
modals: this.modals,
columnsCollection: this.columnsCollection,
analysisDefinitionNodeModel: sqlNode
});
this.view.render();
expect(TableBodyView.prototype.initialize).toHaveBeenCalledWith(jasmine.objectContaining({
canHideColumns: false
}));
expect(TableHeadView.prototype.initialize).toHaveBeenCalledWith(jasmine.objectContaining({
canHideColumns: false
}));
});
});
describe('bind', function () {
it('should reset table view attributes when query has changed', function () {
this.view._tableViewModel.set({
page: 3,
order_by: 'description',
sort_order: 'desc'
});
this.querySchemaModel.set('query', 'SELECT * FROM new_table');
expect(this.view._tableViewModel.get('page')).toBe(0);
expect(this.view._tableViewModel.get('order_by')).toBe('');
expect(this.view._tableViewModel.get('sort_order')).toBe('');
});
it('should include sort and order if the query is custom but the change comes from table-view-model', function () {
this.querySchemaModel.set('query', 'SELECT * FROM new_table', { silent: true });
this.view._tableViewModel.set({
page: 3,
order_by: 'description',
sort_order: 'desc'
});
expect(this.view._tableViewModel.get('page')).toBe(3);
expect(this.view._tableViewModel.get('order_by')).toBe('description');
expect(this.view._tableViewModel.get('sort_order')).toBe('desc');
});
it('should check disabled state each time query schema model changes', function () {
this.querySchemaModel.set('status', 'fetched');
expect(TableView.prototype._setDisableState).toHaveBeenCalled();
TableView.prototype._setDisableState.calls.reset();
this.querySchemaModel.set('status', 'fetching');
expect(TableView.prototype._setDisableState).toHaveBeenCalled();
});
it('should fetch query-schema-model if query has changed and it is possible', function () {
this.querySchemaModel.fetch.calls.reset();
this.querySchemaModel.set({
query: 'SELECT * FROM new_table',
status: 'unfetched'
});
expect(this.querySchemaModel.fetch).toHaveBeenCalled();
this.querySchemaModel.fetch.calls.reset();
this.querySchemaModel.set({
query: '',
status: 'unfetched'
});
expect(this.querySchemaModel.fetch).not.toHaveBeenCalled();
});
it('should fetch new rows when query fetch has finished', function () {
this.querySchemaModel.set('status', 'fetched');
expect(QueryRowsCollection.prototype.fetch).toHaveBeenCalled();
});
it('should render again if synchronization is destroyed', function () {
spyOn(this.analysisDefinitionNodeModel, 'isSourceType').and.returnValue(true);
var tableModel = this.analysisDefinitionNodeModel.getTableModel();
tableModel._syncModel = new Backbone.Model({ id: 'dummy' });
tableModel._syncModel.url = 'dummy';
tableModel._syncModel.sync = function (a, b, opts) {
opts.success();
};
this.view._tableModel = tableModel;
this.view._initBinds();
tableModel._syncModel.destroy();
expect(TableView.prototype.render).toHaveBeenCalled();
});
describe('on table model view changes', function () {
beforeEach(function () {
this.querySchemaModel.set('status', 'fetched');
this.queryGeometryModel.set({
status: 'fetched'
});
});
it('should fetch new rows when table order changes', function () {
this.view._tableViewModel.set('order_by', 'cartodb_id');
expect(QueryRowsCollection.prototype.fetch).toHaveBeenCalled();
});
it('should fetch new rows when table sort changes', function () {
this.view._tableViewModel.set('sort_order', 'desc');
expect(QueryRowsCollection.prototype.fetch).toHaveBeenCalled();
});
});
});
describe('.getColumnsCollection', function () {
it('should provide the _columnsCollection object', function () {
expect(this.view.getColumnsCollection()).toBe(this.view._columnsCollection);
});
});
it('should have no leaks', function () {
this.view.render();
expect(this.view).toHaveNoLeaks();
});
afterEach(function () {
this.view.clean();
});
});