diff options
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 |
commit | 54412ec68e2b8014f37fd7d4135e2e3db8d2835c (patch) | |
tree | fae7a15eb49fcb2f290d4ac944e2be789c7e6252 /test/unit/data | |
parent | 5349b6fafc2166b3de69a166e255fd94cb7dcd3d (diff) |
test: CacheableObject transform/update relationship tests
Diffstat (limited to 'test/unit/data')
-rw-r--r-- | test/unit/data/cacheable-object.js | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/test/unit/data/cacheable-object.js b/test/unit/data/cacheable-object.js index 5ed9a4a9..8c31a5bc 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: { |