Fix unbind with Method (#54)

* add failing test to address #53

* add fix for #53
pull/69/head
Spencer P 6 years ago committed by 小弟调调™
parent 95632ecfd4
commit d5f93601a9

@ -107,9 +107,11 @@ function unbind(key, scope, method) {
for (let r = 0; r < _handlers[key].length; r++) { for (let r = 0; r < _handlers[key].length; r++) {
obj = _handlers[key][r]; obj = _handlers[key][r];
// 通过函数判断,是否解除绑定,函数相等直接返回 // 通过函数判断,是否解除绑定,函数相等直接返回
if (method && obj.method !== method) return; const isMatchingMethod = method ? obj.method === method : true;
// 判断是否在范围内并且键值相同 // 判断是否在范围内并且键值相同
if ( if (
isMatchingMethod &&
obj.scope === scope && obj.scope === scope &&
compareArray(obj.mods, mods) compareArray(obj.mods, mods)
) { ) {

@ -315,6 +315,43 @@ describe('\n Hotkeys.js Test Case.\n', () => {
hotkeys.unbind('a'); hotkeys.unbind('a');
}); });
test('unbind with method test', async () => {
const callbackA = jest.fn();
const callbackB = jest.fn();
hotkeys('shift+a', callbackA);
await __triggerKeyboardEvent(document.body, 65, {
shiftKey: true,
});
expect(callbackA.mock.calls.length).toBe(1);
hotkeys.unbind('shift+a', callbackA);
await __triggerKeyboardEvent(document.body, 65, {
shiftKey: true,
});
expect(callbackA.mock.calls.length).toBe(1);
hotkeys('shift+a', callbackB);
await __triggerKeyboardEvent(document.body, 65, {
shiftKey: true,
});
expect(callbackB.mock.calls.length).toBe(1);
hotkeys.unbind('shift+a', callbackB);
await __triggerKeyboardEvent(document.body, 65, {
shiftKey: true,
});
expect(callbackB.mock.calls.length).toBe(1);
});
test('HotKeys Key combination Test Case', async () => { test('HotKeys Key combination Test Case', async () => {
hotkeys('⌘+d', (e) => { hotkeys('⌘+d', (e) => {
expect(e.keyCode).toBe(82); expect(e.keyCode).toBe(82);

Loading…
Cancel
Save