|
| 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 | + * ActionParamDialog — Confirm is disabled while a file/image param's upload is |
| 11 | + * in flight, so a param can't be submitted before its fileId resolves (the |
| 12 | + * value only becomes the fileId once the presigned upload settles). The upload |
| 13 | + * widget is stubbed here so the mid-upload window is deterministic; the real |
| 14 | + * signal path (FileField/ImageField → useUploadingSignal → onUploadingChange) |
| 15 | + * is covered in the fields package. |
| 16 | + */ |
| 17 | +import { describe, it, expect, vi } from 'vitest'; |
| 18 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 19 | +import type { ActionParamDef } from '@object-ui/core'; |
| 20 | + |
| 21 | +vi.mock('@object-ui/fields', async (importActual) => { |
| 22 | + const actual = await importActual<typeof import('@object-ui/fields')>(); |
| 23 | + return { |
| 24 | + ...actual, |
| 25 | + // Stub only the upload widgets with a control that drives onUploadingChange; |
| 26 | + // every other type resolves to its real widget so paramToField resolution |
| 27 | + // and the rest of the dialog stay authentic. |
| 28 | + getLazyFieldWidget: (type: string) => { |
| 29 | + if (type === 'file' || type === 'image') { |
| 30 | + return function FakeUploadWidget({ |
| 31 | + onChange, |
| 32 | + onUploadingChange, |
| 33 | + }: { |
| 34 | + onChange: (v: unknown) => void; |
| 35 | + onUploadingChange?: (u: boolean) => void; |
| 36 | + }) { |
| 37 | + return ( |
| 38 | + <div> |
| 39 | + <button type="button" data-testid="start-upload" onClick={() => onUploadingChange?.(true)}> |
| 40 | + start |
| 41 | + </button> |
| 42 | + <button |
| 43 | + type="button" |
| 44 | + data-testid="finish-upload" |
| 45 | + onClick={() => { |
| 46 | + onChange('file_123'); |
| 47 | + onUploadingChange?.(false); |
| 48 | + }} |
| 49 | + > |
| 50 | + finish |
| 51 | + </button> |
| 52 | + </div> |
| 53 | + ); |
| 54 | + }; |
| 55 | + } |
| 56 | + return actual.getLazyFieldWidget(type); |
| 57 | + }, |
| 58 | + }; |
| 59 | +}); |
| 60 | + |
| 61 | +// Import AFTER the mock is registered. |
| 62 | +const { ActionParamDialog } = await import('./ActionParamDialog'); |
| 63 | + |
| 64 | +function openDialog(params: ActionParamDef[]) { |
| 65 | + const resolve = vi.fn(); |
| 66 | + render(<ActionParamDialog state={{ open: true, params, resolve }} onOpenChange={() => {}} />); |
| 67 | + return resolve; |
| 68 | +} |
| 69 | + |
| 70 | +const confirmBtn = () => screen.getByText(/actionDialog\.(confirm|uploading)/).closest('button')!; |
| 71 | + |
| 72 | +describe('ActionParamDialog — upload-in-progress guard', () => { |
| 73 | + it('disables Confirm while a file param is uploading and re-enables when it settles', async () => { |
| 74 | + const resolve = openDialog([{ name: 'doc', label: 'Doc', type: 'file' }]); |
| 75 | + await screen.findByTestId('start-upload'); |
| 76 | + |
| 77 | + // Idle → enabled. |
| 78 | + expect(confirmBtn()).not.toBeDisabled(); |
| 79 | + |
| 80 | + // Upload starts → Confirm disabled, label switches. |
| 81 | + fireEvent.click(screen.getByTestId('start-upload')); |
| 82 | + expect(confirmBtn()).toBeDisabled(); |
| 83 | + expect(screen.getByText('actionDialog.uploading')).toBeTruthy(); |
| 84 | + |
| 85 | + // Upload settles → Confirm enabled, value captured. |
| 86 | + fireEvent.click(screen.getByTestId('finish-upload')); |
| 87 | + expect(confirmBtn()).not.toBeDisabled(); |
| 88 | + |
| 89 | + fireEvent.click(confirmBtn()); |
| 90 | + expect(resolve).toHaveBeenCalledWith({ doc: 'file_123' }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('does not submit while an upload is in flight (keyboard-submit guard)', async () => { |
| 94 | + const resolve = openDialog([{ name: 'doc', label: 'Doc', type: 'file' }]); |
| 95 | + await screen.findByTestId('start-upload'); |
| 96 | + fireEvent.click(screen.getByTestId('start-upload')); |
| 97 | + // Even a direct click on the (disabled) button must not resolve. |
| 98 | + fireEvent.click(confirmBtn()); |
| 99 | + expect(resolve).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments