datatable-sort-tests.js revision fda6488421a920c5d67c89b52d03acad03df65b3
YUI.add('datatable-sort-tests', function(Y) {
var suite = new Y.Test.Suite("Y.DataTable.Sortable");
suite.add(new Y.Test.Case({
name: "lifecycle and instantiation",
"Y.DataTable should be augmented": function () {
Y.Assert.isTrue(
new Y.DataTable().hasImpl(Y.DataTable.Sortable));
},
"Y.DataTable.Base should not be augmented": function () {
Y.Assert.isFalse(
new Y.DataTable.Base().hasImpl(Y.DataTable.Sortable));
},
"Y.DataTable constructor should not error": function () {
var table = new Y.DataTable({
columns: ['a'],
data: [{a:1}]
});
Y.Assert.isInstanceOf(Y.DataTable, table);
Y.Assert.isTrue(table.hasImpl(Y.DataTable.Sortable));
},
"test sortable values": function () {
var config = {
columns: ['a'],
data: [{a:1}]
}, table;
table = new Y.DataTable(config);
Y.Assert.areSame('auto', table.get('sortable'));
config.sortable = false;
table = new Y.DataTable(config);
Y.Assert.isFalse(table.get('sortable'));
config.sortable = true;
table = new Y.DataTable(config);
Y.Assert.isTrue(table.get('sortable'));
config.sortable = 'auto';
table = new Y.DataTable(config);
Y.Assert.areSame('auto', table.get('sortable'));
config.sortable = ['a', 'b'];
table = new Y.DataTable(config);
Y.ArrayAssert.itemsAreSame(['a', 'b'], table.get('sortable'));
/*
* Commented out until #2528732 is fixed
config.sortable = 'a';
table = new Y.DataTable(config);
Y.Assert.areSame('auto', table.get('sortable'));
config.sortable = { a: -1 };
table = new Y.DataTable(config);
Y.Assert.areSame('auto', table.get('sortable'));
*/
},
"test set('sortable')": function () {
var table = new Y.DataTable({
columns: ['a'],
data: [{a:1}]
});
Y.Assert.areSame('auto', table.get('sortable'));
table.set('sortable', false);
Y.Assert.isFalse(table.get('sortable'));
table.set('sortable', ['a', 'b']);
Y.ArrayAssert.itemsAreSame(['a', 'b'], table.get('sortable'));
table.set('sortable', true);
Y.Assert.isTrue(table.get('sortable'));
table.set('sortable', 'a');
Y.Assert.isTrue(table.get('sortable'));
table.set('sortable', { a: -1 });
Y.Assert.isTrue(table.get('sortable'));
},
"test sortBy values": function () {
var config = {
columns: ['a', 'b'],
data: [{ a: 1, b: 1 }]
}, table;
table = new Y.DataTable(config);
Y.Assert.isUndefined(table.get('sortBy'));
config.sortBy = null;
table = new Y.DataTable(config);
Y.Assert.isNull(table.get('sortBy'));
config.sortBy = 'a';
table = new Y.DataTable(config);
Y.Assert.areSame('a', table.get('sortBy'));
config.sortBy = { a: -1 };
table = new Y.DataTable(config);
Y.Assert.isObject(table.get('sortBy'));
Y.Assert.areSame(-1, table.get('sortBy').a);
config.sortBy = ['a', 'b'];
table = new Y.DataTable(config);
Y.ArrayAssert.itemsAreSame(['a', 'b'], table.get('sortBy'));
config.sortBy = [{ a: 1 }, { b: -1 }];
table = new Y.DataTable(config);
Y.Assert.areSame(1, table.get('sortBy')[0].a);
Y.Assert.areSame(-1, table.get('sortBy')[1].b);
},
"test set('sortBy')": function () {
var table = new Y.DataTable({
columns: ['a', 'b'],
data: [{ a:1, b: 1 }]
});
Y.Assert.isUndefined(table.get('sortBy'));
table.set('sortBy', null);
Y.Assert.isNull(table.get('sortBy'));
table.set('sortBy', 'a');
Y.Assert.areSame('a', table.get('sortBy'));
table.set('sortBy', { a: -1 });
Y.Assert.isObject(table.get('sortBy'));
Y.Assert.areSame(-1, table.get('sortBy').a);
table.set('sortBy', ['a', 'b']);
Y.ArrayAssert.itemsAreSame(['a', 'b'], table.get('sortBy'));
table.set('sortBy', [{ a: -1 }, { b: -1 }]);
Y.Assert.areSame(-1, table.get('sortBy')[0].a);
Y.Assert.areSame(-1, table.get('sortBy')[1].b);
// TODO: test invalid values, non-existant columns
}
}));
suite.add(new Y.Test.Case({
name: "DataTable.Sortable tests",
"test ui triggered sort": function () {
var table, link, th;
table = new Y.DataTable({
columns: [{ key: 'a', sortable: true }],
data: [{ a: "a1" }, { a: "a2" }, { a: "a3" }]
}).render();
th = table._theadNode.one('th');
Y.Assert.isTrue(th.hasClass(table.getClassName('sortable', 'column')));
// Should not error
th.all('*').pop().simulate('click');
Y.Assert.isArray(table.get('sortBy'));
Y.Assert.areSame(1, table.get('sortBy')[0].a);
table.destroy();
}
// test sort state classes
// test sort event
// test sort(...)
// test toggleSort(...)
}));
Y.Test.Runner.add(suite);
}, '@VERSION@' ,{requires:['datatable-sort', 'test', 'node-event-simulate']});