Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPDX - (#8032, #8033, #7533, #8034, #8035, #8036, #8037) - FixCommitmentInfo #974

Merged
merged 10 commits into from
Aug 28, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { useRouter } from 'next/router';
import { ThemeProvider } from '@mui/material/styles';
import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ErgonoMockShape } from 'graphql-ergonomock';
import { getSession } from 'next-auth/react';
import { SnackbarProvider } from 'notistack';
import { I18nextProvider } from 'react-i18next';
import { VirtuosoMockContext } from 'react-virtuoso';
import TestRouter from '__tests__/util/TestRouter';
import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
import { mockInvalidStatusesResponse } from 'src/components/Tool/FixCommitmentInfo/FixCommitmentInfoMocks';
import { InvalidStatusesQuery } from 'src/components/Tool/FixCommitmentInfo/GetInvalidStatuses.generated';
import i18n from 'src/lib/i18n';
import theme from 'src/theme';
import FixCommitmentInfoPage from './[[...contactId]].page';

jest.mock('next/router', () => ({
useRouter: jest.fn(),
}));

const pushFn = jest.fn();
const accountListId = 'account-list-1';
const session = {
expires: '2021-10-28T14:48:20.897Z',
user: {
email: 'Chair Library Bed',
image: null,
name: 'Dung Tapestry',
token: 'superLongJwtString',
},
};
const Components = ({
mockNodes = mockInvalidStatusesResponse,
}: {
mockNodes?: ErgonoMockShape[];
}) => (
<ThemeProvider theme={theme}>
<TestRouter>
<GqlMockedProvider<{
InvalidStatuses: InvalidStatusesQuery;
}>
mocks={{
InvalidStatuses: {
contacts: {
nodes: mockNodes,
},
},
}}
>
<I18nextProvider i18n={i18n}>
<SnackbarProvider>
<VirtuosoMockContext.Provider
value={{ viewportHeight: 1000, itemHeight: 100 }}
>
<FixCommitmentInfoPage />
</VirtuosoMockContext.Provider>
</SnackbarProvider>
</I18nextProvider>
</GqlMockedProvider>
</TestRouter>
</ThemeProvider>
);

describe('FixCommitmentInfoPage', () => {
beforeEach(() => {
(getSession as jest.Mock).mockResolvedValue(session);
(useRouter as jest.Mock).mockReturnValue({
query: {
accountListId,
},
isReady: true,
push: pushFn,
});
});

it('should open up contact details', async () => {
const { getByText, queryByText } = render(<Components />);
await waitFor(() => expect(queryByText('Tester 1')).toBeInTheDocument());

const contactName = getByText('Tester 1');

expect(contactName).toBeInTheDocument();
userEvent.click(contactName);

await waitFor(() => {
expect(pushFn).toHaveBeenCalledWith({
pathname:
'/accountLists/account-list-1/tools/fix/commitmentInfo/tester-1',

query: { tab: 'Donations' },
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const FixCommitmentInfoPage: React.FC = () => {
const { accountListId, handleSelectContact } = useToolsHelper();
const pageUrl = 'tools/fix/commitmentInfo';

const setContactFocus: SetContactFocus = (contactId) => {
handleSelectContact(pageUrl, contactId);
const setContactFocus: SetContactFocus = (contactId, tabKey) => {
handleSelectContact(pageUrl, contactId, tabKey);
};

return (
Expand Down
13 changes: 10 additions & 3 deletions pages/accountLists/[accountListId]/tools/useToolsHelper.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { useRouter } from 'next/router';
import { useCallback } from 'react';
import { TabKey } from 'src/components/Contacts/ContactDetails/ContactDetails';
import { useAccountListId } from 'src/hooks/useAccountListId';
import { getQueryParam } from 'src/utils/queryParam';

export type SetContactFocus = (contactId: string) => void;
export type SetContactFocus = (contactId: string, tab?: TabKey) => void;

export const useToolsHelper = () => {
const { query, push } = useRouter();
const accountListId = useAccountListId();
const selectedContactId = getQueryParam(query, 'contactId');

const handleSelectContact = useCallback(
(pagePath: string, contactId: string) => {
push(`/accountLists/${accountListId}/${pagePath}/${contactId}`);
(pagePath: string, contactId: string, tab?: TabKey) => {
const pathname = `/accountLists/${accountListId}/${pagePath}/${contactId}`;
tab
? push({
pathname,
query: { tab },
})
: push(pathname);
},
[accountListId],
);
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we already have this functionailty on line 61 - 63. Sorry, I didn't catch that before. It uses query?.tab instead but is the same thing.

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const ContactDetailProvider: React.FC<Props> = ({ children }) => {
const [editOtherModalOpen, setEditOtherModalOpen] = useState(false);
const [editMailingModalOpen, setEditMailingModalOpen] = useState(false);
const [selectedTabKey, setSelectedTabKey] = React.useState(
query?.tab ? TabKey[query?.tab.toString()] ?? TabKey.Tasks : TabKey.Tasks,
query?.tab ? TabKey[query.tab.toString()] ?? TabKey.Tasks : TabKey.Tasks,
);
const handleTabChange = (
_event: React.ChangeEvent<Record<string, unknown>>,
Expand Down
Loading
Loading