« get me outta code hell

test: Track.withAlbum (unit) - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/test/unit
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-10-26 17:47:54 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-10-26 19:36:58 -0300
commitbdad6e4abd6c456a65e30b93247964fdd9a1f48c (patch)
treebdc1d5c017a51bd7f085e7e57bc60c458a1f719d /test/unit
parent2532ba76a87971a8d8041d689788d91ae13dc627 (diff)
test: Track.withAlbum (unit)
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/data/composite/things/track/withAlbum.js144
1 files changed, 144 insertions, 0 deletions
diff --git a/test/unit/data/composite/things/track/withAlbum.js b/test/unit/data/composite/things/track/withAlbum.js
new file mode 100644
index 0000000..30f8cc5
--- /dev/null
+++ b/test/unit/data/composite/things/track/withAlbum.js
@@ -0,0 +1,144 @@
+import t from 'tap';
+
+import {compositeFrom, input} from '#composite';
+import {exposeConstant, exposeDependency} from '#composite/control-flow';
+import {withAlbum} from '#composite/things/track';
+
+t.test(`withAlbum: basic behavior`, t => {
+  t.plan(3);
+
+  const composite = compositeFrom({
+    compose: false,
+    steps: [
+      withAlbum(),
+      exposeDependency({dependency: '#album'}),
+    ],
+  });
+
+  t.match(composite, {
+    expose: {
+      dependencies: ['albumData', 'this'],
+    },
+  });
+
+  const fakeTrack1 = {directory: 'foo'};
+  const fakeTrack2 = {directory: 'bar'};
+  const fakeAlbum = {directory: 'baz', tracks: [fakeTrack1]};
+
+  t.equal(
+    composite.expose.compute({
+      albumData: [fakeAlbum],
+      this: fakeTrack1,
+    }),
+    fakeAlbum);
+
+  t.equal(
+    composite.expose.compute({
+      albumData: [fakeAlbum],
+      this: fakeTrack2,
+    }),
+    null);
+});
+
+t.test(`withAlbum: early exit conditions (notFoundMode: null)`, t => {
+  t.plan(4);
+
+  const composite = compositeFrom({
+    compose: false,
+    steps: [
+      withAlbum(),
+      exposeConstant({
+        value: input.value('bimbam'),
+      }),
+    ],
+  });
+
+  const fakeTrack1 = {directory: 'foo'};
+  const fakeTrack2 = {directory: 'bar'};
+  const fakeAlbum = {directory: 'baz', tracks: [fakeTrack1]};
+
+  t.equal(
+    composite.expose.compute({
+      albumData: [fakeAlbum],
+      this: fakeTrack1,
+    }),
+    'bimbam',
+    `does not early exit if albumData is present and contains the track`);
+
+  t.equal(
+    composite.expose.compute({
+      albumData: [fakeAlbum],
+      this: fakeTrack2,
+    }),
+    'bimbam',
+    `does not early exit if albumData is present and does not contain the track`);
+
+  t.equal(
+    composite.expose.compute({
+      albumData: [],
+      this: fakeTrack1,
+    }),
+    'bimbam',
+    `does not early exit if albumData is empty array`);
+
+  t.equal(
+    composite.expose.compute({
+      albumData: null,
+      this: fakeTrack1,
+    }),
+    null,
+    `early exits if albumData is null`);
+});
+
+t.test(`withAlbum: early exit conditions (notFoundMode: exit)`, t => {
+  t.plan(4);
+
+  const composite = compositeFrom({
+    compose: false,
+    steps: [
+      withAlbum({
+        notFoundMode: input.value('exit'),
+      }),
+
+      exposeConstant({
+        value: input.value('bimbam'),
+      }),
+    ],
+  });
+
+  const fakeTrack1 = {directory: 'foo'};
+  const fakeTrack2 = {directory: 'bar'};
+  const fakeAlbum = {directory: 'baz', tracks: [fakeTrack1]};
+
+  t.equal(
+    composite.expose.compute({
+      albumData: [fakeAlbum],
+      this: fakeTrack1,
+    }),
+    'bimbam',
+    `does not early exit if albumData is present and contains the track`);
+
+  t.equal(
+    composite.expose.compute({
+      albumData: [fakeAlbum],
+      this: fakeTrack2,
+    }),
+    null,
+    `early exits if albumData is present and does not contain the track`);
+
+  t.equal(
+    composite.expose.compute({
+      albumData: [],
+      this: fakeTrack1,
+    }),
+    null,
+    `early exits if albumData is empty array`);
+
+  t.equal(
+    composite.expose.compute({
+      albumData: null,
+      this: fakeTrack1,
+    }),
+    null,
+    `early exits if albumData is null`);
+});