diff options
Diffstat (limited to 'spec/frontend/blob_edit')
-rw-r--r-- | spec/frontend/blob_edit/blob_bundle_spec.js | 14 | ||||
-rw-r--r-- | spec/frontend/blob_edit/edit_blob_spec.js | 41 |
2 files changed, 45 insertions, 10 deletions
diff --git a/spec/frontend/blob_edit/blob_bundle_spec.js b/spec/frontend/blob_edit/blob_bundle_spec.js index 2c9ddfaf867..644539308c2 100644 --- a/spec/frontend/blob_edit/blob_bundle_spec.js +++ b/spec/frontend/blob_edit/blob_bundle_spec.js @@ -1,4 +1,5 @@ import $ from 'jquery'; +import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures'; import { mockTracking, unmockTracking } from 'helpers/tracking_helper'; import waitForPromises from 'helpers/wait_for_promises'; import blobBundle from '~/blob_edit/blob_bundle'; @@ -14,15 +15,17 @@ describe('BlobBundle', () => { }); it('loads SourceEditor for the edit screen', async () => { - setFixtures(`<div class="js-edit-blob-form"></div>`); + setHTMLFixture(`<div class="js-edit-blob-form"></div>`); blobBundle(); await waitForPromises(); expect(SourceEditor).toHaveBeenCalled(); + + resetHTMLFixture(); }); describe('No Suggest Popover', () => { beforeEach(() => { - setFixtures(` + setHTMLFixture(` <div class="js-edit-blob-form" data-blob-filename="blah"> <button class="js-commit-button"></button> <button id='cancel-changes'></button> @@ -31,6 +34,10 @@ describe('BlobBundle', () => { blobBundle(); }); + afterEach(() => { + resetHTMLFixture(); + }); + it('sets the window beforeunload listener to a function returning a string', () => { expect(window.onbeforeunload()).toBe(''); }); @@ -52,7 +59,7 @@ describe('BlobBundle', () => { let trackingSpy; beforeEach(() => { - setFixtures(` + setHTMLFixture(` <div class="js-edit-blob-form" data-blob-filename="blah" id="target"> <div class="js-suggest-gitlab-ci-yml" data-target="#target" @@ -73,6 +80,7 @@ describe('BlobBundle', () => { afterEach(() => { unmockTracking(); + resetHTMLFixture(); }); it('sends a tracking event when the commit button is clicked', () => { diff --git a/spec/frontend/blob_edit/edit_blob_spec.js b/spec/frontend/blob_edit/edit_blob_spec.js index 9c974e79e6e..c031cae11df 100644 --- a/spec/frontend/blob_edit/edit_blob_spec.js +++ b/spec/frontend/blob_edit/edit_blob_spec.js @@ -1,9 +1,12 @@ +import { Emitter } from 'monaco-editor'; +import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures'; import waitForPromises from 'helpers/wait_for_promises'; import EditBlob from '~/blob_edit/edit_blob'; import { SourceEditorExtension } from '~/editor/extensions/source_editor_extension_base'; import { FileTemplateExtension } from '~/editor/extensions/source_editor_file_template_ext'; import { EditorMarkdownExtension } from '~/editor/extensions/source_editor_markdown_ext'; import { EditorMarkdownPreviewExtension } from '~/editor/extensions/source_editor_markdown_livepreview_ext'; +import { ToolbarExtension } from '~/editor/extensions/source_editor_toolbar_ext'; import SourceEditor from '~/editor/source_editor'; jest.mock('~/editor/source_editor'); @@ -11,11 +14,13 @@ jest.mock('~/editor/extensions/source_editor_extension_base'); jest.mock('~/editor/extensions/source_editor_file_template_ext'); jest.mock('~/editor/extensions/source_editor_markdown_ext'); jest.mock('~/editor/extensions/source_editor_markdown_livepreview_ext'); +jest.mock('~/editor/extensions/source_editor_toolbar_ext'); const PREVIEW_MARKDOWN_PATH = '/foo/bar/preview_markdown'; const defaultExtensions = [ { definition: SourceEditorExtension }, { definition: FileTemplateExtension }, + { definition: ToolbarExtension }, ]; const markdownExtensions = [ { definition: EditorMarkdownExtension }, @@ -26,15 +31,20 @@ const markdownExtensions = [ ]; describe('Blob Editing', () => { - const useMock = jest.fn(); + let blobInstance; + const useMock = jest.fn(() => markdownExtensions); + const unuseMock = jest.fn(); + const emitter = new Emitter(); const mockInstance = { use: useMock, + unuse: unuseMock, setValue: jest.fn(), getValue: jest.fn().mockReturnValue('test value'), focus: jest.fn(), + onDidChangeModelLanguage: emitter.event, }; beforeEach(() => { - setFixtures(` + setHTMLFixture(` <form class="js-edit-blob-form"> <div id="file_path"></div> <div id="editor"></div> @@ -44,17 +54,18 @@ describe('Blob Editing', () => { jest.spyOn(SourceEditor.prototype, 'createInstance').mockReturnValue(mockInstance); }); afterEach(() => { - SourceEditorExtension.mockClear(); - EditorMarkdownExtension.mockClear(); - EditorMarkdownPreviewExtension.mockClear(); - FileTemplateExtension.mockClear(); + jest.clearAllMocks(); + unuseMock.mockClear(); + useMock.mockClear(); + resetHTMLFixture(); }); const editorInst = (isMarkdown) => { - return new EditBlob({ + blobInstance = new EditBlob({ isMarkdown, previewMarkdownPath: PREVIEW_MARKDOWN_PATH, }); + return blobInstance; }; const initEditor = async (isMarkdown = false) => { @@ -79,6 +90,22 @@ describe('Blob Editing', () => { expect(useMock).toHaveBeenCalledTimes(2); expect(useMock.mock.calls[1]).toEqual([markdownExtensions]); }); + + it('correctly handles switching from markdown and un-uses markdown extensions', async () => { + await initEditor(true); + expect(unuseMock).not.toHaveBeenCalled(); + await emitter.fire({ newLanguage: 'plaintext', oldLanguage: 'markdown' }); + expect(unuseMock).toHaveBeenCalledWith(markdownExtensions); + }); + + it('correctly handles switching from non-markdown to markdown extensions', async () => { + const mdSpy = jest.fn(); + await initEditor(); + blobInstance.fetchMarkdownExtension = mdSpy; + expect(mdSpy).not.toHaveBeenCalled(); + await emitter.fire({ newLanguage: 'markdown', oldLanguage: 'plaintext' }); + expect(mdSpy).toHaveBeenCalled(); + }); }); it('adds trailing newline to the blob content on submit', async () => { |