2022-03-21 02:59:26 -05:00
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
//
|
2024-01-01 02:43:48 -06:00
|
|
|
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
|
2022-03-21 02:59:26 -05:00
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
import React from 'react';
|
2023-10-23 07:13:17 -05:00
|
|
|
|
2022-03-21 02:59:26 -05:00
|
|
|
import { withTheme } from '../fake_theme';
|
2024-05-06 00:45:44 -05:00
|
|
|
import { fireEvent, render } from '@testing-library/react';
|
2023-10-23 07:13:17 -05:00
|
|
|
|
2024-05-06 00:45:44 -05:00
|
|
|
import * as keyShort from '../../../pgadmin/static/js/keyboard_shortcuts';
|
2022-03-21 02:59:26 -05:00
|
|
|
import KeyboardShortcuts from '../../../pgadmin/static/js/components/KeyboardShortcuts';
|
|
|
|
|
|
|
|
/* MUI Components need to be wrapped in Theme for theme vars */
|
|
|
|
describe('KeyboardShortcuts', () => {
|
|
|
|
let defult_value = {
|
2022-03-29 05:57:33 -05:00
|
|
|
'control': true,
|
2022-03-21 02:59:26 -05:00
|
|
|
'alt': true,
|
|
|
|
'key': {
|
|
|
|
'char': 'a',
|
|
|
|
'key_code': 97
|
2022-03-23 02:58:35 -05:00
|
|
|
},
|
|
|
|
'shift': false
|
2022-03-21 02:59:26 -05:00
|
|
|
};
|
|
|
|
let fields = [{
|
|
|
|
type: 'keyCode',
|
|
|
|
label: 'Key'
|
|
|
|
}, {
|
|
|
|
name: 'shift',
|
|
|
|
label: 'Shift',
|
|
|
|
type: 'checkbox'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'control',
|
|
|
|
label: 'Control',
|
|
|
|
type: 'checkbox'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'alt',
|
|
|
|
label: 'Alt/Option',
|
|
|
|
type: 'checkbox'
|
|
|
|
}];
|
|
|
|
|
|
|
|
describe('KeyboardShortcuts', () => {
|
2023-10-23 07:13:17 -05:00
|
|
|
let ThemedFormInputKeyboardShortcuts = withTheme(KeyboardShortcuts);
|
|
|
|
let onChange = jest.fn();
|
2024-05-06 00:45:44 -05:00
|
|
|
|
|
|
|
const ctrlRender = ()=>{
|
|
|
|
return render(
|
2022-03-21 02:59:26 -05:00
|
|
|
<ThemedFormInputKeyboardShortcuts
|
|
|
|
value={defult_value}
|
|
|
|
fields={fields}
|
|
|
|
controlProps={{
|
2022-03-23 02:58:35 -05:00
|
|
|
extraprop: 'test',
|
2023-12-21 00:37:26 -06:00
|
|
|
'keydown': onChange
|
2022-03-21 02:59:26 -05:00
|
|
|
}}
|
2022-03-23 02:58:35 -05:00
|
|
|
onChange={onChange}
|
2022-03-21 02:59:26 -05:00
|
|
|
/>);
|
2024-05-06 00:45:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll(()=>{
|
|
|
|
jest.spyOn(keyShort, 'isMac').mockReturnValue(true);
|
2022-03-21 02:59:26 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('init', () => {
|
2024-05-06 00:45:44 -05:00
|
|
|
const ctrl = ctrlRender();
|
|
|
|
expect(ctrl.container.querySelector('input').getAttribute('value')).toBe('a');
|
2022-03-21 02:59:26 -05:00
|
|
|
});
|
|
|
|
|
2023-10-23 07:13:17 -05:00
|
|
|
it('Key change', () => {
|
2024-05-06 00:45:44 -05:00
|
|
|
const ctrl = ctrlRender();
|
|
|
|
fireEvent.keyDown(ctrl.container.querySelector('input'), {
|
2023-10-23 07:13:17 -05:00
|
|
|
key: 'Space', code: 32, keyCode: 32
|
|
|
|
});
|
2022-03-29 05:57:33 -05:00
|
|
|
expect(onChange).toHaveBeenCalledWith({ control: true, alt: true, key: { char: 'Space', key_code: 32 }, shift: false });
|
2022-03-23 02:58:35 -05:00
|
|
|
});
|
2022-03-21 02:59:26 -05:00
|
|
|
|
2023-10-23 07:13:17 -05:00
|
|
|
it('Shift option', () => {
|
2024-05-06 00:45:44 -05:00
|
|
|
const ctrl = ctrlRender();
|
|
|
|
const input = ctrl.container.querySelectorAll('button')[0];
|
2023-10-23 07:13:17 -05:00
|
|
|
fireEvent.click(input);
|
2022-03-29 05:57:33 -05:00
|
|
|
expect(onChange).toHaveBeenCalledWith({ control: true, alt: true, key: { char: 'a', key_code: 97 }, shift: true });
|
2022-03-23 02:58:35 -05:00
|
|
|
});
|
|
|
|
|
2023-10-23 07:13:17 -05:00
|
|
|
it('Control option', () => {
|
2024-05-06 00:45:44 -05:00
|
|
|
const ctrl = ctrlRender();
|
|
|
|
const input = ctrl.container.querySelectorAll('button')[1];
|
2023-10-23 07:13:17 -05:00
|
|
|
fireEvent.click(input);
|
2024-05-06 00:45:44 -05:00
|
|
|
expect(onChange).toHaveBeenCalledWith({ control: false, ctrl_is_meta: false, alt: true, key: { char: 'a', key_code: 97 }, shift: false });
|
2022-03-23 02:58:35 -05:00
|
|
|
});
|
|
|
|
|
2024-05-06 00:45:44 -05:00
|
|
|
it('Cmd option', () => {
|
|
|
|
const ctrl = ctrlRender();
|
|
|
|
const input = ctrl.container.querySelectorAll('button')[2];
|
|
|
|
fireEvent.click(input);
|
|
|
|
expect(onChange).toHaveBeenCalledWith({ control: true, ctrl_is_meta: true, alt: true, key: { char: 'a', key_code: 97 }, shift: false });
|
|
|
|
});
|
2022-03-23 02:58:35 -05:00
|
|
|
|
2023-10-23 07:13:17 -05:00
|
|
|
it('Alt option', () => {
|
2024-05-06 00:45:44 -05:00
|
|
|
const ctrl = ctrlRender();
|
|
|
|
const input = ctrl.container.querySelectorAll('button')[3];
|
2023-10-23 07:13:17 -05:00
|
|
|
fireEvent.click(input);
|
2022-03-29 05:57:33 -05:00
|
|
|
expect(onChange).toHaveBeenCalledWith({ control: true, alt: false, key: { char: 'a', key_code: 97 }, shift: false });
|
2022-03-21 02:59:26 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|