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

View File

@@ -0,0 +1,57 @@
describe("common.ui.Dialog", function() {
var dialog;
beforeEach(function() {
dialog = new cdb.ui.common.Dialog({el: $('<div>')});
dialog.ok = function() {};
dialog.cancel = function() {};
spyOn(dialog, 'ok');
spyOn(dialog, 'cancel');
});
it("should show element on open", function() {
dialog.open();
expect(dialog.$el.css('display')).toEqual('block');
});
it("should hide element on close", function() {
dialog.open();
dialog.hide();
expect(dialog.$el.css('display')).toEqual('none');
});
it("should hide element on ok", function() {
dialog.open();
dialog._ok();
expect(dialog.$el.css('display')).toEqual('none');
});
it("should call cancel on _cancel", function() {
dialog._ok();
expect(dialog.ok).toHaveBeenCalled();
});
it("should call ok on _ok", function() {
dialog._cancel();
expect(dialog.cancel).toHaveBeenCalled();
});
it("should append it to body and be rendered", function() {
var s = sinon.stub(dialog, 'render');
s.returns(dialog);
var r = dialog.appendToBody();
expect(s.called).toEqual(true);
expect(dialog.$el.parent()[0]).toEqual(document.body);
expect(r).toEqual(dialog);
});
it("should render title", function() {
var dialog = new cdb.ui.common.Dialog({
title: 'test',
template_base: '<%= title %>'
});
expect(dialog.render().$el.html()).toEqual('test');
});
});

View File

@@ -0,0 +1,44 @@
describe('common.ui.Dropdown', function() {
beforeEach(function() {
this.$el = $('<div><button id="btn"></button></div>');
this.view = new cdb.ui.common.Dropdown({
el: $('<div>'),
target: this.$el.find('#btn')
});
});
describe('.clean', function() {
it('should unbind click handler on target', function() {
this.targetClickSpy = jasmine.createSpy('click');
this.$el.on('click', this.targetClickSpy);
// Event should not bubble up since there is a handler that prevents it
this.$el.find('#btn').click();
expect(this.targetClickSpy).not.toHaveBeenCalled();
// Verify click bubbles up as expected again
this.view.clean();
this.$el.find('#btn').click();
expect(this.targetClickSpy).toHaveBeenCalled();
});
it('should unbind event handlers on document', function() {
// spy on internal call, since spying on _keydown fn do not work for some reason
spyOn(this.view, 'hide');
var keyEsc = function() {
var e = $.Event('keydown');
e.keyCode = 27; // ESC
$(document).trigger(e);
};
// Should hide on ESC
keyEsc();
expect(this.view.hide).toHaveBeenCalled();
// Callback should not be triggered again
this.view.clean();
keyEsc();
expect(this.view.hide.calls.count()).toEqual(1);
});
});
});

View File

@@ -0,0 +1,37 @@
describe("common.ui.Notification", function() {
var notification;
beforeEach(function() {
notification = new cdb.ui.common.Notification({
el: $('<div>'),
template: 'template'
});
//spyOn(dialog, 'cancel');
});
it("open should show the element", function(done) {
expect(notification.$el.css('display')).toEqual('none');
notification.open();
setTimeout(function () {
expect(notification.$el.css('display')).toEqual('block');
done();
}, 500);
});
it("should be closed on timeout", function(done) {
notification = new cdb.ui.common.Notification({
el: $('<div>'),
timeout: 250,
template: 'template'
});
notification.open();
setTimeout(function () {
expect(notification.$el.css('display')).toEqual('none');
done();
}, 500);
});
});

View File

@@ -0,0 +1,279 @@
describe("common.ui.Table", function() {
var cols;
var tableMetadata;
describe("Row", function() {
beforeEach(function() {
});
// it("", function() {
// });
});
describe("TableData", function() {
beforeEach(function() {
cols = new cdb.ui.common.TableData();
cols.reset([
{'id': 1, 'col1': 1, 'col2': 2, 'col3': 3},
{'id': 2, 'col1': 4, 'col2': 5, 'col3': 6}
]);
});
it("should return the value for cell", function() {
expect(cols.getCell(0, 'col1')).toEqual(1);
});
it("should return null for non existing cell", function() {
expect(cols.getCell(10, 'col1')).toEqual(null);
});
});
describe("RowView", function() {
it("should render in a row", function() {
var row = new cdb.ui.common.Row({test0: 'a', test1: 'b'});
var r = new cdb.ui.common.RowView({model: row});
expect(r.render().$('td').length).toEqual(3); // two rows plus one blank row before them
});
it("should render in order", function() {
var row = new cdb.ui.common.Row({test0: 'a', test1: 'b'});
var r = new cdb.ui.common.RowView({model: row, order: ['test1', 'test0']});
r.render();
expect($(r.$('td')[1]).html()).toEqual('b');
expect($(r.$('td')[2]).html()).toEqual('a');
r = new cdb.ui.common.RowView({model: row, order: ['test0', 'test1']});
r.render();
expect($(r.$('td')[1]).html()).toEqual('a');
expect($(r.$('td')[2]).html()).toEqual('b');
});
it("should render row header", function() {
var row = new cdb.ui.common.Row({test0: 'a', test1: 'b'});
var r = new cdb.ui.common.RowView({
model: row,
row_header: true
});
r.render();
expect(r.$('td').length).toEqual(3);
});
it("should return cell x", function() {
var row = new cdb.ui.common.Row({test0: 'a', test1: 'b'});
var r = new cdb.ui.common.RowView({model: row});
r.render();
expect(r.getCell(2).html()).toEqual('b');
});
});
describe("Table", function() {
var table;
beforeEach(function() {
cdb.ui.common.Row.url = 'test';
cols = new cdb.ui.common.TableData();
cols.url = 'test';
tableMetadata = new cdb.ui.common.TableProperties({
schema: [
['id', 'number'],
['col1','number'],
['col2','number'],
['col3','number']
]
});
cols.reset([
{'id': 1, 'col1': 1, 'col2': 2, 'col3': 3},
{'id': 2, 'col1': 4, 'col2': 5, 'col3': 6}
]);
table = new cdb.ui.common.Table({
dataModel: cols,
model: tableMetadata
});
this.server = sinon.fakeServer.create();
});
it("should render a table", function() {
expect(table.render().$el.is('table')).toEqual(true);
});
it("should render a header", function() {
expect(table.render().$('thead')).toBeTruthy();
});
it("should have 2 rows and the header", function() {
expect(table.render().$('tr').length).toEqual(3);
});
it("should have 6 cells and header", function() {
expect(table.render().$('th').length).toEqual(5);
expect(table.render().$('td').length).toEqual(2*5);
});
it("each row has an id", function() {
expect($(table.render().$('tr')[1]).attr('id')).toEqual('row_' + cols.at(0).id);
});
it("should change value when model changes", function() {
table.render();
cols.at(0).set('col1', 10);
expect(table.$('#cell_1_col1').html()).toEqual(cols.at(0).get('col1').toString());
});
it("should rerender on data reset", function() {
expect(table.render().$('tr').length).toEqual(3);
cols.reset([
{'id': 1, 'col1': 1, 'col2': 2, 'col3': 3}
]);
expect(table.$('tr').length).toEqual(2);
});
it("should remove rows on remove", function() {
table.render();
cols.at(0).destroy();
expect(table.$('tr').length).toEqual(2);
expect(table.rowViews.length).toEqual(1);
});
it("should add rows", function() {
table.render();
cols.add({'id': 4, 'col1': 1, 'col2': 2, 'col3': 3});
expect(table.$('tr').length).toEqual(4);
});
it("should add rows at index", function() {
table.render();
cols.add({'id': 10, 'col1': 11, 'col2': 12, 'col3': 13}, {at: 1});
expect(table.$('tr').length).toEqual(4);
var cell = table.getCell(1, 1);
expect(cell.html()).toEqual('10');
expect(cell.parent().attr('data-y')).toEqual('1');
expect(table.getCell(1, 2).parent().attr('data-y')).toEqual('2');
});
it("should update cell indexes when remove a column", function() {
table.render();
cols.add({'id': 10, 'col1': 11, 'col2': 12, 'col3': 13}, {at: 1});
expect(table.$('tr').length).toEqual(4);
cols.remove(cols.at(0));
var cell = table.getCell(0, 1);
expect(cell.parent().attr('data-y')).toEqual('1');
/*
var cell = table.getCell(0, 1);
expect(cell.html()).toEqual('10');
expect(cell.parent().attr('data-y')).toEqual('1');
expect(table.getCell(0, 2).parent().attr('data-y')).toEqual('2');
*/
});
it("should remove rows", function() {
table.render();
cols.remove(cols.at(0));
expect(table.$('tr').length).toEqual(2);
});
it("should return cell x,y", function() {
//$('#foo').trigger('click');
var cell = table.render().getCell(1, 1);
expect(cell.html()).toEqual('2');
cell = table.getCell(2, 1);
expect(cell.html()).toEqual('4');
});
it("should trigger cell clicked on click and dblclick", function() {
var cell = table.render().getCell(0, 1);
spy = {
click: function() {},
dblClick: function() {}
};
spyOn(spy, 'click');
spyOn(spy, 'dblClick');
table.bind('cellClick', spy.click, spy);
cell.trigger('click');
expect(spy.click).toHaveBeenCalled();
expect(spy.click.calls.mostRecent().args[1][0]).toEqual(cell[0]);
expect(spy.click.calls.mostRecent().args[2]).toEqual(0);
expect(spy.click.calls.mostRecent().args[3]).toEqual(1);
table.bind('cellDblClick', spy.dblClick, spy);
cell.trigger('dblclick');
expect(spy.dblClick).toHaveBeenCalled();
expect(spy.dblClick.calls.mostRecent().args[1][0]).toEqual(cell[0]);
expect(spy.dblClick.calls.mostRecent().args[2]).toEqual(0);
expect(spy.dblClick.calls.mostRecent().args[3]).toEqual(1);
});
it("should render new data on change data source", function() {
cols = new cdb.ui.common.TableData();
table.setDataSource(cols);
cols.reset([
{'id': 100, 'col1': 1, 'col2': 2, 'col3': 3}
]);
cell = table.getCell(1, 0);
expect(cell.html()).toEqual('100');
});
it("should clear rows after a reset", function() {
expect(table.render().$('tr').length).toEqual(3);
cols.reset([]);
expect(table.$('tr').length).toEqual(1); // only the header
});
it("should call renderEmpty after an error", function() {
spyOn(table, '_renderEmpty');
cols.reset([], { silent: true });
cols.trigger('error');
expect(table._renderEmpty).toHaveBeenCalled();
});
it("should render faster than light", function() {
var NCOLUMNS = 100;
var NROWS = 260;
var schema = []
var rows = [];
_(NCOLUMNS).times(function(n) {
schema.push(['column_' + n, 'string']);
});
_(NROWS).times(function(n) {
var row = {}
_(schema).each(function(c) {
row[c[0]] = "testestestest"
})
rows.push(row);
});
tableMetadata = new cdb.ui.common.TableProperties({
schema: schema
});
cols.reset(rows);
table = new cdb.ui.common.Table({
dataModel: cols,
model: tableMetadata
});
var mean = 0;
var count = 5;
for(var i = 0; i < count; ++i) {
var t0 = new Date().getTime();
table.render();
var t1 = new Date().getTime();
mean += t1 - t0;
}
// God please, forgive me.
expect(mean/count).toBeLessThan(10000);
});
});
});

View File

@@ -0,0 +1,286 @@
describe('core.ui.common.TabPane', function() {
var pane;
beforeEach(function() {
pane = new cdb.ui.common.TabPane();
});
it("getPreviousPane should return the last pane if the active pane is the first one", function() {
var // Let's create the views
v1 = new cdb.core.View(),
v2 = new cdb.core.View(),
v3 = new cdb.core.View();
// Add some tabs
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
pane.addTab('tab3', v3);
pane.active('tab1');
expect(pane.getPreviousPane()).toEqual(v3);
});
it("getPreviousPane should return the previous pane", function() {
var // Let's create the views
v1 = new cdb.core.View(),
v2 = new cdb.core.View(),
v3 = new cdb.core.View();
// Add some tabs
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
pane.addTab('tab3', v3);
pane.active('tab2');
expect(pane.getPreviousPane()).toEqual(v1);
});
it("getNextPane should return the next pane", function() {
var // Let's create the views
v1 = new cdb.core.View(),
v2 = new cdb.core.View(),
v3 = new cdb.core.View();
// Add some tabs
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
pane.addTab('tab3', v3);
pane.active('tab1');
expect(pane.getNextPane()).toEqual(v2);
});
it("getNextPane should return the first pane if the last pane is active", function() {
var // Let's create the views
v1 = new cdb.core.View(),
v2 = new cdb.core.View(),
v3 = new cdb.core.View();
// Add some tabs
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
pane.addTab('tab3', v3);
pane.active('tab3');
expect(pane.getNextPane()).toEqual(v1);
});
it("getActive should return the desired pane", function() {
var // Let's create the views
v1 = new cdb.core.View(),
v2 = new cdb.core.View(),
v3 = new cdb.core.View();
// Add some tabs
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
pane.addTab('tab3', v3);
expect(pane.getPane('tab1')).toEqual(v1);
expect(pane.getPane('tab3')).toEqual(v3);
expect(pane.getPane('tab2')).toEqual(v2);
});
it("getActivePane should return the active pane", function() {
var // Let's create the views
v1 = new cdb.core.View(),
v2 = new cdb.core.View(),
v3 = new cdb.core.View();
// Add some tabs
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
pane.addTab('tab3', v3);
// Finally, activate one of them
pane.active('tab2');
expect(pane.getActivePane()).toEqual(v2);
});
it("activating a tab should return the view", function() {
var // Let's create the views
v1 = new cdb.core.View(),
v2 = new cdb.core.View(),
v3 = new cdb.core.View();
// Add some tabs
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
pane.addTab('tab3', v3);
// Finally, activate one of them
var activeView = pane.active('tab2');
expect(activeView).toEqual(v2);
});
it("should allow to add a pane", function() {
var v1 = new cdb.core.View();
spy = {
tabAdded: function(){}
};
spyOn(spy, 'tabAdded');
pane.bind('tabAdded', spy.tabAdded, spy);
pane.addTab('tab1', v1);
expect(pane._subviews[v1.cid]).toBeTruthy();
expect(pane.activeTab).toEqual('tab1');
expect(pane.$el.children()[0]).toEqual(v1.el);
expect(spy.tabAdded).toHaveBeenCalledWith('tab1', v1);
});
it("should allow to remove a pane", function() {
var // Let's create the views
v1 = new cdb.core.View(),
v2 = new cdb.core.View();
// Add the views
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
pane.active('tab1');
pane.removeTab('tab1');
expect(pane._subviews[v1.cid]).toBeFalsy();
expect(pane.activeTab).toEqual('tab2');
// There shold be only one children
expect(pane.$el.children()[0]).toEqual(v2.el);
expect(pane.$el.children().length).toEqual(1);
});
it("should remove all panels ", function() {
var v1 = new cdb.core.View();
var v2 = new cdb.core.View();
spyOn(v1, 'clean');
spyOn(v2, 'clean');
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
pane.removeTabs();
expect(_.keys(pane.tabs).length).toEqual(0);
expect(v1.clean).toHaveBeenCalled()
expect(v2.clean).toHaveBeenCalled()
});
it("should trigger on activate", function() {
var // Let's create the views
v1 = new cdb.core.View(),
v2 = new cdb.core.View();
spy = {
tabEnabled: function(){},
tabDisabled: function(){}
};
spyOn(spy, 'tabDisabled');
spyOn(spy, 'tabEnabled');
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
expect(pane.activeTab).toEqual('tab2');
pane.bind('tabEnabled', spy.tabEnabled, spy);
pane.bind('tabDisabled', spy.tabDisabled, spy);
pane.active('tab1');
expect(spy.tabEnabled).toHaveBeenCalledWith('tab1', v1);
expect(spy.tabDisabled).toHaveBeenCalledWith('tab2', v2);
expect(v1.el.style.display).toEqual('block');
expect(v2.el.style.display).toEqual('none');
});
it("should call activated and deactivaed on tab if exists", function() {
var // Let's create the views
v1 = new cdb.core.View(),
v2 = new cdb.core.View();
v1.activated = function() {};
v2.deactivated = function() {};
spyOn(spy, 'tabDisabled');
spyOn(spy, 'tabEnabled');
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
var a = spyOn(v1, 'activated');
var d = spyOn(v2, 'deactivated');
pane.active('tab1');
expect(a).toHaveBeenCalled();
expect(d).toHaveBeenCalled();
});
it("each should call function for each tab", function() {
var // Let's create the views
v1 = new cdb.core.View(),
v2 = new cdb.core.View();
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
var t = [];
pane.each(function(name, tab) {
t.push([name, tab]);
});
expect(t.length).toEqual(2);
expect(t[0][0]).toEqual('tab1')
expect(t[1][0]).toEqual('tab2')
expect(t[0][1].cid).toEqual(v1.cid);
expect(t[1][1].cid).toEqual(v2.cid);
});
it("user after option inserting view after specified index", function() {
var v1, v2, v3;
v1 = new cdb.core.View(),
v2 = new cdb.core.View();
v3 = new cdb.core.View();
pane.addTab('tab1', v1);
pane.addTab('tab2', v2);
pane.addTab('tab3', v3, { after: 0 });
expect(pane.$el.children()[0]).toEqual(v1.el);
expect(pane.$el.children()[1]).toEqual(v3.el);
expect(pane.$el.children()[2]).toEqual(v2.el);
});
it("clean should remove all tabs", function() {
spyOn(pane, "removeTabs");
pane.clean();
expect(pane.removeTabs).toHaveBeenCalled();
});
});