2021-05-26 18:10:52 +00:00
|
|
|
|
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
|
|
|
|
|
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
|
2021-07-29 09:08:46 +00:00
|
|
|
|
import EditorStateObserver from '~/content_editor/components/editor_state_observer.vue';
|
2021-05-26 18:10:52 +00:00
|
|
|
|
import ToolbarTextStyleDropdown from '~/content_editor/components/toolbar_text_style_dropdown.vue';
|
|
|
|
|
import { TEXT_STYLE_DROPDOWN_ITEMS } from '~/content_editor/constants';
|
2021-07-27 21:09:42 +00:00
|
|
|
|
import Heading from '~/content_editor/extensions/heading';
|
2022-02-24 15:15:02 +00:00
|
|
|
|
import eventHubFactory from '~/helpers/event_hub_factory';
|
2021-07-29 09:08:46 +00:00
|
|
|
|
import { createTestEditor, mockChainedCommands, emitEditorEvent } from '../test_utils';
|
2021-05-26 18:10:52 +00:00
|
|
|
|
|
2021-07-29 09:08:46 +00:00
|
|
|
|
describe('content_editor/components/toolbar_text_style_dropdown', () => {
|
2021-05-26 18:10:52 +00:00
|
|
|
|
let wrapper;
|
|
|
|
|
let tiptapEditor;
|
|
|
|
|
|
|
|
|
|
const buildEditor = () => {
|
|
|
|
|
tiptapEditor = createTestEditor({
|
2021-06-21 15:07:30 +00:00
|
|
|
|
extensions: [Heading],
|
2021-05-26 18:10:52 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
jest.spyOn(tiptapEditor, 'isActive');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const buildWrapper = (propsData = {}) => {
|
|
|
|
|
wrapper = shallowMountExtended(ToolbarTextStyleDropdown, {
|
|
|
|
|
stubs: {
|
|
|
|
|
GlDropdown,
|
|
|
|
|
GlDropdownItem,
|
2021-07-29 09:08:46 +00:00
|
|
|
|
EditorStateObserver,
|
2021-05-26 18:10:52 +00:00
|
|
|
|
},
|
2021-07-29 09:08:46 +00:00
|
|
|
|
provide: {
|
2021-05-26 18:10:52 +00:00
|
|
|
|
tiptapEditor,
|
2022-02-24 15:15:02 +00:00
|
|
|
|
eventHub: eventHubFactory(),
|
2021-07-29 09:08:46 +00:00
|
|
|
|
},
|
|
|
|
|
propsData: {
|
2021-05-26 18:10:52 +00:00
|
|
|
|
...propsData,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
const findDropdown = () => wrapper.findComponent(GlDropdown);
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
buildEditor();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
wrapper.destroy();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('renders all text styles as dropdown items', () => {
|
|
|
|
|
buildWrapper();
|
|
|
|
|
|
|
|
|
|
TEXT_STYLE_DROPDOWN_ITEMS.forEach((textStyle) => {
|
|
|
|
|
expect(wrapper.findByText(textStyle.label).exists()).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when there is an active item ', () => {
|
|
|
|
|
let activeTextStyle;
|
|
|
|
|
|
2021-07-29 09:08:46 +00:00
|
|
|
|
beforeEach(async () => {
|
2021-05-26 18:10:52 +00:00
|
|
|
|
[, activeTextStyle] = TEXT_STYLE_DROPDOWN_ITEMS;
|
|
|
|
|
|
|
|
|
|
tiptapEditor.isActive.mockImplementation(
|
|
|
|
|
(contentType, params) =>
|
|
|
|
|
activeTextStyle.contentType === contentType && activeTextStyle.commandParams === params,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
buildWrapper();
|
2021-07-29 09:08:46 +00:00
|
|
|
|
await emitEditorEvent({ event: 'transaction', tiptapEditor });
|
2021-05-26 18:10:52 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('displays the active text style label as the dropdown toggle text ', () => {
|
|
|
|
|
expect(findDropdown().props().text).toBe(activeTextStyle.label);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sets dropdown as enabled', () => {
|
|
|
|
|
expect(findDropdown().props().disabled).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sets active item as active', () => {
|
|
|
|
|
const activeItem = wrapper
|
|
|
|
|
.findAllComponents(GlDropdownItem)
|
|
|
|
|
.filter((item) => item.text() === activeTextStyle.label)
|
|
|
|
|
.at(0);
|
|
|
|
|
expect(activeItem.props().isChecked).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when there isn’t an active item', () => {
|
2021-07-29 09:08:46 +00:00
|
|
|
|
beforeEach(async () => {
|
2021-05-26 18:10:52 +00:00
|
|
|
|
tiptapEditor.isActive.mockReturnValue(false);
|
|
|
|
|
buildWrapper();
|
2021-07-29 09:08:46 +00:00
|
|
|
|
await emitEditorEvent({ event: 'transaction', tiptapEditor });
|
2021-05-26 18:10:52 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sets dropdown as disabled', () => {
|
|
|
|
|
expect(findDropdown().props().disabled).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sets dropdown toggle text to Text style', () => {
|
|
|
|
|
expect(findDropdown().props().text).toBe('Text style');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when a text style is selected', () => {
|
|
|
|
|
it('executes the tiptap command related to that text style', () => {
|
|
|
|
|
buildWrapper();
|
|
|
|
|
|
|
|
|
|
TEXT_STYLE_DROPDOWN_ITEMS.forEach((textStyle, index) => {
|
|
|
|
|
const { editorCommand, commandParams } = textStyle;
|
2021-06-21 15:07:30 +00:00
|
|
|
|
const commands = mockChainedCommands(tiptapEditor, [editorCommand, 'focus', 'run']);
|
2021-05-26 18:10:52 +00:00
|
|
|
|
|
|
|
|
|
wrapper.findAllComponents(GlDropdownItem).at(index).vm.$emit('click');
|
2021-06-21 15:07:30 +00:00
|
|
|
|
expect(commands[editorCommand]).toHaveBeenCalledWith(commandParams || {});
|
|
|
|
|
expect(commands.focus).toHaveBeenCalled();
|
|
|
|
|
expect(commands.run).toHaveBeenCalled();
|
2021-05-26 18:10:52 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('emits execute event with contentType and value params that indicates the heading level', () => {
|
|
|
|
|
TEXT_STYLE_DROPDOWN_ITEMS.forEach((textStyle, index) => {
|
|
|
|
|
buildWrapper();
|
|
|
|
|
const { contentType, commandParams } = textStyle;
|
|
|
|
|
|
|
|
|
|
wrapper.findAllComponents(GlDropdownItem).at(index).vm.$emit('click');
|
|
|
|
|
expect(wrapper.emitted('execute')).toEqual([
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
contentType,
|
|
|
|
|
value: commandParams?.level,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
wrapper.destroy();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|