Skip to content

Commit

Permalink
Fixed Remember-sort class and wrote some tests for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeWannacott committed May 19, 2023
1 parent 1627d97 commit 8250a99
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 17 deletions.
25 changes: 9 additions & 16 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,26 +262,20 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}
}


function rememberSort(timesClickedColumn,columnIndex, columnIndexesClicked) {
function rememberSort() {
// if user clicked different column from first column reset times clicked.
console.log(columnIndex);
console.log(timesClickedColumn);
columnIndexesClicked.push(columnIndex);
console.log(columnIndexesClicked);
if (timesClickedColumn === 1 && columnIndexesClicked.length > 1) {
const lastColumnClicked =
columnIndexesClicked[columnIndexesClicked.length - 1];
const secondLastColumnClicked =
columnIndexesClicked[columnIndexesClicked.length - 2];
console.log("1");
if (lastColumnClicked !== secondLastColumnClicked) {
console.log("2", timesClickedColumn, "time");
columnIndexesClicked.shift();
console.log("3", timesClickedColumn);
return timesClickedColumn = 0;
timesClickedColumn = 0;
}
}
return timesClickedColumn;
}

function getColSpanData(sortableTable, column) {
Expand Down Expand Up @@ -451,11 +445,10 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
let timesClickedColumn = 0;
th.addEventListener("click", function () {
const isRememberSort = sortableTable.classList.contains("remember-sort");
// if (!isRememberSort) {
console.log("times",timesClickedColumn)
rememberSort(timesClickedColumn,columnIndex, columnIndexesClicked);
// }
console.log(timesClickedColumn,"after")
if (!isRememberSort) {
timesClickedColumn = rememberSort();
}

timesClickedColumn += 1;
const column = {
// column used for sorting; better name?
Expand All @@ -464,15 +457,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
spanSum: {},
};

getColSpanData(sortableTable, column);

const visibleTableRows = Array.prototype.filter.call(
tableBody.querySelectorAll("tr"),
(tr) => {
return tr.style.display !== "none";
}
);

getColSpanData(sortableTable, column);

const isDataAttribute = th.classList.contains("data-sort");
if (isDataAttribute) {
sortDataAttributes(visibleTableRows, column);
Expand Down
42 changes: 42 additions & 0 deletions test/order-by-desc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,45 @@ test("onload-sort: testing that it sorts without a click from user", () => {
)
).toStrictEqual({ col0: ["5", "4", "3", "2", "1"] });
});

test("order-by-desc Remember-sort: Sorting cols without rememeber-sort class.", () => {
expect(
createTestTable(
{
col0: ["charlie", "alpha", "beta"],
col1: ["carrie", "doris", "fisher"],
},
{
classTags: "order-by-desc",
},
{
colsToClick: [0, 1, 0],
tableTags: "",
}
)
).toStrictEqual({
col0: ["charlie", "beta", "alpha"],
col1: ["carrie", "fisher", "doris"],
});
});

test("order-by-desc Remember-sort: Sorting cols with rememeber-sort class.", () => {
expect(
createTestTable(
{
col0: ["charlie", "alpha", "beta"],
col1: ["carrie", "doris", "fisher"],
},
{
classTags: "order-by-desc",
},
{
colsToClick: [0, 1, 0],
tableTags: "remember-sort",
}
)
).toStrictEqual({
col0: ["alpha", "beta", "charlie"],
col1: ["doris", "fisher", "carrie"],
});
});
2 changes: 1 addition & 1 deletion test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function createTestTable(
<head>
</head>
<body>
<table class="table-sort">
<table class="table-sort ${props.tableTags}">
<thead>
${testTableHeaders}
</thead>
Expand Down
63 changes: 63 additions & 0 deletions test/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,69 @@ test("Clicking multiple times (>2) doesn't break sorting", () => {
});
});

test("Sorting columns without rememeber-sort ", () => {
expect(
createTestTable(
{
col0: ["charlie", "alpha", "beta"],
col1: ["doris", "carrie", "fisher"],
},
{
classTags: "",
},
{
colsToClick: [0, 1, 0],
tableTags: "",
}
)
).toStrictEqual({
col0: ["alpha", "beta", "charlie"],
col1: ["carrie", "fisher", "doris"],
});
});

test("Remember-sort: Sorting cols that have the rememeber-sort class.", () => {
expect(
createTestTable(
{
col0: ["charlie", "alpha", "beta"],
col1: ["doris", "carrie", "fisher"],
},
{
classTags: "",
},
{
colsToClick: [0, 1, 0],
tableTags: "remember-sort",
}
)
).toStrictEqual({
col0: ["charlie", "beta", "alpha"],
col1: ["doris", "fisher", "carrie"],
});
});

test("Checking ", () => {
expect(
createTestTable(
{
col0: ["charlie", "alpha", "beta"],
col1: ["doris", "carrie", "fisher"],
},
{
classTags: "",
},
{
colsToClick: [0, 1, 0],
tableTags: "",
}
)
).toStrictEqual({
col0: ["alpha", "beta", "charlie"],
col1: ["carrie", "fisher", "doris"],
});
});

test("runtime-sort", () => {
expect(
createTestTable(
Expand Down

0 comments on commit 8250a99

Please sign in to comment.