Skip to content

Commit 0890a52

Browse files
Updated test file
1 parent 3d3c708 commit 0890a52

File tree

1 file changed

+31
-57
lines changed

1 file changed

+31
-57
lines changed

contentcuration/contentcuration/frontend/administration/pages/Users/__tests__/emailUsersDialog.spec.js

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,25 @@ const renderComponent = (props = {}) => {
6767
routes: new VueRouter(),
6868
});
6969
};
70-
7170
describe('EmailUsersDialog', () => {
7271
beforeEach(() => {
7372
jest.clearAllMocks();
7473
window.senderEmail = '[email protected]';
7574
});
7675

77-
it('shows email dialog with correct title', () => {
76+
it('renders email dialog with correct title and sender', () => {
7877
renderComponent();
7978
expect(screen.getByText('Send Email')).toBeInTheDocument();
80-
});
81-
82-
it('displays sender email in From field', () => {
83-
renderComponent();
8479
expect(screen.getByText('[email protected]')).toBeInTheDocument();
8580
});
8681

87-
it('displays individual user chips when initialRecipients are provided', () => {
82+
it('shows user chips when initialRecipients are provided', () => {
8883
renderComponent({ initialRecipients: [userId, userId2] });
8984
expect(screen.getByText('Testy User')).toBeInTheDocument();
9085
expect(screen.getByText('Testier User')).toBeInTheDocument();
9186
});
9287

93-
it('displays filter description when usersFilterFetchQueryParams are provided', () => {
88+
it('shows filter description when usersFilterFetchQueryParams exist', () => {
9489
renderComponent({
9590
userTypeFilter: 'active',
9691
locationFilter: 'Czech Republic',
@@ -106,13 +101,11 @@ describe('EmailUsersDialog', () => {
106101
).toBeInTheDocument();
107102
});
108103

109-
it('displays all placeholder buttons', () => {
104+
it('shows placeholder buttons', () => {
110105
renderComponent();
111-
expect(screen.getByText('First name')).toBeInTheDocument();
112-
expect(screen.getByText('Last name')).toBeInTheDocument();
113-
expect(screen.getByText('Email')).toBeInTheDocument();
114-
expect(screen.getByText('Date')).toBeInTheDocument();
115-
expect(screen.getByText('Time')).toBeInTheDocument();
106+
['First name', 'Last name', 'Email', 'Date', 'Time'].forEach(text =>
107+
expect(screen.getByText(text)).toBeInTheDocument(),
108+
);
116109
});
117110

118111
describe('form validation', () => {
@@ -122,36 +115,30 @@ describe('EmailUsersDialog', () => {
122115

123116
await user.click(screen.getByText('Send email'));
124117

125-
const errorMessages = screen.getAllByText('Field is required');
126-
expect(errorMessages.length).toBeGreaterThan(0);
118+
expect(screen.getAllByText('Field is required').length).toBeGreaterThan(0);
127119
expect(mockActions.sendEmail).not.toHaveBeenCalled();
128120
});
129121

130-
it('does not submit when subject is empty', async () => {
122+
it('does not submit with missing subject or message', async () => {
131123
const user = userEvent.setup();
132124
renderComponent({ initialRecipients: [userId] });
133125

134126
const messageInput = screen.getByLabelText(/email body/i);
127+
const subjectInput = screen.getByLabelText(/subject line/i);
128+
135129
await user.type(messageInput, 'Test Message');
136130
await user.click(screen.getByText('Send email'));
137-
138131
expect(mockActions.sendEmail).not.toHaveBeenCalled();
139-
});
140-
141-
it('does not submit when message is empty', async () => {
142-
const user = userEvent.setup();
143-
renderComponent({ initialRecipients: [userId] });
144132

145-
const subjectInput = screen.getByLabelText(/subject line/i);
133+
await user.clear(messageInput);
146134
await user.type(subjectInput, 'Test Subject');
147135
await user.click(screen.getByText('Send email'));
148-
149136
expect(mockActions.sendEmail).not.toHaveBeenCalled();
150137
});
151138
});
152139

153-
describe('placeholder functionality', () => {
154-
it('adds placeholder to message when placeholder button is clicked', async () => {
140+
describe('placeholders', () => {
141+
it('adds placeholder text to message when clicked', async () => {
155142
const user = userEvent.setup();
156143
renderComponent({ initialRecipients: [userId] });
157144

@@ -163,34 +150,26 @@ describe('EmailUsersDialog', () => {
163150
});
164151
});
165152

166-
describe('with individual users', () => {
167-
it('calls sendEmail with correct arguments when form is submitted', async () => {
153+
describe('recipients handling', () => {
154+
it('submits with correct arguments for individual users', async () => {
168155
const user = userEvent.setup();
169156
renderComponent({ initialRecipients: [userId, userId2] });
170157

171-
expect(screen.getByText('Testy User')).toBeInTheDocument();
172-
expect(screen.getByText('Testier User')).toBeInTheDocument();
173-
174158
await user.type(screen.getByLabelText(/subject line/i), 'Test Subject');
175159
await user.type(screen.getByLabelText(/email body/i), 'Test Message');
176160
await user.click(screen.getByText('Send email'));
177161

178162
expect(mockActions.sendEmail).toHaveBeenCalledWith(expect.any(Object), {
179163
subject: 'Test Subject',
180164
message: 'Test Message',
181-
query: {
182-
ids: `${userId},${userId2}`,
183-
},
165+
query: { ids: `${userId},${userId2}` },
184166
});
185167
});
186168

187-
it('removes user from recipients when remove button is clicked', async () => {
169+
it('removes a recipient when the remove button is clicked', async () => {
188170
const user = userEvent.setup();
189171
renderComponent({ initialRecipients: [userId, userId2] });
190172

191-
expect(screen.getByText('Testy User')).toBeInTheDocument();
192-
expect(screen.getByText('Testier User')).toBeInTheDocument();
193-
194173
const removeButton = screen.getByRole('button', { name: 'Remove Testy User' });
195174
await user.click(removeButton);
196175

@@ -199,8 +178,8 @@ describe('EmailUsersDialog', () => {
199178
});
200179
});
201180

202-
describe('with user filters', () => {
203-
it('calls sendEmail with filter parameters when form is submitted', async () => {
181+
describe('filtered users', () => {
182+
it('calls sendEmail with filter parameters', async () => {
204183
const user = userEvent.setup();
205184
renderComponent({
206185
usersFilterFetchQueryParams: {
@@ -226,8 +205,8 @@ describe('EmailUsersDialog', () => {
226205
});
227206
});
228207

229-
describe('warning modal', () => {
230-
it('shows warning modal when canceling with draft content', async () => {
208+
describe('draft warning modal', () => {
209+
it('shows modal when canceling with draft content', async () => {
231210
const user = userEvent.setup();
232211
renderComponent({ initialRecipients: [userId] });
233212

@@ -242,7 +221,7 @@ describe('EmailUsersDialog', () => {
242221
).toBeInTheDocument();
243222
});
244223

245-
it('does not show warning modal when canceling without draft content', async () => {
224+
it('does not show modal when no draft content', async () => {
246225
const user = userEvent.setup();
247226
renderComponent({ initialRecipients: [userId] });
248227

@@ -251,37 +230,32 @@ describe('EmailUsersDialog', () => {
251230
expect(screen.queryByText('Draft in progress')).not.toBeInTheDocument();
252231
});
253232

254-
it('closes dialog when confirming discard in warning modal', async () => {
233+
it('closes dialog when confirming discard', async () => {
255234
const user = userEvent.setup();
256235
const { emitted } = renderComponent({ initialRecipients: [userId] });
257236

258237
await user.type(screen.getByLabelText(/subject line/i), 'Draft Subject');
259238
await user.click(screen.getByText('Cancel'));
239+
await user.click(screen.getByRole('button', { name: /discard draft/i }));
260240

261-
const discardButton = screen.getByRole('button', { name: /discard draft/i });
262-
await user.click(discardButton);
263-
264-
expect(emitted().input).toBeTruthy();
265-
expect(emitted().input[0]).toEqual([false]);
241+
expect(emitted().input?.[0]).toEqual([false]);
266242
});
267243

268-
it('keeps dialog open when canceling warning modal', async () => {
244+
it('keeps dialog open when canceling discard', async () => {
269245
const user = userEvent.setup();
270246
renderComponent({ initialRecipients: [userId] });
271247

272248
await user.type(screen.getByLabelText(/subject line/i), 'Draft Subject');
273249
await user.click(screen.getByText('Cancel'));
274-
275-
const keepOpenButton = screen.getByRole('button', { name: /keep open/i });
276-
await user.click(keepOpenButton);
250+
await user.click(screen.getByRole('button', { name: /keep open/i }));
277251

278252
expect(screen.getByText('Send Email')).toBeInTheDocument();
279253
expect(screen.queryByText('Draft in progress')).not.toBeInTheDocument();
280254
});
281255
});
282256

283-
describe('success and error handling', () => {
284-
it('shows success snackbar when email is sent successfully', async () => {
257+
describe('success & error handling', () => {
258+
it('shows success snackbar when email sends successfully', async () => {
285259
const user = userEvent.setup();
286260
renderComponent({ initialRecipients: [userId] });
287261

@@ -292,7 +266,7 @@ describe('EmailUsersDialog', () => {
292266
expect(mockActions.showSnackbarSimple).toHaveBeenCalledWith(expect.any(Object), 'Email sent');
293267
});
294268

295-
it('shows error snackbar when email fails to send', async () => {
269+
it('shows error snackbar when sending fails', async () => {
296270
mockActions.sendEmail.mockRejectedValueOnce(new Error('Send failed'));
297271
const user = userEvent.setup();
298272
renderComponent({ initialRecipients: [userId] });

0 commit comments

Comments
 (0)