2020-06-23 00:09:42 +05:30
|
|
|
import $ from 'jquery';
|
2020-11-24 15:15:51 +05:30
|
|
|
import { flatten } from 'lodash';
|
2022-07-16 23:28:13 +05:30
|
|
|
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2020-06-23 00:09:42 +05:30
|
|
|
import Shortcuts from '~/behaviors/shortcuts/shortcuts';
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
const mockMousetrap = {
|
|
|
|
bind: jest.fn(),
|
|
|
|
unbind: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
jest.mock('mousetrap', () => {
|
|
|
|
return jest.fn().mockImplementation(() => mockMousetrap);
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.mock('mousetrap/plugins/pause/mousetrap-pause', () => {});
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
describe('Shortcuts', () => {
|
|
|
|
const fixtureName = 'snippets/show.html';
|
|
|
|
const createEvent = (type, target) =>
|
|
|
|
$.Event(type, {
|
|
|
|
target,
|
|
|
|
});
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
beforeEach(() => {
|
2022-07-16 23:28:13 +05:30
|
|
|
loadHTMLFixture(fixtureName);
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
jest.spyOn(document.querySelector('.js-new-note-form .js-md-preview-button'), 'focus');
|
|
|
|
jest.spyOn(document.querySelector('.edit-note .js-md-preview-button'), 'focus');
|
2022-01-26 12:08:38 +05:30
|
|
|
jest.spyOn(document.querySelector('#search'), 'focus');
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
new Shortcuts(); // eslint-disable-line no-new
|
|
|
|
});
|
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('toggleMarkdownPreview', () => {
|
2020-06-23 00:09:42 +05:30
|
|
|
it('focuses preview button in form', () => {
|
|
|
|
Shortcuts.toggleMarkdownPreview(
|
|
|
|
createEvent('KeyboardEvent', document.querySelector('.js-new-note-form .js-note-text')),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(
|
|
|
|
document.querySelector('.js-new-note-form .js-md-preview-button').focus,
|
|
|
|
).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('focuses preview button inside edit comment form', () => {
|
2020-06-23 00:09:42 +05:30
|
|
|
document.querySelector('.js-note-edit').click();
|
|
|
|
|
|
|
|
Shortcuts.toggleMarkdownPreview(
|
|
|
|
createEvent('KeyboardEvent', document.querySelector('.edit-note .js-note-text')),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(
|
|
|
|
document.querySelector('.js-new-note-form .js-md-preview-button').focus,
|
|
|
|
).not.toHaveBeenCalled();
|
|
|
|
expect(document.querySelector('.edit-note .js-md-preview-button').focus).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
describe('markdown shortcuts', () => {
|
|
|
|
let shortcuts;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// Get all shortcuts specified with md-shortcuts attributes in the fixture.
|
|
|
|
// `shortcuts` will look something like this:
|
|
|
|
// [
|
|
|
|
// [ 'mod+b' ],
|
|
|
|
// [ 'mod+i' ],
|
|
|
|
// [ 'mod+k' ]
|
|
|
|
// ]
|
|
|
|
shortcuts = $('.edit-note .js-md')
|
|
|
|
.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', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
Shortcuts.initMarkdownEditorShortcuts($('.edit-note textarea'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('attaches a Mousetrap handler for every markdown shortcut specified with md-shortcuts', () => {
|
2021-03-08 18:12:59 +05:30
|
|
|
const expectedCalls = shortcuts.map((s) => [s, expect.any(Function)]);
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
expect(mockMousetrap.bind.mock.calls).toEqual(expectedCalls);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('attaches a stopCallback that allows each markdown shortcut specified with md-shortcuts', () => {
|
2021-03-08 18:12:59 +05:30
|
|
|
flatten(shortcuts).forEach((s) => {
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(mockMousetrap.stopCallback(null, null, s)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeMarkdownEditorShortcuts', () => {
|
|
|
|
it('does nothing if initMarkdownEditorShortcuts was not previous called', () => {
|
|
|
|
Shortcuts.removeMarkdownEditorShortcuts($('.edit-note textarea'));
|
|
|
|
|
|
|
|
expect(mockMousetrap.unbind.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes Mousetrap handlers for every markdown shortcut specified with md-shortcuts', () => {
|
|
|
|
Shortcuts.initMarkdownEditorShortcuts($('.edit-note textarea'));
|
|
|
|
Shortcuts.removeMarkdownEditorShortcuts($('.edit-note textarea'));
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
const expectedCalls = shortcuts.map((s) => [s]);
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
expect(mockMousetrap.unbind.mock.calls).toEqual(expectedCalls);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
describe('focusSearch', () => {
|
|
|
|
it('focuses the search bar', () => {
|
|
|
|
Shortcuts.focusSearch(createEvent('KeyboardEvent'));
|
|
|
|
|
|
|
|
expect(document.querySelector('#search').focus).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2020-06-23 00:09:42 +05:30
|
|
|
});
|