-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcreateStream.ts
117 lines (105 loc) · 3.16 KB
/
createStream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* eslint-disable max-nested-callbacks */
import {
expect,
} from 'chai';
// eslint-disable-next-line import/no-namespace
import * as Sinon from 'sinon';
import {
getBorderCharacters,
createStream,
} from '../src';
describe('createStream', () => {
context('"config.columnDefault.width" property is not provided', () => {
it('throws an error', () => {
expect(() => {
createStream({
columnCount: 1,
columnDefault: {},
} as never);
}).to.throw(Error, 'Must provide config.columnDefault.width when creating a stream.');
});
});
context('Table data cell count does not match the columnCount.', () => {
it('throws an error', () => {
expect(() => {
const stream = createStream({
columnCount: 10,
columnDefault: {
width: 10,
},
});
stream.write(['foo']);
}).to.throw(Error, 'Row cell count does not match the config.columnCount.');
});
});
context('normal stream', () => {
let stub: Sinon.SinonStub;
beforeEach(() => {
stub = Sinon.stub(process.stdout, 'write');
});
afterEach(() => {
stub.restore();
stub.resetHistory();
});
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']);
Sinon.assert.callCount(stub, 2);
Sinon.assert.calledWithExactly(stub.getCall(0), '+------+-----+-------+\n| a | cc | d |\n| b | c | |\n+------+-----+-------+');
Sinon.assert.calledWithExactly(stub.getCall(1), '\r\u001b[K|------|-----|-------|\n| e | f | g |\n+------+-----+-------+');
});
context('given custom drawVerticalLine', () => {
it('use the callback to draw vertical lines', () => {
const stream = createStream({
columnCount: 2,
columnDefault: {
width: 2,
},
drawVerticalLine: (index) => {
return index === 1;
},
});
stream.write(['a', 'b']);
Sinon.assert.callCount(stub, 1);
Sinon.assert.calledOnceWithExactly(stub, '════╤════\n a │ b \n════╧════');
});
});
context('append empty row', () => {
it('does not add a new line', () => {
const stream = createStream({
border: getBorderCharacters('void'),
columnCount: 1,
columnDefault: {
width: 2,
},
});
stream.write(['a']);
stream.write(['']);
stream.write(['a']);
Sinon.assert.calledWithExactly(stub.getCall(1), '');
Sinon.assert.calledWithExactly(stub.getCall(2), '\n a');
});
});
});
});