Skip to content

Commit 8efdf95

Browse files
committed
test: added test case for field priority
1 parent 28bd453 commit 8efdf95

1 file changed

Lines changed: 72 additions & 8 deletions

File tree

packages/vee-validate/tests/Form.spec.ts

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ describe('<Form />', () => {
849849
};
850850
},
851851
template: `
852-
<VForm as="form" :validationSchema="schema" v-slot="{ errors, values }" :unsetValuesOnUnmount="false">
852+
<VForm as="form" :validationSchema="schema" v-slot="{ errors, values }" keep-values>
853853
<template v-if="showFields">
854854
<Field name="field" as="input" />
855855
<Field name="nested.field" />
@@ -911,7 +911,7 @@ describe('<Form />', () => {
911911
};
912912
},
913913
template: `
914-
<VForm @submit="onSubmit" as="form" v-slot="{ errors }" :unsetValuesOnUnmount="false">
914+
<VForm @submit="onSubmit" as="form" v-slot="{ errors }" keep-values>
915915
<template v-if="showFields">
916916
<Field name="field" as="input" rules="required" />
917917
<Field name="nested.field" rules="required" />
@@ -978,10 +978,10 @@ describe('<Form />', () => {
978978
<VForm as="form" :validationSchema="schema" v-slot="{ errors, values }">
979979
<template v-if="showFields">
980980
<Field name="field" as="input" />
981-
<Field name="nested.field" :unsetValueOnUnmount="false" />
982-
<Field name="[non-nested.field]" :unsetValueOnUnmount="false" />
981+
<Field name="nested.field" keep-value />
982+
<Field name="[non-nested.field]" keep-value />
983983
<Field name="drink" as="input" type="checkbox" value="" /> Coffee
984-
<Field name="drink" as="input" type="checkbox" value="Tea" :unsetValueOnUnmount="false" /> Tea
984+
<Field name="drink" as="input" type="checkbox" value="Tea" keep-value /> Tea
985985
</template>
986986
<Field name="drink" as="input" type="checkbox" value="Coke" /> Coke
987987
@@ -1043,10 +1043,10 @@ describe('<Form />', () => {
10431043
<VForm @submit="onSubmit" as="form" v-slot="{ errors }">
10441044
<template v-if="showFields">
10451045
<Field name="field" as="input" rules="required" />
1046-
<Field name="nested.field" rules="required" :unsetValueOnUnmount="false" />
1047-
<Field name="[non-nested.field]" rules="required" :unsetValueOnUnmount="false" />
1046+
<Field name="nested.field" rules="required" keep-value />
1047+
<Field name="[non-nested.field]" rules="required" keep-value />
10481048
<Field name="drink" as="input" type="checkbox" value="" rules="required" /> Coffee
1049-
<Field name="drink" as="input" type="checkbox" value="Tea" rules="required" :unsetValueOnUnmount="false" /> Tea
1049+
<Field name="drink" as="input" type="checkbox" value="Tea" rules="required" keep-value /> Tea
10501050
</template>
10511051
<Field name="drink" as="input" type="checkbox" value="Coke" rules="required" /> Coke
10521052
@@ -1092,6 +1092,70 @@ describe('<Form />', () => {
10921092
});
10931093
});
10941094

1095+
test('can specify which fields get their value kept with field setting priority', async () => {
1096+
const showFields = ref(true);
1097+
const wrapper = mountWithHoc({
1098+
setup() {
1099+
const schema = computed(() => ({
1100+
field: showFields.value ? 'required' : '',
1101+
drink: 'required',
1102+
}));
1103+
1104+
return {
1105+
schema,
1106+
showFields,
1107+
};
1108+
},
1109+
template: `
1110+
<VForm as="form" :validationSchema="schema" v-slot="{ errors, values }" keep-values>
1111+
<template v-if="showFields">
1112+
<Field name="field" as="input" />
1113+
<Field name="nested.field" :keep-value="false" />
1114+
<Field name="[non-nested.field]" :keep-value="false" />
1115+
<Field name="drink" as="input" type="checkbox" value="" /> Coffee
1116+
<Field name="drink" as="input" type="checkbox" value="Tea" :keep-value="false" /> Tea
1117+
<Field name="drink" as="input" type="checkbox" value="Coke" /> Coke
1118+
</template>
1119+
1120+
<span id="errors">{{ errors }}</span>
1121+
<span id="values">{{ values }}</span>
1122+
1123+
<button>Validate</button>
1124+
</VForm>
1125+
`,
1126+
});
1127+
1128+
await flushPromises();
1129+
const errors = wrapper.$el.querySelector('#errors');
1130+
const values = wrapper.$el.querySelector('#values');
1131+
const inputs = wrapper.$el.querySelectorAll('input');
1132+
1133+
wrapper.$el.querySelector('button').click();
1134+
await flushPromises();
1135+
expect(errors.textContent).toBeTruthy();
1136+
setChecked(inputs[4]);
1137+
setChecked(inputs[5]);
1138+
setValue(inputs[0], 'test');
1139+
setValue(inputs[1], '12');
1140+
setValue(inputs[2], '12');
1141+
await flushPromises();
1142+
expect(JSON.parse(values.textContent)).toEqual({
1143+
drink: ['Tea', 'Coke'],
1144+
field: 'test',
1145+
'non-nested.field': '12',
1146+
nested: { field: '12' },
1147+
});
1148+
1149+
showFields.value = false;
1150+
await flushPromises();
1151+
// errors were cleared
1152+
expect(errors.textContent).toBe('{}');
1153+
expect(JSON.parse(values.textContent)).toEqual({
1154+
drink: ['Coke'],
1155+
field: 'test',
1156+
});
1157+
});
1158+
10951159
test('checkboxes with yup schema', async () => {
10961160
const wrapper = mountWithHoc({
10971161
setup() {

0 commit comments

Comments
 (0)