Skip to content

Commit fa1ef26

Browse files
Change createTestTable() and associated tests to take in thAttributes object with classTags property on it.
1 parent c8d4273 commit fa1ef26

File tree

2 files changed

+70
-79
lines changed

2 files changed

+70
-79
lines changed

test/table.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
const jsdom = require("jsdom");
2+
23
const { JSDOM } = jsdom;
34
require("iconv-lite").encodingExists("foo");
45
const tableSortJs = require("../public/table-sort");
56

6-
function createTestTable(testTableData, classTags = "", invisibleIndex = []) {
7+
function createTestTable(
8+
testTableData,
9+
thAttributes = { classTags: "" },
10+
invisibleIndex = []
11+
) {
712
let getClassTagsForTH = [];
8-
let testTableThRow = `<tr><th class="${classTags}">Testing Column</th></tr>`;
13+
let testTableThRow = `<tr><th class="${thAttributes.classTags}">Testing Column</th></tr>`;
914
getClassTagsForTH.push(testTableThRow);
1015

1116
let testTableTdRows = [];
1217
for (let i = 0; i < testTableData.length; i++) {
1318
let testTableTdRow;
14-
if (classTags.includes("data-sort")) {
19+
if (thAttributes.classTags.includes("data-sort")) {
1520
testTableTdRow = `<tr><td data-sort="${i}">${testTableData[i]}</td></tr>`;
1621
} else if (invisibleIndex.includes(i)) {
1722
testTableTdRow = `<tr style="display: none;"><td>${testTableData[i]}</td></tr>`;

test/table.test.js

Lines changed: 62 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,38 @@ const createTestTable = require("./table");
44

55
test("Alpha - Capitalized ", () => {
66
expect(
7-
createTestTable(["Echo", "Alpha", "Bravo", "Charlie", "Delta"])
7+
createTestTable(["Echo", "Alpha", "Bravo", "Charlie", "Delta"], {
8+
classTags: "",
9+
})
810
).toStrictEqual(["Alpha", "Bravo", "Charlie", "Delta", "Echo"]);
911
});
1012

1113
test("Alpha - Capitalized: order-by-desc", () => {
1214
expect(
13-
createTestTable(
14-
["Echo", "Alpha", "Bravo", "Charlie", "Delta"],
15-
"order-by-desc"
16-
)
15+
createTestTable(["Echo", "Alpha", "Bravo", "Charlie", "Delta"], {
16+
classTags: "order-by-desc",
17+
})
1718
).toStrictEqual(["Echo", "Delta", "Charlie", "Bravo", "Alpha"]);
1819
});
1920

2021
test("Alpha - Lowercase", () => {
2122
expect(
22-
createTestTable(["echo", "alpha", "bravo", "charlie", "delta"])
23+
createTestTable(["echo", "alpha", "bravo", "charlie", "delta"], {
24+
classTags: "",
25+
})
2326
).toStrictEqual(["alpha", "bravo", "charlie", "delta", "echo"]);
2427
});
2528

2629
test("Alpha - Lowercase: order-by-desc ", () => {
2730
expect(
28-
createTestTable(
29-
["echo", "alpha", "bravo", "charlie", "delta"],
30-
"order-by-desc"
31-
)
31+
createTestTable(["echo", "alpha", "bravo", "charlie", "delta"], {
32+
classTags: "order-by-desc",
33+
})
3234
).toStrictEqual(["echo", "delta", "charlie", "bravo", "alpha"]);
3335
});
3436

3537
test("Numerical", () => {
36-
expect(createTestTable([5, 3, 4, 1, 2])).toStrictEqual([
38+
expect(createTestTable([5, 3, 4, 1, 2], { classTags: "" })).toStrictEqual([
3739
"1",
3840
"2",
3941
"3",
@@ -43,39 +45,33 @@ test("Numerical", () => {
4345
});
4446

4547
test("Numerical: order-by-desc", () => {
46-
expect(createTestTable([5, 3, 4, 1, 2], "order-by-desc")).toStrictEqual([
47-
"5",
48-
"4",
49-
"3",
50-
"2",
51-
"1",
52-
]);
48+
expect(
49+
createTestTable([5, 3, 4, 1, 2], { classTags: "order-by-desc" })
50+
).toStrictEqual(["5", "4", "3", "2", "1"]);
5351
});
5452

5553
test("Alphanumeric", () => {
5654
expect(
57-
createTestTable(["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"])
55+
createTestTable(["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"], {
56+
classTags: "",
57+
})
5858
).toStrictEqual(["Alpha1", "Bravo2", "Charlie3", "Delta4", "Echo5"]);
5959
});
6060

6161
test("Alphanumeric: order-by-desc", () => {
6262
expect(
63-
createTestTable(
64-
["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"],
65-
"order-by-desc"
66-
)
63+
createTestTable(["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"], {
64+
classTags: "order-by-desc",
65+
})
6766
).toStrictEqual(["Echo5", "Delta4", "Charlie3", "Bravo2", "Alpha1"]);
6867
});
6968

7069
test("Dates", () => {
7170
expect(
72-
createTestTable([
73-
"1979/9/6",
74-
"2008/4/9",
75-
"1879/12/16",
76-
"1978/4/6",
77-
"1978/4/16",
78-
])
71+
createTestTable(
72+
["1979/9/6", "2008/4/9", "1879/12/16", "1978/4/6", "1978/4/16"],
73+
{ classTags: "" }
74+
)
7975
).toStrictEqual([
8076
"1879/12/16",
8177
"1978/4/6",
@@ -89,7 +85,7 @@ test("Dates: order-by-desc", () => {
8985
expect(
9086
createTestTable(
9187
["1979/9/6", "2008/4/9", "1879/12/16", "1978/4/6", "1978/4/16"],
92-
"order-by-desc"
88+
{ classTags: "order-by-desc" }
9389
)
9490
).toStrictEqual([
9591
"2008/4/9",
@@ -101,34 +97,30 @@ test("Dates: order-by-desc", () => {
10197
});
10298

10399
test("Money", () => {
104-
expect(createTestTable(["$29", "$93", "$84", "$20", "$58"])).toStrictEqual([
105-
"$20",
106-
"$29",
107-
"$58",
108-
"$84",
109-
"$93",
110-
]);
100+
expect(
101+
createTestTable(["$29", "$93", "$84", "$20", "$58"], { classTags: "" })
102+
).toStrictEqual(["$20", "$29", "$58", "$84", "$93"]);
111103
});
112104

113105
test("Money: order-by-desc", () => {
114106
expect(
115-
createTestTable(["$29", "$93", "$84", "$20", "$58"], "order-by-desc")
107+
createTestTable(["$29", "$93", "$84", "$20", "$58"], {
108+
classTags: "order-by-desc",
109+
})
116110
).toStrictEqual(["$93", "$84", "$58", "$29", "$20"]);
117111
});
118112

119113
test("Empty cells sort at the end.", () => {
120-
expect(createTestTable(["Echo", "", "Bravo", "", "Alpha"])).toStrictEqual([
121-
"Alpha",
122-
"Bravo",
123-
"Echo",
124-
"",
125-
"",
126-
]);
114+
expect(
115+
createTestTable(["Echo", "", "Bravo", "", "Alpha"], { classTags: "" })
116+
).toStrictEqual(["Alpha", "Bravo", "Echo", "", ""]);
127117
});
128118

129119
test("Empty cells sort at the end: order-by-desc", () => {
130120
expect(
131-
createTestTable(["Echo", "", "Bravo", "", "Alpha"], "order-by-desc")
121+
createTestTable(["Echo", "", "Bravo", "", "Alpha"], {
122+
classTags: "order-by-desc",
123+
})
132124
).toStrictEqual(["", "", "Echo", "Bravo", "Alpha"]);
133125
});
134126

@@ -147,7 +139,7 @@ test("Order by file-size: file-size", () => {
147139
"10KB",
148140
"10GiB",
149141
],
150-
["file-size"]
142+
{ classTags: "file-size" }
151143
)
152144
).toStrictEqual([
153145
"10.00 B",
@@ -176,7 +168,7 @@ test("Order by file-size: file-size order-by-desc", () => {
176168
"10KB",
177169
"10GiB",
178170
],
179-
"file-size order-by-desc"
171+
{ classTags: "order-by-desc file-size" }
180172
)
181173
).toStrictEqual([
182174
"10.00 TiB",
@@ -195,38 +187,39 @@ test("Order by file-size: file-size order-by-desc", () => {
195187

196188
test("Alpha - lower & upper", () => {
197189
expect(
198-
createTestTable(["AlPhA", "bRaVo", "EcHo", "ChArLiE", "dElTa"])
190+
createTestTable(["AlPhA", "bRaVo", "EcHo", "ChArLiE", "dElTa"], {
191+
classTags: "",
192+
})
199193
).toStrictEqual(["AlPhA", "bRaVo", "ChArLiE", "dElTa", "EcHo"]);
200194
});
201195

202196
test("Floating point numbers", () => {
203-
expect(createTestTable([6.23, 0.25, 3.15, 9.09, 0.35])).toStrictEqual([
204-
"0.25",
205-
"0.35",
206-
"3.15",
207-
"6.23",
208-
"9.09",
209-
]);
197+
expect(
198+
createTestTable([6.23, 0.25, 3.15, 9.09, 0.35], { classTags: "" })
199+
).toStrictEqual(["0.25", "0.35", "3.15", "6.23", "9.09"]);
210200
});
211201

212202
test("Floating point numbers: order-by-desc", () => {
213203
expect(
214-
createTestTable([6.23, 0.25, 3.15, 9.09, 0.35], "order-by-desc")
204+
createTestTable([6.23, 0.25, 3.15, 9.09, 0.35], {
205+
classTags: "order-by-desc",
206+
})
215207
).toStrictEqual(["9.09", "6.23", "3.15", "0.35", "0.25"]);
216208
});
217209

218210
test("Release Versions", () => {
219211
expect(
220-
createTestTable(["4.0.1", "3.0.2", "4.1.0", "3.0.4", "4.2.0"])
212+
createTestTable(["4.0.1", "3.0.2", "4.1.0", "3.0.4", "4.2.0"], {
213+
classTags: "",
214+
})
221215
).toStrictEqual(["3.0.2", "3.0.4", "4.0.1", "4.1.0", "4.2.0"]);
222216
});
223217

224218
test("Release Versions: order-by-desc", () => {
225219
expect(
226-
createTestTable(
227-
["4.0.1", "3.0.2", "4.1.0", "3.0.4", "4.2.0"],
228-
"order-by-desc"
229-
)
220+
createTestTable(["4.0.1", "3.0.2", "4.1.0", "3.0.4", "4.2.0"], {
221+
classTags: "order-by-desc",
222+
})
230223
).toStrictEqual(["4.2.0", "4.1.0", "4.0.1", "3.0.4", "3.0.2"]);
231224
});
232225

@@ -242,7 +235,7 @@ test("data-sort: example days of week", () => {
242235
"Tuesday",
243236
"Monday",
244237
],
245-
"data-sort"
238+
{ classTags: "data-sort" }
246239
)
247240
).toStrictEqual([
248241
"Saturday",
@@ -267,7 +260,7 @@ test("data-sort: example days of week - reversed", () => {
267260
"Tuesday",
268261
"Monday",
269262
],
270-
"data-sort order-by-desc"
263+
{ classTags: "data-sort order-by-desc" }
271264
)
272265
).toStrictEqual([
273266
"Monday",
@@ -290,23 +283,16 @@ test("visible-tr-sort: example sort only visible trs", () => {
290283
"row4",
291284
"row5",
292285
],
293-
"order-by-desc",
286+
{ classTags: "order-by-desc" },
294287
[0, 2]
295288
)
296289
).toStrictEqual(["row5", "row4", "row2"]);
297290
});
298291

299292
test("disable-sort: disable sorting on a column", () => {
300293
expect(
301-
createTestTable(
302-
[
303-
"row2",
304-
"row1",
305-
"row4",
306-
"row3",
307-
],
308-
"disable-sort"
309-
)
294+
createTestTable(["row2", "row1", "row4", "row3"], {
295+
classTags: "disable-sort",
296+
})
310297
).toStrictEqual(["row2", "row1", "row4", "row3"]);
311298
});
312-

0 commit comments

Comments
 (0)