From 340d368791eb8e02a9b06a188a2a7688d9ced0f9 Mon Sep 17 00:00:00 2001
From: Karishma Chadha <kchadha@media.mit.edu>
Date: Mon, 19 Nov 2018 13:16:38 -0500
Subject: [PATCH] Set VM's cloud provider to null when disconnecting from the
 cloud server. Add unit test for disconnecting on unmount.

---
 src/lib/cloud-manager-hoc.jsx             |  1 +
 test/unit/util/cloud-manager-hoc.test.jsx | 31 +++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/src/lib/cloud-manager-hoc.jsx b/src/lib/cloud-manager-hoc.jsx
index 08104abf1..7e33cbf9f 100644
--- a/src/lib/cloud-manager-hoc.jsx
+++ b/src/lib/cloud-manager-hoc.jsx
@@ -74,6 +74,7 @@ const cloudManagerHOC = function (WrappedComponent) {
             if (this.cloudProvider) {
                 this.cloudProvider.requestCloseConnection();
                 this.cloudProvider = null;
+                this.props.vm.setCloudProvider(null);
             }
         }
         render () {
diff --git a/test/unit/util/cloud-manager-hoc.test.jsx b/test/unit/util/cloud-manager-hoc.test.jsx
index 8f2716a3c..d112c0392 100644
--- a/test/unit/util/cloud-manager-hoc.test.jsx
+++ b/test/unit/util/cloud-manager-hoc.test.jsx
@@ -50,6 +50,8 @@ describe('CloudManagerHOC', () => {
         );
         expect(vm.setCloudProvider.mock.calls.length).toBe(1);
         expect(CloudProvider).toHaveBeenCalledTimes(1);
+        const cloudProviderInstance = CloudProvider.mock.instances[0];
+        expect(vm.setCloudProvider).toHaveBeenCalledWith(cloudProviderInstance);
     });
 
     test('when cloudHost is missing, the cloud provider is not set on the vm', () => {
@@ -115,6 +117,8 @@ describe('CloudManagerHOC', () => {
         });
         expect(vm.setCloudProvider.mock.calls.length).toBe(1);
         expect(CloudProvider).toHaveBeenCalledTimes(1);
+        const cloudProviderInstance = CloudProvider.mock.instances[0];
+        expect(vm.setCloudProvider).toHaveBeenCalledWith(cloudProviderInstance);
     });
 
     test('projectId change should not trigger cloudProvider connection unless isShowingWithId becomes true', () => {
@@ -139,5 +143,32 @@ describe('CloudManagerHOC', () => {
         });
         expect(vm.setCloudProvider.mock.calls.length).toBe(1);
         expect(CloudProvider).toHaveBeenCalledTimes(1);
+        const cloudProviderInstance = CloudProvider.mock.instances[0];
+        expect(vm.setCloudProvider).toHaveBeenCalledWith(cloudProviderInstance);
+    });
+
+    test('when it unmounts, the cloud provider is set on the vm', () => {
+        const Component = () => (<div />);
+        const WrappedComponent = cloudManagerHOC(Component);
+        const mounted = mount(
+            <WrappedComponent
+                cloudHost="nonEmpty"
+                store={store}
+                username="user"
+                vm={vm}
+            />
+        );
+
+        expect(CloudProvider).toHaveBeenCalledTimes(1);
+        const cloudProviderInstance = CloudProvider.mock.instances[0];
+        const requestCloseConnection = cloudProviderInstance.requestCloseConnection;
+
+        mounted.unmount();
+
+        // vm.setCloudProvider is called twice,
+        // once during mount and once during unmount
+        expect(vm.setCloudProvider.mock.calls.length).toBe(2);
+        expect(vm.setCloudProvider).toHaveBeenCalledWith(null);
+        expect(requestCloseConnection).toHaveBeenCalledTimes(1);
     });
 });
-- 
GitLab