Add reset individual keybind button (#3423)

Co-authored-by: Benjamin Lu <templu1107@proton.me>
This commit is contained in:
Benjamin Lu
2025-04-12 16:59:00 -04:00
committed by GitHub
parent 731ce8599d
commit c8b8953e0a
3 changed files with 195 additions and 0 deletions

View File

@@ -286,4 +286,139 @@ describe('useKeybindingStore', () => {
expect(store.getKeybinding(newKeybinding.combo)?.commandId).toBe('command2')
expect(store.getKeybindingsByCommandId('command1')).toHaveLength(0)
})
it('should return false when no default or current keybinding exists during reset', () => {
const store = useKeybindingStore()
const result = store.resetKeybindingForCommand('nonexistent.command')
expect(result).toBe(false)
})
it('should return false when current keybinding equals default keybinding', () => {
const store = useKeybindingStore()
const defaultKeybinding = new KeybindingImpl({
commandId: 'test.command',
combo: { key: 'L', ctrl: true }
})
store.addDefaultKeybinding(defaultKeybinding)
const result = store.resetKeybindingForCommand('test.command')
expect(result).toBe(false)
expect(store.keybindings).toHaveLength(1)
expect(store.getKeybindingByCommandId('test.command')).toEqual(
defaultKeybinding
)
})
it('should unset user keybinding when no default keybinding exists and return true', () => {
const store = useKeybindingStore()
const userKeybinding = new KeybindingImpl({
commandId: 'test.command',
combo: { key: 'M', ctrl: true }
})
store.addUserKeybinding(userKeybinding)
expect(store.keybindings).toHaveLength(1)
const result = store.resetKeybindingForCommand('test.command')
expect(result).toBe(true)
expect(store.keybindings).toHaveLength(0)
})
it('should restore default keybinding when user has overridden it and return true', () => {
const store = useKeybindingStore()
const defaultKeybinding = new KeybindingImpl({
commandId: 'test.command',
combo: { key: 'N', ctrl: true }
})
const userKeybinding = new KeybindingImpl({
commandId: 'test.command',
combo: { key: 'O', alt: true }
})
store.addDefaultKeybinding(defaultKeybinding)
store.updateKeybindingOnCommand(userKeybinding)
expect(store.keybindings).toHaveLength(1)
expect(store.getKeybindingByCommandId('test.command')).toEqual(
userKeybinding
)
const result = store.resetKeybindingForCommand('test.command')
expect(result).toBe(true)
expect(store.keybindings).toHaveLength(1)
expect(store.getKeybindingByCommandId('test.command')).toEqual(
defaultKeybinding
)
})
it('should remove unset record and restore default keybinding when user has unset it', () => {
const store = useKeybindingStore()
const defaultKeybinding = new KeybindingImpl({
commandId: 'test.command',
combo: { key: 'P', ctrl: true }
})
store.addDefaultKeybinding(defaultKeybinding)
store.unsetKeybinding(defaultKeybinding)
expect(store.keybindings).toHaveLength(0)
const serializedCombo = defaultKeybinding.combo.serialize()
const userUnsetKeybindings = store.getUserUnsetKeybindings()
expect(userUnsetKeybindings[serializedCombo]).toBeTruthy()
expect(
userUnsetKeybindings[serializedCombo].equals(defaultKeybinding)
).toBe(true)
const result = store.resetKeybindingForCommand('test.command')
expect(result).toBe(true)
expect(store.keybindings).toHaveLength(1)
expect(store.getKeybindingByCommandId('test.command')).toEqual(
defaultKeybinding
)
expect(store.getUserUnsetKeybindings()[serializedCombo]).toBeUndefined()
})
it('should handle complex scenario with both unset and user keybindings', () => {
const store = useKeybindingStore()
// Create default keybinding
const defaultKeybinding = new KeybindingImpl({
commandId: 'test.command',
combo: { key: 'Q', ctrl: true }
})
store.addDefaultKeybinding(defaultKeybinding)
// Unset default keybinding
store.unsetKeybinding(defaultKeybinding)
expect(store.keybindings).toHaveLength(0)
// Add user keybinding with different combo
const userKeybinding = new KeybindingImpl({
commandId: 'test.command',
combo: { key: 'R', alt: true }
})
store.addUserKeybinding(userKeybinding)
expect(store.keybindings).toHaveLength(1)
expect(store.getKeybindingByCommandId('test.command')).toEqual(
userKeybinding
)
// Reset keybinding to default
const result = store.resetKeybindingForCommand('test.command')
expect(result).toBe(true)
expect(store.keybindings).toHaveLength(1)
expect(store.getKeybindingByCommandId('test.command')).toEqual(
defaultKeybinding
)
})
})