diff options
Diffstat (limited to 'spec/frontend/blob/components/blob_edit_content_spec.js')
-rw-r--r-- | spec/frontend/blob/components/blob_edit_content_spec.js | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/spec/frontend/blob/components/blob_edit_content_spec.js b/spec/frontend/blob/components/blob_edit_content_spec.js index 971ef72521d..3cc210e972c 100644 --- a/spec/frontend/blob/components/blob_edit_content_spec.js +++ b/spec/frontend/blob/components/blob_edit_content_spec.js @@ -1,28 +1,31 @@ import { shallowMount } from '@vue/test-utils'; -import BlobEditContent from '~/blob/components/blob_edit_content.vue'; -import { initEditorLite } from '~/blob/utils'; import { nextTick } from 'vue'; +import BlobEditContent from '~/blob/components/blob_edit_content.vue'; +import * as utils from '~/blob/utils'; +import Editor from '~/editor/editor_lite'; -jest.mock('~/blob/utils', () => ({ - initEditorLite: jest.fn(), -})); +jest.mock('~/editor/editor_lite'); describe('Blob Header Editing', () => { let wrapper; const value = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; const fileName = 'lorem.txt'; + const fileGlobalId = 'snippet_777'; function createComponent(props = {}) { wrapper = shallowMount(BlobEditContent, { propsData: { value, fileName, + fileGlobalId, ...props, }, }); } beforeEach(() => { + jest.spyOn(utils, 'initEditorLite'); + createComponent(); }); @@ -30,6 +33,15 @@ describe('Blob Header Editing', () => { wrapper.destroy(); }); + const triggerChangeContent = val => { + jest.spyOn(Editor.prototype, 'getValue').mockReturnValue(val); + const [cb] = Editor.prototype.onChangeContent.mock.calls[0]; + + cb(); + + jest.runOnlyPendingTimers(); + }; + describe('rendering', () => { it('matches the snapshot', () => { expect(wrapper.element).toMatchSnapshot(); @@ -51,18 +63,15 @@ describe('Blob Header Editing', () => { it('initialises Editor Lite', () => { const el = wrapper.find({ ref: 'editor' }).element; - expect(initEditorLite).toHaveBeenCalledWith({ + expect(utils.initEditorLite).toHaveBeenCalledWith({ el, blobPath: fileName, + blobGlobalId: fileGlobalId, blobContent: value, }); }); it('reacts to the changes in fileName', () => { - wrapper.vm.editor = { - updateModelLanguage: jest.fn(), - }; - const newFileName = 'ipsum.txt'; wrapper.setProps({ @@ -70,21 +79,20 @@ describe('Blob Header Editing', () => { }); return nextTick().then(() => { - expect(wrapper.vm.editor.updateModelLanguage).toHaveBeenCalledWith(newFileName); + expect(Editor.prototype.updateModelLanguage).toHaveBeenCalledWith(newFileName); }); }); + it('registers callback with editor onChangeContent', () => { + expect(Editor.prototype.onChangeContent).toHaveBeenCalledWith(expect.any(Function)); + }); + it('emits input event when the blob content is changed', () => { - const editorEl = wrapper.find({ ref: 'editor' }); - wrapper.vm.editor = { - getValue: jest.fn().mockReturnValue(value), - }; + expect(wrapper.emitted().input).toBeUndefined(); - editorEl.trigger('keyup'); + triggerChangeContent(value); - return nextTick().then(() => { - expect(wrapper.emitted().input[0]).toEqual([value]); - }); + expect(wrapper.emitted().input).toEqual([[value]]); }); }); }); |