Skip to content

Commit

Permalink
test: improve test coverage (#144)
Browse files Browse the repository at this point in the history
* Add tests

* Add test for alignTableData

* Add test for getBorderCharacters and validateConfig

* Add test for createStream

* Fix typo

Co-authored-by: Nam Hoang Le <nam.hoang.le@mgm-tp.com>
  • Loading branch information
nam-hle and Nam Hoang Le committed Apr 12, 2021
1 parent bf7d80e commit 6dd91e3
Show file tree
Hide file tree
Showing 13 changed files with 592 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/createStream.js
Expand Up @@ -22,7 +22,7 @@ const prepareData = (data, config) => {

rows = stringifyTableData(data);

rows = truncateTableData(data, config);
rows = truncateTableData(rows, config);

const rowHeightIndex = calculateRowHeightIndex(rows, config);

Expand Down
2 changes: 1 addition & 1 deletion src/drawRow.js
Expand Up @@ -6,7 +6,7 @@
*/

/**
* @param {number[]} columns
* @param {string[]} columns
* @param {drawRow~border} border
* @returns {string}
*/
Expand Down
66 changes: 66 additions & 0 deletions test/alignTableData.js
@@ -0,0 +1,66 @@
/* eslint-disable max-nested-callbacks */

import {
expect,
} from 'chai';
import chalk from 'chalk';
import alignTableData from '../src/alignTableData';
import makeConfig from '../src/makeConfig';

describe('alignTableData', () => {
context('when the string width is equal to column width config', () => {
it('returns the unchange string', () => {
const rows = [['aaa'], [chalk.red('bbb')]];

expect(alignTableData(rows, makeConfig(rows, {
columns: {
0: {
width: 3,
},
},
}))).to.deep.equal(rows);
});
});

context('when the string is different from the column width config', () => {
it('aligns cells with column width and alignment config', () => {
const rows = [['a', 'b', 'c'],
[chalk.red('a'), chalk.red('b'), chalk.red('c')]];

expect(alignTableData(rows, makeConfig(rows, {
columnDefault: {
width: 3,
},
columns: {
0: {
alignment: 'left',
},
1: {
alignment: 'right',
},
2: {
alignment: 'center',
},
},
}))).to.deep.equal([['a ', ' b', ' c '], [
chalk.red('a') + ' ', ' ' + chalk.red('b'), ' ' + chalk.red('c') + ' ',
]]);
});
});

context('when the string is longer then column width', () => {
it('throws an error', () => {
const rows = [['aaaa']];

expect(() => {
alignTableData(rows, makeConfig(rows, {
columns: {
0: {
width: 3,
},
},
}));
}).to.throw(Error, 'Subject parameter value width cannot be greater than the container width.');
});
});
});
41 changes: 31 additions & 10 deletions test/calculateRowHeightIndex.js
Expand Up @@ -6,14 +6,39 @@ import {
import calculateRowHeightIndex from '../src/calculateRowHeightIndex';

describe('calculateRowHeightIndex', () => {
context('invalid column width', () => {
it('throws an TypeError', () => {
expect(() => {
return calculateRowHeightIndex([['a']], {
columns: {
0: {
width: '3',
},
},
});
}).to.be.throw(TypeError, 'column[index].width must be a number.');
});
});

context('invalid column wrapWord', () => {
it('throws an TypeError', () => {
expect(() => {
return calculateRowHeightIndex([['a']], {
columns: {
0: {
width: 3,
wrapWord: 'true',
},
},
});
}).to.be.throw(TypeError, 'column[index].wrapWord must be a boolean.');
});
});

context('single column', () => {
context('cell content width is lesser than column width', () => {
it('is equal to 1', () => {
const data = [
[
'aaa',
],
];
const data = [['aaa']];

const config = {
columns: {
Expand All @@ -31,11 +56,7 @@ describe('calculateRowHeightIndex', () => {
});
context('cell content width is twice the size of the column width', () => {
it('is equal to 2', () => {
const data = [
[
'aaabbb',
],
];
const data = [['aaabbb']];

const config = {
columns: {
Expand Down
44 changes: 44 additions & 0 deletions test/createStream.js
Expand Up @@ -3,7 +3,11 @@
import {
expect,
} from 'chai';
import {
stub as SinonStub, assert,
} from 'sinon';
import createStream from '../src/createStream';
import getBorderCharacters from '../src/getBorderCharacters';

describe('createStream', () => {
context('"config.columnDefault.width" property is not provided', () => {
Expand Down Expand Up @@ -38,4 +42,44 @@ describe('createStream', () => {
}).to.throw(Error, 'Row cell count does not match the config.columnCount.');
});
});

context('write two rows', () => {
let stub;
before(() => {
stub = SinonStub(process.stdout, 'write');
});
after(() => {
stub.restore();
});

it('process.stdout.write calls twice with proper arguments', () => {
const stream = createStream({
border: getBorderCharacters('ramac'),
columnCount: 3,
columnDefault: {
width: 2,
},
columns: {
0: {
alignment: 'right',
paddingLeft: 3,
},
1: {
alignment: 'center',
paddingRight: 2,
},
2: {
alignment: 'left',
width: 5,
},
},
});
stream.write(['a b', 'ccc', 'd']);
stream.write(['e', 'f', 'g']);

assert.callCount(stub, 2);
assert.calledWithExactly(stub.getCall(0), '+------+-----+-------+\n| a | cc | d |\n| b | c | |\n+------+-----+-------+');
assert.calledWithExactly(stub.getCall(1), '\r\u001b[K|------|-----|-------|\n| e | f | g |\n+------+-----+-------+');
});
});
});
18 changes: 18 additions & 0 deletions test/drawRow.js
@@ -0,0 +1,18 @@
import {
expect,
} from 'chai';
import drawRow from '../src/drawRow';

describe('drawRow', () => {
it('draws a row using parts', () => {
const parts = {
bodyJoin: '│',
bodyLeft: '║',
bodyRight: '║',
};

expect(drawRow([], parts)).to.equal('║║\n');
expect(drawRow(['a'], parts)).to.equal('║a║\n');
expect(drawRow(['a', ' b '], parts)).to.equal('║a│ b ║\n');
});
});
116 changes: 116 additions & 0 deletions test/getBorderCharacters.js
@@ -0,0 +1,116 @@
/* eslint-disable max-nested-callbacks */

import {
expect,
} from 'chai';
import getBorderCharacters from '../src/getBorderCharacters';

describe('getBorderCharacters', () => {
context('given name \'honeywell\'', () => {
it('returns the \'honeywell\' template', () => {
expect(getBorderCharacters('honeywell')).to.be.deep.equal({
bodyJoin: '│',
bodyLeft: '║',
bodyRight: '║',
bottomBody: '═',

bottomJoin: '╧',
bottomLeft: '╚',
bottomRight: '╝',
joinBody: '─',

joinJoin: '┼',
joinLeft: '╟',
joinRight: '╢',

topBody: '═',
topJoin: '╤',
topLeft: '╔',
topRight: '╗',
});
});
});

context('given name \'norc\'', () => {
it('returns the \'norc\' template', () => {
expect(getBorderCharacters('norc')).to.be.deep.equal({
bodyJoin: '│',
bodyLeft: '│',
bodyRight: '│',
bottomBody: '─',

bottomJoin: '┴',
bottomLeft: '└',
bottomRight: '┘',
joinBody: '─',

joinJoin: '┼',
joinLeft: '├',
joinRight: '┤',

topBody: '─',
topJoin: '┬',
topLeft: '┌',
topRight: '┐',
});
});
});

context('given name \'ramac\'', () => {
it('returns the \'ramac\' template', () => {
expect(getBorderCharacters('ramac')).to.be.deep.equal({
bodyJoin: '|',
bodyLeft: '|',
bodyRight: '|',
bottomBody: '-',

bottomJoin: '+',
bottomLeft: '+',
bottomRight: '+',
joinBody: '-',

joinJoin: '|',
joinLeft: '|',
joinRight: '|',

topBody: '-',
topJoin: '+',
topLeft: '+',
topRight: '+',
});
});
});

context('given name \'void\'', () => {
it('returns the \'void\' template', () => {
expect(getBorderCharacters('void')).to.be.deep.equal({
bodyJoin: '',
bodyLeft: '',
bodyRight: '',
bottomBody: '',

bottomJoin: '',
bottomLeft: '',
bottomRight: '',
joinBody: '',

joinJoin: '',
joinLeft: '',
joinRight: '',

topBody: '',
topJoin: '',
topLeft: '',
topRight: '',
});
});
});

context('given another name', () => {
it('throws an error', () => {
expect(() => {
return getBorderCharacters('bold');
}).to.throw(Error, 'Unknown border template "bold".');
});
});
});
19 changes: 19 additions & 0 deletions test/makeConfig.js
Expand Up @@ -72,4 +72,23 @@ describe('makeConfig', () => {
});
});
});

context('"drawHorizontalLine', () => {
context('is not provided', () => {
it('defaults to retuning true', () => {
const config = makeConfig([['aaaaa']]);

expect(config.drawHorizontalLine()).to.equal(true);
});
});
});
context('"drawHorizontalLine', () => {
context('is not provided', () => {
it('defaults to retuning true', () => {
const config = makeConfig([['aaaaa']]);

expect(config.singleLine).to.equal(false);
});
});
});
});

0 comments on commit 6dd91e3

Please sign in to comment.