debian-mirror-gitlab/spec/frontend/shortcuts_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

152 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-06-23 00:09:42 +05:30
import $ from 'jquery';
2020-11-24 15:15:51 +05:30
import { flatten } from 'lodash';
2023-07-09 08:55:56 +05:30
import htmlSnippetsShow from 'test_fixtures/snippets/show.html';
import { Mousetrap } from '~/lib/mousetrap';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import Shortcuts, { LOCAL_MOUSETRAP_DATA_KEY } from '~/behaviors/shortcuts/shortcuts';
import MarkdownPreview from '~/behaviors/preview_markdown';
2020-11-24 15:15:51 +05:30
2020-06-23 00:09:42 +05:30
describe('Shortcuts', () => {
const createEvent = (type, target) =>
$.Event(type, {
target,
});
2023-07-09 08:55:56 +05:30
let shortcuts;
beforeAll(() => {
shortcuts = new Shortcuts();
});
2020-06-23 00:09:42 +05:30
2020-11-24 15:15:51 +05:30
beforeEach(() => {
2023-07-09 08:55:56 +05:30
setHTMLFixture(htmlSnippetsShow);
new Shortcuts(); // eslint-disable-line no-new
new MarkdownPreview(); // eslint-disable-line no-new
2020-06-23 00:09:42 +05:30
2022-01-26 12:08:38 +05:30
jest.spyOn(document.querySelector('#search'), 'focus');
2020-06-23 00:09:42 +05:30
2023-07-09 08:55:56 +05:30
jest.spyOn(Mousetrap.prototype, 'stopCallback');
jest.spyOn(Mousetrap.prototype, 'bind').mockImplementation();
jest.spyOn(Mousetrap.prototype, 'unbind').mockImplementation();
2020-11-24 15:15:51 +05:30
});
2020-06-23 00:09:42 +05:30
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
});
2020-11-24 15:15:51 +05:30
describe('markdown shortcuts', () => {
2023-07-09 08:55:56 +05:30
let shortcutElements;
2020-11-24 15:15:51 +05:30
beforeEach(() => {
// Get all shortcuts specified with md-shortcuts attributes in the fixture.
// `shortcuts` will look something like this:
// [
// [ 'mod+b' ],
// [ 'mod+i' ],
// [ 'mod+k' ]
// ]
2023-07-09 08:55:56 +05:30
shortcutElements = $('.edit-note .js-md')
2020-11-24 15:15:51 +05:30
.map(function getShortcutsFromToolbarBtn() {
const mdShortcuts = $(this).data('md-shortcuts');
// jQuery.map() automatically unwraps arrays, so we
2021-11-11 11:23:49 +05:30
// have to double wrap the array to counteract this
2020-11-24 15:15:51 +05:30
return mdShortcuts ? [mdShortcuts] : undefined;
})
.get();
});
describe('initMarkdownEditorShortcuts', () => {
2023-07-09 08:55:56 +05:30
let $textarea;
let localMousetrapInstance;
2020-11-24 15:15:51 +05:30
beforeEach(() => {
2023-07-09 08:55:56 +05:30
$textarea = $('.edit-note textarea');
Shortcuts.initMarkdownEditorShortcuts($textarea);
localMousetrapInstance = $textarea.data(LOCAL_MOUSETRAP_DATA_KEY);
2020-11-24 15:15:51 +05:30
});
it('attaches a Mousetrap handler for every markdown shortcut specified with md-shortcuts', () => {
2023-07-09 08:55:56 +05:30
const expectedCalls = shortcutElements.map((s) => [s, expect.any(Function)]);
2020-11-24 15:15:51 +05:30
2023-07-09 08:55:56 +05:30
expect(Mousetrap.prototype.bind.mock.calls).toEqual(expectedCalls);
2020-11-24 15:15:51 +05:30
});
it('attaches a stopCallback that allows each markdown shortcut specified with md-shortcuts', () => {
2023-07-09 08:55:56 +05:30
flatten(shortcutElements).forEach((s) => {
expect(
localMousetrapInstance.stopCallback.call(localMousetrapInstance, null, null, s),
).toBe(false);
2020-11-24 15:15:51 +05:30
});
});
});
describe('removeMarkdownEditorShortcuts', () => {
it('does nothing if initMarkdownEditorShortcuts was not previous called', () => {
Shortcuts.removeMarkdownEditorShortcuts($('.edit-note textarea'));
2023-07-09 08:55:56 +05:30
expect(Mousetrap.prototype.unbind.mock.calls).toEqual([]);
2020-11-24 15:15:51 +05:30
});
it('removes Mousetrap handlers for every markdown shortcut specified with md-shortcuts', () => {
Shortcuts.initMarkdownEditorShortcuts($('.edit-note textarea'));
Shortcuts.removeMarkdownEditorShortcuts($('.edit-note textarea'));
2023-07-09 08:55:56 +05:30
const expectedCalls = shortcutElements.map((s) => [s]);
2020-11-24 15:15:51 +05:30
2023-07-09 08:55:56 +05:30
expect(Mousetrap.prototype.unbind.mock.calls).toEqual(expectedCalls);
2020-11-24 15:15:51 +05:30
});
});
});
2022-01-26 12:08:38 +05:30
describe('focusSearch', () => {
2023-06-20 00:43:36 +05:30
describe('when super sidebar is NOT enabled', () => {
let originalGon;
beforeEach(() => {
originalGon = window.gon;
window.gon = { use_new_navigation: false };
});
2022-01-26 12:08:38 +05:30
2023-06-20 00:43:36 +05:30
afterEach(() => {
window.gon = originalGon;
});
it('focuses the search bar', () => {
Shortcuts.focusSearch(createEvent('KeyboardEvent'));
expect(document.querySelector('#search').focus).toHaveBeenCalled();
});
2022-01-26 12:08:38 +05:30
});
});
2023-07-09 08:55:56 +05:30
describe('bindCommand(s)', () => {
it('bindCommand calls Mousetrap.bind correctly', () => {
const mockCommand = { defaultKeys: ['m'] };
const mockCallback = () => {};
shortcuts.bindCommand(mockCommand, mockCallback);
expect(Mousetrap.prototype.bind).toHaveBeenCalledTimes(1);
const [callArguments] = Mousetrap.prototype.bind.mock.calls;
expect(callArguments[0]).toEqual(mockCommand.defaultKeys);
expect(callArguments[1]).toBe(mockCallback);
});
it('bindCommands calls Mousetrap.bind correctly', () => {
const mockCommandsAndCallbacks = [
[{ defaultKeys: ['1'] }, () => {}],
[{ defaultKeys: ['2'] }, () => {}],
];
shortcuts.bindCommands(mockCommandsAndCallbacks);
expect(Mousetrap.prototype.bind).toHaveBeenCalledTimes(mockCommandsAndCallbacks.length);
const { calls } = Mousetrap.prototype.bind.mock;
mockCommandsAndCallbacks.forEach(([mockCommand, mockCallback], i) => {
expect(calls[i][0]).toEqual(mockCommand.defaultKeys);
expect(calls[i][1]).toBe(mockCallback);
});
});
});
2020-06-23 00:09:42 +05:30
});