2026-02-23 21:19:50 +01:00
|
|
|
import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'
|
2026-02-23 19:53:36 +01:00
|
|
|
import { renderHook, act } from '@testing-library/react'
|
|
|
|
|
import { useEditorSave } from './useEditorSave'
|
|
|
|
|
|
|
|
|
|
const mockInvokeFn = vi.fn<(cmd: string, args?: Record<string, unknown>) => Promise<null>>(() => Promise.resolve(null))
|
|
|
|
|
|
|
|
|
|
vi.mock('@tauri-apps/api/core', () => ({
|
|
|
|
|
invoke: vi.fn(),
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
vi.mock('../mock-tauri', () => ({
|
|
|
|
|
isTauri: () => false,
|
|
|
|
|
mockInvoke: (cmd: string, args?: Record<string, unknown>) => mockInvokeFn(cmd, args),
|
|
|
|
|
updateMockContent: vi.fn(),
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
describe('useEditorSave', () => {
|
2026-02-23 21:19:50 +01:00
|
|
|
let updateVaultContent: Mock
|
|
|
|
|
let setTabs: Mock
|
|
|
|
|
let setToastMessage: Mock
|
2026-02-23 19:53:36 +01:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
updateVaultContent = vi.fn()
|
|
|
|
|
setTabs = vi.fn()
|
|
|
|
|
setToastMessage = vi.fn()
|
|
|
|
|
mockInvokeFn.mockClear()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
function renderSaveHook() {
|
|
|
|
|
return renderHook(() => useEditorSave({ updateVaultContent, setTabs, setToastMessage }))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it('handleSave shows "Nothing to save" when no pending content', async () => {
|
|
|
|
|
const { result } = renderSaveHook()
|
|
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.handleSave()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(setToastMessage).toHaveBeenCalledWith('Nothing to save')
|
|
|
|
|
expect(mockInvokeFn).not.toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('handleSave persists pending content and shows "Saved"', async () => {
|
|
|
|
|
const { result } = renderSaveHook()
|
|
|
|
|
|
|
|
|
|
// Buffer content via handleContentChange
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleContentChange('/test/note.md', '---\ntitle: Test\n---\n\n# Test\n\nEdited')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Save via Cmd+S
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.handleSave()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(mockInvokeFn).toHaveBeenCalledWith('save_note_content', {
|
|
|
|
|
path: '/test/note.md',
|
|
|
|
|
content: '---\ntitle: Test\n---\n\n# Test\n\nEdited',
|
|
|
|
|
})
|
|
|
|
|
expect(setToastMessage).toHaveBeenCalledWith('Saved')
|
|
|
|
|
|
|
|
|
|
// Second save should show "Nothing to save" (pending cleared)
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.handleSave()
|
|
|
|
|
})
|
|
|
|
|
expect(setToastMessage).toHaveBeenCalledWith('Nothing to save')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('handleSave shows error toast on failure', async () => {
|
|
|
|
|
mockInvokeFn.mockRejectedValueOnce(new Error('Disk full'))
|
|
|
|
|
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
|
|
|
const { result } = renderSaveHook()
|
|
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleContentChange('/test/note.md', 'content')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.handleSave()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(setToastMessage).toHaveBeenCalledWith(expect.stringContaining('Save failed'))
|
|
|
|
|
consoleSpy.mockRestore()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('savePendingForPath saves content only for the matching path', async () => {
|
|
|
|
|
const { result } = renderSaveHook()
|
|
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleContentChange('/test/note-a.md', 'content A')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Try saving for a different path — should be a no-op
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.savePendingForPath('/test/note-b.md')
|
|
|
|
|
})
|
|
|
|
|
expect(mockInvokeFn).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
|
|
// Save for the correct path
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.savePendingForPath('/test/note-a.md')
|
|
|
|
|
})
|
|
|
|
|
expect(mockInvokeFn).toHaveBeenCalledWith('save_note_content', {
|
|
|
|
|
path: '/test/note-a.md',
|
|
|
|
|
content: 'content A',
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-23 21:15:13 +01:00
|
|
|
it('calls onAfterSave callback after successful save', async () => {
|
|
|
|
|
const onAfterSave = vi.fn()
|
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
|
useEditorSave({ updateVaultContent, setTabs, setToastMessage, onAfterSave })
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleContentChange('/test/note.md', 'new content')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.handleSave()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(onAfterSave).toHaveBeenCalledOnce()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('does not call onAfterSave when save fails', async () => {
|
|
|
|
|
mockInvokeFn.mockRejectedValueOnce(new Error('Disk full'))
|
|
|
|
|
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
|
|
|
const onAfterSave = vi.fn()
|
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
|
useEditorSave({ updateVaultContent, setTabs, setToastMessage, onAfterSave })
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleContentChange('/test/note.md', 'content')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.handleSave()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(onAfterSave).not.toHaveBeenCalled()
|
|
|
|
|
consoleSpy.mockRestore()
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-23 19:53:36 +01:00
|
|
|
it('handleContentChange buffers the latest content', () => {
|
|
|
|
|
const { result } = renderSaveHook()
|
|
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleContentChange('/test/note.md', 'v1')
|
|
|
|
|
result.current.handleContentChange('/test/note.md', 'v2')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// The ref should hold the latest value — verified via save
|
|
|
|
|
// (We'll check via the next handleSave call)
|
|
|
|
|
})
|
2026-02-23 20:34:12 +01:00
|
|
|
|
|
|
|
|
it('savePending flushes pending content to disk silently', async () => {
|
|
|
|
|
const { result } = renderSaveHook()
|
|
|
|
|
|
|
|
|
|
// No pending content — should be a no-op
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.savePending()
|
|
|
|
|
})
|
|
|
|
|
expect(mockInvokeFn).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
|
|
// Buffer content
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current.handleContentChange('/test/note.md', 'pending content')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// savePending should save without toasting
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.savePending()
|
|
|
|
|
})
|
|
|
|
|
expect(mockInvokeFn).toHaveBeenCalledWith('save_note_content', {
|
|
|
|
|
path: '/test/note.md',
|
|
|
|
|
content: 'pending content',
|
|
|
|
|
})
|
|
|
|
|
expect(setToastMessage).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
|
|
// After savePending, pending is cleared
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.savePending()
|
|
|
|
|
})
|
|
|
|
|
// No additional save call
|
|
|
|
|
expect(mockInvokeFn).toHaveBeenCalledTimes(1)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('onAfterSave: called after handleSave but NOT after savePending', async () => {
|
|
|
|
|
const onAfterSave = vi.fn()
|
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
|
useEditorSave({ updateVaultContent, setTabs, setToastMessage, onAfterSave })
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// savePending does not trigger onAfterSave (callers manage their own refresh)
|
|
|
|
|
act(() => { result.current.handleContentChange('/test/note.md', 'v1') })
|
|
|
|
|
await act(async () => { await result.current.savePending() })
|
|
|
|
|
expect(onAfterSave).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
|
|
// handleSave DOES trigger onAfterSave
|
|
|
|
|
act(() => { result.current.handleContentChange('/test/note.md', 'v2') })
|
|
|
|
|
await act(async () => { await result.current.handleSave() })
|
|
|
|
|
expect(onAfterSave).toHaveBeenCalledTimes(1)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('onAfterSave is NOT called when there is nothing to save', async () => {
|
|
|
|
|
const onAfterSave = vi.fn()
|
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
|
useEditorSave({ updateVaultContent, setTabs, setToastMessage, onAfterSave })
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
|
await result.current.handleSave()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(onAfterSave).not.toHaveBeenCalled()
|
|
|
|
|
})
|
2026-02-23 19:53:36 +01:00
|
|
|
})
|