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

Added tests for sort class inferance. #131

Merged
merged 1 commit into from
Mar 29, 2024
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
8 changes: 5 additions & 3 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
let foundMatch = false;
for (let key of Object.keys(inferableClasses)) {
let classRegexp = inferableClasses[key].regexp;
if (tableColumn.innerText !== undefined) {
if (tableColumn.innerText.match(classRegexp)) {
let columnOfTd = testingTableSortJS
? tableColumn.textContent
: tableColumn.innerText;
if (columnOfTd !== undefined && columnOfTd.match(classRegexp) ) {
foundMatch = true;
inferableClasses[key].count++;
}
}
if (inferableClasses[key].count >= threshold) {
th.classList.add(inferableClasses[key].class);
Expand Down Expand Up @@ -563,6 +564,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}
timesClickedColumn += 1;


const hasThClass = {
dataSort: th.classList.contains("data-sort"),
fileSize: th.classList.contains("file-size-sort"),
Expand Down
85 changes: 85 additions & 0 deletions test/tagsInferenceTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// TEST TABLE FOR CLASSES THAT ARE INFERRED FOR <TH>.
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
require("iconv-lite").encodingExists("foo");
const tableSortJs = require("../public/table-sort");

function createTestTable(
testTableData,
thAttributes = { classTags: "", colspan: "" },
props = { colsToClick: [], invisibleIndex: [], tableTags: "", trClasses: "" }
) {
const numberOfTableColumns = Object.keys(testTableData).length;
let testTableHeaders = "";
for (let i = 0; i < numberOfTableColumns; i++) {
testTableHeaders += `<th colspan="${thAttributes.colspan}">Testing Column</th>`;
}
testTableHeaders = `<tr> ${testTableHeaders} </tr>`;

function getRowsOfTd(index, type) {
let rowsOfTd = "";
for (let key in testTableData) {
if (testTableData[key].td) {
if (type === "data-sort") {
rowsOfTd += `<td data-sort="${index}">${testTableData[key].td[index]}</td>`;
} else {
rowsOfTd += `<td>${testTableData[key].td[index]}</td>`;
}
}
}
return rowsOfTd;
}

let testTableTdRows = [];
for (let i = 0; i < testTableData["col0"].td.length; i++) {
let testTableTdRow;
if (thAttributes.classTags.includes("data-sort")) {
testTableTdRow = `${getRowsOfTd(i, "data-sort")}`;
} else {
testTableTdRow = `${getRowsOfTd(i)}`;
}
if (
props.invisibleIndex !== undefined &&
props.invisibleIndex.includes(i)
) {
testTableTdRows.push(`<tr style="display: none;">${testTableTdRow}</tr>`);
} else {
if (props.tableTags === "cells-sort" || props.tableTags === "tr-sort") {
testTableTdRows.push(
`<tr class="${props.trClasses}-${i}"> ${testTableTdRow}</tr>`
);
} else {
testTableTdRows.push(
`<tr class="${props.trClasses}"> ${testTableTdRow}</tr>`
);
}
}
}

const dom = new JSDOM(`<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table class="table-sort ${props.tableTags}">
<thead>
${testTableHeaders}
</thead>
<tbody>
${testTableTdRows}
</tbody>
</table>
</body>
</html>`);

// Call tablesort and make table sortable and simulate clicks from a user.
tableSortJs(true, dom.window.document);
// Make an array from table contents to test if sorted correctly.
let table = dom.window.document.querySelector("table");
const tableHeadWithInferredClassName = table
.querySelectorAll("thead th")
let inferedClassNamesOfTh = Array.from(tableHeadWithInferredClassName).map((e)=>e.getAttribute("class"))
return inferedClassNamesOfTh;
}

module.exports = createTestTable;
164 changes: 164 additions & 0 deletions test/tagsInferenceTable.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
const createTestTable = require("./tagsInferenceTable");


test("InferSortClassesOnTH - FILE SIZE", () => {
expect(
createTestTable({
col0: {
td: [
"10MB",
"10GB",
"10TB",
"10B",
"10MiB",
"10TiB",
"10Kib",
"10KB",
"10GiB",
],
},
// add in space and make some undercase
col1: {
td: [
"10 mB",
"10 GB",
"10 Tb",
"10 B",
"10 mib",
"10 tib",
"10 kib",
"10 kB",
"10 giB",
],
},
})
).toStrictEqual(["file-size-sort", "file-size-sort"]);
});


test("InferSortClassesOnTH - DATES", () => {
expect(
createTestTable({
col0: {
td: ["1979/9/6", "2008/4/9", "1879/12/16", "1978/4/6", "1978/4/16"],
},
col1: {
td: [
"1-14-1992",
"1.13.1992",
"4.30.2008",
"1/20/1992",
"10-12-2017",
"2/14/1992",
],
},
col2: {
td: [
"17/6/1978",
"18.10.2027",
"10-12-2017",
"13/12/2017",
"4.9.2008",
"2.3.1879",
"22.3.1879",
"8/6/1978",
"4/6/1978",
],
},
})
// two dates-dmy-sort as mdy is not an inferred class but explicit override.
).toStrictEqual(["dates-ymd-sort","dates-dmy-sort","dates-dmy-sort"]);
});



test("InferSortClassesOnTH - RUNTIME", () => {
expect(
createTestTable({
col0: {
td: [
"2m 52s",
"1h 20m 10s",
"3s",
"11h 10m 10s",
"7s",
"11m 40s",
"36s",
"1h 10m 10s",
"9m 44s",
"1m 36s",
"41s",
],
},
})
).toStrictEqual(["runtime-sort"]);
});

test("InferSortClassesOnTH - NUMERIC", () => {
expect(
createTestTable({
// commas
col0: {
td: [
"20,000.89",
"30,000.32",
"1",
"0.111",
"21,000.92",
"19845",
"12000",
"-90",
"-10,000.39",
"-10,000.10",
],
},
// negative numbers
col1: { td: ["1.05", "-2.3", "-3", "1", "-6", "(1.4)", "14"] },
// decimals
col2: { td: ["0.1", "0.2", "0.3", "0.11", "0.13", "0.13", "0.14"] },
col3: {
td: [
"1.05",
"-2.3",
"-3",
"1",
"-6",
"",
"(0.5)",
"1a",
"b",
"(c)",
"{1}",
],
},
// TODO HANDLE CURRENCY $ / pounds, etc....
})
).toStrictEqual(["numeric-sort","numeric-sort","numeric-sort","numeric-sort"]);
});


// TODO no-class-infer
// test("InferSortClassesOnTH - no-class-infer", () => {
// expect(
// createTestTable(
// {
// col0: {
// td: [
// "2m 52s",
// "1h 20m 10s",
// "3s",
// "11h 10m 10s",
// "7s",
// "11m 40s",
// "36s",
// "1h 10m 10s",
// "9m 44s",
// "1m 36s",
// "41s",
// ],
// },
// },
// // props={ tableTags: "no-class-infer"},
// )
// ).toStrictEqual(["runtime-sort"]);
// });
Loading