diff options
| author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-02-24 18:13:02 +0000 |
|---|---|---|
| committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-02-24 18:13:02 +0000 |
| commit | d48b87d4675d6b8b56dd9b40afa9eb2dce32ad3b (patch) | |
| tree | 768c3d0900d3ba2910adf6abb24f433b8585be6c /spec/frontend/editor | |
| parent | fd9a56d56f84b36779fc4db2da37204c22585fe4 (diff) | |
| download | gitlab-ce-d48b87d4675d6b8b56dd9b40afa9eb2dce32ad3b.tar.gz | |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/frontend/editor')
| -rw-r--r-- | spec/frontend/editor/source_editor_extension_base_spec.js | 112 |
1 files changed, 110 insertions, 2 deletions
diff --git a/spec/frontend/editor/source_editor_extension_base_spec.js b/spec/frontend/editor/source_editor_extension_base_spec.js index eab39ccaba1..b1b8173188c 100644 --- a/spec/frontend/editor/source_editor_extension_base_spec.js +++ b/spec/frontend/editor/source_editor_extension_base_spec.js @@ -7,6 +7,7 @@ import { EDITOR_TYPE_DIFF, EXTENSION_BASE_LINE_LINK_ANCHOR_CLASS, EXTENSION_BASE_LINE_NUMBERS_CLASS, + EXTENSION_SOFTWRAP_ID, } from '~/editor/constants'; import { SourceEditorExtension } from '~/editor/extensions/source_editor_extension_base'; import EditorInstance from '~/editor/source_editor_instance'; @@ -35,8 +36,18 @@ describe('The basis for an Source Editor extension', () => { }, }; }; - const createInstance = (baseInstance = {}) => { - return new EditorInstance(baseInstance); + const baseInstance = { + getOption: jest.fn(), + }; + + const createInstance = (base = baseInstance) => { + return new EditorInstance(base); + }; + + const toolbar = { + addItems: jest.fn(), + updateItem: jest.fn(), + removeItems: jest.fn(), }; beforeEach(() => { @@ -49,6 +60,66 @@ describe('The basis for an Source Editor extension', () => { resetHTMLFixture(); }); + describe('onSetup callback', () => { + let instance; + beforeEach(() => { + instance = createInstance(); + + instance.toolbar = toolbar; + }); + + it('adds correct buttons to the toolbar', () => { + instance.use({ definition: SourceEditorExtension }); + expect(instance.toolbar.addItems).toHaveBeenCalledWith([ + expect.objectContaining({ + id: EXTENSION_SOFTWRAP_ID, + }), + ]); + }); + + it('does not fail if toolbar is not available', () => { + instance.toolbar = null; + expect(() => instance.use({ definition: SourceEditorExtension })).not.toThrow(); + }); + + it.each` + optionValue | expectSelected + ${'on'} | ${true} + ${'off'} | ${false} + ${'foo'} | ${false} + ${undefined} | ${false} + ${null} | ${false} + `( + 'correctly sets the initial state of the button when wordWrap option is "$optionValue"', + ({ optionValue, expectSelected }) => { + instance.getOption.mockReturnValue(optionValue); + instance.use({ definition: SourceEditorExtension }); + expect(instance.toolbar.addItems).toHaveBeenCalledWith([ + expect.objectContaining({ + selected: expectSelected, + }), + ]); + }, + ); + }); + + describe('onBeforeUnuse', () => { + let instance; + let extension; + + beforeEach(() => { + instance = createInstance(); + + instance.toolbar = toolbar; + extension = instance.use({ definition: SourceEditorExtension }); + }); + it('removes the registered buttons from the toolbar', () => { + expect(instance.toolbar.removeItems).not.toHaveBeenCalled(); + instance.unuse(extension); + expect(instance.toolbar.removeItems).toHaveBeenCalledWith([EXTENSION_SOFTWRAP_ID]); + }); + }); + describe('onUse callback', () => { it('initializes the line highlighting', () => { const instance = createInstance(); @@ -66,6 +137,7 @@ describe('The basis for an Source Editor extension', () => { '$description the line linking for $instanceType instance', ({ instanceType, shouldBeCalled }) => { const instance = createInstance({ + ...baseInstance, getEditorType: jest.fn().mockReturnValue(instanceType), onMouseMove: jest.fn(), onMouseDown: jest.fn(), @@ -82,10 +154,44 @@ describe('The basis for an Source Editor extension', () => { ); }); + describe('toggleSoftwrap', () => { + let instance; + + beforeEach(() => { + instance = createInstance(); + + instance.toolbar = toolbar; + instance.use({ definition: SourceEditorExtension }); + }); + + it.each` + currentWordWrap | newWordWrap | expectSelected + ${'on'} | ${'off'} | ${false} + ${'off'} | ${'on'} | ${true} + ${'foo'} | ${'on'} | ${true} + ${undefined} | ${'on'} | ${true} + ${null} | ${'on'} | ${true} + `( + 'correctly updates wordWrap option in editor and the state of the button when currentWordWrap is "$currentWordWrap"', + ({ currentWordWrap, newWordWrap, expectSelected }) => { + instance.getOption.mockReturnValue(currentWordWrap); + instance.updateOptions = jest.fn(); + instance.toggleSoftwrap(); + expect(instance.updateOptions).toHaveBeenCalledWith({ + wordWrap: newWordWrap, + }); + expect(instance.toolbar.updateItem).toHaveBeenCalledWith(EXTENSION_SOFTWRAP_ID, { + selected: expectSelected, + }); + }, + ); + }); + describe('highlightLines', () => { const revealSpy = jest.fn(); const decorationsSpy = jest.fn(); const instance = createInstance({ + ...baseInstance, revealLineInCenter: revealSpy, deltaDecorations: decorationsSpy, }); @@ -174,6 +280,7 @@ describe('The basis for an Source Editor extension', () => { beforeEach(() => { instance = createInstance({ + ...baseInstance, deltaDecorations: decorationsSpy, lineDecorations, }); @@ -188,6 +295,7 @@ describe('The basis for an Source Editor extension', () => { describe('setupLineLinking', () => { const instance = { + ...baseInstance, onMouseMove: jest.fn(), onMouseDown: jest.fn(), deltaDecorations: jest.fn(), |
