diff --git a/src/screens/Requests/Requests.test.tsx b/src/screens/Requests/Requests.test.tsx index 3853b6f671..60e6a5f5ec 100644 --- a/src/screens/Requests/Requests.test.tsx +++ b/src/screens/Requests/Requests.test.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { MockedProvider } from '@apollo/react-testing'; -import { act, render, screen, fireEvent } from '@testing-library/react'; +import { act, fireEvent, render, screen } from '@testing-library/react'; import 'jest-localstorage-mock'; import 'jest-location-mock'; import { I18nextProvider } from 'react-i18next'; @@ -110,14 +110,46 @@ describe('Testing Request screen', () => { ); + await wait(); + const searchInput = screen.getByTestId('searchByName'); userEvent.type(searchInput, 'l'); - const notFoundDiv = await screen.findByTestId('searchAndNotFound'); + await screen.findByTestId('searchAndNotFound'); + }); + + test('Testing search latest and oldest toggle', async () => { + await act(async () => { + render( + + + + + + + + + + ); + + await wait(); - expect(notFoundDiv).toBeInTheDocument(); + const searchInput = screen.getByTestId('sort'); + expect(searchInput).toBeInTheDocument(); - await wait(); + const inputText = screen.getByTestId('sortDropdown'); + + fireEvent.click(inputText); + const toggleText = screen.getByTestId('latest'); + + fireEvent.click(toggleText); + + expect(searchInput).toBeInTheDocument(); + fireEvent.click(inputText); + const toggleTite = screen.getByTestId('oldest'); + fireEvent.click(toggleTite); + expect(searchInput).toBeInTheDocument(); + }); }); test('Testing accept user functionality', async () => { @@ -195,95 +227,4 @@ describe('Testing Request screen', () => { 'Organizations not found, please create an organization through dashboard' ); }); - - test('Testing search latest and oldest toggle', async () => { - await act(async () => { - render( - - - - - - - - - - ); - - await wait(); - - const searchInput = screen.getByTestId('sort'); - expect(searchInput).toBeInTheDocument(); - - const inputText = screen.getByTestId('sortuser'); - - fireEvent.click(inputText); - const toggleText = screen.getByTestId('latest'); - - fireEvent.click(toggleText); - - expect(searchInput).toBeInTheDocument(); - fireEvent.click(inputText); - const toggleTite = screen.getByTestId('oldest'); - fireEvent.click(toggleTite); - expect(searchInput).toBeInTheDocument(); - }); - }); - test('Testing seach by name functionalities', async () => { - render( - - - - - - - - - - ); - - await wait(); - - const search1 = 'John{backspace}{backspace}{backspace}{backspace}'; - userEvent.type(screen.getByTestId(/searchByName/i), search1); - - const search2 = 'Pete{backspace}{backspace}{backspace}{backspace}'; - userEvent.type(screen.getByTestId(/searchByName/i), search2); - - const search3 = - 'John{backspace}{backspace}{backspace}{backspace}Sam{backspace}{backspace}{backspace}'; - userEvent.type(screen.getByTestId(/searchByName/i), search3); - - const search4 = 'Sam{backspace}{backspace}P{backspace}'; - userEvent.type(screen.getByTestId(/searchByName/i), search4); - - const search5 = 'Xe'; - userEvent.type(screen.getByTestId(/searchByName/i), search5); - userEvent.type(screen.getByTestId(/searchByName/i), ''); - }); - - test('Does not display loading state when isLoading is false and usersData is present', async () => { - // Mock the scenario where isLoading is false and there is some data in usersData - render( - - - - - - - - - - - ); - - // Wait for the component to finish rendering - await wait(); - - // Check if the loading state is NOT displayed - const loadingState = screen.queryByText(/Loading/i); - expect(loadingState).toBeNull(); - - // Add any additional assertions based on your test case - }); }); diff --git a/src/screens/Requests/Requests.tsx b/src/screens/Requests/Requests.tsx index 8c23404c92..2755d4f637 100644 --- a/src/screens/Requests/Requests.tsx +++ b/src/screens/Requests/Requests.tsx @@ -29,6 +29,10 @@ import type { InterfaceQueryRequestListItem, InterfaceUserType, } from 'utils/interfaces'; + +import dayjs from 'dayjs'; +import relativeTime from 'dayjs/plugin/relativeTime'; +dayjs.extend(relativeTime); import styles from './Requests.module.css'; const Requests = (): JSX.Element => { @@ -41,11 +45,10 @@ const Requests = (): JSX.Element => { const [hasMore, sethasMore] = useState(true); const [isLoadingMore, setIsLoadingMore] = useState(false); const [searchByName, setSearchByName] = useState(''); - const [sortingOption, setSortingOption] = useState('latest'); + const [sortingOption, setSortingOption] = useState(''); const [displayedUsers, setDisplayedUsers] = useState< InterfaceQueryRequestListItem[] >([]); - const [acceptAdminFunc] = useMutation(ACCEPT_ADMIN_MUTATION); const [rejectAdminFunc] = useMutation(REJECT_ADMIN_MUTATION); const { @@ -95,17 +98,29 @@ const Requests = (): JSX.Element => { }, []); // To manage loading states - useEffect(() => { - if (!usersData) { - setDisplayedUsers([]); - return; + const updateDisplayedUsers = (data: any): InterfaceQueryRequestListItem[] => { + if (!data) { + return []; } - /* istanbul ignore next */ - if (usersData.users.length < perPageResult) { - sethasMore(false); - } else { - setDisplayedUsers(usersData?.users); + + const newDisplayedUsers = sortRequests(data.users, sortingOption); + return newDisplayedUsers; + }; + + const setHasMoreBasedOnUsers = (data: any): boolean => { + if (!data) { + return false; } + + return data.users.length >= perPageResult; + }; + + useEffect(() => { + const newDisplayedUsers = updateDisplayedUsers(usersData); + setDisplayedUsers(newDisplayedUsers); + + const hasMoreValue = setHasMoreBasedOnUsers(usersData); + sethasMore(hasMoreValue); }, [usersData]); // If the user is not Superadmin, redirect to Organizations screen @@ -248,6 +263,7 @@ const Requests = (): JSX.Element => { t('email'), t('accept'), t('reject'), + '', ]; const handleSorting = (option: string): void => { @@ -301,7 +317,6 @@ const Requests = (): JSX.Element => { onChange={debouncedHandleSearchByName} /> + {dayjs(user.createdAt).fromNow()} ); })} @@ -430,11 +455,6 @@ const Requests = (): JSX.Element => { )} - {!isLoading && displayedUsers.length === 0 && ( -
-
{t('noRequestFound')}
-
- )} );