From 26c405c46c4068f6bc985e6cde82ad162720a124 Mon Sep 17 00:00:00 2001 From: Daniel Bisgrove Date: Fri, 7 Jun 2024 14:56:39 -0400 Subject: [PATCH] Fixing infiniteList tests --- .../ContactFlowColumn.test.tsx | 4 +- .../ContactsContext/ContactsContext.test.tsx | 54 +++++------- .../InfiniteList/InfiniteList.test.tsx | 84 ++++++++++++++----- 3 files changed, 86 insertions(+), 56 deletions(-) diff --git a/src/components/Contacts/ContactFlow/ContactFlowColumn/ContactFlowColumn.test.tsx b/src/components/Contacts/ContactFlow/ContactFlowColumn/ContactFlowColumn.test.tsx index 7eb0add7d..7870bb0af 100644 --- a/src/components/Contacts/ContactFlow/ContactFlowColumn/ContactFlowColumn.test.tsx +++ b/src/components/Contacts/ContactFlow/ContactFlowColumn/ContactFlowColumn.test.tsx @@ -36,7 +36,7 @@ const router = { describe('ContactFlowColumn', () => { it('should render a column with correct details', async () => { - const { getByText, getByTestId } = render( + const { getByText, findByText, getByTestId } = render( @@ -75,7 +75,7 @@ describe('ContactFlowColumn', () => { ); await waitFor(() => expect(getByText(title)).toBeInTheDocument()); expect(getByText('1')).toBeInTheDocument(); - expect(getByText('Test Person')).toBeInTheDocument(); + expect(await findByText('Test Person')).toBeInTheDocument(); expect(getByTestId('column-header')).toHaveStyle({ backgroundColor: 'theme.palette.mpdxBlue.main', }); diff --git a/src/components/Contacts/ContactsContext/ContactsContext.test.tsx b/src/components/Contacts/ContactsContext/ContactsContext.test.tsx index 107efadda..2f909e716 100644 --- a/src/components/Contacts/ContactsContext/ContactsContext.test.tsx +++ b/src/components/Contacts/ContactsContext/ContactsContext.test.tsx @@ -47,39 +47,29 @@ jest.mock('notistack', () => ({ })); const TestRender: React.FC = () => { - const { viewMode, handleViewModeChange, userOptionsLoading } = useContext( + const { viewMode, handleViewModeChange } = useContext( ContactsContext, ) as ContactsType; return ( - {!userOptionsLoading ? ( - <> - {viewMode} - - - - - ) : ( - <>Loading - )} + {viewMode} + + + ); }; @@ -128,7 +118,6 @@ describe('ContactsPageContext', () => { , ); - expect(getByText('Loading')).toBeInTheDocument(); await waitFor(() => expect(getByText('Flows Button')).toBeInTheDocument()); userEvent.click(getByText('Flows Button')); await waitFor(() => expect(getByText('flows')).toBeInTheDocument()); @@ -171,7 +160,6 @@ describe('ContactsPageContext', () => { , ); - expect(getByText('Loading')).toBeInTheDocument(); await waitFor(() => expect(getByText('Map Button')).toBeInTheDocument()); userEvent.click(getByText('Map Button')); await waitFor(() => expect(getByText('map')).toBeInTheDocument()); @@ -192,7 +180,7 @@ describe('ContactsPageContext', () => { }); it('does not have a contact id and changes to map', async () => { - const { getByText, queryByText } = render( + const { getByText } = render( { , ); - expect(getByText('Loading')).toBeInTheDocument(); - await waitFor(() => expect(queryByText('Loading')).not.toBeInTheDocument()); await waitFor(() => expect(getByText('Map Button')).toBeInTheDocument()); userEvent.click(getByText('Map Button')); await waitFor(() => expect(getByText('map')).toBeInTheDocument()); diff --git a/src/components/InfiniteList/InfiniteList.test.tsx b/src/components/InfiniteList/InfiniteList.test.tsx index ceaf7c204..20e0e42ab 100644 --- a/src/components/InfiniteList/InfiniteList.test.tsx +++ b/src/components/InfiniteList/InfiniteList.test.tsx @@ -1,15 +1,23 @@ import React from 'react'; +import { Skeleton } from '@mui/material'; import { ThemeProvider } from '@mui/material/styles'; import { render } from '@testing-library/react'; import { VirtuosoMockContext } from 'react-virtuoso'; import theme from '../../theme'; import { InfiniteList } from './InfiniteList'; +const RowSkeleton: React.FC = () => ( +
+ + +
+); + const endReached = jest.fn(); describe('InfiniteList', () => { it('should show loading indicator', async () => { - const { getByTestId } = render( + const { getAllByTestId } = render( { , ); - expect(getByTestId('infinite-list-skeleton-loading')).toBeVisible(); + expect(getAllByTestId('infinite-list-skeleton-loading').length).toBe(2); + }); + + it('should show custom skeletons', async () => { + const { getAllByTestId } = render( + +
{item}
} + endReached={() => true} + EmptyPlaceholder={
No items
} + /> +
, + ); + + expect(getAllByTestId('infinite-list-skeleton-loading').length).toBe(2); + // 10 skeletons for loading, and another 10 for next items + expect(getAllByTestId('row-skeleton').length).toBe(20); + }); + + it('does not show loading when items are present', async () => { + const { getAllByTestId } = render( + +
{item}
} + endReached={endReached} + EmptyPlaceholder={
No items
} + /> +
, + ); + + expect(getAllByTestId('infinite-list-skeleton-loading').length).toBe(1); + }); + + it('empty', async () => { + const { queryByTestId, getByText } = render( + +
{item}
} + endReached={endReached} + EmptyPlaceholder={
No items
} + /> +
, + ); + + expect(queryByTestId('infinite-list-skeleton-loading')).toBeNull(); + expect(getByText('No items')).toBeInTheDocument(); }); it('should render data', async () => { @@ -75,22 +137,4 @@ describe('InfiniteList', () => { expect(queryByText('No items')).toBeNull(); expect(endReached).not.toHaveBeenCalled(); }); - - it('empty', async () => { - const { queryByTestId, getByText } = render( - -
{item}
} - endReached={endReached} - EmptyPlaceholder={
No items
} - /> -
, - ); - - expect(queryByTestId('infinite-list-skeleton-loading')).toBeNull(); - expect(getByText('No items')).toBeInTheDocument(); - }); });