1818 */
1919
2020const setupOrnamentActions = require ( "../OrnamentActions" ) ;
21- describe ( "OrnamentActions" , ( ) => {
22- let activity , turtleMock ;
2321
24- beforeEach ( ( ) => {
25- global . Singer = {
26- OrnamentActions : null ,
27- } ;
22+ describe ( "OrnamentActions" , ( ) => {
23+ let activity , turtle , dispatchCalls , listenerCalls , beginSlurCalls , endSlurCalls , mouseMB , listenerFunctions ;
2824
29- turtleMock = {
30- singer : {
31- staccato : [ ] ,
32- justCounting : [ ] ,
33- inNeighbor : [ ] ,
34- neighborStepPitch : [ ] ,
35- neighborNoteValue : [ ] ,
36- } ,
37- } ;
25+ beforeEach ( ( ) => {
26+ dispatchCalls = [ ] ; listenerCalls = [ ] ; beginSlurCalls = [ ] ; endSlurCalls = [ ] ; listenerFunctions = { } ; mouseMB = { listeners : [ ] } ;
27+ global . Singer = { OrnamentActions : null } ;
28+ turtle = { singer : { staccato : [ ] , justCounting : [ ] , inNeighbor : [ ] , neighborStepPitch : [ ] , neighborNoteValue : [ ] } } ;
29+ activity = {
30+ turtles : { ithTurtle : ( ) => turtle } ,
31+ blocks : { blockList : { 1 : { } , 2 : { } } } ,
32+ logo : {
33+ setDispatchBlock : ( blk , idx , name ) => dispatchCalls . push ( { blk, turtleIndex : idx , listenerName : name } ) ,
34+ setTurtleListener : ( idx , name , fn ) => { listenerCalls . push ( { turtleIndex : idx , listenerName : name } ) ; listenerFunctions [ name ] = fn ; } ,
35+ notation : { notationBeginSlur : idx => beginSlurCalls . push ( idx ) , notationEndSlur : idx => endSlurCalls . push ( idx ) }
36+ }
37+ } ;
38+ global . MusicBlocks = { isRun : true } ;
39+ global . Mouse = { getMouseFromTurtle : ( ) => ( { MB : mouseMB } ) } ;
40+ setupOrnamentActions ( activity ) ;
41+ } ) ;
3842
39- activity = {
40- turtles : {
41- ithTurtle : jest . fn ( ( ) => turtleMock ) ,
42- } ,
43- blocks : {
44- blockList : { 1 : { } } ,
45- } ,
46- logo : {
47- setDispatchBlock : jest . fn ( ) ,
48- setTurtleListener : jest . fn ( ) ,
49- notation : {
50- notationBeginSlur : jest . fn ( ) ,
51- notationEndSlur : jest . fn ( ) ,
52- } ,
53- } ,
54- } ;
43+ describe ( "setStaccato" , ( ) => {
44+ const scenarios = [
45+ { desc : "block defined" , blk : 1 , expectDispatch : true , expectMouseListener : false , isRun : true , nullMouse : false } ,
46+ { desc : "block undefined" , blk : undefined , expectDispatch : false , expectMouseListener : true , isRun : true , nullMouse : false } ,
47+ { desc : "isRun false" , blk : undefined , expectDispatch : false , expectMouseListener : false , isRun : false , nullMouse : false } ,
48+ { desc : "null mouse" , blk : undefined , expectDispatch : false , expectMouseListener : false , isRun : true , nullMouse : true }
49+ ] ;
5550
56- global . MusicBlocks = { isRun : true } ;
57- global . Mouse = {
58- getMouseFromTurtle : jest . fn ( ( ) => ( {
59- MB : { listeners : [ ] } ,
60- } ) ) ,
61- } ;
62- setupOrnamentActions ( activity ) ;
51+ scenarios . forEach ( ( { desc, blk, expectDispatch, expectMouseListener, isRun, nullMouse } ) => {
52+ test ( desc , ( ) => {
53+ global . MusicBlocks . isRun = isRun ;
54+ if ( nullMouse ) global . Mouse . getMouseFromTurtle = ( ) => null ;
55+ Singer . OrnamentActions . setStaccato ( 2 , 0 , blk ) ;
56+ expect ( turtle . singer . staccato ) . toEqual ( [ 0.5 ] ) ;
57+ expect ( dispatchCalls . length ) . toBe ( expectDispatch ? 1 : 0 ) ;
58+ expect ( listenerCalls . length ) . toBe ( 1 ) ;
59+ if ( expectMouseListener ) expect ( mouseMB . listeners ) . toContain ( '_staccato_0' ) ; else expect ( mouseMB . listeners ) . not . toContain ( '_staccato_0' ) ;
60+ listenerFunctions [ '_staccato_0' ] ( ) ;
61+ expect ( turtle . singer . staccato ) . toEqual ( [ ] ) ;
62+ } ) ;
6363 } ) ;
64+ } ) ;
6465
65- test ( "setStaccato sets up staccato properly" , ( ) => {
66- Singer . OrnamentActions . setStaccato ( 2 , 0 , 1 ) ;
67- expect ( turtleMock . singer . staccato ) . toContain ( 1 / 2 ) ;
68- expect ( activity . logo . setDispatchBlock ) . toHaveBeenCalledWith ( 1 , 0 , "_staccato_0" ) ;
69- expect ( activity . logo . setTurtleListener ) . toHaveBeenCalledWith (
70- 0 ,
71- "_staccato_0" ,
72- expect . any ( Function )
73- ) ;
74- } ) ;
66+ describe ( "setSlur" , ( ) => {
67+ const scenarios = [
68+ { desc : "block defined" , blk : 1 , expectDispatch : true , expectMouseListener : false , isRun : true , nullMouse : false , justCounting : false , expectBeginSlur : true } ,
69+ { desc : "block undefined" , blk : undefined , expectDispatch : false , expectMouseListener : true , isRun : true , nullMouse : false , justCounting : false , expectBeginSlur : true } ,
70+ { desc : "isRun false" , blk : undefined , expectDispatch : false , expectMouseListener : false , isRun : false , nullMouse : false , justCounting : false , expectBeginSlur : true } ,
71+ { desc : "null mouse" , blk : undefined , expectDispatch : false , expectMouseListener : false , isRun : true , nullMouse : true , justCounting : false , expectBeginSlur : true } ,
72+ { desc : "justCounting" , blk : 1 , expectDispatch : true , expectMouseListener : false , isRun : true , nullMouse : false , justCounting : true , expectBeginSlur : false }
73+ ] ;
7574
76- test ( "setSlur sets up slur properly" , ( ) => {
77- Singer . OrnamentActions . setSlur ( 2 , 0 , 1 ) ;
78- expect ( turtleMock . singer . staccato ) . toContain ( - 1 / 2 ) ;
79- expect ( activity . logo . notation . notationBeginSlur ) . toHaveBeenCalledWith ( 0 ) ;
80- expect ( activity . logo . setDispatchBlock ) . toHaveBeenCalledWith ( 1 , 0 , "_staccato_0" ) ;
81- expect ( activity . logo . setTurtleListener ) . toHaveBeenCalledWith (
82- 0 ,
83- "_staccato_0" ,
84- expect . any ( Function )
85- ) ;
75+ scenarios . forEach ( ( { desc, blk, expectDispatch, expectMouseListener, isRun, nullMouse, justCounting, expectBeginSlur } ) => {
76+ test ( desc , ( ) => {
77+ turtle . singer . justCounting = justCounting ? [ 1 ] : [ ] ;
78+ global . MusicBlocks . isRun = isRun ;
79+ if ( nullMouse ) global . Mouse . getMouseFromTurtle = ( ) => null ;
80+ Singer . OrnamentActions . setSlur ( 2 , 0 , blk ) ;
81+ expect ( turtle . singer . staccato ) . toEqual ( [ - 0.5 ] ) ;
82+ expect ( beginSlurCalls . length ) . toBe ( expectBeginSlur ? 1 : 0 ) ;
83+ expect ( dispatchCalls . length ) . toBe ( expectDispatch ? 1 : 0 ) ;
84+ expect ( listenerCalls . length ) . toBe ( 1 ) ;
85+ if ( expectMouseListener ) expect ( mouseMB . listeners ) . toContain ( '_staccato_0' ) ; else expect ( mouseMB . listeners ) . not . toContain ( '_staccato_0' ) ;
86+ listenerFunctions [ '_staccato_0' ] ( ) ;
87+ expect ( turtle . singer . staccato ) . toEqual ( [ ] ) ;
88+ expect ( endSlurCalls . length ) . toBe ( expectBeginSlur ? 1 : 0 ) ;
89+ } ) ;
8690 } ) ;
91+ } ) ;
8792
88- test ( "doNeighbor sets up neighbor action properly" , ( ) => {
89- Singer . OrnamentActions . doNeighbor ( 3 , 4 , 0 , 1 ) ;
90- expect ( turtleMock . singer . inNeighbor ) . toContain ( 1 ) ;
91- expect ( turtleMock . singer . neighborStepPitch ) . toContain ( 3 ) ;
92- expect ( turtleMock . singer . neighborNoteValue ) . toContain ( 4 ) ;
93- expect ( activity . logo . setDispatchBlock ) . toHaveBeenCalledWith ( 1 , 0 , "_neighbor_0_1" ) ;
94- expect ( activity . logo . setTurtleListener ) . toHaveBeenCalledWith (
95- 0 ,
96- "_neighbor_0_1" ,
97- expect . any ( Function )
98- ) ;
99- } ) ;
100- } ) ;
93+ describe ( "doNeighbor" , ( ) => {
94+ const scenarios = [
95+ { desc : "block defined" , blk : 1 , expectDispatch : true , expectMouseListener : false , isRun : true , nullMouse : false } ,
96+ { desc : "block undefined" , blk : undefined , expectDispatch : false , expectMouseListener : true , isRun : true , nullMouse : false } ,
97+ { desc : "isRun false" , blk : undefined , expectDispatch : false , expectMouseListener : false , isRun : false , nullMouse : false } ,
98+ { desc : "null mouse" , blk : undefined , expectDispatch : false , expectMouseListener : false , isRun : true , nullMouse : true }
99+ ] ;
101100
101+ scenarios . forEach ( ( { desc, blk, expectDispatch, expectMouseListener, isRun, nullMouse } ) => {
102+ test ( desc , ( ) => {
103+ global . MusicBlocks . isRun = isRun ;
104+ if ( nullMouse ) global . Mouse . getMouseFromTurtle = ( ) => null ;
105+ Singer . OrnamentActions . doNeighbor ( 3 , 4 , 0 , blk ) ;
106+ expect ( turtle . singer . inNeighbor ) . toEqual ( [ blk ] ) ;
107+ expect ( turtle . singer . neighborStepPitch ) . toEqual ( [ 3 ] ) ;
108+ expect ( turtle . singer . neighborNoteValue ) . toEqual ( [ 4 ] ) ;
109+ expect ( dispatchCalls . length ) . toBe ( expectDispatch ? 1 : 0 ) ;
110+ expect ( listenerCalls . length ) . toBe ( 1 ) ;
111+ const name = `_neighbor_0_${ blk } ` ;
112+ if ( expectMouseListener ) expect ( mouseMB . listeners ) . toContain ( name ) ; else expect ( mouseMB . listeners ) . not . toContain ( name ) ;
113+ listenerFunctions [ name ] ( ) ;
114+ expect ( turtle . singer . inNeighbor ) . toEqual ( [ ] ) ;
115+ expect ( turtle . singer . neighborStepPitch ) . toEqual ( [ ] ) ;
116+ expect ( turtle . singer . neighborNoteValue ) . toEqual ( [ ] ) ;
117+ } ) ;
118+ } ) ;
119+ } ) ;
120+ } ) ;
0 commit comments