|
| 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 | + * #25933 — the edit form's `status` select offers only the moves the object's |
| 11 | + * error-severity `state_machine` allows from the stored value. |
| 12 | + * |
| 13 | + * Measured on the showcase task before the fix: a `backlog` task's edit dialog |
| 14 | + * listed all five states, and picking `done` came back from the server as |
| 15 | + * "Invalid task status transition." Pinned through the console's edit dialog |
| 16 | + * (the global `ModalForm`) and the in-page simple form, and read off the |
| 17 | + * hidden native `<select>` Radix renders inside a form, which lists exactly |
| 18 | + * the options the widget was handed. |
| 19 | + */ |
| 20 | + |
| 21 | +import React from 'react'; |
| 22 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 23 | +import { render, waitFor, cleanup } from '@testing-library/react'; |
| 24 | +import { registerAllFields } from '@object-ui/fields'; |
| 25 | +import type { DataSource } from '@object-ui/types'; |
| 26 | +import { ModalForm } from '../ModalForm'; |
| 27 | +import { ObjectForm } from '../ObjectForm'; |
| 28 | + |
| 29 | +registerAllFields(); |
| 30 | + |
| 31 | +const STATUS_OPTIONS = [ |
| 32 | + { label: 'Backlog', value: 'backlog' }, |
| 33 | + { label: 'To Do', value: 'todo' }, |
| 34 | + { label: 'In Progress', value: 'in_progress' }, |
| 35 | + { label: 'In Review', value: 'in_review' }, |
| 36 | + { label: 'Done', value: 'done' }, |
| 37 | +]; |
| 38 | + |
| 39 | +/** The showcase task (`examples/app-showcase` task.object.ts), as `/meta` serves it. */ |
| 40 | +const TASK = (rule: Record<string, unknown> = {}) => ({ |
| 41 | + name: 'showcase_task', |
| 42 | + fields: { |
| 43 | + title: { type: 'text', label: 'Title' }, |
| 44 | + status: { type: 'select', label: 'Status', options: STATUS_OPTIONS }, |
| 45 | + }, |
| 46 | + validations: [ |
| 47 | + { |
| 48 | + type: 'state_machine', |
| 49 | + name: 'task_status_flow', |
| 50 | + field: 'status', |
| 51 | + events: ['update'], |
| 52 | + message: 'Invalid task status transition.', |
| 53 | + transitions: { |
| 54 | + backlog: ['todo'], |
| 55 | + todo: ['in_progress', 'backlog'], |
| 56 | + in_progress: ['in_review', 'todo'], |
| 57 | + in_review: ['done', 'in_progress'], |
| 58 | + done: ['in_progress'], |
| 59 | + }, |
| 60 | + ...rule, |
| 61 | + }, |
| 62 | + ], |
| 63 | +}); |
| 64 | + |
| 65 | +const makeDS = (objectSchema: unknown, record: Record<string, unknown>): DataSource => |
| 66 | + ({ |
| 67 | + getObjectSchema: vi.fn().mockResolvedValue(objectSchema), |
| 68 | + create: vi.fn().mockResolvedValue({ id: 't1' }), |
| 69 | + update: vi.fn().mockResolvedValue({ id: 't1' }), |
| 70 | + findOne: vi.fn().mockResolvedValue(record), |
| 71 | + }) as unknown as DataSource; |
| 72 | + |
| 73 | +/** The values the hidden native select offers, empty placeholder left out. */ |
| 74 | +const offered = () => |
| 75 | + Array.from( |
| 76 | + (document.body.querySelector('select[name="status"]') as HTMLSelectElement | null)?.options ?? [], |
| 77 | + ) |
| 78 | + .map((o) => o.value) |
| 79 | + .filter((v) => v !== ''); |
| 80 | + |
| 81 | +const ALL = STATUS_OPTIONS.map((o) => o.value); |
| 82 | + |
| 83 | +afterEach(() => cleanup()); |
| 84 | + |
| 85 | +const renderModal = (mode: 'edit' | 'create', ds: DataSource) => |
| 86 | + render( |
| 87 | + <ModalForm |
| 88 | + schema={{ |
| 89 | + type: 'object-form', |
| 90 | + formType: 'modal', |
| 91 | + objectName: 'showcase_task', |
| 92 | + mode, |
| 93 | + ...(mode === 'edit' ? { recordId: 't1' } : {}), |
| 94 | + title: 'Task', |
| 95 | + open: true, |
| 96 | + onOpenChange: vi.fn(), |
| 97 | + fields: ['title', 'status'], |
| 98 | + } as any} |
| 99 | + dataSource={ds} |
| 100 | + />, |
| 101 | + ); |
| 102 | + |
| 103 | +describe('state-machine select narrowing (#25933)', () => { |
| 104 | + it('the console edit dialog offers a backlog task only backlog and todo', async () => { |
| 105 | + renderModal('edit', makeDS(TASK(), { id: 't1', title: 'T', status: 'backlog' })); |
| 106 | + await waitFor(() => expect(offered()).toEqual(['backlog', 'todo'])); |
| 107 | + }); |
| 108 | + |
| 109 | + it('narrows from the stored value of an in-review task', async () => { |
| 110 | + renderModal('edit', makeDS(TASK(), { id: 't1', title: 'T', status: 'in_review' })); |
| 111 | + await waitFor(() => expect(offered()).toEqual(['in_progress', 'in_review', 'done'])); |
| 112 | + }); |
| 113 | + |
| 114 | + it('CONTROL: a create dialog offers every option', async () => { |
| 115 | + renderModal('create', makeDS(TASK(), {})); |
| 116 | + await waitFor(() => expect(offered()).toEqual(ALL)); |
| 117 | + }); |
| 118 | + |
| 119 | + it('CONTROL: a warning-severity machine keeps every option', async () => { |
| 120 | + renderModal('edit', makeDS(TASK({ severity: 'warning' }), { id: 't1', title: 'T', status: 'backlog' })); |
| 121 | + await waitFor(() => expect(document.body.querySelector('select[name="status"]')).toBeTruthy()); |
| 122 | + // Let the record load settle before reading the list. |
| 123 | + await waitFor(() => expect(offered()).toEqual(ALL)); |
| 124 | + }); |
| 125 | + |
| 126 | + it('the in-page simple edit form narrows the same way', async () => { |
| 127 | + render( |
| 128 | + <ObjectForm |
| 129 | + schema={{ |
| 130 | + type: 'object-form', |
| 131 | + objectName: 'showcase_task', |
| 132 | + mode: 'edit', |
| 133 | + recordId: 't1', |
| 134 | + fields: ['title', 'status'], |
| 135 | + } as any} |
| 136 | + dataSource={makeDS(TASK(), { id: 't1', title: 'T', status: 'done' })} |
| 137 | + />, |
| 138 | + ); |
| 139 | + await waitFor(() => expect(offered()).toEqual(['in_progress', 'done'])); |
| 140 | + }); |
| 141 | +}); |
0 commit comments