@@ -27,14 +27,19 @@ describe("TemperamentWidget basic tests", () => {
2727 refreshWheel : jest . fn ( )
2828 } ) ) ;
2929
30- global . platformColor = { selectorBackground : "#fff" } ;
30+ global . platformColor = {
31+ selectorBackground : "#fff" ,
32+ selectorBackgroundHOVER : "#eee" ,
33+ labelColor : "#ddd"
34+ } ;
3135
3236 global . Singer = { defaultBPMFactor : 1 } ;
3337
3438 global . getTemperamentKeys = jest . fn ( ( ) => [ ] ) ;
3539 global . isCustomTemperament = jest . fn ( ( ) => false ) ;
3640 global . getTemperament = jest . fn ( ( ) => ( {
37- interval : [ ]
41+ interval : [ ] ,
42+ pitchNumber : 0
3843 } ) ) ;
3944 global . pitchToFrequency = jest . fn ( ( ) => 440 ) ;
4045 global . frequencyToPitch = jest . fn ( ( ) => [ "C" , 4 , 0 ] ) ;
@@ -45,13 +50,47 @@ describe("TemperamentWidget basic tests", () => {
4550 DonutSliceCustomization : ( ) => ( { } )
4651 } ) ) ;
4752
48- global . docById = jest . fn ( id => ( {
53+ global . FLAT = "♭" ;
54+ global . SHARP = "♯" ;
55+
56+ const createMockElement = ( id ) => ( {
57+ id : id ,
4958 innerHTML : "" ,
5059 style : { } ,
60+ width : 100 ,
61+ height : 100 ,
62+ dataset : { message : "1" } ,
5163 append : jest . fn ( ) ,
52- getElementsByTagName : jest . fn ( ( ) => [ ] ) ,
53- addEventListener : jest . fn ( )
54- } ) ) ;
64+ appendChild : jest . fn ( ) ,
65+ remove : jest . fn ( ) ,
66+ getElementsByTagName : jest . fn ( ( ) => [ createMockElement ( "img" ) ] ) ,
67+ addEventListener : jest . fn ( ) ,
68+ getContext : jest . fn ( ( ) => ( {
69+ beginPath : jest . fn ( ) ,
70+ arc : jest . fn ( ) ,
71+ fill : jest . fn ( ) ,
72+ stroke : jest . fn ( ) ,
73+ lineWidth : 0 ,
74+ fillStyle : "" ,
75+ strokeStyle : ""
76+ } ) ) ,
77+ getBoundingClientRect : jest . fn ( ( ) => ( { left : 0 , top : 0 } ) ) ,
78+ insertCell : jest . fn ( ( ) => createMockElement ( "cell" ) ) ,
79+ createTHead : jest . fn ( ( ) => ( {
80+ insertRow : jest . fn ( ( ) => ( {
81+ id : "" ,
82+ insertCell : jest . fn ( ( ) => createMockElement ( "cell" ) )
83+ } ) )
84+ } ) )
85+ } ) ;
86+
87+ const mockElements = { } ;
88+ global . docById = jest . fn ( id => {
89+ if ( ! mockElements [ id ] ) {
90+ mockElements [ id ] = createMockElement ( id ) ;
91+ }
92+ return mockElements [ id ] ;
93+ } ) ;
5594
5695 widget = new TemperamentWidget ( ) ;
5796 } ) ;
@@ -512,4 +551,117 @@ describe("TemperamentWidget basic tests", () => {
512551
513552 expect ( widget . activity . blocks . loadNewBlocks ) . toHaveBeenCalled ( ) ;
514553 } ) ;
554+
555+ test ( "init sets up widget correctly" , ( ) => {
556+ const mockWidgetWindow = {
557+ clear : jest . fn ( ) ,
558+ show : jest . fn ( ) ,
559+ getWidgetBody : jest . fn ( ( ) => ( {
560+ append : jest . fn ( ) ,
561+ style : { }
562+ } ) ) ,
563+ addButton : jest . fn ( ( ) => ( {
564+ onclick : null ,
565+ getElementsByTagName : jest . fn ( ( ) => [ { } ] )
566+ } ) ) ,
567+ sendToCenter : jest . fn ( )
568+ } ;
569+
570+ global . window . widgetWindows = {
571+ windowFor : jest . fn ( ( ) => mockWidgetWindow )
572+ } ;
573+
574+ global . window . innerWidth = 1200 ;
575+ global . buildScale = jest . fn ( ( ) => [ [ "C" ] , [ ] ] ) ;
576+ global . getNoteFromInterval = jest . fn ( ( ) => [ "C" , 4 ] ) ;
577+
578+ const mockActivity = {
579+ logo : {
580+ synth : {
581+ startingPitch : "C4" ,
582+ _getFrequency : jest . fn ( ( ) => 440 )
583+ }
584+ }
585+ } ;
586+
587+ widget . inTemperament = "equal" ;
588+ widget . scale = [ "C" , "Major" ] ;
589+ widget . init ( mockActivity ) ;
590+
591+ expect ( mockWidgetWindow . clear ) . toHaveBeenCalled ( ) ;
592+ expect ( mockWidgetWindow . show ) . toHaveBeenCalled ( ) ;
593+ expect ( widget . activity ) . toBe ( mockActivity ) ;
594+ expect ( widget . pitchNumber ) . toBe ( 0 ) ; // getTemperament("equal") returns 0 in our mock
595+ } ) ;
596+
597+ test ( "showNoteInfo creates a popup" , ( ) => {
598+ widget . notesCircle = {
599+ navItemCount : 1
600+ } ;
601+ widget . frequencies = [ 440 ] ;
602+ widget . ratios = [ 1 ] ;
603+ widget . powerBase = 2 ;
604+ widget . ratiosNotesPair = [ [ 1 , [ "C" , 4 ] ] ] ;
605+
606+ const event = {
607+ target : { id : "wheelnav-wheelDiv2-slice-0" } ,
608+ clientX : 100 ,
609+ clientY : 100
610+ } ;
611+
612+ widget . showNoteInfo ( event ) ;
613+
614+ expect ( global . docById ( "information" ) . innerHTML ) . toContain ( "popup" ) ;
615+ } ) ;
616+
617+ test ( "editFrequency sets up a frequency slider" , ( ) => {
618+ widget . frequencies = [ 440 , 466 , 494 ] ;
619+ widget . ratios = [ 1 , 1.059 , 1.122 ] ;
620+ widget . temporaryRatios = [ ] ;
621+
622+ const event = {
623+ target : { dataset : { message : "1" } }
624+ } ;
625+
626+ widget . editFrequency ( event ) ;
627+
628+ expect ( global . docById ( "noteInfo" ) . innerHTML ) . toContain ( 'type="range"' ) ;
629+ expect ( global . docById ( "noteInfo" ) . innerHTML ) . toContain ( "frequencySlider1" ) ;
630+ } ) ;
631+
632+ test ( "checkTemperament identifies predefined temperament" , ( ) => {
633+ global . getTemperamentKeys = jest . fn ( ( ) => [ "equal" , "just" ] ) ;
634+ global . getTemperament = jest . fn ( key => {
635+ if ( key === "equal" ) {
636+ return {
637+ interval : [ "0" , "1" ] ,
638+ pitchNumber : 1 ,
639+ "0" : 1 ,
640+ "1" : 2
641+ } ;
642+ }
643+ return { interval : [ ] , pitchNumber : 0 } ;
644+ } ) ;
645+ global . isCustomTemperament = jest . fn ( ( ) => false ) ;
646+ global . buildScale = jest . fn ( ( ) => [ [ "C" ] , [ ] ] ) ;
647+ global . getNoteFromInterval = jest . fn ( ( ) => [ "C" , 4 ] ) ;
648+
649+ const mockActivity = {
650+ logo : {
651+ synth : {
652+ startingPitch : "C4" ,
653+ _getFrequency : jest . fn ( ( ) => 440 )
654+ }
655+ }
656+ } ;
657+
658+ // Call init to initialize temperamentCell
659+ widget . inTemperament = "equal" ;
660+ widget . scale = [ "C" , "Major" ] ;
661+ widget . init ( mockActivity ) ;
662+
663+ widget . checkTemperament ( [ "1.00" , "2.00" ] ) ;
664+
665+ expect ( widget . inTemperament ) . toBe ( "equal" ) ;
666+ } ) ;
515667} ) ;
0 commit comments