summaryrefslogtreecommitdiff
path: root/spec/frontend/behaviors/shortcuts/keybindings_spec.js
blob: 3ad44a16ae1542f24cd425cd3d0e2b6fa9df67ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { flatten } from 'lodash';
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import {
  keysFor,
  getCustomizations,
  keybindingGroups,
  TOGGLE_PERFORMANCE_BAR,
  HIDE_APPEARING_CONTENT,
  LOCAL_STORAGE_KEY,
  WEB_IDE_COMMIT,
} from '~/behaviors/shortcuts/keybindings';

describe('~/behaviors/shortcuts/keybindings', () => {
  beforeAll(() => {
    useLocalStorageSpy();
  });

  const setupCustomizations = (customizationsAsString) => {
    localStorage.clear();

    if (customizationsAsString) {
      localStorage.setItem(LOCAL_STORAGE_KEY, customizationsAsString);
    }

    getCustomizations.cache.clear();
  };

  describe('keybinding definition errors', () => {
    beforeEach(() => {
      setupCustomizations();
    });

    it('has no duplicate group IDs', () => {
      const allGroupIds = keybindingGroups.map((group) => group.id);
      expect(allGroupIds).toHaveLength(new Set(allGroupIds).size);
    });

    it('has no duplicate commands IDs', () => {
      const allCommandIds = flatten(
        keybindingGroups.map((group) => group.keybindings.map((kb) => kb.id)),
      );
      expect(allCommandIds).toHaveLength(new Set(allCommandIds).size);
    });
  });

  describe('when a command has not been customized', () => {
    beforeEach(() => {
      setupCustomizations('{}');
    });

    it('returns the default keybindings for the command', () => {
      expect(keysFor(TOGGLE_PERFORMANCE_BAR)).toEqual(['p b']);
    });
  });

  describe('when a command has been customized', () => {
    const customization = ['p b a r'];

    beforeEach(() => {
      setupCustomizations(JSON.stringify({ [TOGGLE_PERFORMANCE_BAR.id]: customization }));
    });

    it('returns the custom keybindings for the command', () => {
      expect(keysFor(TOGGLE_PERFORMANCE_BAR)).toEqual(customization);
    });
  });

  describe('when a command is marked as non-customizable', () => {
    const customization = ['mod+shift+c'];

    beforeEach(() => {
      setupCustomizations(JSON.stringify({ [WEB_IDE_COMMIT.id]: customization }));
    });

    it('returns the default keybinding for the command', () => {
      expect(keysFor(WEB_IDE_COMMIT)).toEqual(['mod+enter']);
    });
  });

  describe("when the localStorage entry isn't valid JSON", () => {
    beforeEach(() => {
      setupCustomizations('{');
    });

    it('returns the default keybinding for the command', () => {
      expect(keysFor(TOGGLE_PERFORMANCE_BAR)).toEqual(['p b']);
    });
  });

  describe(`when localStorage doesn't contain the ${LOCAL_STORAGE_KEY} key`, () => {
    beforeEach(() => {
      setupCustomizations();
    });

    it('returns the default keybinding for the command', () => {
      expect(keysFor(TOGGLE_PERFORMANCE_BAR)).toEqual(['p b']);
    });
  });

  describe('when tooltips or popovers are visible', () => {
    beforeEach(() => {
      setupCustomizations();
    });

    it('returns the default keybinding for the command', () => {
      expect(keysFor(HIDE_APPEARING_CONTENT)).toEqual(['esc']);
    });
  });
});