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

Test colspan #113

Merged
merged 5 commits into from
May 30, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
<th colspan="2" class="data-sort">On Our Dates</th>
<th>First Sold Out</th>
</tr>
</thead>
<tbody>
<tr>
</thead>
<td class="tags"></td>
<td class="category">Comedy</td>
<td class="show_name">Show 1</td>
Expand Down Expand Up @@ -161,7 +160,6 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
<td class="pct ours">50%</td>
<td>2022-07-31</td>
</tr>
</tbody>
<thead>
<tr>
<th>Last Name</th>
Expand All @@ -174,7 +172,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
<th class="data-sort">data-sort days</th>
<th>dates in dd/mm/yyyy</th>
</tr>
</thead>
</thead>
<tr class="table-row-1">
<td>Franklin</td>
<td>Benjamin</td>
Expand Down Expand Up @@ -230,6 +228,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
<td data-sort="1">Monday</td>
<td>8/6/1978</td>
</tr>
</tbody>
<thead>
<tr>
<th>Last Name</th>
Expand All @@ -242,7 +241,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
<th class="data-sort">data-sort days</th>
<th>dates in dd/mm/yyyy</th>
</tr>
</thead>
</thead>
<tr class="table-row-1">
<td>Franklin</td>
<td>Benjamin</td>
Expand Down
14 changes: 7 additions & 7 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
if (sortableTable.getElementsByTagName("thead").length === 0) {
createMissingTableHead(sortableTable);
if (sortableTable.querySelectorAll("tbody").length > 1) {
// Why index 1?; I don't remember
return sortableTable.querySelectorAll("tbody")[1];
// don't select empty tbody that the browser creates
return sortableTable.querySelectorAll('tbody:not(:nth-child(2))');
} else {
return sortableTable.querySelectorAll("tbody");
}
Expand Down Expand Up @@ -85,8 +85,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
let foundMatch = false;
for (let key of Object.keys(inferableClasses)) {
let classRegexp = inferableClasses[key].regexp;
if (tableColumn.innerText) {
if (tableColumn.innerText.match(classRegexp) !== null) {
if (tableColumn?.innerText !== undefined) {
if (tableColumn.innerText.match(classRegexp)) {
foundMatch = true;
inferableClasses[key].count++;
}
Expand Down Expand Up @@ -114,21 +114,21 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
rows: [],
headers: [],
};
for (let index of table.theads.keys()) {
table.headers.push(table.theads.item(index).querySelectorAll("th"));
}
for (let index of table.bodies.keys()) {
if (table.bodies.item(index) == null) {
return;
}
table.headers.push(table.theads.item(index).querySelectorAll("th"));
table.rows.push(table.bodies.item(index).querySelectorAll("tr"));
}

table.hasClass = {
noClassInfer: sortableTable.classList.contains("no-class-infer"),
cellsSort: sortableTable.classList.contains("cells-sort"),
tableArrows: sortableTable.classList.contains("table-arrows"),
rememberSort: sortableTable.classList.contains("remember-sort"),
};

for (
let headerIndex = 0;
headerIndex < table.theads.length;
Expand Down
40 changes: 20 additions & 20 deletions test/order-by-desc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const createTestTable = require("./table");
test("Alpha - Capitalized: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["Echo", "Alpha", "Bravo", "Charlie", "Delta"] },
{ col0: {td: ["Echo", "Alpha", "Bravo", "Charlie", "Delta"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -14,7 +14,7 @@ test("Alpha - Capitalized: order-by-desc", () => {
test("Alpha - Lowercase: order-by-desc ", () => {
expect(
createTestTable(
{ col0: ["echo", "alpha", "bravo", "charlie", "delta"] },
{ col0: {td: ["echo", "alpha", "bravo", "charlie", "delta"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -24,14 +24,14 @@ test("Alpha - Lowercase: order-by-desc ", () => {

test("Numerical: order-by-desc", () => {
expect(
createTestTable({ col0: [5, 3, 4, 1, 2] }, { classTags: "order-by-desc" })
createTestTable({ col0: {td:[5, 3, 4, 1, 2] }}, { classTags: "order-by-desc" })
).toStrictEqual({ col0: ["5", "4", "3", "2", "1"] });
});

test("Alphanumeric: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"] },
{ col0:{td: ["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -44,7 +44,7 @@ test("Alphanumeric: order-by-desc", () => {
test("Dates: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["1979/9/6", "2008/4/9", "1879/12/16", "1978/4/6", "1978/4/16"] },
{ col0: {td: ["1979/9/6", "2008/4/9", "1879/12/16", "1978/4/6", "1978/4/16"] }},
{ classTags: "order-by-desc" }
)
).toStrictEqual({
Expand All @@ -55,7 +55,7 @@ test("Dates: order-by-desc", () => {
test("Money: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["$29", "$93", "$84", "$20", "$58"] },
{ col0: {td: ["$29", "$93", "$84", "$20", "$58"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -66,7 +66,7 @@ test("Money: order-by-desc", () => {
test("Empty cells sort at the end: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["Echo", "", "Bravo", "", "Alpha"] },
{ col0: {td: ["Echo", "", "Bravo", "", "Alpha"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -78,7 +78,7 @@ test("Order by file-size: file-size order-by-desc", () => {
expect(
createTestTable(
{
col0: [
col0: {td:[
"10MB",
"10GB",
"10TB",
Expand All @@ -89,7 +89,7 @@ test("Order by file-size: file-size order-by-desc", () => {
"10KB",
"10GiB",
],
},
}},
{ classTags: "order-by-desc file-size-sort" }
)
).toStrictEqual({
Expand All @@ -110,7 +110,7 @@ test("Order by file-size: file-size order-by-desc", () => {
test("Floating point numbers: order-by-desc", () => {
expect(
createTestTable(
{ col0: [6.23, 0.25, 3.15, 9.09, 0.35] },
{ col0: {td:[6.23, 0.25, 3.15, 9.09, 0.35] }},
{
classTags: "order-by-desc",
}
Expand All @@ -121,7 +121,7 @@ test("Floating point numbers: order-by-desc", () => {
test("Release Versions: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["4.0.1", "3.0.2", "4.1.0", "3.0.4", "4.2.0"] },
{ col0: {td: ["4.0.1", "3.0.2", "4.1.0", "3.0.4", "4.2.0"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -133,7 +133,7 @@ test("data-sort: example days of week - reversed", () => {
expect(
createTestTable(
{
col0: [
col0: {td:[
"Saturday",
"Wednesday",
"Sunday",
Expand All @@ -142,7 +142,7 @@ test("data-sort: example days of week - reversed", () => {
"Tuesday",
"Monday",
],
},
}},
{ classTags: "data-sort order-by-desc" }
)
).toStrictEqual({
Expand All @@ -162,14 +162,14 @@ test("visible-tr-sort: example sort only visible trs", () => {
expect(
createTestTable(
{
col0: [
col0: {td:[
"row1", // invisible
"row2",
"row3", // invisible
"row4",
"row5",
],
},
}},
{ classTags: "order-by-desc" },
{ invisibleIndex: [0, 2] }
)
Expand All @@ -179,7 +179,7 @@ test("visible-tr-sort: example sort only visible trs", () => {
test("onload-sort: testing that it sorts without a click from user", () => {
expect(
createTestTable(
{ col0: [5, 3, 4, 1, 2] },
{ col0: {td: [5, 3, 4, 1, 2] }},
{ classTags: "order-by-desc onload-sort" }
)
).toStrictEqual({ col0: ["5", "4", "3", "2", "1"] });
Expand All @@ -189,8 +189,8 @@ test("order-by-desc Remember-sort: Sorting cols without rememeber-sort class.",
expect(
createTestTable(
{
col0: ["charlie", "alpha", "beta"],
col1: ["carrie", "doris", "fisher"],
col0: {td: ["charlie", "alpha", "beta"]},
col1: {td: ["carrie", "doris", "fisher"]},
},
{
classTags: "order-by-desc",
Expand All @@ -210,8 +210,8 @@ test("order-by-desc Remember-sort: Sorting cols with rememeber-sort class.", ()
expect(
createTestTable(
{
col0: ["charlie", "alpha", "beta"],
col1: ["carrie", "doris", "fisher"],
col0: {td:["charlie", "alpha", "beta"]},
col1: {td:["carrie", "doris", "fisher"]},
},
{
classTags: "order-by-desc",
Expand Down
18 changes: 9 additions & 9 deletions test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ function createTestTable(
function getRowsOfTd(index, type) {
let rowsOfTd = "";
for (let key in testTableData) {
if (testTableData[key]) {
if (testTableData[key].td) {
if (type === "data-sort") {
rowsOfTd += `<td data-sort="${index}">${testTableData[key][index]}</td>`;
rowsOfTd += `<td data-sort="${index}">${testTableData[key].td[index]}</td>`;
} else {
rowsOfTd += `<td>${testTableData[key][index]}</td>`;
rowsOfTd += `<td>${testTableData[key].td[index]}</td>`;
}
}
}
return rowsOfTd;
}

let testTableTdRows = [];
for (let i = 0; i < testTableData["col0"].length; i++) {
for (let i = 0; i < testTableData["col0"].td.length; i++) {
let testTableTdRow;
if (thAttributes.classTags.includes("data-sort")) {
testTableTdRow = `${getRowsOfTd(i, "data-sort")}`;
Expand Down Expand Up @@ -63,12 +63,12 @@ function createTestTable(
<body>
<table class="table-sort ${props.tableTags}">
<thead>
${testTableHeaders}
${testTableHeaders}
</thead>
<tbody>
${testTableTdRows}
</tbody>
</table>
<tbody>
${testTableTdRows}
</tbody>
</table>
</body>
</html>`);

Expand Down
Loading