From 7144f9cfea7c52af8b3923f28d6b0b676c40034d Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Wed, 1 May 2024 20:28:05 -1000 Subject: [PATCH] test(sidebar): increase coverage --- src/components/Sidebar.test.tsx | 88 ++++++++++++++++++++++++--------- src/components/Sidebar.tsx | 16 +++--- 2 files changed, 74 insertions(+), 30 deletions(-) diff --git a/src/components/Sidebar.test.tsx b/src/components/Sidebar.test.tsx index 281ea73c0..0dad3f782 100644 --- a/src/components/Sidebar.test.tsx +++ b/src/components/Sidebar.test.tsx @@ -57,32 +57,74 @@ describe('components/Sidebar.tsx', () => { expect(tree).toMatchSnapshot(); }); - it('should refresh the notifications', () => { - render( - - - - - , - ); - fetchNotifications.mockReset(); - fireEvent.click(screen.getByTitle('Refresh Notifications')); + describe('Refresh Notifications', () => { + it('should refresh the notifications when status is not loading', () => { + render( + + + + + , + ); + fetchNotifications.mockReset(); + fireEvent.click(screen.getByTitle('Refresh Notifications')); + + expect(fetchNotifications).toHaveBeenCalledTimes(1); + }); - expect(fetchNotifications).toHaveBeenCalledTimes(1); + it('should not refresh the notifications when status is loading', () => { + render( + + + + + , + ); + fetchNotifications.mockReset(); + fireEvent.click(screen.getByTitle('Refresh Notifications')); + + expect(fetchNotifications).not.toHaveBeenCalled(); + }); }); - it('go to the settings route', () => { - render( - - - - - , - ); - fireEvent.click(screen.getByTitle('Settings')); - expect(mockNavigate).toHaveBeenNthCalledWith(1, '/settings'); + describe('Settings', () => { + it('go to the settings route', () => { + render( + + + + + , + ); + fireEvent.click(screen.getByTitle('Settings')); + expect(mockNavigate).toHaveBeenCalledWith('/settings'); + }); + + it('go to the home if settings path already shown', () => { + render( + + + + + , + ); + fireEvent.click(screen.getByTitle('Settings')); + expect(mockNavigate).toHaveBeenCalledWith('/', { replace: true }); + }); }); it('opens github in the notifications page', () => { diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index e47171b0e..7117e193b 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -34,6 +34,14 @@ export const Sidebar: FC = () => { ipcRenderer.send('app-quit'); }, []); + const toggleSettings = () => { + if (location.pathname.startsWith('/settings')) { + navigate('/', { replace: true }); + } else { + navigate('/settings'); + } + }; + const notificationsCount = useMemo(() => { return getNotificationCount(notifications); }, [notifications]); @@ -93,13 +101,7 @@ export const Sidebar: FC = () => { type="button" className={sidebarButtonClasses} title="Settings" - onClick={() => { - if (location.pathname.startsWith('/settings')) { - navigate('/', { replace: true }); - } else { - navigate('/settings'); - } - }} + onClick={toggleSettings} >