From cd5b80f789a5179807be6628f21d7771f07c5ce6 Mon Sep 17 00:00:00 2001 From: maikb Date: Thu, 21 Dec 2023 19:48:17 +0100 Subject: [PATCH] test(frontend): Refactor SidebarMenu component and its tests Simplified the SidebarMenu component by restructuring styles and text structures. Also refactored the SidebarMenu unit tests. Now, the mockUseNavigation function returns an object directly instead of using a mockImplementation call, providing clearer and more concise testing code. This ensures better readability and maintainability going forward. Refs: #1 --- src/components/SidebarMenu.test.tsx | 35 ++++++++----- .../__snapshots__/SidebarMenu.test.tsx.snap | 50 +++---------------- 2 files changed, 30 insertions(+), 55 deletions(-) diff --git a/src/components/SidebarMenu.test.tsx b/src/components/SidebarMenu.test.tsx index 5f2f5eb..5ac938d 100644 --- a/src/components/SidebarMenu.test.tsx +++ b/src/components/SidebarMenu.test.tsx @@ -4,20 +4,24 @@ import SidebarMenu from './SidebarMenu'; import Link from 'next/link'; import { Icon } from '@iconify/react'; import renderer from 'react-test-renderer'; +import useNavigation from "@/hooks/Navigation"; + +jest.mock('@/hooks/Navigation'); +const mockUseNavigation = useNavigation as jest.MockedFunction describe('', () => { - const mockUseNavigation = jest.fn(); - jest.mock('@/hooks/Navigation', () => ({ - __esModule: true, - useNavigation: mockUseNavigation(), - })); afterEach(() => { jest.clearAllMocks(); }); it('should render the Home menu item', () => { + mockUseNavigation.mockReturnValue({ + isHomeActive: false, + isProfilesActive: false, + }); + render(); const homeMenuItem = screen.getByText('Home'); @@ -25,6 +29,11 @@ describe('', () => { }); it('should render the Profiles menu item', () => { + mockUseNavigation.mockReturnValue({ + isHomeActive: false, + isProfilesActive: false, + }); + render(); const profilesMenuItem = screen.getByText('Profiles'); @@ -32,10 +41,10 @@ describe('', () => { }); it('should render inactive Home icon when isHomeActive is false', () => { - mockUseNavigation.mockImplementation(() => ({ + mockUseNavigation.mockReturnValue({ isHomeActive: false, isProfilesActive: false, - })); + }); const tree = renderer .create( @@ -53,10 +62,10 @@ describe('', () => { }); it('should render active Home icon when isHomeActive is true', () => { - mockUseNavigation.mockImplementation(() => ({ + mockUseNavigation.mockReturnValue({ isHomeActive: true, isProfilesActive: false, - })); + }); const tree = renderer .create( @@ -65,7 +74,7 @@ describe('', () => { className='flex w-full flex-row items-center space-x-4 px-4 py-3 duration-200 hover:bg-white/10' > - Home + Home ) .toJSON(); @@ -95,10 +104,10 @@ describe('', () => { }); it('should render active Profiles icon when isProfilesActive is true', () => { - mockUseNavigation.mockImplementation(() => ({ + mockUseNavigation.mockReturnValue({ isHomeActive: false, isProfilesActive: true, - })); + }); const tree = renderer .create( @@ -107,7 +116,7 @@ describe('', () => { className='flex w-full flex-row items-center space-x-4 px-4 py-3 duration-200 hover:bg-white/10' > - Home + Home ) .toJSON(); diff --git a/src/components/__snapshots__/SidebarMenu.test.tsx.snap b/src/components/__snapshots__/SidebarMenu.test.tsx.snap index 53fb591..ba0c83e 100644 --- a/src/components/__snapshots__/SidebarMenu.test.tsx.snap +++ b/src/components/__snapshots__/SidebarMenu.test.tsx.snap @@ -2,7 +2,7 @@ exports[` should render active Home icon when isHomeActive is true 1`] = ` should render active Home icon when isHomeActive is tru > - Home - - -`; - -exports[` should render active Profiles icon when isHomeActive is true 1`] = ` - - - Home @@ -36,7 +19,7 @@ exports[` should render active Profiles icon when isHomeActive is exports[` should render active Profiles icon when isProfilesActive is true 1`] = ` should render active Profiles icon when isProfilesActiv > Home @@ -53,24 +36,7 @@ exports[` should render active Profiles icon when isProfilesActiv exports[` should render inactive Home icon when isHomeActive is false 1`] = ` - - - Home - - -`; - -exports[` should render inactive Profiles icon when isHomeActive is false 1`] = ` - should render inactive Profiles icon when isHomeActive > Home @@ -87,7 +53,7 @@ exports[` should render inactive Profiles icon when isHomeActive exports[` should render inactive Profiles icon when isProfilesActive is false 1`] = ` should render inactive Profiles icon when isProfilesAct > Home