|
| 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 | + * A create form does not apply `readonlyWhen` (objectui#13436). |
| 11 | + * |
| 12 | + * objectql exempts INSERT from `readonlyWhen` entirely, but the renderer used |
| 13 | + * to evaluate it on a create form against the seeded defaults. Two shapes |
| 14 | + * locked fields the server accepts on insert: |
| 15 | + * |
| 16 | + * - a predicate over a column with a default: |
| 17 | + * `record.status == 'pending_verify' || record.status == 'active'` with |
| 18 | + * `status` defaulted to `'pending_verify'`; |
| 19 | + * - a `!=` predicate over an unset column: |
| 20 | + * `record.approval_status != 'returned'` (`null != 'returned'` is TRUE). |
| 21 | + * |
| 22 | + * A create form is the one the host hands no `previousValues` (the same |
| 23 | + * `isCreateForm` signal the read-only submit strip gates on). An edit form |
| 24 | + * (with `previousValues`) keeps applying the rule, and static `readonly: true` |
| 25 | + * still locks on create. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 29 | +import { render, screen } from '@testing-library/react'; |
| 30 | +import { ComponentRegistry } from '@object-ui/core'; |
| 31 | +// Module-scope import (not `beforeAll`) so the cold transform is billed to the |
| 32 | +// import phase — objectui#3010 / object-ui/no-dynamic-import-in-test-hook. |
| 33 | +import '../../../renderers'; |
| 34 | + |
| 35 | +function renderForm(schema: Record<string, unknown>) { |
| 36 | + const Form = ComponentRegistry.get('form')!; |
| 37 | + return render(<Form schema={{ type: 'form', showSubmit: false, showCancel: false, ...schema }} />); |
| 38 | +} |
| 39 | + |
| 40 | +const LOCKED_ON_STATUS = "record.status == 'pending_verify' || record.status == 'active'"; |
| 41 | +const LOCKED_UNLESS_RETURNED = "record.approval_status != 'returned'"; |
| 42 | + |
| 43 | +const defaultedFields = [ |
| 44 | + { name: 'status', label: 'Status', type: 'input', readonly: true }, |
| 45 | + { name: 'bank_name', label: 'Bank name', type: 'input', required: true, readonlyWhen: LOCKED_ON_STATUS }, |
| 46 | + { name: 'account_no', label: 'Account no', type: 'input', required: true, readonlyWhen: LOCKED_ON_STATUS }, |
| 47 | +]; |
| 48 | + |
| 49 | +const unsetFields = [ |
| 50 | + { name: 'approval_status', label: 'Approval status', type: 'input' }, |
| 51 | + { name: 'title', label: 'Title', type: 'input', readonlyWhen: LOCKED_UNLESS_RETURNED }, |
| 52 | +]; |
| 53 | + |
| 54 | +afterEach(() => { |
| 55 | + vi.restoreAllMocks(); |
| 56 | +}); |
| 57 | + |
| 58 | +describe('form renderer — readonlyWhen on a create form (objectui#13436)', () => { |
| 59 | + it('leaves a field editable on create when the predicate is TRUE against a seeded default', () => { |
| 60 | + renderForm({ fields: defaultedFields, defaultValues: { status: 'pending_verify' } }); |
| 61 | + |
| 62 | + expect(screen.getByLabelText(/^Bank name/)).not.toHaveAttribute('readonly'); |
| 63 | + expect(screen.getByLabelText(/^Account no/)).not.toHaveAttribute('readonly'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('still locks a static `readonly: true` field on create', () => { |
| 67 | + renderForm({ fields: defaultedFields, defaultValues: { status: 'pending_verify' } }); |
| 68 | + |
| 69 | + expect(screen.getByLabelText(/^Status/)).toHaveAttribute('readonly'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('leaves a field editable on create when a `!=` predicate is TRUE over an unset column', () => { |
| 73 | + renderForm({ fields: unsetFields }); |
| 74 | + |
| 75 | + expect(screen.getByLabelText(/^Title/)).not.toHaveAttribute('readonly'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('keeps locking on an edit form: the defaulted-column predicate', () => { |
| 79 | + renderForm({ |
| 80 | + fields: defaultedFields, |
| 81 | + defaultValues: { status: 'pending_verify', bank_name: 'B', account_no: '1' }, |
| 82 | + previousValues: { status: 'pending_verify', bank_name: 'B', account_no: '1' }, |
| 83 | + }); |
| 84 | + |
| 85 | + expect(screen.getByLabelText(/^Bank name/)).toHaveAttribute('readonly'); |
| 86 | + expect(screen.getByLabelText(/^Account no/)).toHaveAttribute('readonly'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('keeps locking on an edit form: the `!=` predicate over an unset column', () => { |
| 90 | + renderForm({ |
| 91 | + fields: unsetFields, |
| 92 | + defaultValues: { title: 'T' }, |
| 93 | + previousValues: { title: 'T' }, |
| 94 | + }); |
| 95 | + |
| 96 | + expect(screen.getByLabelText(/^Title/)).toHaveAttribute('readonly'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('reports no readonlyWhen fault on create, where the rule is not applied; an edit form still does', () => { |
| 100 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 101 | + const readonlyWhenWarnings = () => |
| 102 | + warn.mock.calls.filter((args) => String(args[0]).includes('readonlyWhen of field')); |
| 103 | + |
| 104 | + // Unique sources: `warnPredicateFailure` dedupes per source module-wide. |
| 105 | + renderForm({ |
| 106 | + fields: [{ name: 'memo', label: 'Memo', type: 'input', visibleWhen: 'true', readonlyWhen: 'record.memo_13436_create ==' }], |
| 107 | + }); |
| 108 | + expect(readonlyWhenWarnings()).toHaveLength(0); |
| 109 | + |
| 110 | + renderForm({ |
| 111 | + fields: [{ name: 'memo', label: 'Memo', type: 'input', visibleWhen: 'true', readonlyWhen: 'record.memo_13436_edit ==' }], |
| 112 | + defaultValues: { memo: 'm' }, |
| 113 | + previousValues: { memo: 'm' }, |
| 114 | + }); |
| 115 | + expect(readonlyWhenWarnings().length).toBeGreaterThan(0); |
| 116 | + }); |
| 117 | +}); |
0 commit comments