diff --git a/src/components/UserPortal/UserSidebar/UserSidebar.test.tsx b/src/components/UserPortal/UserSidebar/UserSidebar.test.tsx index 7218bc745c..0bbf33a782 100644 --- a/src/components/UserPortal/UserSidebar/UserSidebar.test.tsx +++ b/src/components/UserPortal/UserSidebar/UserSidebar.test.tsx @@ -3,7 +3,7 @@ import type { RenderResult } from '@testing-library/react'; import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import { MockedProvider } from '@apollo/react-testing'; import { I18nextProvider } from 'react-i18next'; - +import styles from './UserSidebar.module.css'; import { USER_DETAILS, USER_JOINED_ORGANIZATIONS, @@ -15,6 +15,7 @@ import i18nForTest from 'utils/i18nForTest'; import { StaticMockLink } from 'utils/StaticMockLink'; import UserSidebar from './UserSidebar'; import useLocalStorage from 'utils/useLocalstorage'; +import userEvent from '@testing-library/user-event'; const { setItem } = useLocalStorage(); @@ -363,40 +364,57 @@ const renderUserSidebar = ( ); }; -describe('Testing UserSidebar Component [User Portal]', () => { +describe('UserSidebar Component Tests in User Portal', () => { beforeEach(() => { jest.clearAllMocks(); }); - test('Component should be rendered properly', async () => { + test('UserSidebar component renders correctly with user data present', async () => { await act(async () => { renderUserSidebar('properId', link); await wait(); }); + expect(screen.getByText('Talawa User Portal')).toBeInTheDocument(); }); - test('Component should be rendered properly when userImage is present', async () => { + test('Displays the logo and title text of the User Portal', async () => { await act(async () => { - renderUserSidebar('imagePresent', link); + renderUserSidebar('properId', link); await wait(); }); + expect(screen.getByText('Talawa User Portal')).toBeInTheDocument(); + expect(screen.getByTestId('leftDrawerContainer')).toBeVisible(); }); - test('Component should be rendered properly when organizationImage is present', async () => { + test('UserSidebar renders correctly when joinedOrganizations list is empty', async () => { + await act(async () => { + renderUserSidebar('orgEmpty', link); + await wait(); + }); + expect(screen.getByText('My Organizations')).toBeInTheDocument(); + }); + + test('Renders UserSidebar component with organization image when present', async () => { await act(async () => { renderUserSidebar('imagePresent', link); await wait(); }); + expect(screen.getByText('Settings')).toBeInTheDocument(); }); - test('Component should be rendered properly when joinedOrganizations list is empty', async () => { + test('User profile data renders with all expected navigation links visible', async () => { await act(async () => { - renderUserSidebar('orgEmpty', link); + renderUserSidebar('properId', link); await wait(); }); + + const expectedLinks = ['My Organizations', 'Settings', 'Chat']; + expectedLinks.forEach((link) => { + expect(screen.getByText(link)).toBeInTheDocument(); + }); }); - test('Testing Drawer when the screen size is less than or equal to 820px', async () => { + test('UserSidebar renders correctly on smaller screens and toggles drawer visibility', async () => { await act(async () => { resizeWindow(800); render( @@ -411,28 +429,124 @@ describe('Testing UserSidebar Component [User Portal]', () => { , ); }); - expect(screen.getByText('My Organizations')).toBeInTheDocument(); - expect(screen.getByText('Settings')).toBeInTheDocument(); - expect(screen.getByText('Talawa User Portal')).toBeInTheDocument(); - const settingsBtn = screen.getByText('Settings'); + const orgsBtn = screen.getByTestId('orgsBtn'); + act(() => orgsBtn.click()); + expect(props.setHideDrawer).toHaveBeenCalledWith(true); + }); - const orgsBtn = screen.getAllByTestId(/orgsBtn/i); + test('Active route button style changes correctly upon click', async () => { + await act(async () => { + renderUserSidebar('properId', link); + await wait(); + }); - act(() => { - orgsBtn[0].click(); + const orgsBtn = screen.getByTestId('orgsBtn'); + const settingsBtn = screen.getByTestId('settingsBtn'); + + fireEvent.click(orgsBtn); + expect(orgsBtn).toHaveClass('text-white btn btn-success'); + fireEvent.click(settingsBtn); + expect(settingsBtn).toHaveClass('text-white btn btn-success'); + }); + + test('Translation hook displays expected text in UserSidebar', async () => { + await act(async () => { + renderUserSidebar('properId', link); + await wait(); + }); + expect( + screen.getByText(i18nForTest.t('common:settings')), + ).toBeInTheDocument(); + }); + + test('handleLinkClick function closes the sidebar on mobile view when a link is clicked', async () => { + resizeWindow(800); + await act(async () => { + renderUserSidebar('properId', link); + await wait(); }); - await waitFor(() => - expect( - orgsBtn[0].className.includes('text-white btn btn-success'), - ).toBeTruthy(), - ); - act(() => { - settingsBtn.click(); + const chatBtn = screen.getByTestId('chatBtn'); + fireEvent.click(chatBtn); + expect(props.setHideDrawer).toHaveBeenCalledWith(true); + }); + + describe('UserSidebar Drawer Visibility Tests on Smaller Screens', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + test('Clicking a link closes the drawer when window width is 820px or less', () => { + act(() => { + window.innerWidth = 820; + window.dispatchEvent(new Event('resize')); + }); + + render( + + + + + + + + + , + ); + + const linkElement = screen.getByText('My Organizations'); // Adjust text if different + fireEvent.click(linkElement); + + expect(props.setHideDrawer).toHaveBeenCalledWith(true); + }); + + describe('UserSidebar Drawer State Tests', () => { + test('Drawer visibility changes based on hideDrawer prop', () => { + const { rerender } = render( + + + + + + + + + , + ); + + expect(screen.getByTestId('leftDrawerContainer')).toHaveClass( + styles.hideElemByDefault, + ); + + rerender( + + + + + + + + + , + ); + expect(screen.getByTestId('leftDrawerContainer')).toHaveClass( + styles.inactiveDrawer, + ); + + rerender( + + + + + + + + + , + ); + expect(screen.getByTestId('leftDrawerContainer')).toHaveClass( + styles.activeDrawer, + ); + }); }); - await waitFor(() => - expect( - settingsBtn.className.includes('text-white btn btn-success'), - ).toBeTruthy(), - ); }); });