117 lines
3.4 KiB
JavaScript
117 lines
3.4 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
import { nextTick, ref } from 'vue'
|
|
import { useAdminMarkdownHistory } from '../composables/useAdminMarkdownHistory.js'
|
|
|
|
/**
|
|
* 테스트용 관리자 Markdown 히스토리 상태를 만든다.
|
|
* @param {Object} options - 테스트 옵션
|
|
* @returns {{ value: import('vue').Ref<string>, lastSelectionState: import('vue').Ref<Object>, restoredSelections: Array<Object|null>, history: Object }}
|
|
*/
|
|
const createHistoryState = (options = {}) => {
|
|
const value = ref(options.value ?? '')
|
|
const lastSelectionState = ref(options.selection ?? { start: 0, end: 0 })
|
|
const restoredSelections = []
|
|
const history = useAdminMarkdownHistory({
|
|
value,
|
|
lastSelectionState,
|
|
restoreFocusAfterHistory: (selection) => {
|
|
restoredSelections.push(selection)
|
|
},
|
|
undoLimit: options.undoLimit,
|
|
groupDelayMs: options.groupDelayMs
|
|
})
|
|
|
|
return {
|
|
value,
|
|
lastSelectionState,
|
|
restoredSelections,
|
|
history
|
|
}
|
|
}
|
|
|
|
test('관리자 Markdown 히스토리는 이전 값을 undo 스택에 기록한다', () => {
|
|
const { history } = createHistoryState({
|
|
selection: { start: 1, end: 3 },
|
|
groupDelayMs: 0
|
|
})
|
|
|
|
history.recordEditorHistory('이전')
|
|
|
|
assert.deepEqual(history.editorUndoStack.value, [{
|
|
value: '이전',
|
|
selectionStart: 1,
|
|
selectionEnd: 2
|
|
}])
|
|
assert.deepEqual(history.editorRedoStack.value, [])
|
|
})
|
|
|
|
test('관리자 Markdown 히스토리 undo는 현재 값을 redo에 넣고 선택 위치를 복원한다', async () => {
|
|
const {
|
|
value,
|
|
lastSelectionState,
|
|
restoredSelections,
|
|
history
|
|
} = createHistoryState({
|
|
value: '현재',
|
|
selection: { start: 2, end: 2 },
|
|
groupDelayMs: 0
|
|
})
|
|
|
|
history.recordEditorHistory('이전')
|
|
lastSelectionState.value = { start: 1, end: 1 }
|
|
history.restoreEditorHistory('undo')
|
|
await nextTick()
|
|
|
|
assert.equal(value.value, '이전')
|
|
assert.deepEqual(history.editorUndoStack.value, [])
|
|
assert.deepEqual(history.editorRedoStack.value, [{
|
|
value: '현재',
|
|
selectionStart: 1,
|
|
selectionEnd: 1
|
|
}])
|
|
assert.deepEqual(restoredSelections, [{ start: 2, end: 2 }])
|
|
})
|
|
|
|
test('관리자 Markdown 히스토리 redo는 redo 스택 값을 다시 적용한다', async () => {
|
|
const { value, history } = createHistoryState({
|
|
value: '현재',
|
|
groupDelayMs: 0
|
|
})
|
|
|
|
history.recordEditorHistory('이전')
|
|
history.restoreEditorHistory('undo')
|
|
await nextTick()
|
|
history.restoreEditorHistory('redo')
|
|
await nextTick()
|
|
|
|
assert.equal(value.value, '현재')
|
|
assert.deepEqual(history.editorRedoStack.value, [])
|
|
assert.equal(history.editorUndoStack.value.length, 1)
|
|
})
|
|
|
|
test('관리자 Markdown 히스토리는 빠른 연속 기록을 하나의 undo 항목으로 묶는다', () => {
|
|
const { history } = createHistoryState({
|
|
groupDelayMs: 60_000
|
|
})
|
|
|
|
history.recordEditorHistory('첫 값')
|
|
history.recordEditorHistory('두 번째 값')
|
|
|
|
assert.equal(history.editorUndoStack.value.length, 1)
|
|
assert.equal(history.editorUndoStack.value[0].value, '첫 값')
|
|
})
|
|
|
|
test('관리자 Markdown 히스토리는 undo 보관 개수를 제한한다', () => {
|
|
const { history } = createHistoryState({
|
|
undoLimit: 2,
|
|
groupDelayMs: 0
|
|
})
|
|
|
|
history.recordEditorHistory('1')
|
|
history.recordEditorHistory('2')
|
|
history.recordEditorHistory('3')
|
|
|
|
assert.deepEqual(history.editorUndoStack.value.map((entry) => entry.value), ['2', '3'])
|
|
})
|