« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/content/dependencies/generatePreviousNextLinks.js25
-rw-r--r--tap-snapshots/test/snapshot/generatePreviousNextLinks.js.test.cjs28
-rw-r--r--test/snapshot/generatePreviousNextLinks.js36
3 files changed, 80 insertions, 9 deletions
diff --git a/src/content/dependencies/generatePreviousNextLinks.js b/src/content/dependencies/generatePreviousNextLinks.js
index 6cffcef4..e3417cb8 100644
--- a/src/content/dependencies/generatePreviousNextLinks.js
+++ b/src/content/dependencies/generatePreviousNextLinks.js
@@ -8,25 +8,32 @@ export default {
   slots: {
     previousLink: {type: 'html'},
     nextLink: {type: 'html'},
+    id: {type: 'boolean', default: true},
   },
 
   generate(slots, {html, language}) {
-    return [
-      !html.isBlank(slots.previousLink) &&
+    const previousNext = [];
+
+    if (!html.isBlank(slots.previousLink)) {
+      previousNext.push(
         slots.previousLink.slots({
           tooltip: true,
           color: false,
-          attributes: {id: 'previous-button'},
+          attributes: {id: slots.id && 'previous-button'},
           content: language.$('misc.nav.previous'),
-        }),
+        }));
+    }
 
-      !html.isBlank(slots.nextLink) &&
-        slots.nextLink?.slots({
+    if (!html.isBlank(slots.nextLink)) {
+      previousNext.push(
+        slots.nextLink.slots({
           tooltip: true,
           color: false,
-          attributes: {id: 'next-button'},
+          attributes: {id: slots.id && 'next-button'},
           content: language.$('misc.nav.next'),
-        }),
-    ];
+        }));
+    }
+
+    return previousNext;
   },
 };
diff --git a/tap-snapshots/test/snapshot/generatePreviousNextLinks.js.test.cjs b/tap-snapshots/test/snapshot/generatePreviousNextLinks.js.test.cjs
new file mode 100644
index 00000000..07268581
--- /dev/null
+++ b/tap-snapshots/test/snapshot/generatePreviousNextLinks.js.test.cjs
@@ -0,0 +1,28 @@
+/* IMPORTANT
+ * This snapshot file is auto-generated, but designed for humans.
+ * It should be checked into source control and tracked carefully.
+ * Re-generate by setting TAP_SNAPSHOT=1 and running tests.
+ * Make sure to inspect the output below.  Do not ignore changes!
+ */
+'use strict'
+exports[`test/snapshot/generatePreviousNextLinks.js TAP generatePreviousNextLinks (snapshot) > basic behavior 1`] = `
+previous: {"tooltip":true,"color":false,"attributes":{"id":"previous-button"},"content":"Previous"}
+next: {"tooltip":true,"color":false,"attributes":{"id":"next-button"},"content":"Next"}
+`
+
+exports[`test/snapshot/generatePreviousNextLinks.js TAP generatePreviousNextLinks (snapshot) > disable id 1`] = `
+previous: {"tooltip":true,"color":false,"attributes":{"id":false},"content":"Previous"}
+next: {"tooltip":true,"color":false,"attributes":{"id":false},"content":"Next"}
+`
+
+exports[`test/snapshot/generatePreviousNextLinks.js TAP generatePreviousNextLinks (snapshot) > neither link present 1`] = `
+
+`
+
+exports[`test/snapshot/generatePreviousNextLinks.js TAP generatePreviousNextLinks (snapshot) > next missing 1`] = `
+previous: {"tooltip":true,"color":false,"attributes":{"id":"previous-button"},"content":"Previous"}
+`
+
+exports[`test/snapshot/generatePreviousNextLinks.js TAP generatePreviousNextLinks (snapshot) > previous missing 1`] = `
+next: {"tooltip":true,"color":false,"attributes":{"id":"next-button"},"content":"Next"}
+`
diff --git a/test/snapshot/generatePreviousNextLinks.js b/test/snapshot/generatePreviousNextLinks.js
new file mode 100644
index 00000000..d0b61078
--- /dev/null
+++ b/test/snapshot/generatePreviousNextLinks.js
@@ -0,0 +1,36 @@
+import t from 'tap';
+import {testContentFunctions} from '../lib/content-function.js';
+
+import * as html from '../../src/util/html.js';
+
+testContentFunctions(t, 'generatePreviousNextLinks (snapshot)', async (t, evaluate) => {
+  await evaluate.load();
+
+  const quickSnapshot = (message, slots) =>
+    evaluate.snapshot(message, {
+      name: 'generatePreviousNextLinks',
+      slots,
+      postprocess: template => template.content.join('\n'),
+    });
+
+  quickSnapshot('basic behavior', {
+    previousLink: evaluate.stubTemplate('previous'),
+    nextLink: evaluate.stubTemplate('next'),
+  });
+
+  quickSnapshot('previous missing', {
+    nextLink: evaluate.stubTemplate('next'),
+  });
+
+  quickSnapshot('next missing', {
+    previousLink: evaluate.stubTemplate('previous'),
+  });
+
+  quickSnapshot('neither link present', {});
+
+  quickSnapshot('disable id', {
+    previousLink: evaluate.stubTemplate('previous'),
+    nextLink: evaluate.stubTemplate('next'),
+    id: false,
+  });
+});