|
| 1 | +import 'web-audio-test-api'; |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import configureStore from 'redux-mock-store'; |
| 5 | +import {mount} from 'enzyme'; |
| 6 | +import VM from 'scratch-vm'; |
| 7 | +import {LoadingState} from '../../../src/reducers/project-state'; |
| 8 | + |
| 9 | +import vmManagerHOC from '../../../src/lib/vm-manager-hoc.jsx'; |
| 10 | + |
| 11 | +describe('VMManagerHOC', () => { |
| 12 | + const mockStore = configureStore(); |
| 13 | + let store; |
| 14 | + let vm; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + store = mockStore({ |
| 18 | + scratchGui: { |
| 19 | + projectState: {} |
| 20 | + } |
| 21 | + }); |
| 22 | + vm = new VM(); |
| 23 | + vm.attachAudioEngine = jest.fn(); |
| 24 | + vm.setCompatibilityMode = jest.fn(); |
| 25 | + vm.start = jest.fn(); |
| 26 | + }); |
| 27 | + test('when it mounts, the vm is initialized', () => { |
| 28 | + const Component = () => (<div />); |
| 29 | + const WrappedComponent = vmManagerHOC(Component); |
| 30 | + mount( |
| 31 | + <WrappedComponent |
| 32 | + store={store} |
| 33 | + vm={vm} |
| 34 | + /> |
| 35 | + ); |
| 36 | + expect(vm.attachAudioEngine.mock.calls.length).toBe(1); |
| 37 | + expect(vm.setCompatibilityMode.mock.calls.length).toBe(1); |
| 38 | + expect(vm.start.mock.calls.length).toBe(1); |
| 39 | + expect(vm.initialized).toBe(true); |
| 40 | + }); |
| 41 | + test('if it mounts with an initialized vm, it does not reinitialize the vm', () => { |
| 42 | + const Component = () => <div />; |
| 43 | + const WrappedComponent = vmManagerHOC(Component); |
| 44 | + vm.initialized = true; |
| 45 | + mount( |
| 46 | + <WrappedComponent |
| 47 | + store={store} |
| 48 | + vm={vm} |
| 49 | + /> |
| 50 | + ); |
| 51 | + expect(vm.attachAudioEngine.mock.calls.length).toBe(0); |
| 52 | + expect(vm.setCompatibilityMode.mock.calls.length).toBe(0); |
| 53 | + expect(vm.start.mock.calls.length).toBe(0); |
| 54 | + expect(vm.initialized).toBe(true); |
| 55 | + }); |
| 56 | + test('if the isLoadingWithId prop becomes true, it loads project data into the vm', () => { |
| 57 | + vm.loadProject = jest.fn(() => Promise.resolve()); |
| 58 | + const mockedOnLoadedProject = jest.fn(); |
| 59 | + const Component = () => <div />; |
| 60 | + const WrappedComponent = vmManagerHOC(Component); |
| 61 | + const mounted = mount( |
| 62 | + <WrappedComponent |
| 63 | + isLoadingWithId={false} |
| 64 | + store={store} |
| 65 | + vm={vm} |
| 66 | + onLoadedProject={mockedOnLoadedProject} |
| 67 | + /> |
| 68 | + ); |
| 69 | + mounted.setProps({ |
| 70 | + isLoadingWithId: true, |
| 71 | + loadingState: LoadingState.LOADING_VM_WITH_ID, |
| 72 | + projectData: '100' |
| 73 | + }); |
| 74 | + expect(vm.loadProject).toHaveBeenLastCalledWith('100'); |
| 75 | + // nextTick needed since vm.loadProject is async, and we have to wait for it :/ |
| 76 | + process.nextTick(() => expect(mockedOnLoadedProject).toHaveBeenLastCalledWith(LoadingState.LOADING_VM_WITH_ID)); |
| 77 | + }); |
| 78 | + test('if there is projectData, the child is rendered', () => { |
| 79 | + const Component = () => <div />; |
| 80 | + const WrappedComponent = vmManagerHOC(Component); |
| 81 | + const mounted = mount( |
| 82 | + <WrappedComponent |
| 83 | + projectData="100" |
| 84 | + store={store} |
| 85 | + vm={vm} |
| 86 | + /> |
| 87 | + ); |
| 88 | + expect(mounted.find('div').length).toBe(1); |
| 89 | + }); |
| 90 | + test('if there is no projectData, nothing is rendered', () => { |
| 91 | + const Component = () => <div />; |
| 92 | + const WrappedComponent = vmManagerHOC(Component); |
| 93 | + const mounted = mount( |
| 94 | + <WrappedComponent |
| 95 | + store={store} |
| 96 | + vm={vm} |
| 97 | + /> |
| 98 | + ); |
| 99 | + expect(mounted.find('div').length).toBe(0); |
| 100 | + }); |
| 101 | +}); |
0 commit comments