123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- import 'web-audio-test-api';
- import React from 'react';
- import configureStore from 'redux-mock-store';
- import {mount} from 'enzyme';
- import VM from 'scratch-vm';
- import {LoadingState} from '../../../src/reducers/project-state';
- import CloudProvider from '../../../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';
- describe('CloudManagerHOC', () => {
- const mockStore = configureStore();
- let store;
- let vm;
- let stillLoadingStore;
- beforeEach(() => {
- store = mockStore({
- scratchGui: {
- projectState: {
- projectId: '1234',
- loadingState: LoadingState.SHOWING_WITH_ID
- },
- mode: {
- hasEverEnteredEditor: false
- }
- }
- });
- stillLoadingStore = mockStore({
- scratchGui: {
- projectState: {
- projectId: '1234',
- loadingState: LoadingState.LOADING_WITH_ID
- },
- mode: {
- hasEverEnteredEditor: false
- }
- }
- });
- vm = new VM();
- vm.setCloudProvider = jest.fn();
- vm.runtime = {
- hasCloudData: jest.fn(() => true)
- };
- CloudProvider.mockClear();
- mockCloudProviderInstance.requestCloseConnection.mockClear();
- });
- test('when it mounts, the cloud provider is set on the vm', () => {
- const Component = () => (<div />);
- const WrappedComponent = cloudManagerHOC(Component);
- const onShowCloudInfo = jest.fn();
- mount(
- <WrappedComponent
- hasCloudPermission
- cloudHost="nonEmpty"
- store={store}
- username="user"
- vm={vm}
- onShowCloudInfo={onShowCloudInfo}
- />
- );
- expect(vm.setCloudProvider.mock.calls.length).toBe(1);
- expect(CloudProvider).toHaveBeenCalledTimes(1);
- expect(vm.setCloudProvider).toHaveBeenCalledWith(mockCloudProviderInstance);
- expect(onShowCloudInfo).not.toHaveBeenCalled();
- });
- test('when cloudHost is missing, the cloud provider is not set on the vm', () => {
- const Component = () => (<div />);
- const WrappedComponent = cloudManagerHOC(Component);
- mount(
- <WrappedComponent
- hasCloudPermission
- store={store}
- username="user"
- vm={vm}
- />
- );
- expect(vm.setCloudProvider.mock.calls.length).toBe(0);
- expect(CloudProvider).not.toHaveBeenCalled();
- });
- test('when projectID is missing, the cloud provider is not set on the vm', () => {
- const Component = () => (<div />);
- const WrappedComponent = cloudManagerHOC(Component);
- mount(
- <WrappedComponent
- hasCloudPermission
- cloudHost="nonEmpty"
- store={store}
- vm={vm}
- />
- );
- expect(vm.setCloudProvider.mock.calls.length).toBe(0);
- expect(CloudProvider).not.toHaveBeenCalled();
- });
- test('when project is not showingWithId, the cloud provider is not set on the vm', () => {
- const Component = () => (<div />);
- const WrappedComponent = cloudManagerHOC(Component);
- mount(
- <WrappedComponent
- hasCloudPermission
- cloudHost="nonEmpty"
- store={stillLoadingStore}
- username="user"
- vm={vm}
- />
- );
- expect(vm.setCloudProvider.mock.calls.length).toBe(0);
- expect(CloudProvider).not.toHaveBeenCalled();
- });
- test('when hasCloudPermission is false, the cloud provider is not set on the vm', () => {
- const Component = () => <div />;
- const WrappedComponent = cloudManagerHOC(Component);
- mount(
- <WrappedComponent
- cloudHost="nonEmpty"
- hasCloudPermission={false}
- store={store}
- username="user"
- vm={vm}
- />
- );
- expect(vm.setCloudProvider.mock.calls.length).toBe(0);
- expect(CloudProvider).not.toHaveBeenCalled();
- });
- test('if the isShowingWithId prop becomes true, it sets the cloud provider on the vm', () => {
- const Component = () => <div />;
- const WrappedComponent = cloudManagerHOC(Component);
- const onShowCloudInfo = jest.fn();
- vm.runtime.hasCloudData = jest.fn(() => false);
- const mounted = mount(
- <WrappedComponent
- hasCloudPermission
- cloudHost="nonEmpty"
- store={stillLoadingStore}
- username="user"
- vm={vm}
- onShowCloudInfo={onShowCloudInfo}
- />
- );
- expect(onShowCloudInfo).not.toHaveBeenCalled();
- vm.runtime.hasCloudData = jest.fn(() => true);
- vm.emit('HAS_CLOUD_DATA_UPDATE', true);
- mounted.setProps({
- isShowingWithId: true,
- loadingState: LoadingState.SHOWING_WITH_ID
- });
- expect(vm.setCloudProvider.mock.calls.length).toBe(1);
- expect(CloudProvider).toHaveBeenCalledTimes(1);
- expect(vm.setCloudProvider).toHaveBeenCalledWith(mockCloudProviderInstance);
- expect(onShowCloudInfo).not.toHaveBeenCalled();
- });
- test('projectId change should not trigger cloudProvider connection unless isShowingWithId becomes true', () => {
- const Component = () => <div />;
- const WrappedComponent = cloudManagerHOC(Component);
- const mounted = mount(
- <WrappedComponent
- hasCloudPermission
- cloudHost="nonEmpty"
- store={stillLoadingStore}
- username="user"
- vm={vm}
- />
- );
- mounted.setProps({
- projectId: 'a different id'
- });
- expect(vm.setCloudProvider.mock.calls.length).toBe(0);
- expect(CloudProvider).not.toHaveBeenCalled();
- mounted.setProps({
- isShowingWithId: true,
- loadingState: LoadingState.SHOWING_WITH_ID
- });
- expect(vm.setCloudProvider.mock.calls.length).toBe(1);
- expect(CloudProvider).toHaveBeenCalledTimes(1);
- expect(vm.setCloudProvider).toHaveBeenCalledWith(mockCloudProviderInstance);
- });
- test('when it unmounts, the cloud provider is reset to null on the vm', () => {
- const Component = () => (<div />);
- const WrappedComponent = cloudManagerHOC(Component);
- const mounted = mount(
- <WrappedComponent
- hasCloudPermission
- cloudHost="nonEmpty"
- store={store}
- username="user"
- vm={vm}
- />
- );
- expect(CloudProvider).toHaveBeenCalled();
- const requestCloseConnection = mockCloudProviderInstance.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);
- });
- test('projectId changing should trigger cloudProvider disconnection', () => {
- const Component = () => <div />;
- const WrappedComponent = cloudManagerHOC(Component);
- const mounted = mount(
- <WrappedComponent
- hasCloudPermission
- 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
- hasCloudPermission
- 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);
- });
- test('project without cloud data should not trigger cloud connection', () => {
- // Mock the vm runtime function so that has cloud data is not
- // initially true
- vm.runtime.hasCloudData = jest.fn(() => false);
- const Component = () => <div />;
- const WrappedComponent = cloudManagerHOC(Component);
- mount(
- <WrappedComponent
- hasCloudPermission
- cloudHost="nonEmpty"
- store={store}
- username="user"
- vm={vm}
- />
- );
- expect(vm.setCloudProvider.mock.calls.length).toBe(0);
- expect(CloudProvider).not.toHaveBeenCalled();
- });
- test('projectHasCloudData becoming true should trigger a cloud connection', () => {
- // Mock the vm runtime function so that has cloud data is not
- // initially true
- vm.runtime.hasCloudData = jest.fn(() => false);
- const onShowCloudInfo = jest.fn();
- const Component = () => <div />;
- const WrappedComponent = cloudManagerHOC(Component);
- mount(
- <WrappedComponent
- hasCloudPermission
- cloudHost="nonEmpty"
- store={store}
- username="user"
- vm={vm}
- onShowCloudInfo={onShowCloudInfo}
- />
- );
- expect(vm.setCloudProvider.mock.calls.length).toBe(0);
- expect(CloudProvider).not.toHaveBeenCalled();
- expect(onShowCloudInfo).not.toHaveBeenCalled();
- // Mock VM hasCloudData becoming true and emitting an update
- vm.runtime.hasCloudData = jest.fn(() => true);
- vm.emit('HAS_CLOUD_DATA_UPDATE', true);
- expect(vm.setCloudProvider.mock.calls.length).toBe(1);
- expect(CloudProvider).toHaveBeenCalledTimes(1);
- expect(vm.setCloudProvider).toHaveBeenCalledWith(mockCloudProviderInstance);
- expect(onShowCloudInfo).toHaveBeenCalled();
- });
- test('projectHasCloudDataUpdate becoming false should trigger cloudProvider disconnection', () => {
- const Component = () => <div />;
- const WrappedComponent = cloudManagerHOC(Component);
- mount(
- <WrappedComponent
- hasCloudPermission
- cloudHost="nonEmpty"
- store={store}
- username="user"
- vm={vm}
- />
- );
- expect(CloudProvider).toHaveBeenCalled();
- const requestCloseConnection = mockCloudProviderInstance.requestCloseConnection;
- vm.runtime.hasCloudData = jest.fn(() => false);
- vm.emit('HAS_CLOUD_DATA_UPDATE', false);
- expect(vm.setCloudProvider.mock.calls.length).toBe(2);
- expect(vm.setCloudProvider).toHaveBeenCalledWith(null);
- expect(requestCloseConnection).toHaveBeenCalledTimes(1);
- });
- // Editor Mode Connection/Disconnection Tests
- test('Entering editor mode and can\'t save project should disconnect cloud provider', () => {
- const Component = () => <div />;
- const WrappedComponent = cloudManagerHOC(Component);
- const mounted = mount(
- <WrappedComponent
- hasCloudPermission
- cloudHost="nonEmpty"
- store={store}
- username="user"
- vm={vm}
- />
- );
- expect(CloudProvider).toHaveBeenCalled();
- const requestCloseConnection = mockCloudProviderInstance.requestCloseConnection;
- mounted.setProps({
- canModifyCloudData: false
- });
- expect(vm.setCloudProvider.mock.calls.length).toBe(2);
- expect(vm.setCloudProvider).toHaveBeenCalledWith(null);
- expect(requestCloseConnection).toHaveBeenCalledTimes(1);
- });
- });
|