-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddAddressModal.test.tsx
More file actions
208 lines (175 loc) · 7.45 KB
/
Copy pathAddAddressModal.test.tsx
File metadata and controls
208 lines (175 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React from 'react';
import { ThemeProvider } from '@mui/material/styles';
import { act, render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { SnackbarProvider } from 'notistack';
import { placePromise, setupMocks } from '__tests__/util/googlePlacesMock';
import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
import theme from '../../../../../../theme';
import { SetContactPrimaryAddressMutation } from '../SetPrimaryAddress.generated';
import { AddAddressModal } from './AddAddressModal';
import { CreateContactAddressMutation } from './CreateContactAddress.generated';
const accountListId = 'abc';
const contactId = '123';
const mockEnqueue = jest.fn();
jest.mock('notistack', () => ({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
...jest.requireActual('notistack'),
useSnackbar: () => {
return {
enqueueSnackbar: mockEnqueue,
};
},
}));
jest.mock('@react-google-maps/api');
const mutationSpy = jest.fn();
const handleClose = jest.fn();
const TestComponent: React.FC = () => (
<SnackbarProvider>
<ThemeProvider theme={theme}>
<GqlMockedProvider<{
CreateContactAddress: CreateContactAddressMutation;
SetContactPrimaryAddress: SetContactPrimaryAddressMutation;
}>
onCall={mutationSpy}
>
<AddAddressModal
accountListId={accountListId}
contactId={contactId}
handleClose={handleClose}
/>
</GqlMockedProvider>
</ThemeProvider>
</SnackbarProvider>
);
describe('AddAddressModal', () => {
beforeEach(() => {
setupMocks();
});
it('should render edit contact address modal', async () => {
const { getByText } = render(<TestComponent />);
expect(getByText('Add Address')).toBeInTheDocument();
});
it('should close edit contact other modal', () => {
const { getByText, getByLabelText } = render(<TestComponent />);
expect(getByText('Add Address')).toBeInTheDocument();
userEvent.click(getByLabelText('Close'));
expect(handleClose).toHaveBeenCalled();
});
it('should handle cancel click', () => {
const { getByText } = render(<TestComponent />);
expect(getByText('Add Address')).toBeInTheDocument();
userEvent.click(getByText('Cancel'));
expect(handleClose).toHaveBeenCalled();
});
it('requires at least one field to be filled', async () => {
const { getByRole } = render(<TestComponent />);
const saveButton = getByRole('button', { name: 'Save' });
await waitFor(() => expect(saveButton).toBeDisabled());
userEvent.type(getByRole('textbox', { name: 'City' }), 'City');
await waitFor(() => expect(saveButton).not.toBeDisabled());
});
it('should create contact address', async () => {
const newStreet = '4321 Neat Street';
const newCity = 'Orlando';
const newState = 'FL';
const newPostalCode = '55555';
const newCountry = 'United States';
const newRegion = 'New Region';
const newMetroArea = 'New Metro';
const { getByRole, getByText, getByLabelText } = render(<TestComponent />);
userEvent.clear(getByRole('combobox', { name: 'Street' }));
userEvent.clear(getByLabelText('City'));
userEvent.clear(getByLabelText('State'));
userEvent.clear(getByLabelText('Zip'));
userEvent.clear(getByLabelText('Country'));
userEvent.clear(getByLabelText('Region'));
userEvent.clear(getByLabelText('Metro'));
userEvent.click(getByLabelText('Location'));
userEvent.click(getByLabelText('Mailing'));
userEvent.type(getByRole('combobox', { name: 'Street' }), newStreet);
userEvent.type(getByLabelText('City'), newCity);
userEvent.type(getByLabelText('State'), newState);
userEvent.type(getByLabelText('Zip'), newPostalCode);
userEvent.type(getByLabelText('Country'), newCountry);
userEvent.type(getByLabelText('Region'), newRegion);
userEvent.type(getByLabelText('Metro'), newMetroArea);
userEvent.click(getByLabelText('Address no longer valid'));
userEvent.click(getByText('Save'));
await waitFor(() =>
expect(mockEnqueue).toHaveBeenCalledWith('Address added successfully', {
variant: 'success',
}),
);
const { operation } = mutationSpy.mock.calls[0][0];
expect(operation.variables.accountListId).toEqual(accountListId);
expect(operation.variables.attributes.street).toEqual(newStreet);
expect(operation.variables.attributes.location).toEqual('Mailing');
expect(operation.variables.attributes.city).toEqual(newCity);
expect(operation.variables.attributes.state).toEqual(newState);
expect(operation.variables.attributes.postalCode).toEqual(newPostalCode);
expect(operation.variables.attributes.country).toEqual(newCountry);
expect(operation.variables.attributes.region).toEqual(newRegion);
expect(operation.variables.attributes.metroArea).toEqual(newMetroArea);
expect(operation.variables.attributes.historic).toEqual(true);
}, 10000);
it('handles chosen address predictions', async () => {
jest.useFakeTimers();
const { getByRole } = render(<TestComponent />);
// Let Google Maps initialize
jest.runOnlyPendingTimers();
const addressAutocomplete = getByRole('combobox', { name: 'Street' });
userEvent.type(addressAutocomplete, '100 Lake Hart');
jest.advanceTimersByTime(2000);
await act(async () => {
await placePromise;
});
userEvent.click(
getByRole('option', { name: '100 Lake Hart Dr, Orlando, FL 32832, USA' }),
);
expect(addressAutocomplete).toHaveValue('100 Lake Hart Drive A');
expect(getByRole('textbox', { name: 'City' })).toHaveValue('Orlando');
expect(getByRole('textbox', { name: 'State' })).toHaveValue('FL');
expect(getByRole('textbox', { name: 'Zip' })).toHaveValue('32832');
expect(getByRole('textbox', { name: 'Country' })).toHaveValue(
'United States',
);
expect(getByRole('textbox', { name: 'Region' })).toHaveValue(
'Orange County',
);
expect(getByRole('textbox', { name: 'Metro' })).toHaveValue('Orlando');
}, 20000);
it('should set new address as primary', async () => {
const newStreet = '4321 Neat Street';
const { getByText, getByRole } = render(<TestComponent />);
const street = getByRole('combobox', { name: 'Street' });
userEvent.clear(street);
userEvent.type(street, newStreet);
userEvent.click(getByText('Save'));
await waitFor(() =>
expect(mockEnqueue).toHaveBeenCalledWith('Address added successfully', {
variant: 'success',
}),
);
const { operation } = mutationSpy.mock.calls[0][0];
expect(operation.variables.accountListId).toEqual(accountListId);
expect(operation.variables.attributes.street).toEqual(newStreet);
const { operation: operation2 } = mutationSpy.mock.calls[1][0];
expect(operation2.variables.accountListId).toEqual(accountListId);
expect(operation2.variables.primaryAddressId).not.toBeNull();
}, 30000);
it('should not set new address as primary if it is unchecked', async () => {
const newStreet = '4321 Neat Street';
const { getByText, getByLabelText, getByRole } = render(<TestComponent />);
userEvent.type(getByRole('combobox', { name: 'Street' }), newStreet);
userEvent.click(getByLabelText('Primary'));
userEvent.click(getByText('Save'));
await waitFor(() =>
expect(mockEnqueue).toHaveBeenCalledWith('Address added successfully', {
variant: 'success',
}),
);
expect(mutationSpy).toHaveBeenCalledTimes(1);
}, 30000);
});