|
| 1 | +import { cleanup } from '@testing-library/react'; |
| 2 | +import '@testing-library/jest-dom'; |
| 3 | +import { constants } from 'mo/services/builtinService/const'; |
| 4 | +import { container } from 'tsyringe'; |
| 5 | +import { MenuBarController, ActivityBarController } from 'mo/controller'; |
| 6 | +import { ActivityBarService, SettingsService } from 'mo/services'; |
| 7 | +import { MonacoService } from 'mo/monaco/monacoService'; |
| 8 | + |
| 9 | +const menuBarController = container.resolve(MenuBarController); |
| 10 | +const activityBarController = container.resolve(ActivityBarController); |
| 11 | +const activityBarService = container.resolve(ActivityBarService); |
| 12 | +const monacoService = container.resolve(MonacoService); |
| 13 | +const settingsService = container.resolve(SettingsService); |
| 14 | + |
| 15 | +describe('The ActivityBar Controller', () => { |
| 16 | + afterEach(cleanup); |
| 17 | + |
| 18 | + test('Should support to inject the default value', () => { |
| 19 | + activityBarController.initView(); |
| 20 | + expect(activityBarService.getState().data?.length).toBeGreaterThan(0); |
| 21 | + expect( |
| 22 | + activityBarService.getState().contextMenu?.length |
| 23 | + ).toBeGreaterThan(0); |
| 24 | + activityBarService.reset(); |
| 25 | + }); |
| 26 | + |
| 27 | + test('Should support to execute onContextMenuClick', () => { |
| 28 | + const mockFn = jest.fn(); |
| 29 | + const item = { id: constants.CONTEXT_MENU_MENU }; |
| 30 | + let originalFunc; |
| 31 | + |
| 32 | + // click menu item |
| 33 | + originalFunc = menuBarController.updateMenuBar; |
| 34 | + menuBarController.updateMenuBar = mockFn; |
| 35 | + activityBarController.onContextMenuClick({} as any, item); |
| 36 | + expect(mockFn).toBeCalled(); |
| 37 | + menuBarController.updateMenuBar = originalFunc; |
| 38 | + mockFn.mockClear(); |
| 39 | + |
| 40 | + // click explorer item |
| 41 | + item.id = constants.CONTEXT_MENU_EXPLORER; |
| 42 | + originalFunc = activityBarService.toggleBar; |
| 43 | + activityBarService.toggleBar = mockFn; |
| 44 | + activityBarController.onContextMenuClick({} as any, item); |
| 45 | + expect(mockFn).toBeCalled(); |
| 46 | + activityBarService.toggleBar = originalFunc; |
| 47 | + mockFn.mockClear(); |
| 48 | + |
| 49 | + // click search item |
| 50 | + item.id = constants.CONTEXT_MENU_SEARCH; |
| 51 | + originalFunc = activityBarService.toggleBar; |
| 52 | + activityBarService.toggleBar = mockFn; |
| 53 | + activityBarController.onContextMenuClick({} as any, item); |
| 54 | + expect(mockFn).toBeCalled(); |
| 55 | + activityBarService.toggleBar = originalFunc; |
| 56 | + mockFn.mockClear(); |
| 57 | + |
| 58 | + // click hide item |
| 59 | + item.id = constants.CONTEXT_MENU_HIDE; |
| 60 | + originalFunc = menuBarController.updateActivityBar; |
| 61 | + menuBarController.updateActivityBar = mockFn; |
| 62 | + activityBarController.onContextMenuClick({} as any, item); |
| 63 | + expect(mockFn).toBeCalled(); |
| 64 | + menuBarController.updateActivityBar = originalFunc; |
| 65 | + mockFn.mockClear(); |
| 66 | + |
| 67 | + // click command item |
| 68 | + item.id = constants.ACTION_QUICK_COMMAND; |
| 69 | + originalFunc = monacoService.commandService.executeCommand; |
| 70 | + monacoService.commandService.executeCommand = mockFn; |
| 71 | + activityBarController.onContextMenuClick({} as any, item); |
| 72 | + expect(mockFn).toBeCalled(); |
| 73 | + monacoService.commandService.executeCommand = originalFunc; |
| 74 | + mockFn.mockClear(); |
| 75 | + |
| 76 | + // click theme item |
| 77 | + item.id = constants.ACTION_SELECT_THEME; |
| 78 | + originalFunc = monacoService.commandService.executeCommand; |
| 79 | + monacoService.commandService.executeCommand = mockFn; |
| 80 | + activityBarController.onContextMenuClick({} as any, item); |
| 81 | + expect(mockFn).toBeCalled(); |
| 82 | + monacoService.commandService.executeCommand = originalFunc; |
| 83 | + mockFn.mockClear(); |
| 84 | + |
| 85 | + // click settings item |
| 86 | + item.id = constants.ACTION_QUICK_ACCESS_SETTINGS; |
| 87 | + originalFunc = settingsService.openSettingsInEditor; |
| 88 | + settingsService.openSettingsInEditor = mockFn; |
| 89 | + activityBarController.onContextMenuClick({} as any, item); |
| 90 | + expect(mockFn).toBeCalled(); |
| 91 | + settingsService.openSettingsInEditor = originalFunc; |
| 92 | + mockFn.mockClear(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments