Files
directus/app/src/composables/use-shortcut.test.ts
Azri Kahar ce8f571c72 Fix key combinations being prevented in dbSafe v-input when it's a leading number (#16668)
* Fix key combinations being prevented in v-input

* should normalize accented characters first

* add tests for processValue and emitValue

* export keyMap from use-shortcut to keep things DRY

* try to add test for use-shortcut composable

* move systemKeys to use-shortcut

* add "capslock" & "enter" to systemKeys
2022-11-30 19:56:44 +08:00

53 lines
1.5 KiB
TypeScript

import { mount } from '@vue/test-utils';
import { afterEach, beforeEach, describe, expect, Mock, test, vi } from 'vitest';
import { defineComponent, h } from 'vue';
import { useShortcut } from './use-shortcut';
function getTestComponent(shortcut: string, handler: () => void) {
return defineComponent({
setup() {
useShortcut(shortcut, handler);
},
render: () => h('div'),
});
}
describe('useShortcut', () => {
let shortcutHandler: Mock;
beforeEach(() => {
shortcutHandler = vi.fn();
});
afterEach(() => {
vi.clearAllMocks();
});
test.each(['Control', 'Command'])('should map "%s" key to "meta"', async (testKey) => {
const keys = ['meta', 's'];
const testComponent = getTestComponent(keys.join('+'), shortcutHandler);
const wrapper = mount(testComponent, { attachTo: document.body });
// intentionally not using keys[0] as we want to test Control/Command, not meta itself
await wrapper.trigger('keydown', { key: testKey });
await wrapper.trigger('keydown', { key: keys[1] });
await wrapper.trigger('keyup', { key: testKey });
expect(shortcutHandler).toHaveBeenCalledOnce();
});
test('should trigger with combination of shortcut keys', async () => {
const keys = ['meta', 'alt', 'c'];
const testComponent = getTestComponent(keys.join('+'), shortcutHandler);
const wrapper = mount(testComponent, { attachTo: document.body });
for (const key of keys) {
await wrapper.trigger('keydown', { key });
}
await wrapper.trigger('keyup', { key: keys[0] });
expect(shortcutHandler).toHaveBeenCalledOnce();
});
});