|
| 1 | +import { EditorTreeEvent, ExplorerEvent, FileTypes } from 'mo/model'; |
| 2 | +import { |
| 3 | + ActivityBarService, |
| 4 | + SidebarService, |
| 5 | + ExplorerService, |
| 6 | + BuiltinService, |
| 7 | +} from 'mo/services'; |
| 8 | +import { constants, modules } from 'mo/services/builtinService/const'; |
| 9 | +import 'reflect-metadata'; |
| 10 | +import { container } from 'tsyringe'; |
| 11 | +import { ExplorerController, FolderTreeController } from '..'; |
| 12 | + |
| 13 | +const explorerController = container.resolve(ExplorerController); |
| 14 | + |
| 15 | +const activityBarService = container.resolve(ActivityBarService); |
| 16 | +const sidebarService = container.resolve(SidebarService); |
| 17 | +const explorerService = container.resolve(ExplorerService); |
| 18 | +const folderTreeController = container.resolve(FolderTreeController); |
| 19 | +const builtinService = container.resolve(BuiltinService); |
| 20 | + |
| 21 | +describe('The explorer controller', () => { |
| 22 | + test('Should support to inject the default value into service', () => { |
| 23 | + explorerController.initView(); |
| 24 | + |
| 25 | + const { headerToolBar, data } = explorerService.getState(); |
| 26 | + expect(headerToolBar.id).toBe(modules.builtInExplorerHeaderToolbar.id); |
| 27 | + expect(headerToolBar.icon).toBe( |
| 28 | + modules.builtInExplorerHeaderToolbar.icon |
| 29 | + ); |
| 30 | + expect(headerToolBar.title).toBe( |
| 31 | + modules.builtInExplorerHeaderToolbar.title |
| 32 | + ); |
| 33 | + expect(headerToolBar.contextMenu).toHaveLength(1); |
| 34 | + |
| 35 | + expect(data).toHaveLength(1); |
| 36 | + expect(data[0]).toEqual( |
| 37 | + expect.objectContaining(modules.builtInExplorerFolderPanel) |
| 38 | + ); |
| 39 | + |
| 40 | + const { |
| 41 | + data: activityBarData, |
| 42 | + selected, |
| 43 | + } = activityBarService.getState(); |
| 44 | + expect(activityBarData).toHaveLength(1); |
| 45 | + expect(activityBarData![0]).toEqual( |
| 46 | + expect.objectContaining(modules.builtInExplorerActivityItem) |
| 47 | + ); |
| 48 | + expect(selected).toBe(modules.builtInExplorerActivityItem.id); |
| 49 | + |
| 50 | + const { current, panes } = sidebarService.getState(); |
| 51 | + expect(panes).toHaveLength(1); |
| 52 | + expect(panes[0]).toEqual( |
| 53 | + expect.objectContaining({ |
| 54 | + id: constants.EXPLORER_ACTIVITY_ITEM, |
| 55 | + title: 'EXPLORER', |
| 56 | + }) |
| 57 | + ); |
| 58 | + expect(current).toBe(constants.EXPLORER_ACTIVITY_ITEM); |
| 59 | + |
| 60 | + explorerService.reset(); |
| 61 | + activityBarService.reset(); |
| 62 | + sidebarService.reset(); |
| 63 | + }); |
| 64 | + |
| 65 | + test('Should support to controll the default value by built-in service', () => { |
| 66 | + builtinService.inactiveModule('builtInExplorerActivityItem'); |
| 67 | + builtinService.inactiveModule('builtInExplorerFolderPanel'); |
| 68 | + builtinService.inactiveModule('builtInExplorerHeaderToolbar'); |
| 69 | + |
| 70 | + explorerController.initView(); |
| 71 | + const { data, headerToolBar } = explorerService.getState(); |
| 72 | + expect(headerToolBar).toEqual({}); |
| 73 | + expect(data).toHaveLength(0); |
| 74 | + |
| 75 | + const { |
| 76 | + data: activityBarData, |
| 77 | + selected, |
| 78 | + } = activityBarService.getState(); |
| 79 | + expect(activityBarData).toHaveLength(0); |
| 80 | + expect(selected).toBe(''); |
| 81 | + |
| 82 | + const { current, panes } = sidebarService.getState(); |
| 83 | + expect(panes).toHaveLength(0); |
| 84 | + expect(current).toBe(''); |
| 85 | + |
| 86 | + builtinService.reset(); |
| 87 | + }); |
| 88 | + |
| 89 | + test('Should support to emit the onClick method', () => { |
| 90 | + explorerController.initView(); |
| 91 | + |
| 92 | + const mockFn = jest.fn(); |
| 93 | + explorerService.onClick(mockFn); |
| 94 | + |
| 95 | + const mockEvent = {} as any; |
| 96 | + const mockItem = { id: 'test' }; |
| 97 | + explorerController.onClick(mockEvent, mockItem); |
| 98 | + |
| 99 | + expect(mockFn).toBeCalled(); |
| 100 | + expect(mockFn.mock.calls[0][0]).toBe(mockEvent); |
| 101 | + expect(mockFn.mock.calls[0][1]).toBe(mockItem); |
| 102 | + }); |
| 103 | + |
| 104 | + test('Should execute onActionsContextMenuClick', () => { |
| 105 | + const original = explorerService.togglePanel; |
| 106 | + const mockFn = jest.fn(); |
| 107 | + explorerService.togglePanel = mockFn; |
| 108 | + |
| 109 | + const mockItem = { id: 'test' }; |
| 110 | + explorerController.onActionsContextMenuClick({} as any, mockItem); |
| 111 | + expect(mockFn).toBeCalled(); |
| 112 | + expect(mockFn.mock.calls[0][0]).toBe(mockItem.id); |
| 113 | + |
| 114 | + explorerService.togglePanel = original; |
| 115 | + }); |
| 116 | + |
| 117 | + test('Should support to emit the onCollapseChange method', () => { |
| 118 | + const mockFn = jest.fn(); |
| 119 | + explorerController.subscribe(ExplorerEvent.onCollapseChange, mockFn); |
| 120 | + |
| 121 | + const mockKeys = [1, 2, 3]; |
| 122 | + explorerController.onCollapseChange(mockKeys); |
| 123 | + |
| 124 | + expect(mockFn).toBeCalled(); |
| 125 | + expect(mockFn.mock.calls[0][0]).toBe(mockKeys); |
| 126 | + }); |
| 127 | + |
| 128 | + test('Should support to execute the onToolbarClick method', () => { |
| 129 | + const original = folderTreeController.createTreeNode; |
| 130 | + const mockFn = jest.fn(); |
| 131 | + folderTreeController.createTreeNode = mockFn; |
| 132 | + |
| 133 | + const mockItem = { id: constants.NEW_FILE_COMMAND_ID }; |
| 134 | + const mockParentPanel = { id: 'test', name: 'test' }; |
| 135 | + explorerController.onToolbarClick(mockItem, mockParentPanel); |
| 136 | + |
| 137 | + expect(mockFn).toBeCalled(); |
| 138 | + expect(mockFn.mock.calls[0][0]).toBe(FileTypes.File); |
| 139 | + |
| 140 | + mockFn.mockClear(); |
| 141 | + |
| 142 | + mockItem.id = constants.NEW_FOLDER_COMMAND_ID; |
| 143 | + explorerController.onToolbarClick(mockItem, mockParentPanel); |
| 144 | + |
| 145 | + expect(mockFn).toBeCalled(); |
| 146 | + expect(mockFn.mock.calls[0][0]).toBe(FileTypes.Folder); |
| 147 | + |
| 148 | + folderTreeController.createTreeNode = original; |
| 149 | + }); |
| 150 | + |
| 151 | + test('Should support to execute the onToolbarClick method', () => { |
| 152 | + // REMOVE_COMMAND_ID |
| 153 | + const mockItem = { id: constants.REMOVE_COMMAND_ID }; |
| 154 | + const mockParentPanel = { id: 'test', name: 'test' }; |
| 155 | + |
| 156 | + const mockFn = jest.fn(); |
| 157 | + explorerService.onRemovePanel(mockFn); |
| 158 | + |
| 159 | + explorerController.onToolbarClick(mockItem, mockParentPanel); |
| 160 | + |
| 161 | + expect(mockFn).toBeCalled(); |
| 162 | + expect(mockFn.mock.calls[0][0]).toBe(mockParentPanel); |
| 163 | + |
| 164 | + // EXPLORER_TOGGLE_CLOSE_ALL_EDITORS |
| 165 | + mockItem.id = constants.EXPLORER_TOGGLE_CLOSE_ALL_EDITORS; |
| 166 | + mockFn.mockClear(); |
| 167 | + expect(mockFn).not.toBeCalled(); |
| 168 | + explorerController.subscribe(EditorTreeEvent.onCloseAll, mockFn); |
| 169 | + |
| 170 | + explorerController.onToolbarClick(mockItem, mockParentPanel); |
| 171 | + |
| 172 | + expect(mockFn).toBeCalled(); |
| 173 | + |
| 174 | + // EXPLORER_TOGGLE_SAVE_ALL |
| 175 | + mockItem.id = constants.EXPLORER_TOGGLE_SAVE_ALL; |
| 176 | + mockFn.mockClear(); |
| 177 | + expect(mockFn).not.toBeCalled(); |
| 178 | + explorerController.subscribe(EditorTreeEvent.onSaveAll, mockFn); |
| 179 | + |
| 180 | + explorerController.onToolbarClick(mockItem, mockParentPanel); |
| 181 | + |
| 182 | + expect(mockFn).toBeCalled(); |
| 183 | + |
| 184 | + // EXPLORER_TOGGLE_VERTICAL |
| 185 | + mockItem.id = constants.EXPLORER_TOGGLE_VERTICAL; |
| 186 | + mockFn.mockClear(); |
| 187 | + expect(mockFn).not.toBeCalled(); |
| 188 | + explorerController.subscribe( |
| 189 | + EditorTreeEvent.onSplitEditorLayout, |
| 190 | + mockFn |
| 191 | + ); |
| 192 | + |
| 193 | + explorerController.onToolbarClick(mockItem, mockParentPanel); |
| 194 | + |
| 195 | + expect(mockFn).toBeCalled(); |
| 196 | + |
| 197 | + // default |
| 198 | + mockItem.id = 'custom-id'; |
| 199 | + mockFn.mockClear(); |
| 200 | + expect(mockFn).not.toBeCalled(); |
| 201 | + explorerService.onPanelToolbarClick(mockFn); |
| 202 | + |
| 203 | + explorerController.onToolbarClick(mockItem, mockParentPanel); |
| 204 | + |
| 205 | + expect(mockFn).toBeCalled(); |
| 206 | + expect(mockFn.mock.calls[0][0]).toBe(mockParentPanel); |
| 207 | + expect(mockFn.mock.calls[0][1]).toBe(mockItem.id); |
| 208 | + }); |
| 209 | +}); |
0 commit comments