debian-mirror-gitlab/spec/frontend/behaviors/shortcuts/keybindings_spec.js

110 lines
3 KiB
JavaScript
Raw Normal View History

2021-04-17 20:07:23 +05:30
import { flatten } from 'lodash';
2021-01-03 14:25:43 +05:30
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
2021-04-17 20:07:23 +05:30
import {
keysFor,
getCustomizations,
keybindingGroups,
TOGGLE_PERFORMANCE_BAR,
2021-09-04 01:27:46 +05:30
HIDE_APPEARING_CONTENT,
2021-04-17 20:07:23 +05:30
LOCAL_STORAGE_KEY,
WEB_IDE_COMMIT,
} from '~/behaviors/shortcuts/keybindings';
2021-01-03 14:25:43 +05:30
2021-04-17 20:07:23 +05:30
describe('~/behaviors/shortcuts/keybindings', () => {
2021-01-03 14:25:43 +05:30
beforeAll(() => {
useLocalStorageSpy();
});
2021-04-17 20:07:23 +05:30
const setupCustomizations = (customizationsAsString) => {
2021-01-03 14:25:43 +05:30
localStorage.clear();
if (customizationsAsString) {
localStorage.setItem(LOCAL_STORAGE_KEY, customizationsAsString);
}
2021-04-17 20:07:23 +05:30
getCustomizations.cache.clear();
2021-01-03 14:25:43 +05:30
};
2021-04-17 20:07:23 +05:30
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);
});
});
2021-01-03 14:25:43 +05:30
describe('when a command has not been customized', () => {
2021-04-17 20:07:23 +05:30
beforeEach(() => {
setupCustomizations('{}');
2021-01-03 14:25:43 +05:30
});
2021-04-17 20:07:23 +05:30
it('returns the default keybindings for the command', () => {
2021-01-03 14:25:43 +05:30
expect(keysFor(TOGGLE_PERFORMANCE_BAR)).toEqual(['p b']);
});
});
describe('when a command has been customized', () => {
const customization = ['p b a r'];
2021-04-17 20:07:23 +05:30
beforeEach(() => {
setupCustomizations(JSON.stringify({ [TOGGLE_PERFORMANCE_BAR.id]: customization }));
2021-01-03 14:25:43 +05:30
});
2021-04-17 20:07:23 +05:30
it('returns the custom keybindings for the command', () => {
2021-01-03 14:25:43 +05:30
expect(keysFor(TOGGLE_PERFORMANCE_BAR)).toEqual(customization);
});
});
2021-04-17 20:07:23 +05:30
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']);
});
});
2021-01-03 14:25:43 +05:30
describe("when the localStorage entry isn't valid JSON", () => {
2021-04-17 20:07:23 +05:30
beforeEach(() => {
setupCustomizations('{');
2021-01-03 14:25:43 +05:30
});
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`, () => {
2021-04-17 20:07:23 +05:30
beforeEach(() => {
setupCustomizations();
2021-01-03 14:25:43 +05:30
});
it('returns the default keybinding for the command', () => {
expect(keysFor(TOGGLE_PERFORMANCE_BAR)).toEqual(['p b']);
});
});
2021-09-04 01:27:46 +05:30
describe('when tooltips or popovers are visible', () => {
beforeEach(() => {
setupCustomizations();
});
it('returns the default keybinding for the command', () => {
expect(keysFor(HIDE_APPEARING_CONTENT)).toEqual(['esc']);
});
});
2021-01-03 14:25:43 +05:30
});