Skip to content

Commit 3bd1052

Browse files
Setup testing for which columns are being clicked.
1 parent bbeba21 commit 3bd1052

File tree

3 files changed

+103
-84
lines changed

3 files changed

+103
-84
lines changed

test/order-by-desc.test.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,82 @@ const createTestTable = require("./table");
33
test("Alpha - Capitalized: order-by-desc", () => {
44
expect(
55
createTestTable(
6-
{ col1: ["Echo", "Alpha", "Bravo", "Charlie", "Delta"] },
6+
{ col0: ["Echo", "Alpha", "Bravo", "Charlie", "Delta"] },
77
{
88
classTags: "order-by-desc",
99
}
1010
)
11-
).toStrictEqual({ col1: ["Echo", "Delta", "Charlie", "Bravo", "Alpha"] });
11+
).toStrictEqual({ col0: ["Echo", "Delta", "Charlie", "Bravo", "Alpha"] });
1212
});
1313

1414
test("Alpha - Lowercase: order-by-desc ", () => {
1515
expect(
1616
createTestTable(
17-
{ col1: ["echo", "alpha", "bravo", "charlie", "delta"] },
17+
{ col0: ["echo", "alpha", "bravo", "charlie", "delta"] },
1818
{
1919
classTags: "order-by-desc",
2020
}
2121
)
22-
).toStrictEqual({ col1: ["echo", "delta", "charlie", "bravo", "alpha"] });
22+
).toStrictEqual({ col0: ["echo", "delta", "charlie", "bravo", "alpha"] });
2323
});
2424

2525
test("Numerical: order-by-desc", () => {
2626
expect(
27-
createTestTable({ col1: [5, 3, 4, 1, 2] }, { classTags: "order-by-desc" })
28-
).toStrictEqual({ col1: ["5", "4", "3", "2", "1"] });
27+
createTestTable({ col0: [5, 3, 4, 1, 2] }, { classTags: "order-by-desc" })
28+
).toStrictEqual({ col0: ["5", "4", "3", "2", "1"] });
2929
});
3030

3131
test("Alphanumeric: order-by-desc", () => {
3232
expect(
3333
createTestTable(
34-
{ col1: ["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"] },
34+
{ col0: ["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"] },
3535
{
3636
classTags: "order-by-desc",
3737
}
3838
)
3939
).toStrictEqual({
40-
col1: ["Echo5", "Delta4", "Charlie3", "Bravo2", "Alpha1"],
40+
col0: ["Echo5", "Delta4", "Charlie3", "Bravo2", "Alpha1"],
4141
});
4242
});
4343

4444
test("Dates: order-by-desc", () => {
4545
expect(
4646
createTestTable(
47-
{ col1: ["1979/9/6", "2008/4/9", "1879/12/16", "1978/4/6", "1978/4/16"] },
47+
{ col0: ["1979/9/6", "2008/4/9", "1879/12/16", "1978/4/6", "1978/4/16"] },
4848
{ classTags: "order-by-desc" }
4949
)
5050
).toStrictEqual({
51-
col1: ["2008/4/9", "1979/9/6", "1978/4/16", "1978/4/6", "1879/12/16"],
51+
col0: ["2008/4/9", "1979/9/6", "1978/4/16", "1978/4/6", "1879/12/16"],
5252
});
5353
});
5454

5555
test("Money: order-by-desc", () => {
5656
expect(
5757
createTestTable(
58-
{ col1: ["$29", "$93", "$84", "$20", "$58"] },
58+
{ col0: ["$29", "$93", "$84", "$20", "$58"] },
5959
{
6060
classTags: "order-by-desc",
6161
}
6262
)
63-
).toStrictEqual({ col1: ["$93", "$84", "$58", "$29", "$20"] });
63+
).toStrictEqual({ col0: ["$93", "$84", "$58", "$29", "$20"] });
6464
});
6565

6666
test("Empty cells sort at the end: order-by-desc", () => {
6767
expect(
6868
createTestTable(
69-
{ col1: ["Echo", "", "Bravo", "", "Alpha"] },
69+
{ col0: ["Echo", "", "Bravo", "", "Alpha"] },
7070
{
7171
classTags: "order-by-desc",
7272
}
7373
)
74-
).toStrictEqual({ col1: ["", "", "Echo", "Bravo", "Alpha"] });
74+
).toStrictEqual({ col0: ["", "", "Echo", "Bravo", "Alpha"] });
7575
});
7676

7777
test("Order by file-size: file-size order-by-desc", () => {
7878
expect(
7979
createTestTable(
8080
{
81-
col1: [
81+
col0: [
8282
"10MB",
8383
"10GB",
8484
"10TB",
@@ -93,7 +93,7 @@ test("Order by file-size: file-size order-by-desc", () => {
9393
{ classTags: "order-by-desc file-size-sort" }
9494
)
9595
).toStrictEqual({
96-
col1: [
96+
col0: [
9797
"10.00 TiB",
9898
"9.09 TiB",
9999
"10.00 GiB",
@@ -110,30 +110,30 @@ test("Order by file-size: file-size order-by-desc", () => {
110110
test("Floating point numbers: order-by-desc", () => {
111111
expect(
112112
createTestTable(
113-
{ col1: [6.23, 0.25, 3.15, 9.09, 0.35] },
113+
{ col0: [6.23, 0.25, 3.15, 9.09, 0.35] },
114114
{
115115
classTags: "order-by-desc",
116116
}
117117
)
118-
).toStrictEqual({ col1: ["9.09", "6.23", "3.15", "0.35", "0.25"] });
118+
).toStrictEqual({ col0: ["9.09", "6.23", "3.15", "0.35", "0.25"] });
119119
});
120120

121121
test("Release Versions: order-by-desc", () => {
122122
expect(
123123
createTestTable(
124-
{ col1: ["4.0.1", "3.0.2", "4.1.0", "3.0.4", "4.2.0"] },
124+
{ col0: ["4.0.1", "3.0.2", "4.1.0", "3.0.4", "4.2.0"] },
125125
{
126126
classTags: "order-by-desc",
127127
}
128128
)
129-
).toStrictEqual({ col1: ["4.2.0", "4.1.0", "4.0.1", "3.0.4", "3.0.2"] });
129+
).toStrictEqual({ col0: ["4.2.0", "4.1.0", "4.0.1", "3.0.4", "3.0.2"] });
130130
});
131131

132132
test("data-sort: example days of week - reversed", () => {
133133
expect(
134134
createTestTable(
135135
{
136-
col1: [
136+
col0: [
137137
"Saturday",
138138
"Wednesday",
139139
"Sunday",
@@ -146,7 +146,7 @@ test("data-sort: example days of week - reversed", () => {
146146
{ classTags: "data-sort order-by-desc" }
147147
)
148148
).toStrictEqual({
149-
col1: [
149+
col0: [
150150
"Monday",
151151
"Tuesday",
152152
"Thursday",
@@ -162,7 +162,7 @@ test("visible-tr-sort: example sort only visible trs", () => {
162162
expect(
163163
createTestTable(
164164
{
165-
col1: [
165+
col0: [
166166
"row1", // invisible
167167
"row2",
168168
"row3", // invisible
@@ -171,7 +171,7 @@ test("visible-tr-sort: example sort only visible trs", () => {
171171
],
172172
},
173173
{ classTags: "order-by-desc" },
174-
[0, 2]
174+
{ invisibleIndex: [0, 2] }
175175
)
176-
).toStrictEqual({ col1: ["row5", "row4", "row2"] });
176+
).toStrictEqual({ col0: ["row5", "row4", "row2"] });
177177
});

test/table.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const tableSortJs = require("../public/table-sort");
66

77
function createTestTable(
88
testTableData,
9-
thAttributes = { classTags: "" },
10-
invisibleIndex = []
9+
thAttributes = { classTags: "", colspan: "" },
10+
props = { colsToClick: [], invisibleIndex: [] }
1111
) {
1212
const numberOfTableColumns = Object.keys(testTableData).length;
1313
let testTableHeaders = "";
1414
for (let i = 0; i < numberOfTableColumns; i++) {
15-
testTableHeaders += `<th class="${thAttributes.classTags}">Testing Column</th>`;
15+
testTableHeaders += `<th colspan="${thAttributes.colspan}" class="${thAttributes.classTags}">Testing Column</th>`;
1616
}
1717
testTableHeaders = `<tr> ${testTableHeaders} </tr>`;
1818

@@ -31,14 +31,17 @@ function createTestTable(
3131
}
3232

3333
let testTableTdRows = [];
34-
for (let i = 0; i < testTableData["col1"].length; i++) {
34+
for (let i = 0; i < testTableData["col0"].length; i++) {
3535
let testTableTdRow;
3636
if (thAttributes.classTags.includes("data-sort")) {
3737
testTableTdRow = `${getRowsOfTd(i, "data-sort")}`;
3838
} else {
3939
testTableTdRow = `${getRowsOfTd(i)}`;
4040
}
41-
if (invisibleIndex.includes(i)) {
41+
if (
42+
props.invisibleIndex !== undefined &&
43+
props.invisibleIndex.includes(i)
44+
) {
4245
testTableTdRows.push(`<tr style="display: none;">${testTableTdRow}</tr>`);
4346
} else {
4447
testTableTdRows.push(`<tr> ${testTableTdRow}</tr>`);
@@ -62,10 +65,21 @@ function createTestTable(
6265
</html>`);
6366

6467
// Call tablesort and make table sortable and simulate clicks from a user.
65-
tableSortJs((testing = true), dom.window.document);
68+
tableSortJs(true, dom.window.document);
6669

67-
for (let i = 0; i < numberOfTableColumns; i++) {
68-
dom.window.document.querySelectorAll("table th")[i].click();
70+
if (
71+
typeof props.colsToClick !== "undefined" &&
72+
props.colsToClick.length > 0
73+
) {
74+
for (let i = 0; i < props.colsToClick.length; i++) {
75+
dom.window.document
76+
.querySelectorAll("table th")
77+
[props.colsToClick[i]].click();
78+
}
79+
} else {
80+
for (let i = 0; i < numberOfTableColumns; i++) {
81+
dom.window.document.querySelectorAll("table th")[i].click();
82+
}
6983
}
7084

7185
// Make an array from table contents to test if sorted correctly.
@@ -75,16 +89,15 @@ function createTestTable(
7589
const testIfSortedList = {};
7690

7791
for (let i = 0; i < numberOfTableColumns; i++) {
78-
testIfSortedList[`col${i + 1}`] = [];
92+
testIfSortedList[`col${i}`] = [];
7993
}
8094

8195
for (let [i, tr] of tableRows.entries()) {
8296
if (tr.style.display !== "none") {
8397
for (let i = 0; i < numberOfTableColumns; i++)
84-
testIfSortedList[`col${i + 1}`].push(
98+
testIfSortedList[`col${i}`].push(
8599
tr.querySelectorAll("td").item(i).innerHTML
86100
);
87-
// console.log(tr.querySelectorAll("td").item(i).innerHTML)
88101
}
89102
}
90103
return testIfSortedList;

0 commit comments

Comments
 (0)