|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Legacy `FormField.condition` ({ field, equals/notEquals/in }) — issue #1584. |
| 11 | + * |
| 12 | + * The bespoke JSON `condition` branch is retired: the form renderer now |
| 13 | + * translates it to an equivalent CEL visible-when predicate and evaluates it on |
| 14 | + * the canonical engine (over the seeded live record), exactly like `visibleWhen` |
| 15 | + * / `visibleOn`. This locks the translated semantics and reactivity. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, it, expect, beforeAll } from 'vitest'; |
| 19 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 20 | +import { ComponentRegistry } from '@object-ui/core'; |
| 21 | + |
| 22 | +beforeAll(async () => { |
| 23 | + await import('../../../renderers'); |
| 24 | +}, 30000); |
| 25 | + |
| 26 | +function renderForm(fields: any[]) { |
| 27 | + const Form = ComponentRegistry.get('form')!; |
| 28 | + return render( |
| 29 | + <Form schema={{ type: 'form', showSubmit: false, showCancel: false, fields }} />, |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +describe('form renderer — legacy condition → CEL (#1584)', () => { |
| 34 | + it('equals: shows the field only when the sibling equals the value, reactively', async () => { |
| 35 | + renderForm([ |
| 36 | + { name: 'type', label: 'Type', type: 'input', defaultValue: 'text' }, |
| 37 | + { name: 'referenceTo', label: 'Reference To', type: 'input', condition: { field: 'type', equals: 'lookup' } }, |
| 38 | + ]); |
| 39 | + |
| 40 | + // type = 'text' → hidden |
| 41 | + expect(screen.queryByLabelText(/reference to/i)).not.toBeInTheDocument(); |
| 42 | + |
| 43 | + fireEvent.change(screen.getByLabelText(/type/i), { target: { value: 'lookup' } }); |
| 44 | + await waitFor(() => { |
| 45 | + expect(screen.getByLabelText(/reference to/i)).toBeInTheDocument(); |
| 46 | + }); |
| 47 | + |
| 48 | + fireEvent.change(screen.getByLabelText(/type/i), { target: { value: 'formula' } }); |
| 49 | + await waitFor(() => { |
| 50 | + expect(screen.queryByLabelText(/reference to/i)).not.toBeInTheDocument(); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('notEquals: hidden when the sibling matches, shown otherwise', async () => { |
| 55 | + renderForm([ |
| 56 | + { name: 'status', label: 'Status', type: 'input', defaultValue: 'draft' }, |
| 57 | + { name: 'reason', label: 'Reason', type: 'input', condition: { field: 'status', notEquals: 'draft' } }, |
| 58 | + ]); |
| 59 | + // Drive explicit values (a field default doesn't populate the record until |
| 60 | + // the control registers a value). status == 'draft' → notEquals false → hidden. |
| 61 | + fireEvent.change(screen.getByLabelText(/status/i), { target: { value: 'draft' } }); |
| 62 | + await waitFor(() => expect(screen.queryByLabelText(/reason/i)).not.toBeInTheDocument()); |
| 63 | + // non-draft → shown |
| 64 | + fireEvent.change(screen.getByLabelText(/status/i), { target: { value: 'final' } }); |
| 65 | + await waitFor(() => expect(screen.getByLabelText(/reason/i)).toBeInTheDocument()); |
| 66 | + }); |
| 67 | + |
| 68 | + it('in: shown when the sibling value is in the list, hidden otherwise', async () => { |
| 69 | + renderForm([ |
| 70 | + { name: 'kind', label: 'Kind', type: 'input', defaultValue: 'a' }, |
| 71 | + { name: 'extra', label: 'Extra', type: 'input', condition: { field: 'kind', in: ['b', 'c'] } }, |
| 72 | + ]); |
| 73 | + // 'a' not in list → hidden |
| 74 | + expect(screen.queryByLabelText(/extra/i)).not.toBeInTheDocument(); |
| 75 | + // 'b' in list → shown |
| 76 | + fireEvent.change(screen.getByLabelText(/kind/i), { target: { value: 'b' } }); |
| 77 | + await waitFor(() => expect(screen.getByLabelText(/extra/i)).toBeInTheDocument()); |
| 78 | + // 'x' not in list → hidden |
| 79 | + fireEvent.change(screen.getByLabelText(/kind/i), { target: { value: 'x' } }); |
| 80 | + await waitFor(() => expect(screen.queryByLabelText(/extra/i)).not.toBeInTheDocument()); |
| 81 | + }); |
| 82 | +}); |
0 commit comments