Skip to content

Commit a510eb2

Browse files
Added tests for inferring sort classes. (#131)
1 parent 402d8ae commit a510eb2

File tree

3 files changed

+254
-3
lines changed

3 files changed

+254
-3
lines changed

public/table-sort.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
9191
let foundMatch = false;
9292
for (let key of Object.keys(inferableClasses)) {
9393
let classRegexp = inferableClasses[key].regexp;
94-
if (tableColumn.innerText !== undefined) {
95-
if (tableColumn.innerText.match(classRegexp)) {
94+
let columnOfTd = testingTableSortJS
95+
? tableColumn.textContent
96+
: tableColumn.innerText;
97+
if (columnOfTd !== undefined && columnOfTd.match(classRegexp) ) {
9698
foundMatch = true;
9799
inferableClasses[key].count++;
98-
}
99100
}
100101
if (inferableClasses[key].count >= threshold) {
101102
th.classList.add(inferableClasses[key].class);
@@ -563,6 +564,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
563564
}
564565
timesClickedColumn += 1;
565566

567+
566568
const hasThClass = {
567569
dataSort: th.classList.contains("data-sort"),
568570
fileSize: th.classList.contains("file-size-sort"),

test/tagsInferenceTable.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// TEST TABLE FOR CLASSES THAT ARE INFERRED FOR <TH>.
2+
const jsdom = require("jsdom");
3+
const { JSDOM } = jsdom;
4+
require("iconv-lite").encodingExists("foo");
5+
const tableSortJs = require("../public/table-sort");
6+
7+
function createTestTable(
8+
testTableData,
9+
thAttributes = { classTags: "", colspan: "" },
10+
props = { colsToClick: [], invisibleIndex: [], tableTags: "", trClasses: "" }
11+
) {
12+
const numberOfTableColumns = Object.keys(testTableData).length;
13+
let testTableHeaders = "";
14+
for (let i = 0; i < numberOfTableColumns; i++) {
15+
testTableHeaders += `<th colspan="${thAttributes.colspan}">Testing Column</th>`;
16+
}
17+
testTableHeaders = `<tr> ${testTableHeaders} </tr>`;
18+
19+
function getRowsOfTd(index, type) {
20+
let rowsOfTd = "";
21+
for (let key in testTableData) {
22+
if (testTableData[key].td) {
23+
if (type === "data-sort") {
24+
rowsOfTd += `<td data-sort="${index}">${testTableData[key].td[index]}</td>`;
25+
} else {
26+
rowsOfTd += `<td>${testTableData[key].td[index]}</td>`;
27+
}
28+
}
29+
}
30+
return rowsOfTd;
31+
}
32+
33+
let testTableTdRows = [];
34+
for (let i = 0; i < testTableData["col0"].td.length; i++) {
35+
let testTableTdRow;
36+
if (thAttributes.classTags.includes("data-sort")) {
37+
testTableTdRow = `${getRowsOfTd(i, "data-sort")}`;
38+
} else {
39+
testTableTdRow = `${getRowsOfTd(i)}`;
40+
}
41+
if (
42+
props.invisibleIndex !== undefined &&
43+
props.invisibleIndex.includes(i)
44+
) {
45+
testTableTdRows.push(`<tr style="display: none;">${testTableTdRow}</tr>`);
46+
} else {
47+
if (props.tableTags === "cells-sort" || props.tableTags === "tr-sort") {
48+
testTableTdRows.push(
49+
`<tr class="${props.trClasses}-${i}"> ${testTableTdRow}</tr>`
50+
);
51+
} else {
52+
testTableTdRows.push(
53+
`<tr class="${props.trClasses}"> ${testTableTdRow}</tr>`
54+
);
55+
}
56+
}
57+
}
58+
59+
const dom = new JSDOM(`<!DOCTYPE html>
60+
<html>
61+
<head>
62+
</head>
63+
<body>
64+
<table class="table-sort ${props.tableTags}">
65+
<thead>
66+
${testTableHeaders}
67+
</thead>
68+
<tbody>
69+
${testTableTdRows}
70+
</tbody>
71+
</table>
72+
</body>
73+
</html>`);
74+
75+
// Call tablesort and make table sortable and simulate clicks from a user.
76+
tableSortJs(true, dom.window.document);
77+
// Make an array from table contents to test if sorted correctly.
78+
let table = dom.window.document.querySelector("table");
79+
const tableHeadWithInferredClassName = table
80+
.querySelectorAll("thead th")
81+
let inferedClassNamesOfTh = Array.from(tableHeadWithInferredClassName).map((e)=>e.getAttribute("class"))
82+
return inferedClassNamesOfTh;
83+
}
84+
85+
module.exports = createTestTable;

test/tagsInferenceTable.test.js

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
const createTestTable = require("./tagsInferenceTable");
2+
3+
4+
test("InferSortClassesOnTH - FILE SIZE", () => {
5+
expect(
6+
createTestTable({
7+
col0: {
8+
td: [
9+
"10MB",
10+
"10GB",
11+
"10TB",
12+
"10B",
13+
"10MiB",
14+
"10TiB",
15+
"10Kib",
16+
"10KB",
17+
"10GiB",
18+
],
19+
},
20+
// add in space and make some undercase
21+
col1: {
22+
td: [
23+
"10 mB",
24+
"10 GB",
25+
"10 Tb",
26+
"10 B",
27+
"10 mib",
28+
"10 tib",
29+
"10 kib",
30+
"10 kB",
31+
"10 giB",
32+
],
33+
},
34+
})
35+
).toStrictEqual(["file-size-sort", "file-size-sort"]);
36+
});
37+
38+
39+
test("InferSortClassesOnTH - DATES", () => {
40+
expect(
41+
createTestTable({
42+
col0: {
43+
td: ["1979/9/6", "2008/4/9", "1879/12/16", "1978/4/6", "1978/4/16"],
44+
},
45+
col1: {
46+
td: [
47+
"1-14-1992",
48+
"1.13.1992",
49+
"4.30.2008",
50+
"1/20/1992",
51+
"10-12-2017",
52+
"2/14/1992",
53+
],
54+
},
55+
col2: {
56+
td: [
57+
"17/6/1978",
58+
"18.10.2027",
59+
"10-12-2017",
60+
"13/12/2017",
61+
"4.9.2008",
62+
"2.3.1879",
63+
"22.3.1879",
64+
"8/6/1978",
65+
"4/6/1978",
66+
],
67+
},
68+
})
69+
// two dates-dmy-sort as mdy is not an inferred class but explicit override.
70+
).toStrictEqual(["dates-ymd-sort","dates-dmy-sort","dates-dmy-sort"]);
71+
});
72+
73+
74+
75+
test("InferSortClassesOnTH - RUNTIME", () => {
76+
expect(
77+
createTestTable({
78+
col0: {
79+
td: [
80+
"2m 52s",
81+
"1h 20m 10s",
82+
"3s",
83+
"11h 10m 10s",
84+
"7s",
85+
"11m 40s",
86+
"36s",
87+
"1h 10m 10s",
88+
"9m 44s",
89+
"1m 36s",
90+
"41s",
91+
],
92+
},
93+
})
94+
).toStrictEqual(["runtime-sort"]);
95+
});
96+
97+
test("InferSortClassesOnTH - NUMERIC", () => {
98+
expect(
99+
createTestTable({
100+
// commas
101+
col0: {
102+
td: [
103+
"20,000.89",
104+
"30,000.32",
105+
"1",
106+
"0.111",
107+
"21,000.92",
108+
"19845",
109+
"12000",
110+
"-90",
111+
"-10,000.39",
112+
"-10,000.10",
113+
],
114+
},
115+
// negative numbers
116+
col1: { td: ["1.05", "-2.3", "-3", "1", "-6", "(1.4)", "14"] },
117+
// decimals
118+
col2: { td: ["0.1", "0.2", "0.3", "0.11", "0.13", "0.13", "0.14"] },
119+
col3: {
120+
td: [
121+
"1.05",
122+
"-2.3",
123+
"-3",
124+
"1",
125+
"-6",
126+
"",
127+
"(0.5)",
128+
"1a",
129+
"b",
130+
"(c)",
131+
"{1}",
132+
],
133+
},
134+
// TODO HANDLE CURRENCY $ / pounds, etc....
135+
})
136+
).toStrictEqual(["numeric-sort","numeric-sort","numeric-sort","numeric-sort"]);
137+
});
138+
139+
140+
// TODO no-class-infer
141+
// test("InferSortClassesOnTH - no-class-infer", () => {
142+
// expect(
143+
// createTestTable(
144+
// {
145+
// col0: {
146+
// td: [
147+
// "2m 52s",
148+
// "1h 20m 10s",
149+
// "3s",
150+
// "11h 10m 10s",
151+
// "7s",
152+
// "11m 40s",
153+
// "36s",
154+
// "1h 10m 10s",
155+
// "9m 44s",
156+
// "1m 36s",
157+
// "41s",
158+
// ],
159+
// },
160+
// },
161+
// // props={ tableTags: "no-class-infer"},
162+
// )
163+
// ).toStrictEqual(["runtime-sort"]);
164+
// });

0 commit comments

Comments
 (0)