@@ -2,9 +2,12 @@ import React from 'react';
22import ReactDOM from 'react-dom' ;
33import { shallow } from 'enzyme' ;
44import sinon from 'sinon' ;
5- import { Button } from '@ml/oceans/ui' ;
6- import * as guide from '@ml/oceans/models/guide' ;
7- import * as soundLibrary from '@ml/oceans/models/soundLibrary' ;
5+ import { Button , ConfirmationDialog , Words , wordSet } from '@ml/oceans/ui' ;
6+ import guide from '@ml/oceans/models/guide' ;
7+ import soundLibrary from '@ml/oceans/models/soundLibrary' ;
8+ import modeHelpers from '@ml/oceans/modeHelpers' ;
9+ import { setState , getState , resetState } from '@ml/oceans/state' ;
10+ import { AppMode , Modes } from '@ml/oceans/constants' ;
811
912const DEFAULT_PROPS = {
1013 // radiumConfig.userAgent is required because our unit tests run in the "node" testEnvironment
@@ -14,11 +17,10 @@ const DEFAULT_PROPS = {
1417} ;
1518
1619describe ( 'Button' , ( ) => {
17- let onClickMock , playSoundSpy ;
20+ let onClickMock , playSoundStub ;
1821
1922 beforeEach ( ( ) => {
20- soundLibrary . injectSoundAPIs ( { playSound : sinon . fake ( ) } ) ;
21- playSoundSpy = sinon . spy ( soundLibrary , 'playSound' ) ;
23+ playSoundStub = sinon . stub ( soundLibrary , 'playSound' ) ;
2224 onClickMock = sinon . fake . returns ( false ) ;
2325 } ) ;
2426
@@ -27,13 +29,13 @@ describe('Button', () => {
2729 } ) ;
2830
2931 it ( 'dismisses guide on click' , ( ) => {
30- const dismissCurrentGuideSpy = sinon . spy ( guide , 'dismissCurrentGuide' ) ;
32+ const dismissCurrentGuideSpy = sinon . stub ( guide , 'dismissCurrentGuide' ) ;
3133 const wrapper = shallow (
3234 < Button { ...DEFAULT_PROPS } onClick = { onClickMock } />
3335 ) ;
3436
3537 wrapper . simulate ( 'click' ) ;
36- expect ( dismissCurrentGuideSpy . calledOnce ) ;
38+ expect ( dismissCurrentGuideSpy . callCount ) . toEqual ( 1 ) ;
3739
3840 guide . dismissCurrentGuide . restore ( ) ;
3941 } ) ;
@@ -44,7 +46,7 @@ describe('Button', () => {
4446 ) ;
4547
4648 wrapper . simulate ( 'click' ) ;
47- expect ( onClickMock . calledOnce ) ;
49+ expect ( onClickMock . callCount ) . toEqual ( 1 ) ;
4850 } ) ;
4951
5052 it ( 'does not play a sound if onClick prop returns false' , ( ) => {
@@ -53,7 +55,7 @@ describe('Button', () => {
5355 ) ;
5456
5557 wrapper . simulate ( 'click' ) ;
56- expect ( ! playSoundSpy . called ) ;
58+ expect ( playSoundStub . callCount ) . toEqual ( 0 ) ;
5759 } ) ;
5860
5961 describe ( 'onClick prop does not return false' , ( ) => {
@@ -64,7 +66,7 @@ describe('Button', () => {
6466 ) ;
6567
6668 wrapper . simulate ( 'click' ) ;
67- expect ( playSoundSpy . withArgs ( 'sortyes' ) . calledOnce ) ;
69+ expect ( playSoundStub . withArgs ( 'sortyes' ) . calledOnce ) . toBeTruthy ( ) ;
6870 } ) ;
6971
7072 it ( 'plays "other" sound if sound not supplied' , ( ) => {
@@ -74,7 +76,124 @@ describe('Button', () => {
7476 ) ;
7577
7678 wrapper . simulate ( 'click' ) ;
77- expect ( playSoundSpy . withArgs ( 'other' ) . calledOnce ) ;
79+ expect ( playSoundStub . withArgs ( 'other' ) . calledOnce ) . toBeTruthy ( ) ;
80+ } ) ;
81+ } ) ;
82+ } ) ;
83+
84+ describe ( 'ConfirmationDialog' , ( ) => {
85+ let onYesClickSpy , onNoClickSpy ;
86+
87+ beforeEach ( ( ) => {
88+ onYesClickSpy = sinon . spy ( ) ;
89+ onNoClickSpy = sinon . spy ( ) ;
90+ } ) ;
91+
92+ it ( 'calls onYesClick prop when erase button is clicked' , ( ) => {
93+ const wrapper = shallow (
94+ < ConfirmationDialog
95+ { ...DEFAULT_PROPS }
96+ onYesClick = { onYesClickSpy }
97+ onNoClick = { onNoClickSpy }
98+ />
99+ ) ;
100+
101+ const eraseButton = wrapper . find ( 'Button' ) . at ( 0 ) ;
102+ eraseButton . simulate ( 'click' ) ;
103+ expect ( onYesClickSpy . callCount ) . toEqual ( 1 ) ;
104+ expect ( onNoClickSpy . callCount ) . toEqual ( 0 ) ;
105+ } ) ;
106+
107+ it ( 'calls onNoClick prop when cancel button is clicked' , ( ) => {
108+ const wrapper = shallow (
109+ < ConfirmationDialog
110+ { ...DEFAULT_PROPS }
111+ onYesClick = { onYesClickSpy }
112+ onNoClick = { onNoClickSpy }
113+ />
114+ ) ;
115+
116+ const cancelButton = wrapper . find ( 'Button' ) . at ( 1 ) ;
117+ cancelButton . simulate ( 'click' ) ;
118+ expect ( onNoClickSpy . callCount ) . toEqual ( 1 ) ;
119+ expect ( onYesClickSpy . callCount ) . toEqual ( 0 ) ;
120+ } ) ;
121+ } ) ;
122+
123+ describe ( 'Words' , ( ) => {
124+ afterEach ( ( ) => {
125+ resetState ( ) ;
126+ } ) ;
127+
128+ it ( 'selects the set of words based on the current appMode' , ( ) => {
129+ const appMode = AppMode . FishShort ;
130+ setState ( { appMode} ) ;
131+ const wrapper = shallow ( < Words { ...DEFAULT_PROPS } /> ) ;
132+ const wordChoices = wrapper . state ( ) . choices ;
133+ // Flatten the array of choices as we know it is 2D.
134+ const expectedChoices = [ ] . concat . apply ( [ ] , wordSet [ appMode ] . choices ) ;
135+
136+ // We expect the actual word choices to be randomly sorted
137+ expect ( wordChoices ) . not . toEqual ( expectedChoices ) ;
138+ expect ( wordChoices . sort ( ) ) . toEqual ( expectedChoices . sort ( ) ) ;
139+ } ) ;
140+
141+ it ( 'throws an error if no set of words are found for the current appMode' , ( ) => {
142+ setState ( { appMode : 'a-fake-one!' } ) ;
143+
144+ expect ( ( ) => {
145+ shallow ( < Words { ...DEFAULT_PROPS } /> ) ;
146+ } ) . toThrowError (
147+ new Error (
148+ "Could not find a set of choices in wordSet for appMode 'a-fake-one!'"
149+ )
150+ ) ;
151+ } ) ;
152+
153+ describe ( 'onChangeWord' , ( ) => {
154+ let toModeStub ;
155+
156+ beforeEach ( ( ) => {
157+ toModeStub = sinon . stub ( modeHelpers , 'toMode' ) ;
158+ setState ( { appMode : AppMode . FishLong } ) ;
159+ } ) ;
160+
161+ afterEach ( ( ) => {
162+ modeHelpers . toMode . restore ( ) ;
163+ resetState ( ) ;
164+ } ) ;
165+
166+ it ( 'sets the selected word in state' , ( ) => {
167+ const wrapper = shallow ( < Words { ...DEFAULT_PROPS } /> ) ;
168+
169+ const i = 1 ;
170+ wrapper
171+ . find ( 'Button' )
172+ . at ( i )
173+ . simulate ( 'click' ) ;
174+ const expectedWord = wrapper . state ( ) . choices [ i ] ;
175+ expect ( getState ( ) . word ) . toEqual ( expectedWord ) ;
176+ } ) ;
177+
178+ it ( 'transitions to Modes.Training' , ( ) => {
179+ const wrapper = shallow ( < Words { ...DEFAULT_PROPS } /> ) ;
180+
181+ wrapper
182+ . find ( 'Button' )
183+ . at ( 0 )
184+ . simulate ( 'click' ) ;
185+ expect ( toModeStub . withArgs ( Modes . Training ) . calledOnce ) . toBeTruthy ( ) ;
186+ } ) ;
187+
188+ it ( 'reports an analytics event if window.trackEvent is provided' , ( ) => {
189+ window . trackEvent = sinon . spy ( ) ;
190+ const wrapper = shallow ( < Words { ...DEFAULT_PROPS } /> ) ;
191+
192+ wrapper
193+ . find ( 'Button' )
194+ . at ( 0 )
195+ . simulate ( 'click' ) ;
196+ expect ( window . trackEvent . calledOnce ) . toBeTruthy ( ) ;
78197 } ) ;
79198 } ) ;
80199} ) ;
0 commit comments