Write tests for DataManager, Separate Error declarations
This commit is contained in:
parent
d3a4add85b
commit
44cc809da4
3
.gitignore
vendored
3
.gitignore
vendored
@ -29,3 +29,6 @@ node_modules
|
|||||||
# Remove some common IDE working directories
|
# Remove some common IDE working directories
|
||||||
.idea
|
.idea
|
||||||
.vscode
|
.vscode
|
||||||
|
|
||||||
|
# npm debug logs
|
||||||
|
npm-debug.log.*
|
||||||
|
|||||||
@ -2529,6 +2529,8 @@ var DataManager = function () {
|
|||||||
this.rowCount = 0;
|
this.rowCount = 0;
|
||||||
this.columns = [];
|
this.columns = [];
|
||||||
this.rows = [];
|
this.rows = [];
|
||||||
|
this._serialNoColumnAdded = false;
|
||||||
|
this._checkboxColumnAdded = false;
|
||||||
|
|
||||||
this.columns = this.prepareColumns(columns);
|
this.columns = this.prepareColumns(columns);
|
||||||
this.rows = this.prepareRows(rows);
|
this.rows = this.prepareRows(rows);
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -3,21 +3,29 @@ import { isNumeric, promisify } from './utils';
|
|||||||
export default class DataManager {
|
export default class DataManager {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.currentSort = {
|
|
||||||
colIndex: -1,
|
|
||||||
sortOrder: 'none' // asc, desc, none
|
|
||||||
};
|
|
||||||
this.sortRows = promisify(this.sortRows, this);
|
this.sortRows = promisify(this.sortRows, this);
|
||||||
this.switchColumn = promisify(this.switchColumn, this);
|
this.switchColumn = promisify(this.switchColumn, this);
|
||||||
this.removeColumn = promisify(this.removeColumn, this);
|
this.removeColumn = promisify(this.removeColumn, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
init(data) {
|
init(data) {
|
||||||
|
if (!data) {
|
||||||
|
data = this.options.data;
|
||||||
|
}
|
||||||
|
|
||||||
let { columns, rows } = data;
|
let { columns, rows } = data;
|
||||||
|
|
||||||
this.rowCount = 0;
|
this.rowCount = 0;
|
||||||
this.columns = [];
|
this.columns = [];
|
||||||
this.rows = [];
|
this.rows = [];
|
||||||
|
this._serialNoColumnAdded = false;
|
||||||
|
this._checkboxColumnAdded = false;
|
||||||
|
|
||||||
|
// initialize sort state
|
||||||
|
this.currentSort = {
|
||||||
|
colIndex: -1,
|
||||||
|
sortOrder: 'none' // asc, desc, none
|
||||||
|
};
|
||||||
|
|
||||||
this.columns = this.prepareColumns(columns);
|
this.columns = this.prepareColumns(columns);
|
||||||
this.rows = this.prepareRows(rows);
|
this.rows = this.prepareRows(rows);
|
||||||
@ -27,7 +35,12 @@ export default class DataManager {
|
|||||||
|
|
||||||
prepareColumns(columns) {
|
prepareColumns(columns) {
|
||||||
if (!Array.isArray(columns)) {
|
if (!Array.isArray(columns)) {
|
||||||
throw new TypeError('`columns` must be an array');
|
throw ColumnsTypeError;
|
||||||
|
}
|
||||||
|
for (const column of columns) {
|
||||||
|
if (typeof column !== 'string' && typeof column !== 'object') {
|
||||||
|
throw ColumnTypeError;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.options.addSerialNoColumn && !this._serialNoColumnAdded) {
|
if (this.options.addSerialNoColumn && !this._serialNoColumnAdded) {
|
||||||
@ -78,8 +91,19 @@ export default class DataManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
prepareRows(rows) {
|
prepareRows(rows) {
|
||||||
if (!Array.isArray(rows) || !Array.isArray(rows[0])) {
|
if (!Array.isArray(rows)) {
|
||||||
throw new TypeError('`rows` must be an array of arrays');
|
throw RowsTypeError;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const row of rows) {
|
||||||
|
|
||||||
|
if (!Array.isArray(row)) {
|
||||||
|
throw RowTypeError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (row.length !== this.getColumnCount()) {
|
||||||
|
throw RowLengthError;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rows = rows.map((row, i) => {
|
rows = rows.map((row, i) => {
|
||||||
@ -304,3 +328,17 @@ function prepareCell(col, i) {
|
|||||||
colIndex: i
|
colIndex: i
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ColumnsTypeError = new TypeError('`columns` must be an array');
|
||||||
|
const RowsTypeError = new TypeError('`rows` must be an array');
|
||||||
|
const RowTypeError = new TypeError('`row` must be an array');
|
||||||
|
const ColumnTypeError = new TypeError('`column` must be a string or an object');
|
||||||
|
const RowLengthError = new RangeError('A Row length doesn\'t match column length');
|
||||||
|
|
||||||
|
export {
|
||||||
|
ColumnsTypeError,
|
||||||
|
RowsTypeError,
|
||||||
|
ColumnTypeError,
|
||||||
|
RowTypeError,
|
||||||
|
RowLengthError
|
||||||
|
};
|
||||||
|
|||||||
159
test/datamanager.spec.js
Normal file
159
test/datamanager.spec.js
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
/* global describe, it, before */
|
||||||
|
|
||||||
|
import chai from 'chai';
|
||||||
|
import DataManager, {
|
||||||
|
ColumnTypeError,
|
||||||
|
RowTypeError,
|
||||||
|
ColumnsTypeError,
|
||||||
|
RowsTypeError,
|
||||||
|
RowLengthError
|
||||||
|
} from '../src/datamanager';
|
||||||
|
|
||||||
|
chai.expect();
|
||||||
|
const expect = chai.expect;
|
||||||
|
|
||||||
|
describe.only('DataManager instance', () => {
|
||||||
|
|
||||||
|
it('should initialize rows and columns given options', () => {
|
||||||
|
const datamanager = getDataManagerInstance();
|
||||||
|
expect(datamanager).has.property('rows');
|
||||||
|
expect(datamanager).has.property('columns');
|
||||||
|
expect(datamanager.rowCount).to.equal(3);
|
||||||
|
expect(datamanager._serialNoColumnAdded).to.equal(false);
|
||||||
|
expect(datamanager._checkboxColumnAdded).to.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('prepareRows', () => {
|
||||||
|
const datamanager = getDataManagerInstance();
|
||||||
|
|
||||||
|
it('should properly build row object when bare minimum options are given', () => {
|
||||||
|
const firstRow = datamanager.getRow(0);
|
||||||
|
expect(firstRow).to.deep.equal([
|
||||||
|
{
|
||||||
|
colIndex: 0,
|
||||||
|
content: 'Faris',
|
||||||
|
rowIndex: 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
colIndex: 1,
|
||||||
|
content: 'faris@test.com',
|
||||||
|
rowIndex: 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
colIndex: 2,
|
||||||
|
content: 'Software Developer',
|
||||||
|
rowIndex: 0
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw when rows parameter is not an Array', () => {
|
||||||
|
expect(() => datamanager.init({
|
||||||
|
columns: ['Name'],
|
||||||
|
rows: 2
|
||||||
|
})).to.throw(RowsTypeError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw when any of the row is not an Array', () => {
|
||||||
|
expect(() => datamanager.init({
|
||||||
|
columns: ['Name'],
|
||||||
|
rows: [2]
|
||||||
|
})).to.throw(RowTypeError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw when any of the row\'s length doesn\'t match column length', () => {
|
||||||
|
expect(() => datamanager.init({
|
||||||
|
columns: ['Name'],
|
||||||
|
rows: [[]]
|
||||||
|
})).to.throw(RowLengthError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not throw given valid data', () => {
|
||||||
|
expect(() => datamanager.init({
|
||||||
|
columns: ['Name'],
|
||||||
|
rows: [['Faris']]
|
||||||
|
})).to.not.throw();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('prepareColumns', () => {
|
||||||
|
const datamanager = getDataManagerInstance();
|
||||||
|
|
||||||
|
it('should properly build column object with bare minimum options', () => {
|
||||||
|
const firstColumn = datamanager.getColumn(0);
|
||||||
|
expect(firstColumn.colIndex).eq(0);
|
||||||
|
expect(firstColumn.content).eq('Name');
|
||||||
|
expect(firstColumn.isHeader).eq(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw when columns parameter is not an Array', () => {
|
||||||
|
expect(() => datamanager.init({
|
||||||
|
columns: 2
|
||||||
|
})).to.throw(ColumnsTypeError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw when any of the column is not a string or object', () => {
|
||||||
|
expect(() => datamanager.init({
|
||||||
|
columns: [2]
|
||||||
|
})).to.throw(ColumnTypeError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not throw given valid params', () => {
|
||||||
|
expect(() => datamanager.init({
|
||||||
|
columns: ['Name'],
|
||||||
|
rows: [['Test']]
|
||||||
|
})).to.not.throw();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should properly build column object when editable is false', () => {
|
||||||
|
const data = {
|
||||||
|
columns: [
|
||||||
|
{ content: 'Name', editable: false }
|
||||||
|
],
|
||||||
|
rows: [
|
||||||
|
['Faris']
|
||||||
|
]
|
||||||
|
};
|
||||||
|
datamanager.init(data);
|
||||||
|
const firstColumn = datamanager.getColumn(0);
|
||||||
|
expect(firstColumn.colIndex).eq(0);
|
||||||
|
expect(firstColumn.content).eq('Name');
|
||||||
|
expect(firstColumn.isHeader).eq(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('prepareNumericColumns', () => {
|
||||||
|
const datamanager = getDataManagerInstance();
|
||||||
|
it('should assign `align: right` to columns with numeric data', () => {
|
||||||
|
datamanager.init({
|
||||||
|
columns: ['Name', 'Number'],
|
||||||
|
rows: [
|
||||||
|
['Faris', '123']
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
const column0 = datamanager.getColumn(0);
|
||||||
|
const column1 = datamanager.getColumn(1);
|
||||||
|
expect(column0.align).to.not.equal('right');
|
||||||
|
expect(column1.align).to.equal('right');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function getDataManagerInstance(opts = {}) {
|
||||||
|
const options = Object.assign({}, {
|
||||||
|
data: {
|
||||||
|
columns: ['Name', 'Email', 'Occupation'],
|
||||||
|
rows: [
|
||||||
|
['Faris', 'faris@test.com', 'Software Developer'],
|
||||||
|
['Manas', 'manas@test.com', 'Software Engineer'],
|
||||||
|
['Ameya', 'ameya@test.com', 'Hacker']
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}, opts);
|
||||||
|
|
||||||
|
const datamanager = new DataManager(options);
|
||||||
|
datamanager.init();
|
||||||
|
return datamanager;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user