Skip to content

Commit

Permalink
Change tests (#113)
Browse files Browse the repository at this point in the history
* Decouple thead from tbodies.

* Handle null error by using optional chaining and checking for ! undefined

* WIP fixed missing thead...

* change each col to have td property.

* Fix formatting of table.
  • Loading branch information
LeeWannacott committed May 30, 2023
1 parent 3b60a17 commit 477a36a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 85 deletions.
40 changes: 20 additions & 20 deletions test/order-by-desc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const createTestTable = require("./table");
test("Alpha - Capitalized: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["Echo", "Alpha", "Bravo", "Charlie", "Delta"] },
{ col0: {td: ["Echo", "Alpha", "Bravo", "Charlie", "Delta"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -14,7 +14,7 @@ test("Alpha - Capitalized: order-by-desc", () => {
test("Alpha - Lowercase: order-by-desc ", () => {
expect(
createTestTable(
{ col0: ["echo", "alpha", "bravo", "charlie", "delta"] },
{ col0: {td: ["echo", "alpha", "bravo", "charlie", "delta"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -24,14 +24,14 @@ test("Alpha - Lowercase: order-by-desc ", () => {

test("Numerical: order-by-desc", () => {
expect(
createTestTable({ col0: [5, 3, 4, 1, 2] }, { classTags: "order-by-desc" })
createTestTable({ col0: {td:[5, 3, 4, 1, 2] }}, { classTags: "order-by-desc" })
).toStrictEqual({ col0: ["5", "4", "3", "2", "1"] });
});

test("Alphanumeric: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"] },
{ col0:{td: ["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -44,7 +44,7 @@ test("Alphanumeric: order-by-desc", () => {
test("Dates: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["1979/9/6", "2008/4/9", "1879/12/16", "1978/4/6", "1978/4/16"] },
{ col0: {td: ["1979/9/6", "2008/4/9", "1879/12/16", "1978/4/6", "1978/4/16"] }},
{ classTags: "order-by-desc" }
)
).toStrictEqual({
Expand All @@ -55,7 +55,7 @@ test("Dates: order-by-desc", () => {
test("Money: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["$29", "$93", "$84", "$20", "$58"] },
{ col0: {td: ["$29", "$93", "$84", "$20", "$58"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -66,7 +66,7 @@ test("Money: order-by-desc", () => {
test("Empty cells sort at the end: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["Echo", "", "Bravo", "", "Alpha"] },
{ col0: {td: ["Echo", "", "Bravo", "", "Alpha"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -78,7 +78,7 @@ test("Order by file-size: file-size order-by-desc", () => {
expect(
createTestTable(
{
col0: [
col0: {td:[
"10MB",
"10GB",
"10TB",
Expand All @@ -89,7 +89,7 @@ test("Order by file-size: file-size order-by-desc", () => {
"10KB",
"10GiB",
],
},
}},
{ classTags: "order-by-desc file-size-sort" }
)
).toStrictEqual({
Expand All @@ -110,7 +110,7 @@ test("Order by file-size: file-size order-by-desc", () => {
test("Floating point numbers: order-by-desc", () => {
expect(
createTestTable(
{ col0: [6.23, 0.25, 3.15, 9.09, 0.35] },
{ col0: {td:[6.23, 0.25, 3.15, 9.09, 0.35] }},
{
classTags: "order-by-desc",
}
Expand All @@ -121,7 +121,7 @@ test("Floating point numbers: order-by-desc", () => {
test("Release Versions: order-by-desc", () => {
expect(
createTestTable(
{ col0: ["4.0.1", "3.0.2", "4.1.0", "3.0.4", "4.2.0"] },
{ col0: {td: ["4.0.1", "3.0.2", "4.1.0", "3.0.4", "4.2.0"] }},
{
classTags: "order-by-desc",
}
Expand All @@ -133,7 +133,7 @@ test("data-sort: example days of week - reversed", () => {
expect(
createTestTable(
{
col0: [
col0: {td:[
"Saturday",
"Wednesday",
"Sunday",
Expand All @@ -142,7 +142,7 @@ test("data-sort: example days of week - reversed", () => {
"Tuesday",
"Monday",
],
},
}},
{ classTags: "data-sort order-by-desc" }
)
).toStrictEqual({
Expand All @@ -162,14 +162,14 @@ test("visible-tr-sort: example sort only visible trs", () => {
expect(
createTestTable(
{
col0: [
col0: {td:[
"row1", // invisible
"row2",
"row3", // invisible
"row4",
"row5",
],
},
}},
{ classTags: "order-by-desc" },
{ invisibleIndex: [0, 2] }
)
Expand All @@ -179,7 +179,7 @@ test("visible-tr-sort: example sort only visible trs", () => {
test("onload-sort: testing that it sorts without a click from user", () => {
expect(
createTestTable(
{ col0: [5, 3, 4, 1, 2] },
{ col0: {td: [5, 3, 4, 1, 2] }},
{ classTags: "order-by-desc onload-sort" }
)
).toStrictEqual({ col0: ["5", "4", "3", "2", "1"] });
Expand All @@ -189,8 +189,8 @@ test("order-by-desc Remember-sort: Sorting cols without rememeber-sort class.",
expect(
createTestTable(
{
col0: ["charlie", "alpha", "beta"],
col1: ["carrie", "doris", "fisher"],
col0: {td: ["charlie", "alpha", "beta"]},
col1: {td: ["carrie", "doris", "fisher"]},
},
{
classTags: "order-by-desc",
Expand All @@ -210,8 +210,8 @@ test("order-by-desc Remember-sort: Sorting cols with rememeber-sort class.", ()
expect(
createTestTable(
{
col0: ["charlie", "alpha", "beta"],
col1: ["carrie", "doris", "fisher"],
col0: {td:["charlie", "alpha", "beta"]},
col1: {td:["carrie", "doris", "fisher"]},
},
{
classTags: "order-by-desc",
Expand Down
18 changes: 9 additions & 9 deletions test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ function createTestTable(
function getRowsOfTd(index, type) {
let rowsOfTd = "";
for (let key in testTableData) {
if (testTableData[key]) {
if (testTableData[key].td) {
if (type === "data-sort") {
rowsOfTd += `<td data-sort="${index}">${testTableData[key][index]}</td>`;
rowsOfTd += `<td data-sort="${index}">${testTableData[key].td[index]}</td>`;
} else {
rowsOfTd += `<td>${testTableData[key][index]}</td>`;
rowsOfTd += `<td>${testTableData[key].td[index]}</td>`;
}
}
}
return rowsOfTd;
}

let testTableTdRows = [];
for (let i = 0; i < testTableData["col0"].length; i++) {
for (let i = 0; i < testTableData["col0"].td.length; i++) {
let testTableTdRow;
if (thAttributes.classTags.includes("data-sort")) {
testTableTdRow = `${getRowsOfTd(i, "data-sort")}`;
Expand Down Expand Up @@ -63,12 +63,12 @@ function createTestTable(
<body>
<table class="table-sort ${props.tableTags}">
<thead>
${testTableHeaders}
${testTableHeaders}
</thead>
<tbody>
${testTableTdRows}
</tbody>
</table>
<tbody>
${testTableTdRows}
</tbody>
</table>
</body>
</html>`);

Expand Down
Loading

0 comments on commit 477a36a

Please sign in to comment.