|
| 1 | +import { fireEvent, render } from '@testing-library/react'; |
| 2 | +import type { ComponentProps } from 'react'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | +import type { Project, Section } from '@mindwtr/core'; |
| 5 | + |
| 6 | +import { LanguageProvider } from '../../../contexts/language-context'; |
| 7 | +import { TaskBulkOrganizeModal } from './TaskBulkOrganizeModal'; |
| 8 | + |
| 9 | +const t = (key: string) => key; |
| 10 | + |
| 11 | +const project: Project = { |
| 12 | + id: 'project-1', |
| 13 | + title: 'Launch', |
| 14 | + color: '#3b82f6', |
| 15 | + order: 0, |
| 16 | + status: 'active', |
| 17 | + tagIds: [], |
| 18 | + createdAt: '2026-05-12T00:00:00.000Z', |
| 19 | + updatedAt: '2026-05-12T00:00:00.000Z', |
| 20 | +}; |
| 21 | + |
| 22 | +const otherProject: Project = { ...project, id: 'project-2', title: 'Rewrite' }; |
| 23 | + |
| 24 | +const section: Section = { |
| 25 | + id: 'section-1', |
| 26 | + projectId: project.id, |
| 27 | + title: 'Planning', |
| 28 | + order: 0, |
| 29 | + createdAt: '2026-05-12T00:00:00.000Z', |
| 30 | + updatedAt: '2026-05-12T00:00:00.000Z', |
| 31 | +}; |
| 32 | + |
| 33 | +type Props = ComponentProps<typeof TaskBulkOrganizeModal>; |
| 34 | + |
| 35 | +const renderModal = (overrides: Partial<Props> = {}) => { |
| 36 | + const onApply = vi.fn(); |
| 37 | + const result = render( |
| 38 | + <LanguageProvider> |
| 39 | + <TaskBulkOrganizeModal |
| 40 | + isOpen |
| 41 | + selectedCount={2} |
| 42 | + projects={[project, otherProject]} |
| 43 | + areas={[]} |
| 44 | + isApplying={false} |
| 45 | + t={t} |
| 46 | + onApply={onApply} |
| 47 | + onCancel={vi.fn()} |
| 48 | + {...overrides} |
| 49 | + /> |
| 50 | + </LanguageProvider> |
| 51 | + ); |
| 52 | + return { ...result, onApply }; |
| 53 | +}; |
| 54 | + |
| 55 | +describe('TaskBulkOrganizeModal section picker', () => { |
| 56 | + it('hides the section picker outside a single-project scope', () => { |
| 57 | + const { queryByRole } = renderModal(); |
| 58 | + expect(queryByRole('combobox', { name: 'Project section' })).toBeNull(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('hides the section picker for a project without sections', () => { |
| 62 | + const { queryByRole } = renderModal({ sectionScope: { projectId: project.id, sections: [] } }); |
| 63 | + expect(queryByRole('combobox', { name: 'Project section' })).toBeNull(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('sends the chosen section with the project that owns it', () => { |
| 67 | + const { getByRole, onApply } = renderModal({ |
| 68 | + sectionScope: { projectId: project.id, sections: [section] }, |
| 69 | + }); |
| 70 | + |
| 71 | + fireEvent.change(getByRole('combobox', { name: 'Project section' }), { |
| 72 | + target: { value: section.id }, |
| 73 | + }); |
| 74 | + fireEvent.click(getByRole('button', { name: 'Apply to selected' })); |
| 75 | + |
| 76 | + expect(onApply).toHaveBeenCalledWith(expect.objectContaining({ |
| 77 | + sectionId: section.id, |
| 78 | + sectionProjectId: project.id, |
| 79 | + })); |
| 80 | + }); |
| 81 | + |
| 82 | + it('sends a null section id when clearing the section', () => { |
| 83 | + const { getByRole, onApply } = renderModal({ |
| 84 | + sectionScope: { projectId: project.id, sections: [section] }, |
| 85 | + }); |
| 86 | + |
| 87 | + fireEvent.change(getByRole('combobox', { name: 'Project section' }), { |
| 88 | + target: { value: '__NONE__' }, |
| 89 | + }); |
| 90 | + fireEvent.click(getByRole('button', { name: 'Apply to selected' })); |
| 91 | + |
| 92 | + expect(onApply).toHaveBeenCalledWith(expect.objectContaining({ sectionId: null })); |
| 93 | + }); |
| 94 | + |
| 95 | + it('keeps the section out of the apply when the modal moves tasks to another project', () => { |
| 96 | + const { getByRole, onApply } = renderModal({ |
| 97 | + sectionScope: { projectId: project.id, sections: [section] }, |
| 98 | + }); |
| 99 | + |
| 100 | + const sectionSelect = getByRole('combobox', { name: 'Project section' }); |
| 101 | + fireEvent.change(sectionSelect, { target: { value: section.id } }); |
| 102 | + fireEvent.change(getByRole('combobox', { name: 'Project' }), { target: { value: otherProject.id } }); |
| 103 | + |
| 104 | + expect(sectionSelect).toBeDisabled(); |
| 105 | + |
| 106 | + fireEvent.click(getByRole('button', { name: 'Apply to selected' })); |
| 107 | + |
| 108 | + const input = onApply.mock.calls[0]?.[0] as Record<string, unknown>; |
| 109 | + expect(input.projectId).toBe(otherProject.id); |
| 110 | + expect('sectionId' in input).toBe(false); |
| 111 | + }); |
| 112 | + |
| 113 | + it('omits the section when nothing is picked', () => { |
| 114 | + const { getByRole, onApply } = renderModal({ |
| 115 | + sectionScope: { projectId: project.id, sections: [section] }, |
| 116 | + }); |
| 117 | + |
| 118 | + fireEvent.click(getByRole('button', { name: 'Apply to selected' })); |
| 119 | + |
| 120 | + expect('sectionId' in (onApply.mock.calls[0]?.[0] as Record<string, unknown>)).toBe(false); |
| 121 | + }); |
| 122 | +}); |
0 commit comments