|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import { useTaskStore } from '@mindwtr/core'; |
| 4 | + |
| 5 | +import { LanguageProvider } from '../contexts/language-context'; |
| 6 | +import { QuickAddModal } from './QuickAddModal'; |
| 7 | + |
| 8 | +const coreSpies = vi.hoisted(() => ({ |
| 9 | + parseQuickAdd: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +// The preview and the submit path have to run ONE parse configuration. Spying |
| 13 | +// on the shared entry point is the only way to prove they do: a preview built |
| 14 | +// from a second, hand-rolled options bag would still render plausible chips. |
| 15 | +vi.mock('@mindwtr/core', async () => { |
| 16 | + const actual = await vi.importActual<typeof import('@mindwtr/core')>('@mindwtr/core'); |
| 17 | + coreSpies.parseQuickAdd.mockImplementation(actual.parseQuickAdd); |
| 18 | + return { ...actual, parseQuickAdd: coreSpies.parseQuickAdd }; |
| 19 | +}); |
| 20 | + |
| 21 | +vi.mock('@tauri-apps/api/core', () => ({ invoke: vi.fn(async () => false) })); |
| 22 | +vi.mock('@tauri-apps/api/event', () => ({ emitTo: vi.fn(async () => undefined), listen: vi.fn(async () => () => undefined) })); |
| 23 | +vi.mock('@tauri-apps/api/window', () => ({ getCurrentWindow: () => ({ hide: vi.fn(async () => undefined) }) })); |
| 24 | +vi.mock('@tauri-apps/plugin-fs', () => ({ |
| 25 | + BaseDirectory: { Data: 'Data' }, |
| 26 | + mkdir: vi.fn(async () => undefined), |
| 27 | + readFile: vi.fn(async () => new Uint8Array()), |
| 28 | + remove: vi.fn(async () => undefined), |
| 29 | + writeFile: vi.fn(async () => undefined), |
| 30 | +})); |
| 31 | +vi.mock('@tauri-apps/api/path', () => ({ |
| 32 | + dataDir: vi.fn(async () => '/data'), |
| 33 | + join: vi.fn(async (...parts: string[]) => parts.join('/')), |
| 34 | +})); |
| 35 | + |
| 36 | +const DRAFT = 'call mom @errands #family /due:tomorrow'; |
| 37 | + |
| 38 | +const initialTaskState = useTaskStore.getState(); |
| 39 | +const addTask = vi.fn(async () => ({ success: true, id: 'task-id' })); |
| 40 | + |
| 41 | +const openModalWithDraft = async () => { |
| 42 | + render( |
| 43 | + <LanguageProvider> |
| 44 | + <QuickAddModal /> |
| 45 | + </LanguageProvider> |
| 46 | + ); |
| 47 | + await act(async () => { |
| 48 | + window.dispatchEvent(new CustomEvent('mindwtr:quick-add', { detail: {} })); |
| 49 | + await Promise.resolve(); |
| 50 | + }); |
| 51 | + const input = screen.getByPlaceholderText('Add Task'); |
| 52 | + await act(async () => { |
| 53 | + fireEvent.change(input, { target: { value: DRAFT } }); |
| 54 | + await Promise.resolve(); |
| 55 | + }); |
| 56 | +}; |
| 57 | + |
| 58 | +beforeEach(() => { |
| 59 | + coreSpies.parseQuickAdd.mockClear(); |
| 60 | + addTask.mockClear(); |
| 61 | + act(() => { |
| 62 | + useTaskStore.setState(initialTaskState, true); |
| 63 | + useTaskStore.setState((state) => ({ |
| 64 | + ...state, |
| 65 | + _allProjects: [], |
| 66 | + _allAreas: [], |
| 67 | + addTask, |
| 68 | + tasks: [ |
| 69 | + { id: 'seed', title: 'seed', status: 'inbox', contexts: ['@errands'], tags: ['#family'] }, |
| 70 | + ] as never, |
| 71 | + })); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('QuickAddModal live preview', () => { |
| 76 | + it('shows what the parser found in the draft', async () => { |
| 77 | + await openModalWithDraft(); |
| 78 | + |
| 79 | + const preview = screen.getByTestId('quick-add-preview'); |
| 80 | + expect(preview).toHaveTextContent('@errands'); |
| 81 | + expect(preview).toHaveTextContent('#family'); |
| 82 | + // The resolved due date, not the phrase that produced it. |
| 83 | + expect(preview).toHaveTextContent('Due Date'); |
| 84 | + expect(preview).not.toHaveTextContent('/due:tomorrow'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('parses the preview with the same input and options the save uses', async () => { |
| 88 | + await openModalWithDraft(); |
| 89 | + |
| 90 | + const previewCalls = coreSpies.parseQuickAdd.mock.calls.length; |
| 91 | + expect(previewCalls).toBeGreaterThan(0); |
| 92 | + const previewCall = coreSpies.parseQuickAdd.mock.calls[previewCalls - 1]; |
| 93 | + |
| 94 | + fireEvent.click(screen.getByRole('button', { name: 'Save' })); |
| 95 | + await waitFor(() => expect(addTask).toHaveBeenCalled()); |
| 96 | + |
| 97 | + const submitCall = coreSpies.parseQuickAdd.mock.calls[previewCalls]; |
| 98 | + expect(submitCall).toBeDefined(); |
| 99 | + // input, projects, areas and the options bag: same values, and the bag |
| 100 | + // is literally the same object the preview memo read. |
| 101 | + expect(submitCall[0]).toBe(previewCall[0]); |
| 102 | + expect(submitCall[1]).toBe(previewCall[1]); |
| 103 | + expect(submitCall[3]).toBe(previewCall[3]); |
| 104 | + expect(submitCall[4]).toBe(previewCall[4]); |
| 105 | + }); |
| 106 | + |
| 107 | + it('warns about an invalid date command instead of failing silently on save', async () => { |
| 108 | + render( |
| 109 | + <LanguageProvider> |
| 110 | + <QuickAddModal /> |
| 111 | + </LanguageProvider> |
| 112 | + ); |
| 113 | + await act(async () => { |
| 114 | + window.dispatchEvent(new CustomEvent('mindwtr:quick-add', { detail: {} })); |
| 115 | + await Promise.resolve(); |
| 116 | + }); |
| 117 | + await act(async () => { |
| 118 | + fireEvent.change(screen.getByPlaceholderText('Add Task'), { target: { value: 'call mom /due:notaday' } }); |
| 119 | + await Promise.resolve(); |
| 120 | + }); |
| 121 | + |
| 122 | + expect(screen.getByTestId('quick-add-preview')).toHaveTextContent('/due:notaday'); |
| 123 | + }); |
| 124 | +}); |
0 commit comments