|
| 1 | +import { config } from '../src/app/config-handler'; |
| 2 | +import { mainEvents } from '../src/app/main-event-handler'; |
| 3 | +import { |
| 4 | + DEFAULT_MINI_VIEW_WINDOW_WIDTH, |
| 5 | + MINI_VIEW_THRESHOLD_WINDOW_WIDTH, |
| 6 | + miniViewHandler, |
| 7 | +} from '../src/app/mini-view-handler'; |
| 8 | +import { windowHandler } from '../src/app/window-handler'; |
| 9 | + |
| 10 | +jest.mock('../src/app/config-handler', () => ({ |
| 11 | + config: { |
| 12 | + getUserConfigFields: jest.fn(), |
| 13 | + updateUserConfig: jest.fn(), |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +jest.mock('../src/app/main-event-handler', () => ({ |
| 18 | + mainEvents: { |
| 19 | + publish: jest.fn(), |
| 20 | + }, |
| 21 | +})); |
| 22 | + |
| 23 | +jest.mock('../src/app/window-handler', () => ({ |
| 24 | + windowHandler: { |
| 25 | + getMainWindow: jest.fn(), |
| 26 | + getMainWebContents: jest.fn(), |
| 27 | + setIsMiniViewEnabled: jest.fn(), |
| 28 | + setIsMiniViewFeatureEnabled: jest.fn(), |
| 29 | + setIsMiniViewTransition: jest.fn(), |
| 30 | + getIsMiniViewTransition: jest.fn(), |
| 31 | + }, |
| 32 | +})); |
| 33 | + |
| 34 | +jest.mock('../src/app/window-utils', () => { |
| 35 | + return { |
| 36 | + windowExists: jest.fn(() => true), |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +describe('MiniViewHandler', () => { |
| 41 | + let mockMainWindow: any; |
| 42 | + let mockMainWebContents: any; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + jest.clearAllMocks(); |
| 46 | + |
| 47 | + mockMainWindow = { |
| 48 | + setBounds: jest.fn(), |
| 49 | + getSize: jest.fn(() => [800, 600]), |
| 50 | + isFullScreen: jest.fn(() => false), |
| 51 | + isMaximized: jest.fn(() => false), |
| 52 | + setFullScreen: jest.fn(), |
| 53 | + unmaximize: jest.fn(), |
| 54 | + setSize: jest.fn(), |
| 55 | + setAlwaysOnTop: jest.fn(), |
| 56 | + }; |
| 57 | + |
| 58 | + mockMainWebContents = { |
| 59 | + send: jest.fn(), |
| 60 | + isDestroyed: jest.fn(() => false), |
| 61 | + }; |
| 62 | + |
| 63 | + (windowHandler.getMainWindow as jest.Mock).mockReturnValue(mockMainWindow); |
| 64 | + (windowHandler.getMainWebContents as jest.Mock).mockReturnValue( |
| 65 | + mockMainWebContents, |
| 66 | + ); |
| 67 | + (windowHandler.setIsMiniViewTransition as jest.Mock).mockImplementation(); |
| 68 | + (windowHandler.getIsMiniViewTransition as jest.Mock).mockImplementation( |
| 69 | + () => false, |
| 70 | + ); |
| 71 | + (config.getUserConfigFields as jest.Mock).mockReturnValue({}); |
| 72 | + (config.updateUserConfig as jest.Mock).mockResolvedValue(undefined); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('activateMiniView', () => { |
| 76 | + it('should set correct bounds when mainWinPosInMiniView exists', () => { |
| 77 | + (config.getUserConfigFields as jest.Mock).mockReturnValue({ |
| 78 | + mainWinPosInMiniView: { x: 10, y: 20, width: 500, height: 400 }, |
| 79 | + }); |
| 80 | + |
| 81 | + miniViewHandler.activateMiniView(); |
| 82 | + |
| 83 | + expect(mockMainWindow.setBounds).toHaveBeenCalledWith({ |
| 84 | + x: 10, |
| 85 | + y: 20, |
| 86 | + width: 500, |
| 87 | + height: 400, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should set default width and preserve height when mainWinPosInMiniView does not exist or has width > DEFAULT_MINI_VIEW_WINDOW_WIDTH', () => { |
| 92 | + (config.getUserConfigFields as jest.Mock).mockReturnValue({ |
| 93 | + mainWinPosInMiniView: { x: 10, y: 20, width: 700, height: 400 }, |
| 94 | + }); |
| 95 | + miniViewHandler.activateMiniView(); |
| 96 | + |
| 97 | + expect(mockMainWindow.setSize).toHaveBeenCalledWith( |
| 98 | + DEFAULT_MINI_VIEW_WINDOW_WIDTH, |
| 99 | + 600, |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should call setIsMiniViewTransition and notifyClient with true', (done) => { |
| 104 | + jest.useFakeTimers(); |
| 105 | + miniViewHandler.activateMiniView(); |
| 106 | + |
| 107 | + expect(windowHandler.setIsMiniViewTransition).toHaveBeenLastCalledWith( |
| 108 | + true, |
| 109 | + ); |
| 110 | + |
| 111 | + jest.runAllTimers(); |
| 112 | + expect(windowHandler.setIsMiniViewTransition).toHaveBeenLastCalledWith( |
| 113 | + false, |
| 114 | + ); |
| 115 | + expect(mockMainWebContents.send).toHaveBeenCalledWith( |
| 116 | + 'set-mini-view', |
| 117 | + true, |
| 118 | + ); |
| 119 | + done(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should set fullscreen to false if currently is in fullscreen', () => { |
| 123 | + (mockMainWindow.isFullScreen as jest.Mock).mockReturnValue(true); |
| 124 | + miniViewHandler.activateMiniView(); |
| 125 | + expect(mockMainWindow.setFullScreen).toHaveBeenCalledWith(false); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should call unmaximize if currently is maximized', () => { |
| 129 | + (mockMainWindow.isMaximized as jest.Mock).mockReturnValue(true); |
| 130 | + miniViewHandler.activateMiniView(); |
| 131 | + expect(mockMainWindow.unmaximize).toHaveBeenCalled(); |
| 132 | + expect(mainEvents.publish).toHaveBeenCalledWith('unmaximize'); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('deactivateMiniView', () => { |
| 137 | + it('should set correct bounds when mainWinPos exists and width > MINI_VIEW_THRESHOLD_WINDOW_WIDTH', () => { |
| 138 | + (config.getUserConfigFields as jest.Mock).mockReturnValue({ |
| 139 | + mainWinPos: { x: 10, y: 20, width: 800, height: 400 }, |
| 140 | + }); |
| 141 | + |
| 142 | + miniViewHandler.deactivateMiniView(); |
| 143 | + |
| 144 | + expect(mockMainWindow.setBounds).toHaveBeenCalledWith({ |
| 145 | + x: 10, |
| 146 | + y: 20, |
| 147 | + width: 800, |
| 148 | + height: 400, |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should set default width and preserve height when mainWinPos does not exist or has width <= MINI_VIEW_THRESHOLD_WINDOW_WIDTH', () => { |
| 153 | + (config.getUserConfigFields as jest.Mock).mockReturnValue({ |
| 154 | + mainWinPos: { x: 10, y: 20, width: 600, height: 400 }, |
| 155 | + }); |
| 156 | + miniViewHandler.deactivateMiniView(); |
| 157 | + |
| 158 | + expect(mockMainWindow.setSize).toHaveBeenCalledWith( |
| 159 | + MINI_VIEW_THRESHOLD_WINDOW_WIDTH, |
| 160 | + 600, |
| 161 | + ); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should call setIsMiniViewTransition and notifyClient with false', (done) => { |
| 165 | + jest.useFakeTimers(); |
| 166 | + miniViewHandler.deactivateMiniView(); |
| 167 | + |
| 168 | + expect(windowHandler.setIsMiniViewTransition).toHaveBeenLastCalledWith( |
| 169 | + true, |
| 170 | + ); |
| 171 | + jest.runAllTimers(); |
| 172 | + expect(windowHandler.setIsMiniViewTransition).toHaveBeenLastCalledWith( |
| 173 | + false, |
| 174 | + ); |
| 175 | + expect(mockMainWebContents.send).toHaveBeenCalledWith( |
| 176 | + 'set-mini-view', |
| 177 | + false, |
| 178 | + ); |
| 179 | + done(); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + describe('notifyClient', () => { |
| 184 | + it('should send set-mini-view message to main web contents', () => { |
| 185 | + miniViewHandler.notifyClient(true); |
| 186 | + |
| 187 | + expect(mockMainWebContents.send).toHaveBeenCalledWith( |
| 188 | + 'set-mini-view', |
| 189 | + true, |
| 190 | + ); |
| 191 | + |
| 192 | + miniViewHandler.notifyClient(false); |
| 193 | + expect(mockMainWebContents.send).toHaveBeenCalledWith( |
| 194 | + 'set-mini-view', |
| 195 | + false, |
| 196 | + ); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should not send set-mini-view message if main web contents is destroyed', () => { |
| 200 | + (mockMainWebContents.isDestroyed as jest.Mock).mockReturnValue(true); |
| 201 | + |
| 202 | + miniViewHandler.notifyClient(true); |
| 203 | + |
| 204 | + expect(mockMainWebContents.send).not.toHaveBeenCalled(); |
| 205 | + }); |
| 206 | + }); |
| 207 | +}); |
0 commit comments