|
1 | | -import { defineRule } from '@/vee-validate'; |
| 1 | +import { defineRule, useField } from '@/vee-validate'; |
2 | 2 | import { mountWithHoc, setValue, setChecked, dispatchEvent, flushPromises } from './helpers'; |
3 | 3 | import * as yup from 'yup'; |
4 | | -import { computed, onErrorCaptured, reactive, ref, Ref } from 'vue'; |
| 4 | +import { computed, defineComponent, onErrorCaptured, reactive, ref, Ref } from 'vue'; |
5 | 5 | import { InvalidSubmissionContext } from '../src/types'; |
6 | 6 |
|
7 | 7 | describe('<Form />', () => { |
@@ -2716,4 +2716,47 @@ describe('<Form />', () => { |
2716 | 2716 | expect(inputAt(2).checked).toBe(true); |
2717 | 2717 | expect(inputAt(3).checked).toBe(false); |
2718 | 2718 | }); |
| 2719 | + |
| 2720 | + // #3895 #3894 |
| 2721 | + test('single checkbox component with v-model in a form', async () => { |
| 2722 | + const value = ref(false); |
| 2723 | + const Checkbox = defineComponent({ |
| 2724 | + props: { value: Boolean, modelValue: Boolean }, |
| 2725 | + template: `<input type="checkbox" @change="handleChange" :checked="checked" :value="true" />`, |
| 2726 | + setup() { |
| 2727 | + const { handleChange, checked } = useField('field', undefined, { |
| 2728 | + type: 'checkbox', |
| 2729 | + uncheckedValue: false, |
| 2730 | + checkedValue: true, |
| 2731 | + }); |
| 2732 | + |
| 2733 | + return { |
| 2734 | + handleChange, |
| 2735 | + checked, |
| 2736 | + }; |
| 2737 | + }, |
| 2738 | + }); |
| 2739 | + const wrapper = mountWithHoc({ |
| 2740 | + components: { |
| 2741 | + Checkbox, |
| 2742 | + }, |
| 2743 | + setup() { |
| 2744 | + return { |
| 2745 | + value, |
| 2746 | + }; |
| 2747 | + }, |
| 2748 | + template: ` |
| 2749 | + <VForm> |
| 2750 | + <Checkbox v-model="value" /> |
| 2751 | + </VForm> |
| 2752 | + `, |
| 2753 | + }); |
| 2754 | + |
| 2755 | + await flushPromises(); |
| 2756 | + const inputAt = (idx: number) => wrapper.$el.querySelectorAll('input')[idx] as HTMLInputElement; |
| 2757 | + expect(value.value).toBe(false); |
| 2758 | + setChecked(inputAt(0), true); |
| 2759 | + await flushPromises(); |
| 2760 | + expect(value.value).toBe(true); |
| 2761 | + }); |
2719 | 2762 | }); |
0 commit comments