Add tests for modules/navigation and modules/keyboard-shortcut-menu (#3461)

Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: KlingerMatheus <klinger.matheus@gitstart.dev>
This commit is contained in:
gitstart-twenty 2024-01-17 16:00:58 +01:00 committed by GitHub
parent 4fa9e18920
commit 808100fdd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,83 @@
import { expect } from '@storybook/test';
import { act, renderHook } from '@testing-library/react';
import { RecoilRoot, useRecoilValue } from 'recoil';
import { isKeyboardShortcutMenuOpenedState } from '@/keyboard-shortcut-menu/states/isKeyboardShortcutMenuOpenedState';
import { AppHotkeyScope } from '@/ui/utilities/hotkey/types/AppHotkeyScope';
import { useKeyboardShortcutMenu } from '../useKeyboardShortcutMenu';
const mockSetHotkeyScopeAndMemorizePreviousScope = jest.fn();
const mockGoBackToPreviousHotkeyScope = jest.fn();
jest.mock('@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope', () => ({
usePreviousHotkeyScope: () => ({
setHotkeyScopeAndMemorizePreviousScope:
mockSetHotkeyScopeAndMemorizePreviousScope,
goBackToPreviousHotkeyScope: mockGoBackToPreviousHotkeyScope,
}),
}));
const renderHookConfig = () => {
const { result } = renderHook(
() => {
const isKeyboardShortcutMenuOpened = useRecoilValue(
isKeyboardShortcutMenuOpenedState,
);
return {
...useKeyboardShortcutMenu(),
isKeyboardShortcutMenuOpened,
};
},
{
wrapper: RecoilRoot,
},
);
return { result };
};
describe('useKeyboardShortcutMenu', () => {
it('should toggle keyboard shortcut menu correctly', async () => {
const { result } = renderHookConfig();
expect(result.current.toggleKeyboardShortcutMenu).toBeDefined();
expect(result.current.isKeyboardShortcutMenuOpened).toBe(false);
act(() => {
result.current.toggleKeyboardShortcutMenu();
});
expect(mockSetHotkeyScopeAndMemorizePreviousScope).toHaveBeenCalledWith(
AppHotkeyScope.KeyboardShortcutMenu,
);
expect(result.current.isKeyboardShortcutMenuOpened).toBe(true);
act(() => {
result.current.toggleKeyboardShortcutMenu();
});
expect(mockSetHotkeyScopeAndMemorizePreviousScope).toHaveBeenCalledWith(
AppHotkeyScope.KeyboardShortcutMenu,
);
expect(result.current.isKeyboardShortcutMenuOpened).toBe(false);
});
it('should open and close keyboard shortcut menu correctly', async () => {
const { result } = renderHookConfig();
act(() => {
result.current.openKeyboardShortcutMenu();
});
expect(mockSetHotkeyScopeAndMemorizePreviousScope).toHaveBeenCalledWith(
AppHotkeyScope.KeyboardShortcutMenu,
);
expect(result.current.isKeyboardShortcutMenuOpened).toBe(true);
act(() => {
result.current.closeKeyboardShortcutMenu();
});
expect(mockGoBackToPreviousHotkeyScope).toHaveBeenCalled();
expect(result.current.isKeyboardShortcutMenuOpened).toBe(false);
});
});

View File

@ -0,0 +1,31 @@
import { MemoryRouter } from 'react-router-dom';
import { renderHook } from '@testing-library/react';
import { useIsSettingsPage } from '../useIsSettingsPage';
const getWrapper =
(initialIndex: 0 | 1) =>
({ children }: { children: React.ReactNode }) => (
<MemoryRouter
initialEntries={['/settings/', '/tasks']}
initialIndex={initialIndex}
>
{children}
</MemoryRouter>
);
describe('useIsSettingsPage', () => {
it('should return true for pages which has /settings/ in pathname', () => {
const { result } = renderHook(() => useIsSettingsPage(), {
wrapper: getWrapper(0),
});
expect(result.current).toBe(true);
});
it('should return false for other pages which does not have /settings/ in pathname', () => {
const { result } = renderHook(() => useIsSettingsPage(), {
wrapper: getWrapper(1),
});
expect(result.current).toBe(false);
});
});

View File

@ -0,0 +1,31 @@
import { MemoryRouter } from 'react-router-dom';
import { renderHook } from '@testing-library/react';
import { useIsTasksPage } from '../useIsTasksPage';
const getWrapper =
(initialIndex: 0 | 1) =>
({ children }: { children: React.ReactNode }) => (
<MemoryRouter
initialEntries={['/settings/', '/tasks']}
initialIndex={initialIndex}
>
{children}
</MemoryRouter>
);
describe('useIsSettingsPage', () => {
it('should return true for pages which has /tasks in pathname', () => {
const { result } = renderHook(() => useIsTasksPage(), {
wrapper: getWrapper(1),
});
expect(result.current).toBe(true);
});
it('should return false for other pages which does not have /tasks in pathname', () => {
const { result } = renderHook(() => useIsTasksPage(), {
wrapper: getWrapper(0),
});
expect(result.current).toBe(false);
});
});