Skip to content

Commit

Permalink
Set the initial dropdown value according to the same logic as in the …
Browse files Browse the repository at this point in the history
…Angular app
  • Loading branch information
wrandall22 committed Jul 11, 2024
1 parent c8e0a00 commit 54f1cea
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
135 changes: 135 additions & 0 deletions src/components/Tool/FixSendNewsletter/Contact.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { ThemeProvider } from '@mui/material/styles';
import { render } from '@testing-library/react';
import theme from 'src/theme';
import Contact from './Contact';
import {
ContactPrimaryAddressFragment,
ContactPrimaryPersonFragment,
} from './GetInvalidNewsletter.generated';

const TestComponent = ({
primaryPerson,
primaryAddress,
}: {
primaryPerson: ContactPrimaryPersonFragment;
primaryAddress: ContactPrimaryAddressFragment;
}) => (
<ThemeProvider theme={theme}>
<Contact
id=""
name=""
primaryPerson={primaryPerson}
status=""
primaryAddress={primaryAddress}
updateFunction={jest.fn()}
setContactFocus={jest.fn()}
/>
</ThemeProvider>
);

describe('Fix Newsletter - Contact', () => {
describe('inital value for dropdown', () => {
let primaryPerson = {} as ContactPrimaryPersonFragment;
let primaryAddress = {} as ContactPrimaryAddressFragment;

beforeEach(() => {
primaryPerson = {
firstName: '',
lastName: '',
primaryEmailAddress: {
email: '',
},
optoutEnewsletter: false,
} as ContactPrimaryPersonFragment;

primaryAddress = {
street: '',
city: '',
state: '',
postalCode: '',
source: '',
createdAt: '',
} as ContactPrimaryAddressFragment;
});

it('should be None', () => {
const { getByRole } = render(
<TestComponent
primaryPerson={primaryPerson}
primaryAddress={primaryAddress}
/>,
);

expect(getByRole('combobox')).toHaveDisplayValue(['None']);
});

it('should be Physical', () => {
primaryAddress.street = '100 Test St';

const { getByRole } = render(
<TestComponent
primaryPerson={primaryPerson}
primaryAddress={primaryAddress}
/>,
);

expect(getByRole('combobox')).toHaveDisplayValue(['Physical']);
});

it('should be Email', () => {
primaryPerson.primaryEmailAddress = { email: '[email protected]' };

const { getByRole } = render(
<TestComponent
primaryPerson={primaryPerson}
primaryAddress={primaryAddress}
/>,
);

expect(getByRole('combobox')).toHaveDisplayValue(['Email']);
});

it('should be None when opting out of emails', () => {
primaryPerson.primaryEmailAddress = { email: '[email protected]' };
primaryPerson.optoutEnewsletter = true;

const { getByRole } = render(
<TestComponent
primaryPerson={primaryPerson}
primaryAddress={primaryAddress}
/>,
);

expect(getByRole('combobox')).toHaveDisplayValue(['None']);
});

it('should be Both', () => {
primaryAddress.street = '100 Test St';
primaryPerson.primaryEmailAddress = { email: '[email protected]' };

const { getByRole } = render(
<TestComponent
primaryPerson={primaryPerson}
primaryAddress={primaryAddress}
/>,
);

expect(getByRole('combobox')).toHaveDisplayValue(['Both']);
});

it('should be Physical when opting out of emails', () => {
primaryAddress.street = '100 Test St';
primaryPerson.primaryEmailAddress = { email: '[email protected]' };
primaryPerson.optoutEnewsletter = true;

const { getByRole } = render(
<TestComponent
primaryPerson={primaryPerson}
primaryAddress={primaryAddress}
/>,
);

expect(getByRole('combobox')).toHaveDisplayValue(['Physical']);
});
});
});
23 changes: 21 additions & 2 deletions src/components/Tool/FixSendNewsletter/Contact.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ReactElement, useState } from 'react';
import React, { ReactElement, useEffect, useState } from 'react';
import { mdiCheckboxMarkedCircle } from '@mdi/js';
import { Icon } from '@mdi/react';
import {
Expand Down Expand Up @@ -99,9 +99,28 @@ const Contact = ({
setContactFocus,
}: Props): ReactElement => {
const { t } = useTranslation();
const [newsletter, setNewsletter] = useState(SendNewsletterEnum.Both);
const [newsletter, setNewsletter] = useState(SendNewsletterEnum.None);
const { classes } = useStyles();

useEffect(() => {
let newNewsletterValue = SendNewsletterEnum.None;
if (primaryAddress?.street) {
newNewsletterValue = SendNewsletterEnum.Physical;
}
if (primaryPerson) {
if (!primaryPerson.optoutEnewsletter) {
if (primaryPerson.primaryEmailAddress?.email?.length) {
if (newNewsletterValue === SendNewsletterEnum.Physical) {
newNewsletterValue = SendNewsletterEnum.Both;
} else {
newNewsletterValue = SendNewsletterEnum.Email;
}
}
}
}
setNewsletter(newNewsletterValue);
}, [primaryAddress]);

//TODO: Add button functionality

const handleChange = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ const FixSendNewsletter: React.FC<Props> = ({
primaryEmailAddress: {
email: '',
},
optoutEnewsletter: false,
}
}
key={contact.id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ fragment ContactPrimaryPerson on Person {
primaryEmailAddress {
email
}
optoutEnewsletter
}

0 comments on commit 54f1cea

Please sign in to comment.