Skip to content

Commit

Permalink
Test: added test for using cells-sort table class and the default whi…
Browse files Browse the repository at this point in the history
…ch sort table rows.
  • Loading branch information
LeeWannacott committed May 22, 2023
1 parent c56953d commit 7e44cfe
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
22 changes: 17 additions & 5 deletions test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const tableSortJs = require("../public/table-sort");
function createTestTable(
testTableData,
thAttributes = { classTags: "", colspan: "" },
props = { colsToClick: [], invisibleIndex: [], tableTags: "" }
props = { colsToClick: [], invisibleIndex: [], tableTags: "", trClasses: "" }
) {
const numberOfTableColumns = Object.keys(testTableData).length;
let testTableHeaders = "";
Expand Down Expand Up @@ -44,7 +44,15 @@ function createTestTable(
) {
testTableTdRows.push(`<tr style="display: none;">${testTableTdRow}</tr>`);
} else {
testTableTdRows.push(`<tr> ${testTableTdRow}</tr>`);
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>`
);
}
}
}

Expand Down Expand Up @@ -97,9 +105,13 @@ function createTestTable(
for (let [i, tr] of tableRows.entries()) {
if (tr.style.display !== "none") {
for (let i = 0; i < numberOfTableColumns; i++)
testIfSortedList[`col${i}`].push(
tr.querySelectorAll("td").item(i).innerHTML
);
if (props.tableTags === "cells-sort" || props.tableTags ==="tr-sort") {
testIfSortedList[`col${i}`].push(tr.outerHTML);
} else {
testIfSortedList[`col${i}`].push(
tr.querySelectorAll("td").item(i).innerHTML
);
}
}
}
return testIfSortedList;
Expand Down
49 changes: 49 additions & 0 deletions test/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,52 @@ test("Sort all combination of negative and positive integers and decimal numbers
],
});
});

test("default behavior without cells-sort (tr's move when sorted)", () => {
expect(
createTestTable(
{
col0: ["8", "2", "3", "4", "5", "6", "0", "1", "7"],
},
{ classTags: "" },
// note this isn't an actual class just used for testing purposes
{ tableTags: "tr-sort", trClasses: "test" }
)
).toStrictEqual({
col0: [
'<tr class="test-6"> <td>0</td></tr>',
'<tr class="test-7"> <td>1</td></tr>',
'<tr class="test-1"> <td>2</td></tr>',
'<tr class="test-2"> <td>3</td></tr>',
'<tr class="test-3"> <td>4</td></tr>',
'<tr class="test-4"> <td>5</td></tr>',
'<tr class="test-5"> <td>6</td></tr>',
'<tr class="test-8"> <td>7</td></tr>',
'<tr class="test-0"> <td>8</td></tr>',
],
});
});

test("cells-sort table class (tr's stay in place, but td's are sorted)", () => {
expect(
createTestTable(
{
col0: ["8", "2", "3", "4", "5", "6", "0", "1", "7"],
},
{ classTags: "" },
{ tableTags: "cells-sort", trClasses: "test" }
)
).toStrictEqual({
col0: [
'<tr class="test-0"> <td>0</td></tr>',
'<tr class="test-1"> <td>1</td></tr>',
'<tr class="test-2"> <td>2</td></tr>',
'<tr class="test-3"> <td>3</td></tr>',
'<tr class="test-4"> <td>4</td></tr>',
'<tr class="test-5"> <td>5</td></tr>',
'<tr class="test-6"> <td>6</td></tr>',
'<tr class="test-7"> <td>7</td></tr>',
'<tr class="test-8"> <td>8</td></tr>',
],
});
});

0 comments on commit 7e44cfe

Please sign in to comment.