Skip to content

Commit e631ffd

Browse files
Set testing up for sorting multiple columns.
1 parent 1acb377 commit e631ffd

File tree

2 files changed

+288
-185
lines changed

2 files changed

+288
-185
lines changed

test/table.js

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,40 @@ function createTestTable(
99
thAttributes = { classTags: "" },
1010
invisibleIndex = []
1111
) {
12-
let getClassTagsForTH = [];
13-
let testTableThRow = `<tr><th class="${thAttributes.classTags}">Testing Column</th></tr>`;
14-
getClassTagsForTH.push(testTableThRow);
12+
const numberOfTableColumns = Object.keys(testTableData).length;
13+
let testTableHeaders = "";
14+
for (let i = 0; i < numberOfTableColumns; i++) {
15+
testTableHeaders += `<th class="${thAttributes.classTags}">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]) {
23+
if (type === "data-sort") {
24+
rowsOfTd += `<td data-sort="${index}">${testTableData[key][index]}</td>`;
25+
} else {
26+
rowsOfTd += `<td>${testTableData[key][index]}</td>`;
27+
}
28+
}
29+
}
30+
return rowsOfTd;
31+
}
1532

1633
let testTableTdRows = [];
17-
for (let i = 0; i < testTableData.length; i++) {
34+
for (let i = 0; i < testTableData["col1"].length; i++) {
1835
let testTableTdRow;
1936
if (thAttributes.classTags.includes("data-sort")) {
20-
testTableTdRow = `<tr><td data-sort="${i}">${testTableData[i]}</td></tr>`;
21-
} else if (invisibleIndex.includes(i)) {
22-
testTableTdRow = `<tr style="display: none;"><td>${testTableData[i]}</td></tr>`;
37+
testTableTdRow = `${getRowsOfTd(i, "data-sort")}`;
38+
} else {
39+
testTableTdRow = `${getRowsOfTd(i)}`;
40+
}
41+
if (invisibleIndex.includes(i)) {
42+
testTableTdRows.push(`<tr style="display: none;">${testTableTdRow}</tr>`);
2343
} else {
24-
testTableTdRow = `<tr><td>${testTableData[i]}</td></tr>`;
44+
testTableTdRows.push(`<tr> ${testTableTdRow}</tr>`);
2545
}
26-
testTableTdRows.push(testTableTdRow);
2746
}
2847

2948
const dom = new JSDOM(`<!DOCTYPE html>
@@ -33,7 +52,7 @@ function createTestTable(
3352
<body>
3453
<table class="table-sort">
3554
<thead>
36-
${getClassTagsForTH}
55+
${testTableHeaders}
3756
</thead>
3857
<tbody>
3958
${testTableTdRows}
@@ -42,18 +61,30 @@ function createTestTable(
4261
</body>
4362
</html>`);
4463

45-
// Call tablesort and make table sortable and simulate click from a user.
64+
// Call tablesort and make table sortable and simulate clicks from a user.
4665
tableSortJs((testing = true), dom.window.document);
47-
dom.window.document.querySelector("table th").click();
66+
67+
for (let i = 0; i < numberOfTableColumns; i++) {
68+
dom.window.document.querySelectorAll("table th")[i].click();
69+
}
4870

4971
// Make an array from table contents to test if sorted correctly.
5072
let table = dom.window.document.querySelector("table");
5173
const tableBody = table.querySelector("tbody");
5274
const tableRows = tableBody.querySelectorAll("tr");
53-
const testIfSortedList = [];
75+
const testIfSortedList = {};
76+
77+
for (let i = 0; i < numberOfTableColumns; i++) {
78+
testIfSortedList[`col${i + 1}`] = [];
79+
}
80+
5481
for (let [i, tr] of tableRows.entries()) {
5582
if (tr.style.display !== "none") {
56-
testIfSortedList.push(tr.querySelectorAll("td").item(0).innerHTML);
83+
for (let i = 0; i < numberOfTableColumns; i++)
84+
testIfSortedList[`col${i + 1}`].push(
85+
tr.querySelectorAll("td").item(i).innerHTML
86+
);
87+
// console.log(tr.querySelectorAll("td").item(i).innerHTML)
5788
}
5889
}
5990
return testIfSortedList;

0 commit comments

Comments
 (0)