@@ -666,3 +666,59 @@ test('clears old errors path when last item is removed and value update validati
666666
667667 expect ( errorList . children ) . toHaveLength ( 0 ) ;
668668} ) ;
669+
670+ test ( 'moves items around the array with move()' , async ( ) => {
671+ const onSubmit = jest . fn ( ) ;
672+ mountWithHoc ( {
673+ setup ( ) {
674+ const initialValues = {
675+ users : [ { name : '1' } , { name : '2' } , { name : '3' } , { name : '4' } ] ,
676+ } ;
677+
678+ return {
679+ onSubmit,
680+ initialValues,
681+ } ;
682+ } ,
683+ template : `
684+ <VForm @submit="onSubmit" :initial-values="initialValues">
685+ <FieldArray name="users" v-slot="{ move, fields }">
686+ <fieldset v-for="(field, idx) in fields" :key="field.key">
687+ <legend>User #{{ idx }}</legend>
688+ <label :for="'name_' + idx">Name</label>
689+ <Field :id="'name_' + idx" :name="'users[' + idx + '].name'" />
690+ <ErrorMessage :name="'users[' + idx + '].name'" />
691+
692+ <button class="move" type="button" @click="move(idx, 0)">Move</button>
693+ </fieldset>
694+ </FieldArray>
695+
696+ <button class="submit" type="submit">Submit</button>
697+ </VForm>
698+ ` ,
699+ } ) ;
700+
701+ await flushPromises ( ) ;
702+ const submitBtn = document . querySelector ( '.submit' ) as HTMLButtonElement ;
703+ const inputAt = ( idx : number ) => ( document . querySelectorAll ( 'input' ) || [ ] ) [ idx ] as HTMLInputElement ;
704+ const moveElAt = ( idx : number ) => ( document . querySelectorAll ( '.move' ) || [ ] ) [ idx ] as HTMLInputElement ;
705+
706+ expect ( getValue ( inputAt ( 0 ) ) ) . toBe ( '1' ) ;
707+ expect ( getValue ( inputAt ( 1 ) ) ) . toBe ( '2' ) ;
708+ expect ( getValue ( inputAt ( 2 ) ) ) . toBe ( '3' ) ;
709+ expect ( getValue ( inputAt ( 3 ) ) ) . toBe ( '4' ) ;
710+ dispatchEvent ( moveElAt ( 3 ) , 'click' ) ;
711+ await flushPromises ( ) ;
712+ expect ( getValue ( inputAt ( 0 ) ) ) . toBe ( '4' ) ;
713+ expect ( getValue ( inputAt ( 1 ) ) ) . toBe ( '1' ) ;
714+ expect ( getValue ( inputAt ( 2 ) ) ) . toBe ( '2' ) ;
715+ expect ( getValue ( inputAt ( 3 ) ) ) . toBe ( '3' ) ;
716+ ( submitBtn as HTMLButtonElement ) . click ( ) ;
717+ await flushPromises ( ) ;
718+ expect ( onSubmit ) . toHaveBeenLastCalledWith (
719+ expect . objectContaining ( {
720+ users : [ { name : '4' } , { name : '1' } , { name : '2' } , { name : '3' } ] ,
721+ } ) ,
722+ expect . anything ( )
723+ ) ;
724+ } ) ;
0 commit comments