123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 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 vmManagerHOC from '../../../src/lib/vm-manager-hoc.jsx';
- describe('VMManagerHOC', () => {
- const mockStore = configureStore();
- let store;
- let vm;
- beforeEach(() => {
- store = mockStore({
- scratchGui: {
- projectState: {},
- mode: {},
- vmStatus: {}
- },
- locales: {
- locale: '',
- messages: {}
- }
- });
- vm = new VM();
- vm.attachAudioEngine = jest.fn();
- vm.setCompatibilityMode = jest.fn();
- vm.setLocale = jest.fn();
- vm.start = jest.fn();
- });
- test('when it mounts in player mode, the vm is initialized but not started', () => {
- const Component = () => (<div />);
- const WrappedComponent = vmManagerHOC(Component);
- mount(
- <WrappedComponent
- isPlayerOnly
- isStarted={false}
- store={store}
- vm={vm}
- />
- );
- expect(vm.attachAudioEngine.mock.calls.length).toBe(1);
- expect(vm.setCompatibilityMode.mock.calls.length).toBe(1);
- expect(vm.setLocale.mock.calls.length).toBe(1);
- expect(vm.initialized).toBe(true);
- // But vm should not be started automatically
- expect(vm.start).not.toHaveBeenCalled();
- });
- test('when it mounts in editor mode, the vm is initialized and started', () => {
- const Component = () => (<div />);
- const WrappedComponent = vmManagerHOC(Component);
- mount(
- <WrappedComponent
- isPlayerOnly={false}
- isStarted={false}
- store={store}
- vm={vm}
- />
- );
- expect(vm.attachAudioEngine.mock.calls.length).toBe(1);
- expect(vm.setCompatibilityMode.mock.calls.length).toBe(1);
- expect(vm.setLocale.mock.calls.length).toBe(1);
- expect(vm.initialized).toBe(true);
- expect(vm.start).toHaveBeenCalled();
- });
- test('if it mounts with an initialized vm, it does not reinitialize the vm but will start it', () => {
- const Component = () => <div />;
- const WrappedComponent = vmManagerHOC(Component);
- vm.initialized = true;
- mount(
- <WrappedComponent
- isPlayerOnly={false}
- isStarted={false}
- store={store}
- vm={vm}
- />
- );
- expect(vm.attachAudioEngine.mock.calls.length).toBe(0);
- expect(vm.setCompatibilityMode.mock.calls.length).toBe(0);
- expect(vm.setLocale.mock.calls.length).toBe(0);
- expect(vm.initialized).toBe(true);
- expect(vm.start).toHaveBeenCalled();
- });
- test('if it mounts without starting the VM, it can be started by switching to editor mode', () => {
- const Component = () => <div />;
- const WrappedComponent = vmManagerHOC(Component);
- vm.initialized = true;
- const mounted = mount(
- <WrappedComponent
- isPlayerOnly
- isStarted={false}
- store={store}
- vm={vm}
- />
- );
- expect(vm.start).not.toHaveBeenCalled();
- mounted.setProps({
- isPlayerOnly: false
- });
- expect(vm.start).toHaveBeenCalled();
- });
- test('if it mounts with an initialized and started VM, it does not start again', () => {
- const Component = () => <div />;
- const WrappedComponent = vmManagerHOC(Component);
- vm.initialized = true;
- const mounted = mount(
- <WrappedComponent
- isPlayerOnly
- isStarted
- store={store}
- vm={vm}
- />
- );
- expect(vm.start).not.toHaveBeenCalled();
- mounted.setProps({
- isPlayerOnly: false
- });
- expect(vm.start).not.toHaveBeenCalled();
- });
- test('if the isLoadingWithId prop becomes true, it loads project data into the vm', () => {
- vm.loadProject = jest.fn(() => Promise.resolve());
- const mockedOnLoadedProject = jest.fn();
- const Component = () => <div />;
- const WrappedComponent = vmManagerHOC(Component);
- const mounted = mount(
- <WrappedComponent
- fontsLoaded
- isLoadingWithId={false}
- store={store}
- vm={vm}
- onLoadedProject={mockedOnLoadedProject}
- />
- );
- mounted.setProps({
- canSave: true,
- isLoadingWithId: true,
- loadingState: LoadingState.LOADING_VM_WITH_ID,
- projectData: '100'
- });
- expect(vm.loadProject).toHaveBeenLastCalledWith('100');
- // nextTick needed since vm.loadProject is async, and we have to wait for it :/
- process.nextTick(() => (
- expect(mockedOnLoadedProject).toHaveBeenLastCalledWith(LoadingState.LOADING_VM_WITH_ID, true)
- ));
- });
- test('if the fontsLoaded prop becomes true, it loads project data into the vm', () => {
- vm.loadProject = jest.fn(() => Promise.resolve());
- const mockedOnLoadedProject = jest.fn();
- const Component = () => <div />;
- const WrappedComponent = vmManagerHOC(Component);
- const mounted = mount(
- <WrappedComponent
- isLoadingWithId
- store={store}
- vm={vm}
- onLoadedProject={mockedOnLoadedProject}
- />
- );
- mounted.setProps({
- canSave: false,
- fontsLoaded: true,
- loadingState: LoadingState.LOADING_VM_WITH_ID,
- projectData: '100'
- });
- expect(vm.loadProject).toHaveBeenLastCalledWith('100');
- // nextTick needed since vm.loadProject is async, and we have to wait for it :/
- process.nextTick(() => (
- expect(mockedOnLoadedProject).toHaveBeenLastCalledWith(LoadingState.LOADING_VM_WITH_ID, false)
- ));
- });
- test('if the fontsLoaded prop is false, project data is never loaded', () => {
- vm.loadProject = jest.fn(() => Promise.resolve());
- const mockedOnLoadedProject = jest.fn();
- const Component = () => <div />;
- const WrappedComponent = vmManagerHOC(Component);
- const mounted = mount(
- <WrappedComponent
- isLoadingWithId
- store={store}
- vm={vm}
- onLoadedProject={mockedOnLoadedProject}
- />
- );
- mounted.setProps({
- loadingState: LoadingState.LOADING_VM_WITH_ID,
- projectData: '100'
- });
- expect(vm.loadProject).toHaveBeenCalledTimes(0);
- process.nextTick(() => expect(mockedOnLoadedProject).toHaveBeenCalledTimes(0));
- });
- });
|