Increase test coverage for /modules/ui (#3314)
* Increase test coverage for `/modules/ui` Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: FellipeMTX <fellipefacdir@gmail.com> Co-authored-by: Fellipe Montes <102544529+FellipeMTX@users.noreply.github.com> * Merge main Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: FellipeMTX <fellipefacdir@gmail.com> Co-authored-by: Fellipe Montes <102544529+FellipeMTX@users.noreply.github.com> * Fix tests * Fix tests * Fix --------- Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: FellipeMTX <fellipefacdir@gmail.com> Co-authored-by: Fellipe Montes <102544529+FellipeMTX@users.noreply.github.com> Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -0,0 +1,51 @@
|
||||
import { act } from 'react-dom/test-utils';
|
||||
import { MemoryRouter, useLocation } from 'react-router-dom';
|
||||
import { fireEvent, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useSetHotkeyScope } from '@/ui/utilities/hotkey/hooks/useSetHotkeyScope';
|
||||
import { AppHotkeyScope } from '@/ui/utilities/hotkey/types/AppHotkeyScope';
|
||||
|
||||
import { useGoToHotkeys } from '../useGoToHotkeys';
|
||||
|
||||
const Wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<MemoryRouter
|
||||
initialEntries={['/one', '/two', { pathname: '/three' }]}
|
||||
initialIndex={1}
|
||||
>
|
||||
<RecoilRoot>{children}</RecoilRoot>
|
||||
</MemoryRouter>
|
||||
);
|
||||
const renderHookConfig = {
|
||||
wrapper: Wrapper,
|
||||
};
|
||||
|
||||
describe('useGoToHotkeys', () => {
|
||||
it('should navigate on hotkey trigger', () => {
|
||||
const { result } = renderHook(() => {
|
||||
useGoToHotkeys('a', '/three');
|
||||
|
||||
const setHotkeyScope = useSetHotkeyScope();
|
||||
|
||||
setHotkeyScope(AppHotkeyScope.App, { goto: true });
|
||||
|
||||
const location = useLocation();
|
||||
|
||||
return {
|
||||
pathname: location.pathname,
|
||||
};
|
||||
}, renderHookConfig);
|
||||
|
||||
expect(result.current.pathname).toBe('/two');
|
||||
|
||||
act(() => {
|
||||
fireEvent.keyDown(document, { key: 'g', code: 'KeyG' });
|
||||
});
|
||||
|
||||
act(() => {
|
||||
fireEvent.keyDown(document, { key: 'a', code: 'KeyA' });
|
||||
});
|
||||
|
||||
expect(result.current.pathname).toBe('/three');
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,32 @@
|
||||
import { act } from 'react-dom/test-utils';
|
||||
import { fireEvent, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
|
||||
import { useSetHotkeyScope } from '@/ui/utilities/hotkey/hooks/useSetHotkeyScope';
|
||||
import { AppHotkeyScope } from '@/ui/utilities/hotkey/types/AppHotkeyScope';
|
||||
|
||||
const hotKeyCallback = jest.fn();
|
||||
|
||||
describe('useScopedHotkeys', () => {
|
||||
it('should work as expected', () => {
|
||||
renderHook(
|
||||
() => {
|
||||
useScopedHotkeys('ctrl+k', hotKeyCallback, AppHotkeyScope.App);
|
||||
|
||||
const setHotkeyScope = useSetHotkeyScope();
|
||||
|
||||
setHotkeyScope(AppHotkeyScope.App);
|
||||
},
|
||||
{
|
||||
wrapper: RecoilRoot,
|
||||
},
|
||||
);
|
||||
|
||||
act(() => {
|
||||
fireEvent.keyDown(document, { key: 'k', code: 'KeyK', ctrlKey: true });
|
||||
});
|
||||
|
||||
expect(hotKeyCallback).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,8 @@
|
||||
import { isNonTextWritingKey } from '@/ui/utilities/hotkey/utils/isNonTextWritingKey';
|
||||
|
||||
describe('isNonTextWritingKey', () => {
|
||||
it('should determine non-text-writing keys', () => {
|
||||
expect(isNonTextWritingKey('Tab')).toBe(true);
|
||||
expect(isNonTextWritingKey('a')).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user