|
| 1 | +import { render } from 'vitest-browser-vue' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | +import FieldReactivity from './fixtures/FieldReactivity.vue' |
| 4 | + |
| 5 | +describe.each(['slot', 'injected'] as const)( |
| 6 | + 'compiled Vue field consumers (%s)', |
| 7 | + (mode) => { |
| 8 | + it('updates value, synchronous errors, and pending asynchronous validation without remounting', async () => { |
| 9 | + let resolveValidation!: (error: string | null) => void |
| 10 | + const validation = new Promise<string | null>((resolve) => { |
| 11 | + resolveValidation = resolve |
| 12 | + }) |
| 13 | + const onFieldMount = vi.fn() |
| 14 | + const view = await render(FieldReactivity, { |
| 15 | + props: { |
| 16 | + mode, |
| 17 | + onFieldMount, |
| 18 | + validate: (value) => { |
| 19 | + if (value === 'bad') return 'Synchronous error' |
| 20 | + if (value === 'pending') return validation |
| 21 | + return null |
| 22 | + }, |
| 23 | + }, |
| 24 | + }) |
| 25 | + const input = view.getByRole('textbox', { name: 'Value' }) |
| 26 | + |
| 27 | + await input.fill('bad') |
| 28 | + await expect.element(view.getByTestId('value')).toHaveTextContent('bad') |
| 29 | + await expect |
| 30 | + .element(view.getByTestId('meta-errors')) |
| 31 | + .toHaveTextContent('Synchronous error') |
| 32 | + await expect |
| 33 | + .element(view.getByTestId('errors')) |
| 34 | + .toHaveTextContent('Synchronous error') |
| 35 | + await expect |
| 36 | + .element(view.getByTestId('touched')) |
| 37 | + .toHaveTextContent('true') |
| 38 | + |
| 39 | + await input.fill('valid') |
| 40 | + await expect.element(view.getByTestId('errors')).toBeEmptyDOMElement() |
| 41 | + await expect |
| 42 | + .element(view.getByTestId('meta-errors')) |
| 43 | + .toBeEmptyDOMElement() |
| 44 | + |
| 45 | + await input.fill('pending') |
| 46 | + await expect |
| 47 | + .element(view.getByTestId('validating')) |
| 48 | + .toHaveTextContent('true') |
| 49 | + resolveValidation('Asynchronous error') |
| 50 | + await expect |
| 51 | + .element(view.getByTestId('validating')) |
| 52 | + .toHaveTextContent('false') |
| 53 | + await expect |
| 54 | + .element(view.getByTestId('meta-errors')) |
| 55 | + .toHaveTextContent('Asynchronous error') |
| 56 | + await expect |
| 57 | + .element(view.getByTestId('errors')) |
| 58 | + .toHaveTextContent('Asynchronous error') |
| 59 | + |
| 60 | + await input.fill('valid again') |
| 61 | + await expect.element(view.getByTestId('errors')).toBeEmptyDOMElement() |
| 62 | + await expect |
| 63 | + .element(view.getByTestId('meta-errors')) |
| 64 | + .toBeEmptyDOMElement() |
| 65 | + expect(onFieldMount).toHaveBeenCalledOnce() |
| 66 | + }) |
| 67 | + |
| 68 | + it('reads and writes the current field after reset and a name change without remounting', async () => { |
| 69 | + const onFieldMount = vi.fn() |
| 70 | + const view = await render(FieldReactivity, { |
| 71 | + props: { mode, onFieldMount, validate: () => null }, |
| 72 | + }) |
| 73 | + const input = view.getByRole('textbox', { name: 'Value' }) |
| 74 | + |
| 75 | + await input.fill('Changed') |
| 76 | + await expect |
| 77 | + .element(view.getByTestId('first-value')) |
| 78 | + .toHaveTextContent('Changed') |
| 79 | + await view.getByRole('button', { name: 'Reset' }).click() |
| 80 | + await expect.element(input).toHaveValue('First') |
| 81 | + await expect |
| 82 | + .element(view.getByTestId('touched')) |
| 83 | + .toHaveTextContent('false') |
| 84 | + await input.fill('After reset') |
| 85 | + await expect |
| 86 | + .element(view.getByTestId('first-value')) |
| 87 | + .toHaveTextContent('After reset') |
| 88 | + await expect |
| 89 | + .element(view.getByTestId('value')) |
| 90 | + .toHaveTextContent('After reset') |
| 91 | + |
| 92 | + await view.getByRole('button', { name: 'Switch field' }).click() |
| 93 | + await expect.element(view.getByTestId('name')).toHaveTextContent('second') |
| 94 | + await expect.element(input).toHaveValue('Second') |
| 95 | + await input.fill('Updated second') |
| 96 | + await expect |
| 97 | + .element(view.getByTestId('value')) |
| 98 | + .toHaveTextContent('Updated second') |
| 99 | + await expect |
| 100 | + .element(view.getByTestId('second-value')) |
| 101 | + .toHaveTextContent('Updated second') |
| 102 | + await expect |
| 103 | + .element(view.getByTestId('first-value')) |
| 104 | + .toHaveTextContent('After reset') |
| 105 | + expect(onFieldMount).toHaveBeenCalledOnce() |
| 106 | + }) |
| 107 | + }, |
| 108 | +) |
0 commit comments