|
| 1 | +import 'reflect-metadata'; |
| 2 | +import { container } from 'tsyringe'; |
| 3 | +import { EditorService, StatusBarService, BuiltinService } from 'mo/services'; |
| 4 | +import { EditorController } from '../editor'; |
| 5 | +import { editor as MonacoEditor, IDisposable, Position } from 'mo/monaco'; |
| 6 | + |
| 7 | +const editorController = container.resolve(EditorController); |
| 8 | +const editorService = container.resolve(EditorService); |
| 9 | +const statusBarService = container.resolve(StatusBarService); |
| 10 | +const builtinService = container.resolve(BuiltinService); |
| 11 | + |
| 12 | +describe('The ediotr controller', () => { |
| 13 | + test('The initEditorEvents method', () => { |
| 14 | + const editorInstance = {} as MonacoEditor.IStandaloneCodeEditor; |
| 15 | + const position = { lineNumber: 1, column: 1 } as Position; |
| 16 | + let contentListener; |
| 17 | + let focusListener; |
| 18 | + let cursorListener; |
| 19 | + let blurListener; |
| 20 | + |
| 21 | + editorInstance.onDidChangeModelContent = jest.fn((listener) => { |
| 22 | + contentListener = listener; |
| 23 | + return {} as IDisposable; |
| 24 | + }); |
| 25 | + editorInstance.onDidFocusEditorText = jest.fn((listener) => { |
| 26 | + focusListener = listener; |
| 27 | + return {} as IDisposable; |
| 28 | + }); |
| 29 | + editorInstance.onDidChangeCursorSelection = jest.fn((listener) => { |
| 30 | + cursorListener = listener; |
| 31 | + return {} as IDisposable; |
| 32 | + }); |
| 33 | + editorInstance.onDidBlurEditorText = jest.fn((listener) => { |
| 34 | + blurListener = listener; |
| 35 | + return {} as IDisposable; |
| 36 | + }); |
| 37 | + editorInstance.getPosition = jest.fn(() => position); |
| 38 | + |
| 39 | + const testTab = { |
| 40 | + id: 'testTab', |
| 41 | + name: 'testTab', |
| 42 | + }; |
| 43 | + editorService.open(testTab); |
| 44 | + const { current } = editorService.getState(); |
| 45 | + editorController.initEditorEvents(editorInstance, current?.id!); |
| 46 | + |
| 47 | + // focus |
| 48 | + focusListener?.(); |
| 49 | + expect(editorService.getState().current?.tab?.id).toEqual(testTab.id); |
| 50 | + |
| 51 | + // change content |
| 52 | + editorInstance.getModel = jest.fn(() => { |
| 53 | + return { getValue: () => 'newValue' } as MonacoEditor.ITextModel; |
| 54 | + }); |
| 55 | + contentListener?.(); |
| 56 | + expect(editorService.getState().current?.tab?.data?.value).toEqual( |
| 57 | + 'newValue' |
| 58 | + ); |
| 59 | + |
| 60 | + // change cursor |
| 61 | + const { STATUS_EDITOR_INFO } = builtinService.getModules(); |
| 62 | + statusBarService.setState({ rightItems: [STATUS_EDITOR_INFO] }); |
| 63 | + cursorListener?.(); |
| 64 | + expect(statusBarService.getState().rightItems[0]?.data).toEqual({ |
| 65 | + ln: 1, |
| 66 | + col: 1, |
| 67 | + }); |
| 68 | + |
| 69 | + // blur |
| 70 | + const viewState = { |
| 71 | + viewState: { scrollTop: 10 }, |
| 72 | + } as MonacoEditor.ICodeEditorViewState; |
| 73 | + editorInstance.saveViewState = jest.fn(() => viewState); |
| 74 | + blurListener?.(); |
| 75 | + expect( |
| 76 | + editorController.getViewState(current?.tab?.id?.toString()!) |
| 77 | + ).toEqual(viewState); |
| 78 | + |
| 79 | + // reset services |
| 80 | + statusBarService.reset(); |
| 81 | + editorService.setState({ current: null, groups: [] }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments