|
1 | | -import { render, screen, fireEvent, act, within } from '@testing-library/react'; |
| 1 | +import { render, screen, fireEvent, act } from '@testing-library/react'; |
2 | 2 | import { Tasks } from '../Tasks'; |
3 | 3 |
|
4 | 4 | // Mock props for the Tasks component |
@@ -180,117 +180,6 @@ describe('Tasks Component', () => { |
180 | 180 | expect(screen.getByTestId('current-page')).toHaveTextContent('1'); |
181 | 181 | }); |
182 | 182 |
|
183 | | - test('shows tags as badges in task dialog and allows editing (add on Enter)', async () => { |
184 | | - render(<Tasks {...mockProps} />); |
185 | | - |
186 | | - expect(await screen.findByText('Task 1')).toBeInTheDocument(); |
187 | | - |
188 | | - const taskRow = screen.getByText('Task 1'); |
189 | | - fireEvent.click(taskRow); |
190 | | - |
191 | | - expect(await screen.findByText('Tags:')).toBeInTheDocument(); |
192 | | - |
193 | | - expect(screen.getByText('tag1')).toBeInTheDocument(); |
194 | | - |
195 | | - const tagsLabel = screen.getByText('Tags:'); |
196 | | - const tagsRow = tagsLabel.closest('tr') as HTMLElement; |
197 | | - const pencilButton = within(tagsRow).getByRole('button'); |
198 | | - fireEvent.click(pencilButton); |
199 | | - |
200 | | - const editInput = await screen.findByPlaceholderText( |
201 | | - 'Add a tag (press enter to add)' |
202 | | - ); |
203 | | - |
204 | | - fireEvent.change(editInput, { target: { value: 'newtag' } }); |
205 | | - fireEvent.keyDown(editInput, { key: 'Enter', code: 'Enter' }); |
206 | | - |
207 | | - expect(await screen.findByText('newtag')).toBeInTheDocument(); |
208 | | - |
209 | | - expect((editInput as HTMLInputElement).value).toBe(''); |
210 | | - }); |
211 | | - |
212 | | - test('adds a tag while editing and saves updated tags to backend', async () => { |
213 | | - render(<Tasks {...mockProps} />); |
214 | | - |
215 | | - expect(await screen.findByText('Task 1')).toBeInTheDocument(); |
216 | | - |
217 | | - const taskRow = screen.getByText('Task 1'); |
218 | | - fireEvent.click(taskRow); |
219 | | - |
220 | | - expect(await screen.findByText('Tags:')).toBeInTheDocument(); |
221 | | - |
222 | | - const tagsLabel = screen.getByText('Tags:'); |
223 | | - const tagsRow = tagsLabel.closest('tr') as HTMLElement; |
224 | | - const pencilButton = within(tagsRow).getByRole('button'); |
225 | | - fireEvent.click(pencilButton); |
226 | | - |
227 | | - const editInput = await screen.findByPlaceholderText( |
228 | | - 'Add a tag (press enter to add)' |
229 | | - ); |
230 | | - |
231 | | - fireEvent.change(editInput, { target: { value: 'addedtag' } }); |
232 | | - fireEvent.keyDown(editInput, { key: 'Enter', code: 'Enter' }); |
233 | | - |
234 | | - expect(await screen.findByText('addedtag')).toBeInTheDocument(); |
235 | | - |
236 | | - const actionContainer = editInput.parentElement as HTMLElement; |
237 | | - const actionButtons = within(actionContainer).getAllByRole('button'); |
238 | | - fireEvent.click(actionButtons[0]); |
239 | | - |
240 | | - const hooks = require('../hooks'); |
241 | | - expect(hooks.editTaskOnBackend).toHaveBeenCalled(); |
242 | | - |
243 | | - const callArg = hooks.editTaskOnBackend.mock.calls[0][0]; |
244 | | - expect(callArg.tags).toEqual(expect.arrayContaining(['tag1', 'addedtag'])); |
245 | | - }); |
246 | | - |
247 | | - test('removes a tag while editing and saves updated tags to backend', async () => { |
248 | | - render(<Tasks {...mockProps} />); |
249 | | - |
250 | | - expect(await screen.findByText('Task 1')).toBeInTheDocument(); |
251 | | - |
252 | | - const taskRow = screen.getByText('Task 1'); |
253 | | - fireEvent.click(taskRow); |
254 | | - |
255 | | - expect(await screen.findByText('Tags:')).toBeInTheDocument(); |
256 | | - |
257 | | - const tagsLabel = screen.getByText('Tags:'); |
258 | | - const tagsRow = tagsLabel.closest('tr') as HTMLElement; |
259 | | - const pencilButton = within(tagsRow).getByRole('button'); |
260 | | - fireEvent.click(pencilButton); |
261 | | - |
262 | | - const editInput = await screen.findByPlaceholderText( |
263 | | - 'Add a tag (press enter to add)' |
264 | | - ); |
265 | | - |
266 | | - fireEvent.change(editInput, { target: { value: 'newtag' } }); |
267 | | - fireEvent.keyDown(editInput, { key: 'Enter', code: 'Enter' }); |
268 | | - |
269 | | - expect(await screen.findByText('newtag')).toBeInTheDocument(); |
270 | | - |
271 | | - const tagBadge = screen.getByText('tag1'); |
272 | | - const badgeContainer = (tagBadge.closest('div') || |
273 | | - tagBadge.parentElement) as HTMLElement; |
274 | | - |
275 | | - const removeButton = within(badgeContainer).getByText('✖'); |
276 | | - fireEvent.click(removeButton); |
277 | | - |
278 | | - expect(screen.queryByText('tag2')).not.toBeInTheDocument(); |
279 | | - |
280 | | - const actionContainer = editInput.parentElement as HTMLElement; |
281 | | - |
282 | | - const actionButtons = within(actionContainer).getAllByRole('button'); |
283 | | - |
284 | | - fireEvent.click(actionButtons[0]); |
285 | | - |
286 | | - const hooks = require('../hooks'); |
287 | | - expect(hooks.editTaskOnBackend).toHaveBeenCalled(); |
288 | | - |
289 | | - const callArg = hooks.editTaskOnBackend.mock.calls[0][0]; |
290 | | - |
291 | | - expect(callArg.tags).toEqual(expect.arrayContaining(['newtag', '-tag1'])); |
292 | | - }); |
293 | | - |
294 | 183 | test('shows red background on task ID and Overdue badge for overdue tasks', async () => { |
295 | 184 | render(<Tasks {...mockProps} />); |
296 | 185 |
|
@@ -336,12 +225,13 @@ describe('Tasks Component', () => { |
336 | 225 | test('renders mocked TagSelector in Add Task dialog', async () => { |
337 | 226 | render(<Tasks {...mockProps} />); |
338 | 227 |
|
339 | | - const addButton = screen.getAllByText('Add Task')[0]; |
| 228 | + const addButton = screen.getByRole('button', { name: /add task/i }); |
| 229 | + |
340 | 230 | fireEvent.click(addButton); |
341 | 231 |
|
342 | 232 | expect(await screen.findByTestId('mock-tag-selector')).toBeInTheDocument(); |
343 | 233 | }); |
344 | | - |
| 234 | + |
345 | 235 | test('TagSelector receives correct options and selected values', async () => { |
346 | 236 | render(<Tasks {...mockProps} />); |
347 | 237 |
|
|
0 commit comments