Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to set/override wordWrap and wrapOnWordBoundary options per cell #303

Merged
merged 1 commit into from Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/cell.js
Expand Up @@ -73,7 +73,9 @@ class Cell {
}

computeLines(tableOptions) {
if (this.fixedWidth && (tableOptions.wordWrap || tableOptions.textWrap)) {
const tableWordWrap = tableOptions.wordWrap || tableOptions.textWrap;
const { wordWrap = tableWordWrap } = this.options;
if (this.fixedWidth && wordWrap) {
this.fixedWidth -= this.paddingLeft + this.paddingRight;
if (this.colSpan) {
let i = 1;
Expand All @@ -82,7 +84,8 @@ class Cell {
i++;
}
}
const { wrapOnWordBoundary = true } = tableOptions;
const { wrapOnWordBoundary: tableWrapOnWordBoundary = true } = tableOptions;
const { wrapOnWordBoundary = tableWrapOnWordBoundary } = this.options;
return this.wrapLines(utils.wordWrap(this.fixedWidth, this.content, wrapOnWordBoundary));
}
return this.wrapLines(this.content.split('\n'));
Expand Down
78 changes: 76 additions & 2 deletions test/cell-test.js
Expand Up @@ -2,11 +2,22 @@ describe('Cell', function () {
const colors = require('@colors/colors');
const Cell = require('../src/cell');
const { ColSpanCell, RowSpanCell } = Cell;
const { mergeOptions } = require('../src/utils');
const utils = require('../src/utils');
const { wordWrap: actualWordWrap } = jest.requireActual('../src/utils');

jest.mock('../src/utils', () => ({
...jest.requireActual('../src/utils'),
wordWrap: jest.fn(), // only mock this
}));

beforeEach(() => {
// use the default implementation, unless we override
utils.wordWrap.mockImplementation((...args) => actualWordWrap(...args));
});

function defaultOptions() {
//overwrite coloring of head and border by default for easier testing.
return mergeOptions({ style: { head: [], border: [] } });
return utils.mergeOptions({ style: { head: [], border: [] } });
}

function defaultChars() {
Expand Down Expand Up @@ -89,6 +100,69 @@ describe('Cell', function () {
});

describe('mergeTableOptions', function () {
describe('wordwrap', function () {
let tableOptions, cell;

function initCell({ wordWrap, wrapOnWordBoundary } = {}) {
cell = new Cell({ content: 'some text', wordWrap, wrapOnWordBoundary });
cell.x = cell.y = 0;
}

beforeEach(() => {
utils.wordWrap.mockClear();
tableOptions = { ...defaultOptions(), colWidths: [50] };
});

it('no cell wordWrap override (tableOptions.wordWrap=true)', function () {
tableOptions.wordWrap = true; // wrapOnWordBoundary is true by default
initCell();

cell.mergeTableOptions(tableOptions);
expect(utils.wordWrap).toHaveBeenCalledWith(expect.any(Number), cell.content, true /*wrapOnWordBoundary*/);
});

it('no cell wordWrap override (tableOptions.wordWrap=false)', function () {
tableOptions.wordWrap = false; // wrapOnWordBoundary is true by default
initCell();

cell.mergeTableOptions(tableOptions);
expect(utils.wordWrap).not.toHaveBeenCalled();
});

it('cell wordWrap override (cell.options.wordWrap=false)', function () {
tableOptions.wordWrap = true; // wrapOnWordBoundary is true by default
initCell({ wordWrap: false });

cell.mergeTableOptions(tableOptions);
expect(utils.wordWrap).not.toHaveBeenCalled(); // no wrapping done
});

it('cell wordWrap override (cell.options.wordWrap=true)', function () {
tableOptions.wordWrap = false; // wrapOnWordBoundary is true by default
initCell({ wordWrap: true });

cell.mergeTableOptions(tableOptions);
expect(utils.wordWrap).toHaveBeenCalled();
});

it('cell wrapOnWordBoundary override (cell.options.wrapOnWordBoundary=false)', function () {
tableOptions.wordWrap = true; // wrapOnWordBoundary is true by default
initCell({ wrapOnWordBoundary: false });

cell.mergeTableOptions(tableOptions);
expect(utils.wordWrap).toHaveBeenCalledWith(expect.any(Number), cell.content, false /*wrapOnWordBoundary*/);
});

it('cell wrapOnWordBoundary override (cell.options.wrapOnWordBoundary=true)', function () {
tableOptions.wordWrap = true;
tableOptions.wrapOnWordBoundary = false;
initCell({ wrapOnWordBoundary: true });

cell.mergeTableOptions(tableOptions);
expect(utils.wordWrap).toHaveBeenCalledWith(expect.any(Number), cell.content, true /*wrapOnWordBoundary*/);
});
});

describe('chars', function () {
it('unset chars take on value of table', function () {
let cell = new Cell();
Expand Down