@@ -1988,4 +1988,61 @@ describe('<Form />', () => {
19881988 expect ( passwordError . textContent ) . toBe ( errorMessage ) ;
19891989 expect ( passwordValue . textContent ) . toBe ( value ) ;
19901990 } ) ;
1991+
1992+ // #3325
1993+ test ( 'unsets old path value when array fields are removed' , async ( ) => {
1994+ const onSubmit = jest . fn ( ) ;
1995+ mountWithHoc ( {
1996+ setup ( ) {
1997+ const users = ref ( [
1998+ { id : 1 , name : '111' } ,
1999+ { id : 2 , name : '222' } ,
2000+ { id : 3 , name : '333' } ,
2001+ ] ) ;
2002+
2003+ function remove ( idx : number ) {
2004+ users . value . splice ( idx , 1 ) ;
2005+ }
2006+
2007+ return {
2008+ onSubmit,
2009+ users,
2010+ remove,
2011+ } ;
2012+ } ,
2013+ template : `
2014+ <VForm @submit="onSubmit">
2015+ <fieldset v-for="(user, idx) in users" :key="user.id">
2016+ <legend>User #{{ idx }}</legend>
2017+ <label :for="'name_' + idx">Name</label>
2018+ <Field :id="'name_' + idx" :name="'users[' + idx + '].name'" />
2019+ <ErrorMessage :name="'users[' + idx + '].name'" />
2020+
2021+ <button class="remove" type="button" @click="remove(idx)">X</button>
2022+ </fieldset>
2023+
2024+ <button class="submit" type="submit">Submit</button>
2025+ </VForm>
2026+ ` ,
2027+ } ) ;
2028+
2029+ await flushPromises ( ) ;
2030+ const submitBtn = document . querySelector ( '.submit' ) as HTMLButtonElement ;
2031+ const inputs = Array . from ( document . querySelectorAll ( 'input' ) ) as HTMLInputElement [ ] ;
2032+ const removeBtn = document . querySelectorAll ( '.remove' ) [ 1 ] as HTMLButtonElement ; // remove the second item
2033+ setValue ( inputs [ 0 ] , '111' ) ;
2034+ setValue ( inputs [ 1 ] , '222' ) ;
2035+ setValue ( inputs [ 2 ] , '333' ) ;
2036+ await flushPromises ( ) ;
2037+ removeBtn . click ( ) ;
2038+ await flushPromises ( ) ;
2039+ ( submitBtn as HTMLButtonElement ) . click ( ) ;
2040+ await flushPromises ( ) ;
2041+ expect ( onSubmit ) . toHaveBeenCalledWith (
2042+ expect . objectContaining ( {
2043+ users : [ { name : '111' } , { name : '333' } ] ,
2044+ } ) ,
2045+ expect . anything ( )
2046+ ) ;
2047+ } ) ;
19912048} ) ;
0 commit comments