Skip to content

Commit df0a88f

Browse files
committed
fix: add unit tests
1 parent f506ff3 commit df0a88f

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

test/cell-test.js

+76-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@ describe('Cell', function () {
22
const colors = require('@colors/colors');
33
const Cell = require('../src/cell');
44
const { ColSpanCell, RowSpanCell } = Cell;
5-
const { mergeOptions } = require('../src/utils');
5+
const utils = require('../src/utils');
6+
const { wordWrap: actualWordWrap } = jest.requireActual('../src/utils');
7+
8+
jest.mock('../src/utils', () => ({
9+
...jest.requireActual('../src/utils'),
10+
wordWrap: jest.fn(), // only mock this
11+
}));
12+
13+
beforeEach(() => {
14+
// use the default implementation, unless we override
15+
utils.wordWrap.mockImplementation((...args) => actualWordWrap(...args));
16+
});
617

718
function defaultOptions() {
819
//overwrite coloring of head and border by default for easier testing.
9-
return mergeOptions({ style: { head: [], border: [] } });
20+
return utils.mergeOptions({ style: { head: [], border: [] } });
1021
}
1122

1223
function defaultChars() {
@@ -89,6 +100,69 @@ describe('Cell', function () {
89100
});
90101

91102
describe('mergeTableOptions', function () {
103+
describe('wordwrap', function () {
104+
let tableOptions, cell;
105+
106+
function initCell({ wordWrap, wrapOnWordBoundary } = {}) {
107+
cell = new Cell({ content: 'some text', wordWrap, wrapOnWordBoundary });
108+
cell.x = cell.y = 0;
109+
}
110+
111+
beforeEach(() => {
112+
utils.wordWrap.mockClear();
113+
tableOptions = { ...defaultOptions(), colWidths: [50] };
114+
});
115+
116+
it('no cell wordWrap override (tableOptions.wordWrap=true)', function () {
117+
tableOptions.wordWrap = true; // wrapOnWordBoundary is true by default
118+
initCell();
119+
120+
cell.mergeTableOptions(tableOptions);
121+
expect(utils.wordWrap).toHaveBeenCalledWith(expect.any(Number), cell.content, true /*wrapOnWordBoundary*/);
122+
});
123+
124+
it('no cell wordWrap override (tableOptions.wordWrap=false)', function () {
125+
tableOptions.wordWrap = false; // wrapOnWordBoundary is true by default
126+
initCell();
127+
128+
cell.mergeTableOptions(tableOptions);
129+
expect(utils.wordWrap).not.toHaveBeenCalled();
130+
});
131+
132+
it('cell wordWrap override (cell.options.wordWrap=false)', function () {
133+
tableOptions.wordWrap = true; // wrapOnWordBoundary is true by default
134+
initCell({ wordWrap: false });
135+
136+
cell.mergeTableOptions(tableOptions);
137+
expect(utils.wordWrap).not.toHaveBeenCalled(); // no wrapping done
138+
});
139+
140+
it('cell wordWrap override (cell.options.wordWrap=true)', function () {
141+
tableOptions.wordWrap = false; // wrapOnWordBoundary is true by default
142+
initCell({ wordWrap: true });
143+
144+
cell.mergeTableOptions(tableOptions);
145+
expect(utils.wordWrap).toHaveBeenCalled();
146+
});
147+
148+
it('cell wrapOnWordBoundary override (cell.options.wrapOnWordBoundary=false)', function () {
149+
tableOptions.wordWrap = true; // wrapOnWordBoundary is true by default
150+
initCell({ wrapOnWordBoundary: false });
151+
152+
cell.mergeTableOptions(tableOptions);
153+
expect(utils.wordWrap).toHaveBeenCalledWith(expect.any(Number), cell.content, false /*wrapOnWordBoundary*/);
154+
});
155+
156+
it('cell wrapOnWordBoundary override (cell.options.wrapOnWordBoundary=true)', function () {
157+
tableOptions.wordWrap = true;
158+
tableOptions.wrapOnWordBoundary = false;
159+
initCell({ wrapOnWordBoundary: true });
160+
161+
cell.mergeTableOptions(tableOptions);
162+
expect(utils.wordWrap).toHaveBeenCalledWith(expect.any(Number), cell.content, true /*wrapOnWordBoundary*/);
163+
});
164+
});
165+
92166
describe('chars', function () {
93167
it('unset chars take on value of table', function () {
94168
let cell = new Cell();

0 commit comments

Comments
 (0)