@microsoft/connected-workbooks
Version:
Microsoft backed, Excel advanced xlsx workbook generation JavaScript library
111 lines (110 loc) • 4.64 kB
JavaScript
;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("../src/utils");
var jsdom_1 = require("jsdom");
// Create a JSDOM instance
var window = new jsdom_1.JSDOM("<!DOCTYPE html><html><body></body></html>").window;
var document = window.document;
// Helper function to create table rows and cells
var createRowWithCells = function (cellValues, celltypes) {
if (celltypes === void 0) { celltypes = "td"; }
var row = document.createElement("tr");
cellValues.forEach(function (value) {
var cell = document.createElement(celltypes);
cell.textContent = value;
row.appendChild(cell);
});
return row;
};
describe("extractTableValues", function () {
test("returns an empty array for an empty table", function () {
// Create an empty table element
var table = document.createElement("table");
// Call the method and expect an empty array as the result
expect(utils_1.htmlUtils.extractTableValues(table)).toEqual([]);
});
test("returns an empty row for an empty tr", function () {
var table = document.createElement("table");
var tbody = document.createElement("tbody");
var row = createRowWithCells([]);
tbody.appendChild(row);
table.appendChild(tbody);
expect(utils_1.htmlUtils.extractTableValues(table)).toEqual([[]]);
});
test("extracts values correctly from a table with multiple rows and cells", function () {
var table = document.createElement("table");
var tbody = document.createElement("tbody");
var row1 = createRowWithCells(["A", "B"]);
var row2 = createRowWithCells(["C", "D"]);
tbody.appendChild(row1);
tbody.appendChild(row2);
table.appendChild(tbody);
var expectedResult = [
["A", "B"],
["C", "D"],
];
expect(utils_1.htmlUtils.extractTableValues(table)).toEqual(expectedResult);
});
test("handles empty cells by using an empty string", function () {
var table = document.createElement("table");
var tbody = document.createElement("tbody");
var row1 = createRowWithCells(["A", ""]);
var row2 = createRowWithCells(["", "D"]);
tbody.appendChild(row1);
tbody.appendChild(row2);
table.appendChild(tbody);
var expectedResult = [
["A", ""],
["", "D"],
];
expect(utils_1.htmlUtils.extractTableValues(table)).toEqual(expectedResult);
});
test("handle table header (th) cells", function () {
var table = document.createElement("table");
var tbody = document.createElement("tbody");
var headerRow = createRowWithCells(["Header 1", "Header 2"], "th");
var dataRow = createRowWithCells(["A", "B"]);
tbody.appendChild(headerRow);
tbody.appendChild(dataRow);
table.appendChild(tbody);
var expectedResult = [
["Header 1", "Header 2"],
["A", "B"],
];
expect(utils_1.htmlUtils.extractTableValues(table)).toEqual(expectedResult);
});
test("handles tables with multiple tbody elements", function () {
var table = document.createElement("table");
var tbody1 = document.createElement("tbody");
var row1 = createRowWithCells(["A", "B"]);
tbody1.appendChild(row1);
table.appendChild(tbody1);
var tbody2 = document.createElement("tbody");
var row2 = createRowWithCells(["C", "D"]);
tbody2.appendChild(row2);
table.appendChild(tbody2);
var expectedResult = [
["A", "B"],
["C", "D"],
];
expect(utils_1.htmlUtils.extractTableValues(table)).toEqual(expectedResult);
});
test("handles tables that are not MxN", function () {
var table = document.createElement("table");
var tbody1 = document.createElement("tbody");
var row1 = createRowWithCells(["A", "B"]);
tbody1.appendChild(row1);
table.appendChild(tbody1);
var tbody2 = document.createElement("tbody");
var row2 = createRowWithCells(["C", "D", "E"]);
tbody2.appendChild(row2);
table.appendChild(tbody2);
var expectedResult = [
["A", "B"],
["C", "D", "E"],
];
expect(utils_1.htmlUtils.extractTableValues(table)).toEqual(expectedResult);
});
});