|
1 | | -import { useForm, useFieldArray, FieldEntry, FormContext, FieldArrayContext } from '@/vee-validate'; |
2 | | -import { nextTick, onMounted, Ref } from 'vue'; |
| 1 | +import { useForm, useFieldArray, FieldEntry, FormContext, FieldArrayContext, useField } from '@/vee-validate'; |
| 2 | +import { defineComponent, nextTick, onMounted, Ref } from 'vue'; |
3 | 3 | import * as yup from 'yup'; |
4 | | -import { mountWithHoc, flushPromises } from './helpers'; |
| 4 | +import { mountWithHoc, flushPromises, setValue } from './helpers'; |
5 | 5 |
|
6 | 6 | test('can update a field entry model directly', async () => { |
7 | 7 | mountWithHoc({ |
@@ -522,3 +522,60 @@ test('array move initializes the array if undefined', async () => { |
522 | 522 | await flushPromises(); |
523 | 523 | expect(arr.fields.value).toHaveLength(0); |
524 | 524 | }); |
| 525 | + |
| 526 | +// #4557 |
| 527 | +test('errors are available to the newly inserted items', async () => { |
| 528 | + let arr!: FieldArrayContext; |
| 529 | + const InputText = defineComponent({ |
| 530 | + props: { |
| 531 | + name: { |
| 532 | + type: String, |
| 533 | + required: true, |
| 534 | + }, |
| 535 | + }, |
| 536 | + setup(props) { |
| 537 | + const { value, errorMessage } = useField(() => props.name); |
| 538 | + |
| 539 | + return { |
| 540 | + value, |
| 541 | + errorMessage, |
| 542 | + }; |
| 543 | + }, |
| 544 | + template: '<input v-model="value" /> <span>{{errorMessage}}</span>', |
| 545 | + }); |
| 546 | + |
| 547 | + mountWithHoc({ |
| 548 | + components: { InputText }, |
| 549 | + setup() { |
| 550 | + useForm<any>({ |
| 551 | + initialValues: { |
| 552 | + users: ['one', 'three'], |
| 553 | + }, |
| 554 | + validationSchema: yup.object({ |
| 555 | + users: yup.array().of(yup.string().required().min(1)), |
| 556 | + }), |
| 557 | + }); |
| 558 | + |
| 559 | + arr = useFieldArray('users'); |
| 560 | + |
| 561 | + return { |
| 562 | + fields: arr.fields, |
| 563 | + }; |
| 564 | + }, |
| 565 | + template: ` |
| 566 | + <div> |
| 567 | + <InputText v-for="(field, idx) in fields" :key="field.key" :name="'users[' + idx + ']'" /> |
| 568 | + </div> |
| 569 | + `, |
| 570 | + }); |
| 571 | + const inputAt = (idx: number) => (document.querySelectorAll('input') || [])[idx] as HTMLInputElement; |
| 572 | + const spanAt = (idx: number) => (document.querySelectorAll('span') || [])[idx] as HTMLSpanElement; |
| 573 | + await flushPromises(); |
| 574 | + expect(arr.fields.value).toHaveLength(2); |
| 575 | + arr.insert(1, ''); |
| 576 | + await flushPromises(); |
| 577 | + expect(arr.fields.value).toHaveLength(3); |
| 578 | + setValue(inputAt(1), ''); |
| 579 | + await flushPromises(); |
| 580 | + expect(spanAt(1).textContent).toBeTruthy(); |
| 581 | +}); |
0 commit comments