Repository: 11ty/eleventy
Pull request #2477 (2022-06-30 20:54:48)

Add tests to get UT Coverage for utils/Sortable to 100
This PR contains some changes to the Sortable class, to make the API a little more consistent (IMO) while being backwards compatible.


Patch:
diff --git a/src/Util/Sortable.js b/src/Util/Sortable.js
index e9c052bd..6b375a44 100644
--- a/src/Util/Sortable.js
+++ b/src/Util/Sortable.js
@@ -53,11 +53,11 @@ class Sortable {
     return this.sort(this.getSortFunctionDescending());
   }
 
-  setSortDescending() {
-    this.isSortAscending = false;
+  setSortDescending(isDescending = true) {
+    this.isSortAscending = !isDescending;
   }
 
-  setSortAscending(isAscending) {
+  setSortAscending(isAscending = true) {
     this.isSortAscending = isAscending;
   }
 


Test patch:
diff --git a/test/SortableTest.js b/test/SortableTest.js
index 49cf34f5..a187f164 100644
--- a/test/SortableTest.js
+++ b/test/SortableTest.js
@@ -177,3 +177,50 @@ test("Alphabetic Descending (short str sort arg)", (t) => {
   s.add("m");
   t.deepEqual(s.sort("Z-A"), ["z", "m", "a"]);
 });
+
+test("Invalid Sort Function Name", (t) => {
+  let s = new Sortable();
+  t.throws(() => s.sort("INVALID SORT STRING"));
+});
+
+test("Ascending / Descending Sorting Setters (asc, no param)", (t) => {
+  let s = new Sortable();
+  s.isSortAscending = false;
+  s.setSortAscending();
+  t.is(s.isSortAscending, true);
+});
+
+test("Ascending / Descending Sorting Setters (desc, no param)", (t) => {
+  let s = new Sortable();
+  s.isSortAscending = true;
+  s.setSortDescending();
+  t.is(s.isSortAscending, false);
+});
+
+test("Ascending / Descending Sorting Setters (asc, true)", (t) => {
+  let s = new Sortable();
+  s.isSortAscending = false;
+  s.setSortAscending(true);
+  t.is(s.isSortAscending, true);
+});
+
+test("Ascending / Descending Sorting Setters (asc, false)", (t) => {
+  let s = new Sortable();
+  s.isSortAscending = true;
+  s.setSortAscending(false);
+  t.is(s.isSortAscending, false);
+});
+
+test("Ascending / Descending Sorting Setters (desc, true)", (t) => {
+  let s = new Sortable();
+  s.isSortAscending = true;
+  s.setSortDescending(true);
+  t.is(s.isSortAscending, false);
+});
+
+test("Ascending / Descending Sorting Setters (desc, false)", (t) => {
+  let s = new Sortable();
+  s.isSortAscending = false;
+  s.setSortDescending(false);
+  t.is(s.isSortAscending, true);
+});
diff --git a/test/TemplateCollectionTest.js b/test/TemplateCollectionTest.js
index 770ca34d..28c8de56 100644
--- a/test/TemplateCollectionTest.js
+++ b/test/TemplateCollectionTest.js
@@ -51,6 +51,24 @@ test("Basic setup", async (t) => {
   t.is(c.length, 3);
 });
 
+test("sortFunctionDate", async (t) => {
+  let eleventyConfig = new TemplateConfig();
+  let tmpl1 = getNewTemplateByNumber(1, eleventyConfig);
+  let tmpl4 = getNewTemplateByNumber(4, eleventyConfig);
+  let tmpl5 = getNewTemplateByNumber(5, eleventyConfig);
+
+  let c = new Collection();
+  await addTemplate(c, tmpl1);
+  await addTemplate(c, tmpl4);
+  await addTemplate(c, tmpl5);
+
+  let posts = c.sort(Sortable.sortFunctionDate);
+  t.is(posts.length, 3);
+  t.deepEqual(posts[0].template, tmpl4);
+  t.deepEqual(posts[1].template, tmpl1);
+  t.deepEqual(posts[2].template, tmpl5);
+});
+
 test("sortFunctionDateInputPath", async (t) => {
   let eleventyConfig = new TemplateConfig();
   let tmpl1 = getNewTemplateByNumber(1, eleventyConfig);