import { FormState, FieldState, ArrayFormState } from 'formstate-x' import { bindFormItem } from './FormItem' describe('bindFormItem', () => { it('should work well with FieldState', async () => { const state = new FieldState('foo').withValidator(() => 'foo error') await state.validate() const binding = bindFormItem(state) expect(binding.validateStatus).toBe('error') expect(binding.validateMessage).toBe('foo error') state.reset() const binding2 = bindFormItem(state) expect(binding2.validateStatus).toBe('default') expect(binding2.validateMessage).toBe(undefined) }) it('should work well with FormState', async () => { const state = new FormState({ foo: new FieldState('foo').withValidator(() => 'foo error'), bar: new FieldState('bar') }).withValidator(() => 'form error') await state.validate() const { validateStatus, validateMessage } = bindFormItem(state) expect(validateStatus).toBe('error') expect(validateMessage).toBe('form error') }) it('should work well with ArrayFormState', async () => { const createFieldState = () => new FieldState('foo').withValidator(() => 'foo error') const state = new ArrayFormState([], createFieldState).withValidator(() => 'form error') await state.validate() const { validateStatus, validateMessage } = bindFormItem(state) expect(validateStatus).toBe('error') expect(validateMessage).toBe('form error') }) it('should not include fields\' error message with FormState', async () => { const state = new FormState({ foo: new FieldState('foo').withValidator(() => 'foo error'), bar: new FieldState('bar') }) await state.validate() const { validateStatus, validateMessage } = bindFormItem(state) expect(validateStatus).toBe('default') expect(validateMessage).toBe(undefined) }) it('should not include fields\' error message with ArrayFormState', async () => { const createFieldState = () => new FieldState('foo').withValidator(() => 'foo error') const state = new ArrayFormState([], createFieldState) await state.validate() const { validateStatus, validateMessage } = bindFormItem(state) expect(validateStatus).toBe('default') expect(validateMessage).toBe(undefined) }) })