2021-04-20 18:09:37 +00:00
|
|
|
import { GlButton } from '@gitlab/ui';
|
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2021-07-29 09:08:46 +00:00
|
|
|
import EditorStateObserver from '~/content_editor/components/editor_state_observer.vue';
|
2021-04-20 18:09:37 +00:00
|
|
|
import ToolbarButton from '~/content_editor/components/toolbar_button.vue';
|
2021-07-29 09:08:46 +00:00
|
|
|
import { createTestEditor, mockChainedCommands, emitEditorEvent } from '../test_utils';
|
2021-04-20 18:09:37 +00:00
|
|
|
|
|
|
|
describe('content_editor/components/toolbar_button', () => {
|
|
|
|
let wrapper;
|
2021-04-29 00:09:48 +00:00
|
|
|
let tiptapEditor;
|
2021-04-20 18:09:37 +00:00
|
|
|
const CONTENT_TYPE = 'bold';
|
|
|
|
const ICON_NAME = 'bold';
|
|
|
|
const LABEL = 'Bold';
|
|
|
|
|
|
|
|
const buildEditor = () => {
|
2021-06-21 15:07:30 +00:00
|
|
|
tiptapEditor = createTestEditor();
|
2021-04-29 00:09:48 +00:00
|
|
|
|
|
|
|
jest.spyOn(tiptapEditor, 'isActive');
|
2021-04-20 18:09:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const buildWrapper = (propsData = {}) => {
|
|
|
|
wrapper = shallowMount(ToolbarButton, {
|
|
|
|
stubs: {
|
|
|
|
GlButton,
|
2021-07-29 09:08:46 +00:00
|
|
|
EditorStateObserver,
|
2021-04-20 18:09:37 +00:00
|
|
|
},
|
2021-07-29 09:08:46 +00:00
|
|
|
provide: {
|
2021-04-29 00:09:48 +00:00
|
|
|
tiptapEditor,
|
2021-07-29 09:08:46 +00:00
|
|
|
},
|
|
|
|
propsData: {
|
2021-04-20 18:09:37 +00:00
|
|
|
contentType: CONTENT_TYPE,
|
|
|
|
iconName: ICON_NAME,
|
|
|
|
label: LABEL,
|
|
|
|
...propsData,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const findButton = () => wrapper.findComponent(GlButton);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
buildEditor();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays tertiary, small button with a provided label and icon', () => {
|
|
|
|
buildWrapper();
|
|
|
|
|
|
|
|
expect(findButton().html()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2021-08-06 15:10:05 +00:00
|
|
|
it('allows customizing the variant, category, size of the button', () => {
|
|
|
|
const variant = 'danger';
|
|
|
|
const category = 'secondary';
|
|
|
|
const size = 'medium';
|
|
|
|
|
|
|
|
buildWrapper({
|
|
|
|
variant,
|
|
|
|
category,
|
|
|
|
size,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(findButton().props()).toMatchObject({
|
|
|
|
variant,
|
|
|
|
category,
|
|
|
|
size,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-20 18:09:37 +00:00
|
|
|
it.each`
|
2021-04-29 00:09:48 +00:00
|
|
|
editorState | outcomeDescription | outcome
|
|
|
|
${{ isActive: true, isFocused: true }} | ${'button is active'} | ${true}
|
|
|
|
${{ isActive: false, isFocused: true }} | ${'button is not active'} | ${false}
|
|
|
|
${{ isActive: true, isFocused: false }} | ${'button is not active '} | ${false}
|
2021-07-29 09:08:46 +00:00
|
|
|
`(
|
|
|
|
'$outcomeDescription when when editor state is $editorState',
|
|
|
|
async ({ editorState, outcome }) => {
|
|
|
|
tiptapEditor.isActive.mockReturnValueOnce(editorState.isActive);
|
|
|
|
tiptapEditor.isFocused = editorState.isFocused;
|
2021-04-20 18:09:37 +00:00
|
|
|
|
2021-07-29 09:08:46 +00:00
|
|
|
buildWrapper();
|
|
|
|
|
|
|
|
await emitEditorEvent({ event: 'transaction', tiptapEditor });
|
|
|
|
|
|
|
|
expect(findButton().classes().includes('active')).toBe(outcome);
|
|
|
|
expect(tiptapEditor.isActive).toHaveBeenCalledWith(CONTENT_TYPE);
|
|
|
|
},
|
|
|
|
);
|
2021-04-20 18:09:37 +00:00
|
|
|
|
|
|
|
describe('when button is clicked', () => {
|
|
|
|
it('executes the content type command when executeCommand = true', async () => {
|
2021-06-21 15:07:30 +00:00
|
|
|
const editorCommand = 'toggleFoo';
|
|
|
|
const mockCommands = mockChainedCommands(tiptapEditor, [editorCommand, 'focus', 'run']);
|
|
|
|
|
|
|
|
buildWrapper({ editorCommand });
|
2021-04-20 18:09:37 +00:00
|
|
|
|
|
|
|
await findButton().trigger('click');
|
|
|
|
|
2021-06-21 15:07:30 +00:00
|
|
|
expect(mockCommands[editorCommand]).toHaveBeenCalled();
|
|
|
|
expect(mockCommands.focus).toHaveBeenCalled();
|
|
|
|
expect(mockCommands.run).toHaveBeenCalled();
|
2021-05-07 15:10:39 +00:00
|
|
|
expect(wrapper.emitted().execute).toHaveLength(1);
|
2021-04-20 18:09:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not executes the content type command when executeCommand = false', async () => {
|
2021-06-21 15:07:30 +00:00
|
|
|
const editorCommand = 'toggleFoo';
|
|
|
|
const mockCommands = mockChainedCommands(tiptapEditor, [editorCommand, 'run']);
|
|
|
|
|
2021-04-29 00:09:48 +00:00
|
|
|
buildWrapper();
|
2021-04-20 18:09:37 +00:00
|
|
|
|
|
|
|
await findButton().trigger('click');
|
|
|
|
|
2021-06-21 15:07:30 +00:00
|
|
|
expect(mockCommands[editorCommand]).not.toHaveBeenCalled();
|
2021-05-07 15:10:39 +00:00
|
|
|
expect(wrapper.emitted().execute).toHaveLength(1);
|
2021-04-20 18:09:37 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|