48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { createFormStore, type FormFieldInput, type FormState, type FormField } from '@/lib/state/formStore'
|
|
|
|
const text = (name: string, label = name): FormFieldInput => ({ type: 'text', name, label, required: false })
|
|
|
|
describe('form store', () => {
|
|
it('필드 추가/삭제/이동/업데이트와 action URL, spam 옵션을 관리한다', () => {
|
|
const useForm = createFormStore()
|
|
|
|
// 초기
|
|
let s: FormState = useForm.getState()
|
|
expect(s.fields.length).toBe(0)
|
|
expect(s.actionUrl).toBe('')
|
|
expect(s.spam.honeypotFieldName).toBe('_hp')
|
|
|
|
// 추가
|
|
useForm.getState().addField(text('name', 'Name'))
|
|
useForm.getState().addField(text('email', 'Email'))
|
|
useForm.getState().addField({ type: 'select', name: 'plan', label: 'Plan', required: true, options: ['A','B'] })
|
|
s = useForm.getState()
|
|
expect(s.fields.map((f: FormField) => f.name)).toEqual(['name','email','plan'])
|
|
|
|
// 이동 (0 -> 2)
|
|
useForm.getState().moveField(0, 2)
|
|
s = useForm.getState()
|
|
expect(s.fields.map((f: FormField) => f.name)).toEqual(['email','plan','name'])
|
|
|
|
// 업데이트
|
|
useForm.getState().updateField(1, { label: 'Pricing Plan', required: false })
|
|
s = useForm.getState()
|
|
expect(s.fields[1].label).toBe('Pricing Plan')
|
|
expect(s.fields[1].required).toBe(false)
|
|
|
|
// 삭제
|
|
useForm.getState().removeField(0)
|
|
s = useForm.getState()
|
|
expect(s.fields.map((f: FormField) => f.name)).toEqual(['plan','name'])
|
|
|
|
// action & spam
|
|
useForm.getState().setActionUrl('https://example.com/form')
|
|
useForm.getState().setSpam({ honeypotFieldName: '_trap', minSubmitSeconds: 3 })
|
|
s = useForm.getState()
|
|
expect(s.actionUrl).toBe('https://example.com/form')
|
|
expect(s.spam.honeypotFieldName).toBe('_trap')
|
|
expect(s.spam.minSubmitSeconds).toBe(3)
|
|
})
|
|
})
|