From 477a36ab25bced32ac23b1e57ed032b5e1a3bee4 Mon Sep 17 00:00:00 2001 From: Lee Wannacott Date: Tue, 30 May 2023 17:40:21 +1200 Subject: [PATCH] Change tests (#113) * 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. --- test/order-by-desc.test.js | 40 ++++++------ test/table.js | 18 +++--- test/table.test.js | 128 +++++++++++++++++++++---------------- 3 files changed, 101 insertions(+), 85 deletions(-) diff --git a/test/order-by-desc.test.js b/test/order-by-desc.test.js index 3ca78ee..4181949 100644 --- a/test/order-by-desc.test.js +++ b/test/order-by-desc.test.js @@ -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", } @@ -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", } @@ -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", } @@ -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({ @@ -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", } @@ -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", } @@ -78,7 +78,7 @@ test("Order by file-size: file-size order-by-desc", () => { expect( createTestTable( { - col0: [ + col0: {td:[ "10MB", "10GB", "10TB", @@ -89,7 +89,7 @@ test("Order by file-size: file-size order-by-desc", () => { "10KB", "10GiB", ], - }, + }}, { classTags: "order-by-desc file-size-sort" } ) ).toStrictEqual({ @@ -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", } @@ -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", } @@ -133,7 +133,7 @@ test("data-sort: example days of week - reversed", () => { expect( createTestTable( { - col0: [ + col0: {td:[ "Saturday", "Wednesday", "Sunday", @@ -142,7 +142,7 @@ test("data-sort: example days of week - reversed", () => { "Tuesday", "Monday", ], - }, + }}, { classTags: "data-sort order-by-desc" } ) ).toStrictEqual({ @@ -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] } ) @@ -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"] }); @@ -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", @@ -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", diff --git a/test/table.js b/test/table.js index 0ce6eb3..8f38140 100644 --- a/test/table.js +++ b/test/table.js @@ -19,11 +19,11 @@ 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 += `${testTableData[key][index]}`; + rowsOfTd += `${testTableData[key].td[index]}`; } else { - rowsOfTd += `${testTableData[key][index]}`; + rowsOfTd += `${testTableData[key].td[index]}`; } } } @@ -31,7 +31,7 @@ function createTestTable( } 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")}`; @@ -63,12 +63,12 @@ function createTestTable( - ${testTableHeaders} + ${testTableHeaders} - - ${testTableTdRows} - -
+ + ${testTableTdRows} + + `); diff --git a/test/table.test.js b/test/table.test.js index ac2de01..5c3fae3 100644 --- a/test/table.test.js +++ b/test/table.test.js @@ -5,7 +5,7 @@ const createTestTable = require("./table"); test("Alpha - Capitalized ", () => { expect( createTestTable( - { col0: ["Echo", "Alpha", "Bravo", "Charlie", "Delta"] }, + { col0: {td: ["Echo", "Alpha", "Bravo", "Charlie", "Delta"]} }, { classTags: "", } @@ -16,7 +16,7 @@ test("Alpha - Capitalized ", () => { test("Alpha - Lowercase", () => { expect( createTestTable( - { col0: ["echo", "alpha", "bravo", "charlie", "delta"] }, + { col0: {td:["echo", "alpha", "bravo", "charlie", "delta"]} }, { classTags: "", } @@ -26,14 +26,14 @@ test("Alpha - Lowercase", () => { test("Numerical", () => { expect( - createTestTable({ col0: [5, 3, 4, 1, 2] }, { classTags: "" }) + createTestTable({ col0: {td: [5, 3, 4, 1, 2]} }, { classTags: "" }) ).toStrictEqual({ col0: ["1", "2", "3", "4", "5"] }); }); test("Alphanumeric: natural sort", () => { expect( createTestTable( - { col0: ["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"] }, + { col0: {td:["Alpha1", "Echo5", "Bravo2", "Charlie3", "Delta4"]} }, { classTags: "", } @@ -46,7 +46,7 @@ test("Alphanumeric: natural sort", () => { test("Dates", () => { 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: "" } ) ).toStrictEqual({ @@ -57,7 +57,7 @@ test("Dates", () => { test("Money", () => { expect( createTestTable( - { col0: ["$29", "$93", "$84", "$20", "$58"] }, + { col0: {td: ["$29", "$93", "$84", "$20", "$58"]} }, { classTags: "" } ) ).toStrictEqual({ col0: ["$20", "$29", "$58", "$84", "$93"] }); @@ -66,7 +66,7 @@ test("Money", () => { test("Empty cells sort at the end.", () => { expect( createTestTable( - { col0: ["Echo", "", "Bravo", "", "Alpha"] }, + { col0: {td: ["Echo", "", "Bravo", "", "Alpha"]} }, { classTags: "" } ) ).toStrictEqual({ col0: ["Alpha", "Bravo", "Echo", "", ""] }); @@ -75,19 +75,7 @@ test("Empty cells sort at the end.", () => { test("Order by file-size: file-size", () => { expect( createTestTable( - { - col0: [ - "10MB", - "10GB", - "10TB", - "10B", - "10MiB", - "10TiB", - "10Kib", - "10KB", - "10GiB", - ], - }, + { col0: {td:[ "10MB", "10GB", "10TB", "10B", "10MiB", "10TiB", "10Kib", "10KB", "10GiB", ]}}, { classTags: "file-size-sort" } ) ).toStrictEqual({ @@ -110,7 +98,7 @@ test("Order by file-size: file-size", () => { test("Alpha - lower & upper", () => { expect( createTestTable( - { col0: ["AlPhA", "bRaVo", "EcHo", "ChArLiE", "dElTa"] }, + { col0:{td: ["AlPhA", "bRaVo", "EcHo", "ChArLiE", "dElTa"] }}, { classTags: "", } @@ -120,14 +108,14 @@ test("Alpha - lower & upper", () => { test("Floating point numbers", () => { expect( - createTestTable({ col0: [6.23, 0.25, 3.15, 9.09, 0.35] }, { classTags: "" }) + createTestTable({ col0: {td: [6.23, 0.25, 3.15, 9.09, 0.35]} }, { classTags: "" }) ).toStrictEqual({ col0: ["0.25", "0.35", "3.15", "6.23", "9.09"] }); }); test("Release Versions", () => { expect( createTestTable( - { col0: ["4.18.0", "3.0.2", "4.1.0", "3.0.18", "4.2.0", "4.17.1"] }, + { col0: {td: ["4.18.0", "3.0.2", "4.1.0", "3.0.18", "4.2.0", "4.17.1"] }}, { classTags: "", } @@ -141,7 +129,7 @@ test("data-sort: example days of week", () => { expect( createTestTable( { - col0: [ + col0: {td:[ "Saturday", "Wednesday", "Sunday", @@ -149,7 +137,7 @@ test("data-sort: example days of week", () => { "Thursday", "Tuesday", "Monday", - ], + ],} }, { classTags: "data-sort" } ) @@ -170,7 +158,7 @@ test("data-sort clicked twice: example days of week", () => { expect( createTestTable( { - col0: [ + col0: {td: [ "Saturday", "Wednesday", "Sunday", @@ -179,7 +167,7 @@ test("data-sort clicked twice: example days of week", () => { "Tuesday", "Monday", ], - }, + }}, { classTags: "data-sort" }, { colsToClick: [0, 0], @@ -201,7 +189,7 @@ test("data-sort clicked twice: example days of week", () => { test("disable-sort: disable sorting on a column", () => { expect( createTestTable( - { col0: ["row2", "row1", "row4", "row3"] }, + { col0: {td: ["row2", "row1", "row4", "row3"]} }, { classTags: "disable-sort", } @@ -212,7 +200,7 @@ test("disable-sort: disable sorting on a column", () => { test("alpha-sort: sort alphabetically as opposed to natural sorting", () => { expect( createTestTable( - { col0: ["z2", "z11", "z89", "z82", "z8"] }, + { col0: {td: ["z2", "z11", "z89", "z82", "z8"]} }, { classTags: "alpha-sort", } @@ -223,7 +211,7 @@ test("alpha-sort: sort alphabetically as opposed to natural sorting", () => { test("punc-sort: sort involving punctuation - nat sort", () => { expect( createTestTable( - { col0: ["row2", "*row1", "-row4", "-row3", "#row3"] }, + { col0: {td: ["row2", "*row1", "-row4", "-row3", "#row3"]}}, { classTags: "punct-sort", }, @@ -238,8 +226,8 @@ test("testing that multiple columns works", () => { expect( createTestTable( { - col0: ["charlie", "alpha", "beta"], - col1: ["doris", "carrie", "fisher"], + col0: {td: ["charlie", "alpha", "beta"]}, + col1:{td: ["doris", "carrie", "fisher"]}, }, { classTags: "", @@ -257,7 +245,7 @@ test("testing that multiple columns works", () => { test("onload-sort: testing that it sorts without a click from user", () => { expect( createTestTable( - { col0: ["echo", "alpha", "bravo", "charlie", "delta"] }, + { col0: {td:["echo", "alpha", "bravo", "charlie", "delta"] }}, { classTags: "onload-sort", } @@ -269,8 +257,8 @@ test("Clicking multiple times (>2) doesn't break sorting", () => { expect( createTestTable( { - col0: ["charlie", "alpha", "beta"], - col1: ["doris", "carrie", "fisher"], + col0: {td: ["charlie", "alpha", "beta"]}, + col1: {td: ["doris", "carrie", "fisher"]}, }, { classTags: "", @@ -289,8 +277,8 @@ test("Sorting columns without rememeber-sort ", () => { expect( createTestTable( { - col0: ["charlie", "alpha", "beta"], - col1: ["doris", "carrie", "fisher"], + col0:{td: ["charlie", "alpha", "beta"]}, + col1: {td:["doris", "carrie", "fisher"]}, }, { classTags: "", @@ -310,8 +298,8 @@ test("Remember-sort: Sorting cols that have the rememeber-sort class.", () => { expect( createTestTable( { - col0: ["charlie", "alpha", "beta"], - col1: ["doris", "carrie", "fisher"], + col0: {td: ["charlie", "alpha", "beta"]}, + col1: {td:["doris", "carrie", "fisher"]}, }, { classTags: "", @@ -331,8 +319,8 @@ test("Checking ", () => { expect( createTestTable( { - col0: ["charlie", "alpha", "beta"], - col1: ["doris", "carrie", "fisher"], + col0: {td: ["charlie", "alpha", "beta"]}, + col1: {td: ["doris", "carrie", "fisher"]}, }, { classTags: "", @@ -352,7 +340,7 @@ test("runtime-sort", () => { expect( createTestTable( { - col0: [ + col0: {td:[ "2m 52s", "1h 20m 10s", "3s", @@ -365,7 +353,7 @@ test("runtime-sort", () => { "1m 36s", "41s", ], - }, + }}, { classTags: "runtime-sort", }, @@ -394,7 +382,7 @@ test("dates-dmy-sort: UK style dd/mm/yyyy; delim . or / or -", () => { expect( createTestTable( { - col0: [ + col0: {td: [ "17/6/1978", "18.10.2027", "10-12-2017", @@ -405,7 +393,7 @@ test("dates-dmy-sort: UK style dd/mm/yyyy; delim . or / or -", () => { "8/6/1978", "4/6/1978", ], - }, + }}, { classTags: "dates-dmy-sort" } ) ).toStrictEqual({ @@ -427,7 +415,7 @@ test("dates-mdy-sort: US style mm/dd/yyyy; delim . or / or -", () => { expect( createTestTable( { - col0: [ + col0: {td: [ "1-14-1992", "1.13.1992", "4.30.2008", @@ -435,7 +423,7 @@ test("dates-mdy-sort: US style mm/dd/yyyy; delim . or / or -", () => { "10-12-2017", "2/14/1992", ], - }, + }}, { classTags: "dates-mdy-sort" } ) ).toStrictEqual({ @@ -454,7 +442,7 @@ test("dates-mdy-sort US style overrides inferred class dates-dmy-sort:", () => { expect( createTestTable( { - col0: [ + col0: {td: [ "1-14-1992", "1.13.1992", "4.30.2008", @@ -462,7 +450,7 @@ test("dates-mdy-sort US style overrides inferred class dates-dmy-sort:", () => { "10-12-2017", "2/14/1992", ], - }, + }}, { classTags: "dates-mdy-sort dates-dmy-sort" } ) ).toStrictEqual({ @@ -481,7 +469,7 @@ test("dates-ymd-sort: ISO 8601 style yyyy/mm/dd; delim . or / or -", () => { expect( createTestTable( { - col0: ["2023/09/6", "2023-03-9", "2023.12.16", "2023/4/6", "2023/4/32"], + col0: {td: ["2023/09/6", "2023-03-9", "2023.12.16", "2023/4/6", "2023/4/32"]}, }, { classTags: "dates-ymd-sort" } ) @@ -494,7 +482,7 @@ test("Sort decimal numbers", () => { expect( createTestTable( { - col0: ["0.1", "0.2", "0.3", "0.11", "0.13", "0.13", "0.14"], + col0: {td: ["0.1", "0.2", "0.3", "0.11", "0.13", "0.13", "0.14"]}, }, { classTags: "numeric-sort" } ) @@ -507,7 +495,7 @@ test("Sort all combination positive, negative numbers with parenthesis as well", expect( createTestTable( { - col0: ["1.05", "-2.3", "-3", "1", "-6", "(1.4)", "14"], + col0: {td:["1.05", "-2.3", "-3", "1", "-6", "(1.4)", "14"]}, }, { classTags: "numeric-sort" } ) @@ -520,7 +508,7 @@ test("Sort all combination of negative and positive integers and decimal numbers expect( createTestTable( { - col0: [ + col0: {td:[ "1.05", "-2.3", "-3", @@ -532,7 +520,7 @@ test("Sort all combination of negative and positive integers and decimal numbers "b", "(c)", "{1}", - ], + ]}, }, { classTags: "numeric-sort" } ) @@ -557,7 +545,7 @@ test("default behavior without cells-sort (tr's move when sorted)", () => { expect( createTestTable( { - col0: ["8", "2", "3", "4", "5", "6", "0", "1", "7"], + col0: {td: ["8", "2", "3", "4", "5", "6", "0", "1", "7"]}, }, { classTags: "" }, // note this isn't an actual class just used for testing purposes @@ -582,7 +570,7 @@ test("cells-sort table class (tr's stay in place, but td's are sorted)", () => { expect( createTestTable( { - col0: ["8", "2", "3", "4", "5", "6", "0", "1", "7"], + col0: {td: ["8", "2", "3", "4", "5", "6", "0", "1", "7"]}, }, { classTags: "" }, { tableTags: "cells-sort", trClasses: "test" } @@ -601,3 +589,31 @@ test("cells-sort table class (tr's stay in place, but td's are sorted)", () => { ], }); }); + + +// +// test("colspan", () => { + // expect( + // createTestTable( + // { + // col0: {td:["8", "2", "3", "4", "5", "6", "0", "1", "7"]}, + // col1: {td:["8", "2", "3", "4", "5", "6", "0", "1", "7"]}, + // col2: {td:["8", "2", "3", "4", "5", "6", "0", "1", "7"]}, + // }, + // { classTags: "data-sort" }, + // { tableTags: "tr-sort", trClasses: "test" } + // ) + // ).toStrictEqual({ + // col0: [ + // ' 0', + // ' 1', + // ' 2', + // ' 3', + // ' 4', + // ' 5', + // ' 6', + // ' 7', + // ' 8', + // ], + // }); +// });