diff options
Diffstat (limited to 'spec/frontend/lib/utils/text_markdown_spec.js')
-rw-r--r-- | spec/frontend/lib/utils/text_markdown_spec.js | 108 |
1 files changed, 96 insertions, 12 deletions
diff --git a/spec/frontend/lib/utils/text_markdown_spec.js b/spec/frontend/lib/utils/text_markdown_spec.js index aca299aea0f..2e52958a828 100644 --- a/spec/frontend/lib/utils/text_markdown_spec.js +++ b/spec/frontend/lib/utils/text_markdown_spec.js @@ -232,19 +232,17 @@ describe('init markdown', () => { beforeEach(() => { editor = { - getSelectionRange: () => ({ + getSelectionRange: jest.fn().mockReturnValue({ start: 0, end: 0, }), - getValue: () => 'this is text \n in two lines', - insert: () => {}, - navigateLeft: () => {}, + getValue: jest.fn().mockReturnValue('this is text \n in two lines'), + insert: jest.fn(), + navigateLeft: jest.fn(), }; }); it('uses ace editor insert text when editor is passed in', () => { - jest.spyOn(editor, 'insert').mockReturnValue(); - insertMarkdownText({ text: editor.getValue, tag: '*', @@ -258,8 +256,6 @@ describe('init markdown', () => { }); it('adds block tags on line above and below selection', () => { - jest.spyOn(editor, 'insert').mockReturnValue(); - const selected = 'this text \n is multiple \n lines'; const text = `before \n ${selected} \n after`; @@ -276,8 +272,6 @@ describe('init markdown', () => { }); it('uses ace editor to navigate back tag length when nothing is selected', () => { - jest.spyOn(editor, 'navigateLeft').mockReturnValue(); - insertMarkdownText({ text: editor.getValue, tag: '*', @@ -291,8 +285,6 @@ describe('init markdown', () => { }); it('ace editor does not navigate back when there is selected text', () => { - jest.spyOn(editor, 'navigateLeft').mockReturnValue(); - insertMarkdownText({ text: editor.getValue, tag: '*', @@ -305,4 +297,96 @@ describe('init markdown', () => { expect(editor.navigateLeft).not.toHaveBeenCalled(); }); }); + + describe('Editor Lite', () => { + let editor; + let origGon; + + beforeEach(() => { + origGon = window.gon; + window.gon = { + features: { + monacoBlobs: true, + }, + }; + editor = { + getSelection: jest.fn().mockReturnValue({ + startLineNumber: 1, + startColumn: 1, + endLineNumber: 2, + endColumn: 2, + }), + getValue: jest.fn().mockReturnValue('this is text \n in two lines'), + selectWithinSelection: jest.fn(), + replaceSelectedText: jest.fn(), + moveCursor: jest.fn(), + }; + }); + + afterEach(() => { + window.gon = origGon; + }); + + it('replaces selected text', () => { + insertMarkdownText({ + text: editor.getValue, + tag: '*', + blockTag: null, + selected: '', + wrap: false, + editor, + }); + + expect(editor.replaceSelectedText).toHaveBeenCalled(); + }); + + it('adds block tags on line above and below selection', () => { + const selected = 'this text \n is multiple \n lines'; + const text = `before \n ${selected} \n after`; + + insertMarkdownText({ + text, + tag: '', + blockTag: '***', + selected, + wrap: true, + editor, + }); + + expect(editor.replaceSelectedText).toHaveBeenCalledWith(`***\n${selected}\n***\n`, undefined); + }); + + it('uses ace editor to navigate back tag length when nothing is selected', () => { + editor.getSelection = jest.fn().mockReturnValue({ + startLineNumber: 1, + startColumn: 1, + endLineNumber: 1, + endColumn: 1, + }); + + insertMarkdownText({ + text: editor.getValue, + tag: '*', + blockTag: null, + selected: '', + wrap: true, + editor, + }); + + expect(editor.moveCursor).toHaveBeenCalledWith(-1); + }); + + it('ace editor does not navigate back when there is selected text', () => { + insertMarkdownText({ + text: editor.getValue, + tag: '*', + blockTag: null, + selected: 'foobar', + wrap: true, + editor, + }); + + expect(editor.selectWithinSelection).not.toHaveBeenCalled(); + }); + }); }); |