Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

2.9 KB
Raw Blame
import { allHotkeysReset, hotkeyChanged, hotkeyReset, hotkeysSliceConfig } from 'features/system/store/hotkeysSlice';
import { describe, expect, it } from 'vitest';

import type { HotkeysState } from './hotkeysTypes';

describe('Hotkeys Slice', () => {
  const getInitialState = (): HotkeysState => ({
    _version: 2,
    customHotkeys: {},
  });

  const { reducer } = hotkeysSliceConfig.slice;

  describe('hotkeyChanged', () => {
    it('should add a custom hotkey', () => {
      const state = getInitialState();
      const action = hotkeyChanged({ id: 'app.invoke', hotkeys: ['ctrl+shift+enter'] });
      const result = reducer(state, action);
      expect(result).toEqual({
        _version: 2,
        customHotkeys: {
          'app.invoke': ['ctrl+shift+enter'],
        },
      });
    });

    it('should update an existing custom hotkey', () => {
      const state: HotkeysState = {
        _version: 2,
        customHotkeys: {
          'app.invoke': ['ctrl+enter'],
        },
      };
      const action = hotkeyChanged({ id: 'app.invoke', hotkeys: ['ctrl+shift+enter'] });
      const result = reducer(state, action);
      expect(result).toEqual({
        _version: 2,
        customHotkeys: {
          'app.invoke': ['ctrl+shift+enter'],
        },
      });
    });
  });

  describe('hotkeyReset', () => {
    it('should remove a custom hotkey', () => {
      const state: HotkeysState = {
        _version: 2,
        customHotkeys: {
          'app.invoke': ['ctrl+shift+enter'],
          'app.cancelQueueItem': ['shift+x'],
        },
      };
      const action = hotkeyReset('app.invoke');
      const result = reducer(state, action);
      expect(result.customHotkeys).toEqual({
        'app.cancelQueueItem': ['shift+x'],
      });
    });
  });

  describe('allHotkeysReset', () => {
    it('should clear all custom hotkeys', () => {
      const state: HotkeysState = {
        _version: 2,
        customHotkeys: {
          'app.invoke': ['ctrl+shift+enter'],
          'app.cancelQueueItem': ['shift+x'],
        },
      };
      const action = allHotkeysReset();
      const result = reducer(state, action);
      expect(result).toEqual({
        _version: 2,
        customHotkeys: {},
      });
    });
  });

  describe('migration', () => {
    it('should migrate v1 persisted hotkeys without remapping punctuation glyphs to physical keys', () => {
      const migrate = hotkeysSliceConfig.persistConfig?.migrate;
      if (!migrate) {
        throw new Error('Expected hotkeys slice to be persisted');
      }

      const result = migrate({
        _version: 1,
        customHotkeys: {
          'canvas.decrementToolWidth': ['Shift+[', ' '],
          'canvas.incrementToolWidth': [']'],
        },
      });

      expect(result).toEqual({
        _version: 2,
        customHotkeys: {
          'canvas.decrementToolWidth': ['shift+[', 'space'],
          'canvas.incrementToolWidth': [']'],
        },
      });
    });
  });
});