|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import type { ErrorObject } from 'ajv'; |
| 3 | +import { computed, defineComponent, h, nextTick, provide, ref } from 'vue'; |
| 4 | +import { mount } from '@vue/test-utils'; |
| 5 | +import { useVuetifyArrayControl } from '../../../src/util/composition'; |
| 6 | + |
| 7 | +const makeError = (instancePath: string): ErrorObject => ({ |
| 8 | + keyword: 'required', |
| 9 | + message: 'is required', |
| 10 | + instancePath, |
| 11 | + schemaPath: '#/required', |
| 12 | + params: {}, |
| 13 | +}); |
| 14 | + |
| 15 | +describe('useVuetifyArrayControl', () => { |
| 16 | + it('exposes a reactive rawChildErrors that reflects child error changes', async () => { |
| 17 | + const childErrors = ref<ErrorObject[]>([]); |
| 18 | + |
| 19 | + let captured: ReturnType<typeof useVuetifyArrayControl> | undefined; |
| 20 | + |
| 21 | + const Child = defineComponent({ |
| 22 | + setup() { |
| 23 | + const control = computed(() => ({ |
| 24 | + label: '', |
| 25 | + required: false, |
| 26 | + config: {}, |
| 27 | + uischema: { type: 'Control', scope: '#' }, |
| 28 | + schema: { type: 'array' }, |
| 29 | + data: [], |
| 30 | + childErrors: childErrors.value, |
| 31 | + i18nKeyPrefix: '', |
| 32 | + })); |
| 33 | + captured = useVuetifyArrayControl({ control }); |
| 34 | + return () => h('div'); |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + const Host = defineComponent({ |
| 39 | + components: { Child }, |
| 40 | + setup() { |
| 41 | + provide('jsonforms', { |
| 42 | + core: { data: {}, schema: {}, uischema: { type: 'Control' } }, |
| 43 | + i18n: { |
| 44 | + translate: (_id: string, defaultMessage?: string) => defaultMessage, |
| 45 | + }, |
| 46 | + }); |
| 47 | + return () => h(Child); |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + mount(Host); |
| 52 | + |
| 53 | + expect(captured).toBeDefined(); |
| 54 | + expect(captured!.rawChildErrors.value).toEqual([]); |
| 55 | + |
| 56 | + const next = [makeError('/0/name')]; |
| 57 | + childErrors.value = next; |
| 58 | + await nextTick(); |
| 59 | + |
| 60 | + expect(captured!.rawChildErrors.value).toEqual(next); |
| 61 | + |
| 62 | + const updated = [makeError('/0/name'), makeError('/1/name')]; |
| 63 | + childErrors.value = updated; |
| 64 | + await nextTick(); |
| 65 | + |
| 66 | + expect(captured!.rawChildErrors.value).toEqual(updated); |
| 67 | + }); |
| 68 | +}); |
0 commit comments