Refactor shouldDisconnect logic and add more disconnection unit tests.

This commit is contained in:
Karishma Chadha committed 2018-11-20 00:14:10 -05:00
1 parent 340d368791
commit 3545d3d097
2 files changed
+67 -14

No files matched your search

+63 -10
View File
@@ -6,7 +6,13 @@ import {mount} from 'enzyme';
import VM from 'scratch-vm';
import {LoadingState} from '../../../src/reducers/project-state';
import CloudProvider from '../../../src/lib/cloud-provider';
jest.mock('../../../src/lib/cloud-provider');
const mockCloudProviderInstance = {
connection: true,
requestCloseConnection: jest.fn()
};
jest.mock('../../../src/lib/cloud-provider', () =>
jest.fn().mockImplementation(() => mockCloudProviderInstance)
);
import cloudManagerHOC from '../../../src/lib/cloud-manager-hoc.jsx';
@@ -36,6 +42,7 @@ describe('CloudManagerHOC', () => {
vm = new VM();
vm.setCloudProvider = jest.fn();
CloudProvider.mockClear();
mockCloudProviderInstance.requestCloseConnection.mockClear();
});
test('when it mounts, the cloud provider is set on the vm', () => {
const Component = () => (<div />);
@@ -50,8 +57,7 @@ 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);
expect(vm.setCloudProvider).toHaveBeenCalledWith(mockCloudProviderInstance);
});
test('when cloudHost is missing, the cloud provider is not set on the vm', () => {
@@ -117,8 +123,7 @@ 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);
expect(vm.setCloudProvider).toHaveBeenCalledWith(mockCloudProviderInstance);
});
test('projectId change should not trigger cloudProvider connection unless isShowingWithId becomes true', () => {
@@ -143,8 +148,7 @@ 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);
expect(vm.setCloudProvider).toHaveBeenCalledWith(mockCloudProviderInstance);
});
test('when it unmounts, the cloud provider is set on the vm', () => {
@@ -159,9 +163,8 @@ describe('CloudManagerHOC', () => {
/>
);
expect(CloudProvider).toHaveBeenCalledTimes(1);
const cloudProviderInstance = CloudProvider.mock.instances[0];
const requestCloseConnection = cloudProviderInstance.requestCloseConnection;
expect(CloudProvider).toHaveBeenCalled();
const requestCloseConnection = mockCloudProviderInstance.requestCloseConnection;
mounted.unmount();
@@ -171,4 +174,54 @@ describe('CloudManagerHOC', () => {
expect(vm.setCloudProvider).toHaveBeenCalledWith(null);
expect(requestCloseConnection).toHaveBeenCalledTimes(1);
});
test('projectId changing should trigger cloudProvider disconnection', () => {
const Component = () => <div />;
const WrappedComponent = cloudManagerHOC(Component);
const mounted = mount(
<WrappedComponent
cloudHost="nonEmpty"
store={store}
username="user"
vm={vm}
/>
);
expect(CloudProvider).toHaveBeenCalled();
const requestCloseConnection = mockCloudProviderInstance.requestCloseConnection;
mounted.setProps({
projectId: 'a different id'
});
expect(vm.setCloudProvider.mock.calls.length).toBe(2);
expect(vm.setCloudProvider).toHaveBeenCalledWith(null);
expect(requestCloseConnection).toHaveBeenCalledTimes(1);
});
test('username changing should trigger cloudProvider disconnection', () => {
const Component = () => <div />;
const WrappedComponent = cloudManagerHOC(Component);
const mounted = mount(
<WrappedComponent
cloudHost="nonEmpty"
store={store}
username="user"
vm={vm}
/>
);
expect(CloudProvider).toHaveBeenCalled();
const requestCloseConnection = mockCloudProviderInstance.requestCloseConnection;
mounted.setProps({
username: 'a different user'
});
expect(vm.setCloudProvider.mock.calls.length).toBe(2);
expect(vm.setCloudProvider).toHaveBeenCalledWith(null);
expect(requestCloseConnection).toHaveBeenCalledTimes(1);
});
});