From 34c27a0287c1b67047d5bdb4e8effe917c49940a Mon Sep 17 00:00:00 2001 From: Asli Aykan Date: Mon, 2 Dec 2024 02:56:08 +0100 Subject: [PATCH] refactor --- .../posting-markdown-editor.component.ts | 2 +- .../postings-markdown-editor.component.spec.ts | 11 +++++++++++ .../monaco-editor/monaco-editor.component.spec.ts | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/webapp/app/shared/metis/posting-markdown-editor/posting-markdown-editor.component.ts b/src/main/webapp/app/shared/metis/posting-markdown-editor/posting-markdown-editor.component.ts index 7de0a478e7ca..c176eed51aa0 100644 --- a/src/main/webapp/app/shared/metis/posting-markdown-editor/posting-markdown-editor.component.ts +++ b/src/main/webapp/app/shared/metis/posting-markdown-editor/posting-markdown-editor.component.ts @@ -126,7 +126,7 @@ export class PostingMarkdownEditorComponent implements OnInit, ControlValueAcces const editor = this.markdownEditor.monacoEditor; if (editor) { - editor.onDidChangeModelContent((event: { changes: string | any[] }) => { + editor.onDidChangeModelContent((event: monaco.editor.IModelContentChangedEvent) => { const position = editor.getPosition(); if (!position) { return; diff --git a/src/test/javascript/spec/component/shared/metis/postings-markdown-editor/postings-markdown-editor.component.spec.ts b/src/test/javascript/spec/component/shared/metis/postings-markdown-editor/postings-markdown-editor.component.spec.ts index a184d4438a2f..80ef5cf80a23 100644 --- a/src/test/javascript/spec/component/shared/metis/postings-markdown-editor/postings-markdown-editor.component.spec.ts +++ b/src/test/javascript/spec/component/shared/metis/postings-markdown-editor/postings-markdown-editor.component.spec.ts @@ -566,4 +566,15 @@ describe('PostingsMarkdownEditor', () => { expect(handleActionClickSpy).toHaveBeenCalledWith(expect.any(MouseEvent), bulletedListAction); }); + + it('should handle invalid line content gracefully', () => { + const mockModel = { + getLineContent: jest.fn().mockReturnValue(''), + } as unknown as monaco.editor.ITextModel; + const mockPosition = { lineNumber: 1 } as monaco.Position; + const handleActionClickSpy = jest.spyOn(component.markdownEditor, 'handleActionClick'); + + (component as any).handleKeyDown(mockModel, mockPosition.lineNumber); + expect(handleActionClickSpy).not.toHaveBeenCalled(); + }); }); diff --git a/src/test/javascript/spec/component/shared/monaco-editor/monaco-editor.component.spec.ts b/src/test/javascript/spec/component/shared/monaco-editor/monaco-editor.component.spec.ts index c611f6fbebf1..462ba750f969 100644 --- a/src/test/javascript/spec/component/shared/monaco-editor/monaco-editor.component.spec.ts +++ b/src/test/javascript/spec/component/shared/monaco-editor/monaco-editor.component.spec.ts @@ -313,4 +313,18 @@ describe('MonacoEditorComponent', () => { const lineContent = comp.getLineContent(2); expect(lineContent).toBe('static void main() {'); }); + + it('should handle invalid line numbers in getLineContent', () => { + fixture.detectChanges(); + comp.setText(multiLineText); + + // Invalid line numbers + expect(() => comp.getLineContent(0)).toThrow(); + expect(() => comp.getLineContent(-1)).toThrow(); + expect(() => comp.getLineContent(999)).toThrow(); + + // Empty line + comp.setText('line1\n\nline3'); + expect(comp.getLineContent(2)).toBe(''); + }); });