All files / platform/core/src/utils/hotkeys allowNativeKeysPlugin.test.js

0% Statements 0/51
0% Branches 0/2
0% Functions 0/15
0% Lines 0/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                                                                                                                                                               
import allowNativeKeysPlugin from './allowNativeKeysPlugin';
 
/** Minimal Mousetrap stand-in exposing the prototype the plugin wraps. */
function makeMousetrap(originalReturn = false) {
  const original = jest.fn(() => originalReturn);
  function Mousetrap() {}
  Mousetrap.prototype.stopCallback = original;
  return { Mousetrap, original };
}
 
/** Builds `<parent><child/></parent>` in the document and returns both. */
function mount(parentAttributes = {}) {
  const parent = document.createElement('div');
  Object.entries(parentAttributes).forEach(([name, value]) => parent.setAttribute(name, value));
 
  const child = document.createElement('span');
  parent.appendChild(child);
  document.body.appendChild(parent);
 
  return { parent, child };
}
 
describe('allowNativeKeysPlugin', () => {
  afterEach(() => {
    document.body.innerHTML = '';
  });
 
  it('stops a hotkey that lands inside a modal dialog', () => {
    // A modal renders role="dialog" and focuses its content on open, so the
    // keydown target is the dialog or something inside it.
    const { child } = mount({ role: 'dialog' });
    const { Mousetrap } = makeMousetrap();
    allowNativeKeysPlugin(Mousetrap);
 
    expect(Mousetrap.prototype.stopCallback.call({}, {}, child, 'ctrl+c')).toBe(true);
  });
 
  it('stops every combo there, not just the clipboard ones', () => {
    // ctrl+a is the case a selection test could never reach: its native meaning
    // is select-all, so nothing is selected at the moment it is pressed.
    const { parent } = mount({ role: 'dialog' });
    const { Mousetrap } = makeMousetrap();
    allowNativeKeysPlugin(Mousetrap);
 
    ['ctrl+a', 'ctrl+v', 'ctrl+x', 'ctrl+shift+c', 'r'].forEach(combo => {
      expect(Mousetrap.prototype.stopCallback.call({}, {}, parent, combo)).toBe(true);
    });
  });
 
  it('stops a hotkey inside an element marked .ohif-text-select', () => {
    const { child } = mount({ class: 'ohif-text-select', tabindex: '-1' });
    const { Mousetrap } = makeMousetrap();
    allowNativeKeysPlugin(Mousetrap);
 
    expect(Mousetrap.prototype.stopCallback.call({}, {}, child, 'ctrl+c')).toBe(true);
  });
 
  it('lets a hotkey through everywhere else, so bound commands still fire', () => {
    // A viewport carries neither marker, and a text selection sitting elsewhere
    // in the document is no longer consulted, so its hotkeys are unaffected.
    const { child } = mount({ class: 'viewport-element' });
    const { Mousetrap, original } = makeMousetrap(false);
    allowNativeKeysPlugin(Mousetrap);
 
    expect(Mousetrap.prototype.stopCallback.call({}, {}, child, 'ctrl+c')).toBe(false);
    expect(original).toHaveBeenCalled();
  });
 
  it('forwards every argument Mousetrap passes, including the sequence', () => {
    const { Mousetrap, original } = makeMousetrap(false);
    allowNativeKeysPlugin(Mousetrap);
 
    const event = {};
    Mousetrap.prototype.stopCallback.call({}, event, document.body, 'ctrl+k', 'ctrl+k p');
 
    expect(original).toHaveBeenCalledWith(event, document.body, 'ctrl+k', 'ctrl+k p');
  });
 
  it('keeps the wrapped stopCallback in charge outside those regions', () => {
    // The pause plugin's stopCallback sits underneath this one: while paused it
    // returns true for every combo, and that must still win.
    const { Mousetrap } = makeMousetrap(true);
    allowNativeKeysPlugin(Mousetrap);
 
    expect(Mousetrap.prototype.stopCallback.call({}, {}, document.body, 'ctrl+c')).toBe(true);
  });
 
  it('tolerates a target with no closest(), such as document', () => {
    const { Mousetrap, original } = makeMousetrap(false);
    allowNativeKeysPlugin(Mousetrap);
 
    expect(Mousetrap.prototype.stopCallback.call({}, {}, document, 'ctrl+c')).toBe(false);
    expect(original).toHaveBeenCalled();
  });
});