|
| 1 | +const PlanetInterface = require('../planetInterface'); |
| 2 | +global.platformColor = { |
| 3 | + header: '#8bc34a' |
| 4 | +}; |
| 5 | + |
| 6 | +const mockActivity = { |
| 7 | + hideSearchWidget: jest.fn(), |
| 8 | + prepSearchWidget: jest.fn(), |
| 9 | + sendAllToTrash: jest.fn(), |
| 10 | + refreshCanvas: jest.fn(), |
| 11 | + _loadStart: jest.fn(), |
| 12 | + doLoadAnimation: jest.fn(), |
| 13 | + textMsg: jest.fn(), |
| 14 | + stage: { enableDOMEvents: jest.fn() }, |
| 15 | + blocks: { loadNewBlocks: jest.fn(), palettes: { _hideMenus: jest.fn() }, trashStacks: [] }, |
| 16 | + logo: { doStopTurtles: jest.fn() }, |
| 17 | + canvas: {}, |
| 18 | + turtles: {}, |
| 19 | + loading: false, |
| 20 | + prepareExport: jest.fn(), |
| 21 | + _allClear: jest.fn() |
| 22 | +}; |
| 23 | + |
| 24 | +document.body.innerHTML = ` |
| 25 | + <div id="helpElem"></div> |
| 26 | + <div class="canvasHolder"></div> |
| 27 | + <div id="canvas"></div> |
| 28 | + <meta id="theme-color"> |
| 29 | + <div id="toolbars"></div> |
| 30 | + <div id="palette"></div> |
| 31 | + <div id="buttoncontainerBOTTOM"></div> |
| 32 | + <div id="buttoncontainerTOP"></div> |
| 33 | + <canvas id="overlayCanvas"></canvas> |
| 34 | + <iframe id="planet-iframe"></iframe> |
| 35 | + <input id="myOpenFile" type="file"> |
| 36 | +`; |
| 37 | + |
| 38 | +const docById = jest.fn((id) => document.getElementById(id)); |
| 39 | +global.docById = docById; |
| 40 | + |
| 41 | +beforeAll(() => { |
| 42 | + mockCanvas = { |
| 43 | + click: jest.fn() |
| 44 | + }; |
| 45 | + window.widgetWindows = { |
| 46 | + hideAllWindows: jest.fn(), |
| 47 | + showWindows: jest.fn(), |
| 48 | + }; |
| 49 | + window.scroll = jest.fn(); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('PlanetInterface', () => { |
| 53 | + let planetInterface; |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + planetInterface = new PlanetInterface(mockActivity); |
| 57 | + }); |
| 58 | + |
| 59 | + test('hideMusicBlocks hides relevant elements and disables DOM events', () => { |
| 60 | + planetInterface.hideMusicBlocks(); |
| 61 | + |
| 62 | + expect(mockActivity.hideSearchWidget).toHaveBeenCalled(); |
| 63 | + expect(mockActivity.logo.doStopTurtles).toHaveBeenCalled(); |
| 64 | + expect(docById('helpElem').style.visibility).toBe('hidden'); |
| 65 | + expect(document.querySelector('.canvasHolder').classList.contains('hide')).toBe(true); |
| 66 | + expect(document.querySelector('#canvas').style.display).toBe('none'); |
| 67 | + expect(document.querySelector('#theme-color').content).toBe('#8bc34a'); |
| 68 | + }); |
| 69 | + |
| 70 | + test('showMusicBlocks shows relevant elements and enables DOM events', () => { |
| 71 | + mockActivity.planet = { getCurrentProjectName: jest.fn(() => 'Test Project') }; |
| 72 | + |
| 73 | + planetInterface.showMusicBlocks(); |
| 74 | + |
| 75 | + expect(document.title).toBe('Test Project'); |
| 76 | + expect(docById('toolbars').style.display).toBe('block'); |
| 77 | + expect(docById('palette').style.display).toBe('block'); |
| 78 | + expect(mockActivity.prepSearchWidget).toHaveBeenCalled(); |
| 79 | + expect(document.querySelector('.canvasHolder').classList.contains('hide')).toBe(false); |
| 80 | + expect(document.querySelector('#canvas').style.display).toBe(''); |
| 81 | + }); |
| 82 | + |
| 83 | + test('hidePlanet hides the planet interface', () => { |
| 84 | + planetInterface.iframe = document.querySelector('#planet-iframe'); |
| 85 | + planetInterface.hidePlanet(); |
| 86 | + expect(planetInterface.iframe.style.display).toBe('none'); |
| 87 | + }); |
| 88 | + |
| 89 | + test('openPlanet calls saveLocally, hideMusicBlocks, and showPlanet', () => { |
| 90 | + jest.spyOn(planetInterface, 'saveLocally').mockImplementation(() => {}); |
| 91 | + jest.spyOn(planetInterface, 'hideMusicBlocks').mockImplementation(() => {}); |
| 92 | + jest.spyOn(planetInterface, 'showPlanet').mockImplementation(() => {}); |
| 93 | + planetInterface.openPlanet(); |
| 94 | + expect(planetInterface.saveLocally).toHaveBeenCalled(); |
| 95 | + expect(planetInterface.hideMusicBlocks).toHaveBeenCalled(); |
| 96 | + expect(planetInterface.showPlanet).toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + |
| 99 | + test('closePlanet calls hidePlanet and showMusicBlocks', () => { |
| 100 | + jest.spyOn(planetInterface, 'hidePlanet').mockImplementation(() => {}); |
| 101 | + jest.spyOn(planetInterface, 'showMusicBlocks').mockImplementation(() => {}); |
| 102 | + planetInterface.closePlanet(); |
| 103 | + expect(planetInterface.hidePlanet).toHaveBeenCalled(); |
| 104 | + expect(planetInterface.showMusicBlocks).toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + test('newProject calls closePlanet, initialiseNewProject, _loadStart, and saveLocally', () => { |
| 108 | + jest.spyOn(planetInterface, 'closePlanet').mockImplementation(() => {}); |
| 109 | + jest.spyOn(planetInterface, 'initialiseNewProject').mockImplementation(() => {}); |
| 110 | + jest.spyOn(planetInterface, 'saveLocally').mockImplementation(() => {}); |
| 111 | + planetInterface.newProject(); |
| 112 | + expect(planetInterface.closePlanet).toHaveBeenCalled(); |
| 113 | + expect(planetInterface.initialiseNewProject).toHaveBeenCalled(); |
| 114 | + expect(mockActivity._loadStart).toHaveBeenCalled(); |
| 115 | + expect(planetInterface.saveLocally).toHaveBeenCalled(); |
| 116 | + }); |
| 117 | +}); |
0 commit comments