From 084deee318827224b99c95194760c13d098ac9e7 Mon Sep 17 00:00:00 2001
From: Paul Kaplan <pkaplan@media.mit.edu>
Date: Tue, 21 Nov 2017 09:23:42 -0500
Subject: [PATCH] Play on undo and redo

---
 src/containers/sound-editor.jsx            |  2 ++
 test/unit/containers/sound-editor.test.jsx | 32 +++++++++++++++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/src/containers/sound-editor.jsx b/src/containers/sound-editor.jsx
index 4a1b98b56..155449ce4 100644
--- a/src/containers/sound-editor.jsx
+++ b/src/containers/sound-editor.jsx
@@ -124,6 +124,7 @@ class SoundEditor extends React.Component {
         const samples = this.undoStack.pop();
         if (samples) {
             this.submitNewSamples(samples, this.props.sampleRate, true);
+            this.handlePlay();
         }
     }
     handleRedo () {
@@ -131,6 +132,7 @@ class SoundEditor extends React.Component {
         if (samples) {
             this.undoStack.push(this.props.samples.slice(0));
             this.submitNewSamples(samples, this.props.sampleRate, true);
+            this.handlePlay();
         }
     }
     render () {
diff --git a/test/unit/containers/sound-editor.test.jsx b/test/unit/containers/sound-editor.test.jsx
index 63199ef01..790e6b85c 100644
--- a/test/unit/containers/sound-editor.test.jsx
+++ b/test/unit/containers/sound-editor.test.jsx
@@ -224,7 +224,7 @@ describe('Sound Editor Container', () => {
         expect(mockAudioEffects.instance.process).toHaveBeenCalled();
     });
 
-    test('undo/redo functionality', () => {
+    test('undo/redo stack state', () => {
         const wrapper = mountWithIntl(
             <SoundEditor
                 soundIndex={soundIndex}
@@ -270,4 +270,34 @@ describe('Sound Editor Container', () => {
         component = wrapper.find(SoundEditorComponent);
         expect(component.prop('canRedo')).toEqual(false);
     });
+
+    test('undo and redo submit new samples and play the sound', () => {
+        const wrapper = mountWithIntl(
+            <SoundEditor
+                soundIndex={soundIndex}
+                store={store}
+            />
+        );
+        let component = wrapper.find(SoundEditorComponent);
+
+        // Set up an undoable state
+        component.props().onActivateTrim(); // Activate trimming
+        component.props().onActivateTrim(); // Submit new samples by calling again
+        wrapper.update();
+        component = wrapper.find(SoundEditorComponent);
+
+        // Undo should update the sound buffer and play the new samples
+        component.props().onUndo();
+        expect(mockAudioBufferPlayer.instance.play).toHaveBeenCalled();
+        expect(vm.updateSoundBuffer).toHaveBeenCalled();
+
+        // Clear the mocks call history to assert again for redo.
+        vm.updateSoundBuffer.mockClear();
+        mockAudioBufferPlayer.instance.play.mockClear();
+
+        // Undo should update the sound buffer and play the new samples
+        component.props().onRedo();
+        expect(mockAudioBufferPlayer.instance.play).toHaveBeenCalled();
+        expect(vm.updateSoundBuffer).toHaveBeenCalled();
+    });
 });
-- 
GitLab