Skip to content

Commit

Permalink
[No Jira] Fix Phone Numbers - Contact Links Work (#1005)
Browse files Browse the repository at this point in the history
* Make contact link work, move error messages, styling improvements

* Make contact go to details tab and add test
  • Loading branch information
caleballdrin authored Sep 3, 2024
1 parent 5750ca7 commit e3e6a5a
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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 { getSession } from 'next-auth/react';
import { SnackbarProvider } from 'notistack';
import { I18nextProvider } from 'react-i18next';
import TestRouter from '__tests__/util/TestRouter';
import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
import { GetInvalidPhoneNumbersMocks } from 'src/components/Tool/FixPhoneNumbers/FixPhoneNumbersMocks';
import { GetInvalidPhoneNumbersQuery } from 'src/components/Tool/FixPhoneNumbers/GetInvalidPhoneNumbers.generated';
import i18n from 'src/lib/i18n';
import theme from 'src/theme';
import FixPhoneNumbersPage from './[[...contactId]].page';

jest.mock('next-auth/react');
jest.mock('next/router', () => ({
useRouter: jest.fn(),
}));
jest.mock('src/lib/helpScout', () => ({
suggestArticles: jest.fn(),
}));
jest.mock('notistack', () => ({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
...jest.requireActual('notistack'),
useSnackbar: () => {
return {
enqueueSnackbar: 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 = () => (
<ThemeProvider theme={theme}>
<TestRouter>
<I18nextProvider i18n={i18n}>
<SnackbarProvider>
<GqlMockedProvider<{
GetInvalidPhoneNumbers: GetInvalidPhoneNumbersQuery;
}>
mocks={GetInvalidPhoneNumbersMocks}
>
<FixPhoneNumbersPage />
</GqlMockedProvider>
</SnackbarProvider>
</I18nextProvider>
</TestRouter>
</ThemeProvider>
);

describe('FixPhoneNumbersPage', () => {
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 { findByText, queryByTestId } = render(<Components />);
await waitFor(() =>
expect(queryByTestId('LoadingSpinner')).not.toBeInTheDocument(),
);
const contactName = await findByText('Test Contact');

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

await waitFor(() => {
expect(pushFn).toHaveBeenCalledWith(
`/accountLists/${accountListId}/tools/fix/phoneNumbers/${GetInvalidPhoneNumbersMocks.GetInvalidPhoneNumbers.people.nodes[0].id}`,
);
});
});

it('should show errors', async () => {
const { findAllByRole, findByText } = render(<Components />);

userEvent.clear((await findAllByRole('textbox'))[0]);
expect(await findByText('This field is required')).toBeInTheDocument();
});
});
1 change: 1 addition & 0 deletions src/components/Tool/FixPhoneNumbers/Contact.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const testData = {
lastName: 'Contact',
avatar: 'https://www.example.com',
id: 'testid',
contactId: 'testid',
isNewPhoneNumber: false,
newPhoneNumber: '',
phoneNumbers: {
Expand Down
72 changes: 50 additions & 22 deletions src/components/Tool/FixPhoneNumbers/Contact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { DateTime } from 'luxon';
import { useTranslation } from 'react-i18next';
import { makeStyles } from 'tss-react/mui';
import { SetContactFocus } from 'pages/accountLists/[accountListId]/tools/useToolsHelper';
import { TabKey } from 'src/components/Contacts/ContactDetails/ContactDetails';
import useGetAppSettings from 'src/hooks/useGetAppSettings';
import { useLocale } from 'src/hooks/useLocale';
import { dateFormatShort } from 'src/lib/intlFormat';
Expand Down Expand Up @@ -135,8 +136,6 @@ interface Props {

const Contact: React.FC<Props> = ({
handleDelete,
// Remove below line when function is being used.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
setContactFocus,
handleUpdate,
errors,
Expand All @@ -153,14 +152,8 @@ const Contact: React.FC<Props> = ({
const numbers: PersonPhoneNumberFragment[] = person.phoneNumbers.nodes || [];
const name: string = `${person.firstName} ${person.lastName}`;

//TODO: Add button functionality
//TODO: Make name pop up a modal to edit the person info

const handleContactNameClick = () => {
// This currently doesn't work as we need to add the contactId onto the person graphQL endpoint.
// I've asked Andrew to add it here: https://cru-main.slack.com/archives/CG47BDCG6/p1718721024211409
// You'll need that to run the below function
// setContactFocus(id);
setContactFocus(person.contactId, TabKey.ContactDetails);
};

return (
Expand All @@ -180,7 +173,12 @@ const Contact: React.FC<Props> = ({
}
title={
<Link underline="hover" onClick={handleContactNameClick}>
<Typography variant="subtitle1">{name}</Typography>
<Typography
variant="subtitle1"
sx={{ display: 'inline' }}
>
{name}
</Typography>
</Link>
}
action={
Expand Down Expand Up @@ -301,7 +299,10 @@ const Contact: React.FC<Props> = ({
{t('Source')}:
</Typography>
</Hidden>
<Tooltip title="Set as Primary">
<Tooltip
title={t('Set as Primary')}
placement="left"
>
<StarOutlineIcon
data-testid={`starOutlineIcon-${person.id}-${index}`}
className={classes.hoverHighlight}
Expand Down Expand Up @@ -384,12 +385,6 @@ const Contact: React.FC<Props> = ({
value={phoneNumber.number}
disabled={phoneNumber.source !== 'MPDX'}
/>
<FormHelperText error={true}>
{
errors?.people?.[personIndex]?.phoneNumbers
?.nodes?.[index]?.number
}
</FormHelperText>
</FormControl>

{phoneNumber.source === 'MPDX' ? (
Expand All @@ -406,7 +401,10 @@ const Contact: React.FC<Props> = ({
}
className={classes.paddingX}
>
<Tooltip title="Delete Number">
<Tooltip
title={t('Delete Number')}
placement="left"
>
<Icon
path={mdiDelete}
size={1}
Expand All @@ -432,6 +430,24 @@ const Contact: React.FC<Props> = ({
)}
</Box>
</Grid>
{errors?.people?.[personIndex]?.phoneNumbers?.nodes?.[
index
]?.number && (
<>
<Grid item xs={12} sm={6}></Grid>
<Grid item xs={12} sm={6} pb={'10px'}>
<FormHelperText
error={true}
className={classes.paddingX}
>
{
errors?.people?.[personIndex]?.phoneNumbers
?.nodes?.[index]?.number
}
</FormHelperText>
</Grid>
</>
)}
</Fragment>
))}
<Grid item xs={12} sm={6} className={classes.paddingB2}>
Expand Down Expand Up @@ -491,9 +507,6 @@ const Contact: React.FC<Props> = ({
}}
value={values.people[personIndex].newPhoneNumber}
/>
<FormHelperText error={true}>
{errors?.people?.[personIndex]?.newPhoneNumber}
</FormHelperText>
</FormControl>
<Box
className={classes.paddingX}
Expand Down Expand Up @@ -525,7 +538,9 @@ const Contact: React.FC<Props> = ({
: personValue,
),
};
setValues(updatedValues as FormValues);
if (values.people[personIndex].newPhoneNumber) {
setValues(updatedValues as FormValues);
}
}}
data-testid={`addButton-${person.id}`}
>
Expand All @@ -539,6 +554,19 @@ const Contact: React.FC<Props> = ({
</Box>
</Box>
</Grid>
{errors?.people?.[personIndex]?.newPhoneNumber && (
<>
<Grid item xs={12} sm={6}></Grid>
<Grid item xs={12} sm={6}>
<FormHelperText
error={true}
className={classes.paddingX}
>
{errors?.people?.[personIndex]?.newPhoneNumber}
</FormHelperText>
</Grid>
</>
)}
</Grid>
</CardContent>
</Grid>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Tool/FixPhoneNumbers/DeleteModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('FixPhoneNumbers-DeleteModal', () => {

expect(getByText('Confirm')).toBeInTheDocument();
expect(
getByText('Are you sure you wish to delete this phone number:'),
getByText('Are you sure you wish to delete this phone number?'),
).toBeInTheDocument();
expect(getByText('"623533895895"')).toBeInTheDocument();
expect(getByText('Cancel')).toBeInTheDocument();
Expand Down
8 changes: 6 additions & 2 deletions src/components/Tool/FixPhoneNumbers/DeleteModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ const DeleteModal: React.FC<Props> = ({
justifyContent="center"
>
<Typography>
{t('Are you sure you wish to delete this phone number:')}
{t('Are you sure you wish to delete this phone number?')}
</Typography>
<Typography>{`"${modalState.phoneNumber}"`}</Typography>
{modalState.phoneNumber && (
<Typography
sx={{ fontWeight: 'bold' }}
>{`"${modalState.phoneNumber}"`}</Typography>
)}
</Box>
</CardContent>
<CardActions>
Expand Down
59 changes: 4 additions & 55 deletions src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,19 @@ import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
import { render, waitFor } from '__tests__/util/testingLibraryReactMock';
import theme from '../../../theme';
import FixPhoneNumbers from './FixPhoneNumbers';
import { GetInvalidPhoneNumbersMocks } from './FixPhoneNumbersMocks';
import { GetInvalidPhoneNumbersQuery } from './GetInvalidPhoneNumbers.generated';

const accountListId = 'test121';

const testData =
GetInvalidPhoneNumbersMocks.GetInvalidPhoneNumbers.people.nodes;

const router = {
query: { accountListId },
isReady: true,
};
const setContactFocus = jest.fn();
const testData: ErgonoMockShape[] = [
{
id: 'testid',
firstName: 'Test',
lastName: 'Contact',
phoneNumbers: {
nodes: [
{
id: 'id1',
updatedAt: new Date('2021-06-21T03:40:05-06:00').toISOString(),
number: '+3533895895',
primary: true,
source: 'MPDX',
},
{
id: 'id2',
updatedAt: new Date('2021-06-21T03:40:05-06:00').toISOString(),
number: '3533895895',
primary: false,
source: 'MPDX',
},
{
id: 'id3',
updatedAt: new Date('2021-06-21T03:40:05-06:00').toISOString(),
number: '+623533895895',
primary: false,
source: 'MPDX',
},
],
},
},
{
id: 'testid2',
firstName: 'Simba',
lastName: 'Lion',
phoneNumbers: {
nodes: [
{
id: 'id4',
updatedAt: new Date('2021-06-21T03:40:05-06:00').toISOString(),
number: '+3535785056',
primary: true,
source: 'MPDX',
},
{
id: 'id5',
updatedAt: new Date('2021-06-22T03:40:05-06:00').toISOString(),
number: '+623535785056',
primary: false,
source: 'MPDX',
},
],
},
},
];

const mockEnqueue = jest.fn();
jest.mock('notistack', () => ({
Expand Down
5 changes: 3 additions & 2 deletions src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ const useStyles = makeStyles()(() => ({
display: 'flex',
flexDirection: 'row',
justifyContent: 'end',
width: '70%',
[theme.breakpoints.down('sm')]: {
width: '80%',
maxWidth: '1500px',
[theme.breakpoints.down('md')]: {
width: '100%',
},
},
Expand Down
Loading

0 comments on commit e3e6a5a

Please sign in to comment.