Skip to content

Commit 200f52e

Browse files
committed
Refactor tests
1 parent 690136f commit 200f52e

2 files changed

Lines changed: 124 additions & 247 deletions

File tree

src/components/Contacts/ContactDetails/ContactDetailsTab/Mailing/AddAddressModal/AddAddressModal.test.tsx

Lines changed: 33 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { SnackbarProvider } from 'notistack';
66
import { placePromise, setupMocks } from '__tests__/util/googlePlacesMock';
77
import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
88
import theme from '../../../../../../theme';
9+
import { SetContactPrimaryAddressMutation } from '../SetPrimaryAddress.generated';
910
import { AddAddressModal } from './AddAddressModal';
11+
import { CreateContactAddressMutation } from './CreateContactAddress.generated';
1012

11-
const handleClose = jest.fn();
1213
const accountListId = 'abc';
1314
const contactId = '123';
1415

@@ -27,83 +28,57 @@ jest.mock('notistack', () => ({
2728

2829
jest.mock('@react-google-maps/api');
2930

31+
const mutationSpy = jest.fn();
32+
const handleClose = jest.fn();
33+
34+
const TestComponent: React.FC = () => (
35+
<SnackbarProvider>
36+
<ThemeProvider theme={theme}>
37+
<GqlMockedProvider<{
38+
CreateContactAddress: CreateContactAddressMutation;
39+
SetContactPrimaryAddress: SetContactPrimaryAddressMutation;
40+
}>
41+
onCall={mutationSpy}
42+
>
43+
<AddAddressModal
44+
accountListId={accountListId}
45+
contactId={contactId}
46+
handleClose={handleClose}
47+
/>
48+
</GqlMockedProvider>
49+
</ThemeProvider>
50+
</SnackbarProvider>
51+
);
52+
3053
describe('AddAddressModal', () => {
3154
beforeEach(() => {
3255
setupMocks();
3356
});
3457

3558
it('should render edit contact address modal', async () => {
36-
const { getByText } = render(
37-
<SnackbarProvider>
38-
<ThemeProvider theme={theme}>
39-
<GqlMockedProvider>
40-
<AddAddressModal
41-
accountListId={accountListId}
42-
contactId={contactId}
43-
handleClose={handleClose}
44-
/>
45-
</GqlMockedProvider>
46-
</ThemeProvider>
47-
</SnackbarProvider>,
48-
);
59+
const { getByText } = render(<TestComponent />);
4960

5061
expect(getByText('Add Address')).toBeInTheDocument();
5162
});
5263

5364
it('should close edit contact other modal', () => {
54-
const { getByText, getByLabelText } = render(
55-
<SnackbarProvider>
56-
<ThemeProvider theme={theme}>
57-
<GqlMockedProvider>
58-
<AddAddressModal
59-
accountListId={accountListId}
60-
contactId={contactId}
61-
handleClose={handleClose}
62-
/>
63-
</GqlMockedProvider>
64-
</ThemeProvider>
65-
</SnackbarProvider>,
66-
);
65+
const { getByText, getByLabelText } = render(<TestComponent />);
6766

6867
expect(getByText('Add Address')).toBeInTheDocument();
6968
userEvent.click(getByLabelText('Close'));
7069
expect(handleClose).toHaveBeenCalled();
7170
});
7271

7372
it('should handle cancel click', () => {
74-
const { getByText } = render(
75-
<SnackbarProvider>
76-
<ThemeProvider theme={theme}>
77-
<GqlMockedProvider>
78-
<AddAddressModal
79-
accountListId={accountListId}
80-
contactId={contactId}
81-
handleClose={handleClose}
82-
/>
83-
</GqlMockedProvider>
84-
</ThemeProvider>
85-
</SnackbarProvider>,
86-
);
73+
const { getByText } = render(<TestComponent />);
8774

8875
expect(getByText('Add Address')).toBeInTheDocument();
8976
userEvent.click(getByText('Cancel'));
9077
expect(handleClose).toHaveBeenCalled();
9178
});
9279

9380
it('requires at least one field to be filled', async () => {
94-
const { getByRole } = render(
95-
<SnackbarProvider>
96-
<ThemeProvider theme={theme}>
97-
<GqlMockedProvider>
98-
<AddAddressModal
99-
accountListId={accountListId}
100-
contactId={contactId}
101-
handleClose={handleClose}
102-
/>
103-
</GqlMockedProvider>
104-
</ThemeProvider>
105-
</SnackbarProvider>,
106-
);
81+
const { getByRole } = render(<TestComponent />);
10782

10883
const saveButton = getByRole('button', { name: 'Save' });
10984
await waitFor(() => expect(saveButton).toBeDisabled());
@@ -113,27 +88,14 @@ describe('AddAddressModal', () => {
11388
});
11489

11590
it('should create contact address', async () => {
116-
const mutationSpy = jest.fn();
11791
const newStreet = '4321 Neat Street';
11892
const newCity = 'Orlando';
11993
const newState = 'FL';
12094
const newPostalCode = '55555';
12195
const newCountry = 'United States';
12296
const newRegion = 'New Region';
12397
const newMetroArea = 'New Metro';
124-
const { getByRole, getByText, getByLabelText } = render(
125-
<SnackbarProvider>
126-
<ThemeProvider theme={theme}>
127-
<GqlMockedProvider onCall={mutationSpy}>
128-
<AddAddressModal
129-
accountListId={accountListId}
130-
contactId={contactId}
131-
handleClose={handleClose}
132-
/>
133-
</GqlMockedProvider>
134-
</ThemeProvider>
135-
</SnackbarProvider>,
136-
);
98+
const { getByRole, getByText, getByLabelText } = render(<TestComponent />);
13799

138100
userEvent.clear(getByRole('combobox', { name: 'Street' }));
139101
userEvent.clear(getByLabelText('City'));
@@ -176,19 +138,7 @@ describe('AddAddressModal', () => {
176138
it('handles chosen address predictions', async () => {
177139
jest.useFakeTimers();
178140

179-
const { getByRole } = render(
180-
<SnackbarProvider>
181-
<ThemeProvider theme={theme}>
182-
<GqlMockedProvider>
183-
<AddAddressModal
184-
accountListId={accountListId}
185-
contactId={contactId}
186-
handleClose={handleClose}
187-
/>
188-
</GqlMockedProvider>
189-
</ThemeProvider>
190-
</SnackbarProvider>,
191-
);
141+
const { getByRole } = render(<TestComponent />);
192142

193143
// Let Google Maps initialize
194144
jest.runOnlyPendingTimers();
@@ -218,21 +168,8 @@ describe('AddAddressModal', () => {
218168
}, 20000);
219169

220170
it('should set new address as primary', async () => {
221-
const mutationSpy = jest.fn();
222171
const newStreet = '4321 Neat Street';
223-
const { getByText, getByRole } = render(
224-
<SnackbarProvider>
225-
<ThemeProvider theme={theme}>
226-
<GqlMockedProvider onCall={mutationSpy}>
227-
<AddAddressModal
228-
accountListId={accountListId}
229-
contactId={contactId}
230-
handleClose={handleClose}
231-
/>
232-
</GqlMockedProvider>
233-
</ThemeProvider>
234-
</SnackbarProvider>,
235-
);
172+
const { getByText, getByRole } = render(<TestComponent />);
236173

237174
const street = getByRole('combobox', { name: 'Street' });
238175
userEvent.clear(street);
@@ -249,25 +186,13 @@ describe('AddAddressModal', () => {
249186
expect(operation.variables.attributes.street).toEqual(newStreet);
250187

251188
const { operation: operation2 } = mutationSpy.mock.calls[1][0];
189+
expect(operation2.variables.accountListId).toEqual(accountListId);
252190
expect(operation2.variables.primaryAddressId).not.toBeNull();
253191
}, 30000);
254192

255193
it('should not set new address as primary if it is unchecked', async () => {
256-
const mutationSpy = jest.fn();
257194
const newStreet = '4321 Neat Street';
258-
const { getByText, getByLabelText, getByRole } = render(
259-
<SnackbarProvider>
260-
<ThemeProvider theme={theme}>
261-
<GqlMockedProvider onCall={mutationSpy}>
262-
<AddAddressModal
263-
accountListId={accountListId}
264-
contactId={contactId}
265-
handleClose={handleClose}
266-
/>
267-
</GqlMockedProvider>
268-
</ThemeProvider>
269-
</SnackbarProvider>,
270-
);
195+
const { getByText, getByLabelText, getByRole } = render(<TestComponent />);
271196

272197
userEvent.type(getByRole('combobox', { name: 'Street' }), newStreet);
273198
userEvent.click(getByLabelText('Primary'));

0 commit comments

Comments
 (0)