From bdad6e4abd6c456a65e30b93247964fdd9a1f48c Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 26 Oct 2023 17:47:54 -0300 Subject: test: Track.withAlbum (unit) --- test/unit/data/composite/things/track/withAlbum.js | 144 +++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 test/unit/data/composite/things/track/withAlbum.js (limited to 'test') 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 00000000..30f8cc5d --- /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`); +}); -- cgit 1.3.0-6-gf8a5