Skip to content

Commit

Permalink
added missing test
Browse files Browse the repository at this point in the history
  • Loading branch information
chandel-aman committed Nov 29, 2023
1 parent 632880c commit f5f51a8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 25 deletions.
103 changes: 86 additions & 17 deletions src/screens/Requests/Requests.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import React from 'react';
import { MockedProvider } from '@apollo/react-testing';
import { act, render, screen, fireEvent } from '@testing-library/react';
import {
act,
render,
screen,
fireEvent,
waitFor,
} from '@testing-library/react';
import 'jest-localstorage-mock';
import 'jest-location-mock';
import { I18nextProvider } from 'react-i18next';
Expand Down Expand Up @@ -75,22 +81,6 @@ describe('Testing Request screen', () => {
await wait();
});

test('Testing seach by name functionality', async () => {
render(
<MockedProvider addTypename={false} link={link3}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Requests />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

await wait();
});

test('Testing accept user functionality', async () => {
render(
<MockedProvider addTypename={false} link={link3}>
Expand Down Expand Up @@ -200,4 +190,83 @@ describe('Testing Request screen', () => {
expect(searchInput).toBeInTheDocument();
});
});
test('Testing seach by name functionality', async () => {
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Requests />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

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('Testing User data is not present', async () => {
// render(
// <MockedProvider addTypename={false} link={link}>
// <BrowserRouter>
// <Provider store={store}>
// <I18nextProvider i18n={i18nForTest}>
// <Requests />
// </I18nextProvider>
// </Provider>
// </BrowserRouter>
// </MockedProvider>
// );

// await wait();

// const inputBox = screen.getByTestId(`searchByName`);
// expect(inputBox).toBeInTheDocument();

// fireEvent.change(inputBox, { target: { value: 'Nonexistent User' } });
// expect(screen.getByText(/No results found for/i)).toBeTruthy();
// });

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(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
<Requests />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

// 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
});
});
11 changes: 3 additions & 8 deletions src/screens/Requests/Requests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ const Requests = (): JSX.Element => {
setDisplayedUsers([]);
return;
}
/* istanbul ignore next */
if (usersData.users.length < perPageResult) {
sethasMore(false);
} else {
/* istanbul ignore next */
setDisplayedUsers(usersData?.users);
}
}, [usersData]);
Expand Down Expand Up @@ -301,6 +301,7 @@ const Requests = (): JSX.Element => {
onChange={debouncedHandleSearchByName}
/>
<Button
data-testid="searchButton"
tabIndex={-1}
className={`position-absolute z-10 bottom-0 end-0 h-100 d-flex justify-content-center align-items-center`}
>
Expand Down Expand Up @@ -351,15 +352,9 @@ const Requests = (): JSX.Element => {
</div>
</div>
</div>
{isLoading == false &&
{!isLoading &&
usersData?.users.length === 0 &&
searchByName.length > 0 ? (
<div className={styles.notFound}>
<h4>
{t('noResultsFoundFor')} &quot;{searchByName}&quot;
</h4>
</div>
) : isLoading == false && usersData?.users.length === 0 ? (
<div className={styles.notFound}>
<h4>{t('noRequestFound')}</h4>
</div>
Expand Down

0 comments on commit f5f51a8

Please sign in to comment.