You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.1 KiB
58 lines
2.1 KiB
import { describe, expect, test } from 'bun:test'
|
|
import {
|
|
computeIsDirty,
|
|
createQuickNoteModalState,
|
|
markSaveFailed,
|
|
markSaveSucceeded,
|
|
updateDraftContent,
|
|
} from './quick-note-modal-state'
|
|
|
|
describe('quick-note modal state', () => {
|
|
test('draft differs from saved => dirty', () => {
|
|
expect(computeIsDirty({ savedContent: 'a', draftContent: 'b' })).toBe(true)
|
|
expect(computeIsDirty({ savedContent: 'a', draftContent: 'a' })).toBe(false)
|
|
})
|
|
|
|
test('saving success marks state clean', () => {
|
|
const initial = createQuickNoteModalState('hello')
|
|
const editing = updateDraftContent(initial, 'hello world')
|
|
expect(editing.isDirty).toBe(true)
|
|
|
|
const saved = markSaveSucceeded(editing)
|
|
expect(saved.savedContent).toBe('hello world')
|
|
expect(saved.draftContent).toBe('hello world')
|
|
expect(saved.isDirty).toBe(false)
|
|
expect(saved.lastSaveFailed).toBe(false)
|
|
})
|
|
|
|
test('saving failure keeps draft dirty', () => {
|
|
const initial = createQuickNoteModalState('hello')
|
|
const editing = updateDraftContent(initial, 'draft changed')
|
|
const failed = markSaveFailed(editing)
|
|
|
|
expect(failed.savedContent).toBe('hello')
|
|
expect(failed.draftContent).toBe('draft changed')
|
|
expect(failed.isDirty).toBe(true)
|
|
expect(failed.lastSaveFailed).toBe(true)
|
|
})
|
|
|
|
test('input after failed save resets lastSaveFailed', () => {
|
|
const initial = createQuickNoteModalState('hello')
|
|
const editing = updateDraftContent(initial, 'draft changed')
|
|
const failed = markSaveFailed(editing)
|
|
expect(failed.lastSaveFailed).toBe(true)
|
|
|
|
const editedAgain = updateDraftContent(failed, 'draft changed again')
|
|
expect(editedAgain.lastSaveFailed).toBe(false)
|
|
expect(editedAgain.isDirty).toBe(true)
|
|
})
|
|
|
|
test('save success after a failure clears lastSaveFailed', () => {
|
|
const initial = createQuickNoteModalState('hello')
|
|
const failed = markSaveFailed(updateDraftContent(initial, 'draft changed'))
|
|
const saved = markSaveSucceeded(failed)
|
|
|
|
expect(saved.lastSaveFailed).toBe(false)
|
|
expect(saved.isDirty).toBe(false)
|
|
})
|
|
})
|
|
|