From dda333c6a3724c65cc4d66f2f780c1c595af7442 Mon Sep 17 00:00:00 2001 From: LeeWannacott Date: Sun, 7 May 2023 22:49:48 +1200 Subject: [PATCH] Feature: added the class onload-sort for table headers. --- public/index.html | 6 +++--- public/table-sort.js | 4 ++++ test/order-by-desc.test.js | 7 +++++++ test/table.js | 4 +++- test/table.test.js | 11 +++++++++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/public/index.html b/public/index.html index 7503393..1ebc88e 100644 --- a/public/index.html +++ b/public/index.html @@ -10,11 +10,11 @@

Testing table sort js

- + - + @@ -22,7 +22,7 @@

Testing table sort js

- + diff --git a/public/table-sort.js b/public/table-sort.js index 4939ec3..bcaa6e4 100644 --- a/public/table-sort.js +++ b/public/table-sort.js @@ -326,6 +326,10 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { getTableData(tableProperties); updateTable(tableProperties); }); + let isOnloadSort = th.classList.contains("onload-sort"); + if (isOnloadSort) { + th.click(); + } } } diff --git a/test/order-by-desc.test.js b/test/order-by-desc.test.js index 226d819..67d8664 100644 --- a/test/order-by-desc.test.js +++ b/test/order-by-desc.test.js @@ -175,3 +175,10 @@ test("visible-tr-sort: example sort only visible trs", () => { ) ).toStrictEqual({ col0: ["row5", "row4", "row2"] }); }); + + +test("onload-sort: testing that it sorts without a click from user", () => { + expect( + createTestTable({ col0: [5, 3, 4, 1, 2] }, { classTags: "order-by-desc onload-sort" }) + ).toStrictEqual({ col0: ["5", "4", "3", "2", "1"] }); +}); diff --git a/test/table.js b/test/table.js index d59e0dc..104bf51 100644 --- a/test/table.js +++ b/test/table.js @@ -78,7 +78,9 @@ function createTestTable( } } else { for (let i = 0; i < numberOfTableColumns; i++) { - dom.window.document.querySelectorAll("table th")[i].click(); + if (!thAttributes.classTags.includes("onload-sort")) { + dom.window.document.querySelectorAll("table th")[i].click(); + } } } diff --git a/test/table.test.js b/test/table.test.js index 4fd519a..c18407a 100644 --- a/test/table.test.js +++ b/test/table.test.js @@ -219,3 +219,14 @@ test("testing that multiple columns works", () => { col1: ["carrie", "fisher", "doris"], }); }); + +test("onload-sort: testing that it sorts without a click from user", () => { + expect( + createTestTable( + { col0: ["echo", "alpha", "bravo", "charlie", "delta"] }, + { + classTags: "onload-sort", + } + ) + ).toStrictEqual({ col0: ["alpha", "bravo", "charlie", "delta", "echo"] }); +});
Last NameLast Name First Name Birth Date Employee IDDepartmentDepartment
benjamin 1706,1,17 C-levelk-level
da Vinci