From 2215c0a5b55da6a1e207c02c2245cb2c80c228e4 Mon Sep 17 00:00:00 2001 From: Aadhil Ahamed Date: Fri, 3 Jan 2025 10:46:12 +0530 Subject: [PATCH] Improved Code Coverage in src/components/UserPortal/OrganizationNavbar/OrganizationNavbar.tsx (#3127) --- .../OrganizationNavbar.spec.tsx | 95 +++++++++++++++++++ .../OrganizationNavbar/OrganizationNavbar.tsx | 8 +- 2 files changed, 97 insertions(+), 6 deletions(-) diff --git a/src/components/UserPortal/OrganizationNavbar/OrganizationNavbar.spec.tsx b/src/components/UserPortal/OrganizationNavbar/OrganizationNavbar.spec.tsx index 7f9e8e52bb..d0296b1221 100644 --- a/src/components/UserPortal/OrganizationNavbar/OrganizationNavbar.spec.tsx +++ b/src/components/UserPortal/OrganizationNavbar/OrganizationNavbar.spec.tsx @@ -35,6 +35,9 @@ import { vi } from 'vitest'; * 9. **Plugin removal on uninstallation**: Ensures plugins are removed when uninstalled for the organization. * 10. **Rendering plugins when not uninstalled**: Ensures plugins render if not uninstalled. * 11. **No changes for unmatched plugin**: Ensures no changes when an unrecognized plugin update occurs. + * 12. **Should handle logout properly**: Ensures that local storage is cleared and user is redirected to home screen when logout button is clicked + * 13. **Should navigate to home page on home link click**: Ensures that browser history is correctly updated and navigates to oraganization home page. + * 14. **Should use fallback "en" when cookies.get returns null: Ensures component fallsback to "en" language when i18next cookie is absent. * * Mocked GraphQL queries and subscriptions simulate backend behavior. */ @@ -473,4 +476,96 @@ describe('Testing OrganizationNavbar Component [User Portal]', () => { await wait(); }); + + it('Should handle logout properly', async () => { + const mockStorage = { + clear: vi.fn(), + getItem: vi.fn((key: 'name' | 'talawaPlugins') => { + const items = { + name: JSON.stringify('Test User'), + talawaPlugins: JSON.stringify([]), + }; + return items[key] || null; + }), + setItem: vi.fn(), + removeItem: vi.fn(), + length: 0, + key: vi.fn(), + }; + Object.defineProperty(window, 'localStorage', { + value: mockStorage, + }); + const mockLocation = { + replace: vi.fn(), + }; + Object.defineProperty(window, 'location', { + value: mockLocation, + writable: true, + }); + render( + + + + + + + + + , + ); + await wait(); + userEvent.click(screen.getByTestId('personIcon')); + userEvent.click(screen.getByTestId('logoutBtn')); + expect(mockStorage.clear).toHaveBeenCalled(); + expect(mockLocation.replace).toHaveBeenCalledWith('/'); + }); + + it('Should navigate to home page on home link click', async () => { + const history = createMemoryHistory({ + initialEntries: ['/initial'], + }); + render( + + + + + + + + + , + ); + const homeLink = screen.getByText('Home'); + expect(homeLink).toBeInTheDocument(); + userEvent.click(homeLink); + await wait(); + expect(history.location.pathname).toBe( + `/user/organization/${organizationId}`, + ); + }); +}); + +describe('Testing OrganizationNavbar Cookie Fallback', () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it('should use fallback "en" when cookies.get returns null', async () => { + vi.spyOn(cookies, 'get').mockReturnValue( + null as unknown as { [key: string]: string }, + ); + render( + + + + + + + + + , + ); + expect(cookies.get).toHaveBeenCalledWith('i18next'); + expect(screen.getByText('Home')).toBeInTheDocument(); + }); }); diff --git a/src/components/UserPortal/OrganizationNavbar/OrganizationNavbar.tsx b/src/components/UserPortal/OrganizationNavbar/OrganizationNavbar.tsx index 34022fcfcf..6cfe3078eb 100644 --- a/src/components/UserPortal/OrganizationNavbar/OrganizationNavbar.tsx +++ b/src/components/UserPortal/OrganizationNavbar/OrganizationNavbar.tsx @@ -62,7 +62,6 @@ function organizationNavbar(props: InterfaceNavbarProps): JSX.Element { }); const [currentLanguageCode, setCurrentLanguageCode] = React.useState( - /* istanbul ignore next */ cookies.get('i18next') || 'en', ); @@ -71,7 +70,6 @@ function organizationNavbar(props: InterfaceNavbarProps): JSX.Element { /** * Handles user logout by clearing local storage and redirecting to the home page. */ - /* istanbul ignore next */ const handleLogout = (): void => { localStorage.clear(); window.location.replace('/'); @@ -142,6 +140,7 @@ function organizationNavbar(props: InterfaceNavbarProps): JSX.Element { console.log(`Plugin ${pluginName} is not present.`); } } else { + /* istanbul ignore else -- @preserve */ if (pluginIndexToRemove != -1) { plugins[pluginIndexToRemove].view = true; setItem('talawaPlugins', JSON.stringify(plugins)); @@ -174,10 +173,7 @@ function organizationNavbar(props: InterfaceNavbarProps): JSX.Element {