import { ref } from 'vue'; import { mount, shallowMount } from '@vue/test-utils'; import NovncShortcutKeysMenu, { ShortcutMenuBody } from '../NovncShortcutKeysMenu.vue'; import { keyLabel, normalizeShortcut, readShortcuts, shortcutLabels, writeShortcuts, SHORTCUTS_PREFERENCE, SHORTCUT_NAMES_PREFERENCE } from '../shortcuts'; const mountMenu = (props = {}) => shallowMount(NovncShortcutKeysMenu, { props }); describe('component: NovncShortcutKeysMenu', () => { it('should send a key on its own when no modifier is held', () => { const wrapper = mountMenu(); wrapper.vm.sendKey('F5'); expect(wrapper.emitted('send-keys')).toStrictEqual([[['F5']]]); }); it('should send held modifiers with the next key and then release them', () => { const wrapper = mountMenu(); wrapper.vm.toggleModifier('AltLeft', true); wrapper.vm.toggleModifier('ControlLeft', true); expect(wrapper.vm.heldCombo).toBe('Ctrl+Alt'); wrapper.vm.sendKey('F2'); expect(wrapper.emitted('send-keys')).toStrictEqual([[['ControlLeft', 'AltLeft', 'F2']]]); expect(wrapper.vm.held).toStrictEqual([]); }); it('should offer the keys that go with what is held, like the old menu', async() => { const wrapper = mountMenu(); const keys = () => wrapper.vm.keyOptions.map((K) => K.key); expect(keys()).toStrictEqual(['PrintScreen']); expect(wrapper.vm.showFunctionKeys).toBe(true); wrapper.vm.toggleModifier('ControlLeft', true); expect(keys()).toStrictEqual(['KeyN', 'KeyT', 'KeyW', 'KeyY']); expect(wrapper.vm.showFunctionKeys).toBe(false); wrapper.vm.toggleModifier('AltLeft', true); expect(keys()).toStrictEqual(['Delete']); expect(wrapper.vm.showFunctionKeys).toBe(true); wrapper.vm.toggleModifier('ControlLeft', false); expect(keys()).toStrictEqual(['PrintScreen']); expect(wrapper.vm.showFunctionKeys).toBe(true); }); it('should send Ctrl+W from the menu', () => { const wrapper = mountMenu(); wrapper.vm.toggleModifier('ControlLeft', true); wrapper.vm.sendKey('KeyW'); expect(wrapper.emitted('send-keys')).toStrictEqual([[['ControlLeft', 'KeyW']]]); }); it('should hand Ctrl+Alt+Del to the confirmation instead of sending it', () => { const wrapper = mountMenu(); wrapper.vm.toggleModifier('ControlLeft', true); wrapper.vm.toggleModifier('AltLeft', true); wrapper.vm.sendKey('Delete'); expect(wrapper.emitted('ctrl-alt-del')).toHaveLength(1); expect(wrapper.emitted('send-keys')).toBeUndefined(); expect(wrapper.vm.held).toStrictEqual([]); }); it('should let held modifiers go when the menu closes without a key', () => { const wrapper = mountMenu(); wrapper.vm.toggleModifier('ControlLeft', true); wrapper.vm.onOpen(false); expect(wrapper.vm.held).toStrictEqual([]); }); it('should name saved shortcuts by their keys when they have no name', () => { const wrapper = mountMenu({ shortcuts: [ { name: 'Open terminal', keys: [{ key: 'Control' }, { key: 'Alt' }, { key: 't' }] }, { name: '', keys: [{ key: 'Escape' }] }, ] }); expect(wrapper.vm.items.map(({ name, label }) => [name, label])).toStrictEqual([ ['Open terminal', 'Ctrl+Alt+T'], ['Esc', 'Esc'], ]); }); it('should give the arrow keys the items shown after a modifier swapped them', async() => { const dropdownItems = ref([]); const keys = ref(['PrintScreen']); const wrapper = mount({ components: { ShortcutMenuBody }, setup: () => ({ keys }), template: '