« get me outta code hell

test: CacheableObject transform/update relationship tests - 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>2024-01-06 09:01:23 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-01-06 09:01:23 -0400
commit54412ec68e2b8014f37fd7d4135e2e3db8d2835c (patch)
treefae7a15eb49fcb2f290d4ac944e2be789c7e6252 /test/unit
parent5349b6fafc2166b3de69a166e255fd94cb7dcd3d (diff)
test: CacheableObject transform/update relationship tests
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/data/cacheable-object.js95
1 files changed, 95 insertions, 0 deletions
diff --git a/test/unit/data/cacheable-object.js b/test/unit/data/cacheable-object.js
index 5ed9a4a..8c31a5b 100644
--- a/test/unit/data/cacheable-object.js
+++ b/test/unit/data/cacheable-object.js
@@ -213,6 +213,101 @@ t.test(`CacheableObject validate on update`, t => {
   t.equal(obj.date, date);
 });
 
+t.test(`CacheableObject transform on null value`, t => {
+  let computed = false;
+
+  const obj = newCacheableObject({
+    spookyFactor: {
+      flags: {
+        update: true,
+        expose: true,
+      },
+
+      expose: {
+        transform: value => {
+          computed = true;
+          return (value ? 2 * value : -1);
+        },
+      },
+    },
+  });
+
+  t.plan(4);
+
+  t.equal(obj.spookyFactor, -1);
+  t.ok(computed);
+
+  computed = false;
+  obj.spookyFactor = 1;
+
+  t.equal(obj.spookyFactor, 2);
+  t.ok(computed);
+});
+
+t.test(`CacheableObject don't transform on successful update`, t => {
+  let computed = false;
+
+  const obj = newCacheableObject({
+    original: {
+      flags: {
+        update: true,
+        expose: true,
+      },
+
+      update: {
+        validate: value => value.startsWith('track:'),
+      },
+
+      expose: {
+        transform: value => {
+          computed = true;
+          return (value ? value.split(':')[1] : null);
+        },
+      },
+    },
+  });
+
+  t.plan(4);
+
+  t.doesNotThrow(() => obj.original = 'track:foo');
+  t.notOk(computed);
+
+  t.equal(obj.original, 'foo');
+  t.ok(computed);
+});
+
+t.test(`CacheableObject don't transform on failed update`, t => {
+  let computed = false;
+
+  const obj = newCacheableObject({
+    original: {
+      flags: {
+        update: true,
+        expose: true,
+      },
+
+      update: {
+        validate: value => value.startsWith('track:'),
+      },
+
+      expose: {
+        transform: value => {
+          computed = true;
+          return (value ? value.split(':')[1] : null);
+        },
+      },
+    },
+  });
+
+  t.plan(4);
+
+  t.throws(() => obj.original = 'album:foo');
+  t.notOk(computed);
+
+  t.equal(obj.original, null);
+  t.ok(computed);
+});
+
 t.test(`CacheableObject default update property value`, t => {
   const obj = newCacheableObject({
     fruit: {