|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26578 — a step between screens toasts only a record it saved. |
| 5 | + * |
| 6 | + * A wizard step used to answer every pause with `Saved — next step`, so a flat |
| 7 | + * step that only collected variables told the user something was saved. The |
| 8 | + * toast now follows what the dialog itself persisted: |
| 9 | + * |
| 10 | + * 1. A flat step that pauses again draws the next screen and toasts nothing. |
| 11 | + * 2. An `object-form` step that pauses again has written its record, so it |
| 12 | + * keeps the toast — the form's own confirmation is silenced by `onSuccess`, |
| 13 | + * which makes this the only one the user gets. |
| 14 | + * 3. A flat step that ends the run still gets the completion toast: only the |
| 15 | + * between-steps toast moved. |
| 16 | + * |
| 17 | + * The real `ScreenView` draws every flat screen; an `object-form` step is a |
| 18 | + * stub whose button stands in for the form's Save (as in |
| 19 | + * `FlowRunner.refusedMessageOnly-13342.test.tsx`). |
| 20 | + */ |
| 21 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 22 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 23 | +import userEvent from '@testing-library/user-event'; |
| 24 | + |
| 25 | +const toastError = vi.fn(); |
| 26 | +const toastSuccess = vi.fn(); |
| 27 | +vi.mock('sonner', () => ({ toast: { error: toastError, success: toastSuccess } })); |
| 28 | + |
| 29 | +vi.mock('../ScreenView.js', async (importOriginal) => { |
| 30 | + const actual = await importOriginal<typeof import('../ScreenView.js')>(); |
| 31 | + type Props = Parameters<typeof actual.ScreenView>[0]; |
| 32 | + return { |
| 33 | + ...actual, |
| 34 | + ScreenView: (props: Props) => |
| 35 | + actual.isObjectFormScreen(props.screen) ? ( |
| 36 | + <button type="button" onClick={() => props.objectForm?.onSuccess?.({ id: 'ctr_1' })}> |
| 37 | + Stub save |
| 38 | + </button> |
| 39 | + ) : ( |
| 40 | + <actual.ScreenView {...props} /> |
| 41 | + ), |
| 42 | + }; |
| 43 | +}); |
| 44 | + |
| 45 | +const { FlowRunner } = await import('../FlowRunner'); |
| 46 | +type ScreenFlowState = import('../FlowRunner').ScreenFlowState; |
| 47 | +type ScreenSpec = ScreenFlowState['screen']; |
| 48 | + |
| 49 | +const INFO_SCREEN: ScreenSpec = { |
| 50 | + nodeId: 'screen_info', |
| 51 | + title: 'Contract details', |
| 52 | + fields: [{ name: 'contract_title', label: 'Title', type: 'text', required: true }], |
| 53 | +}; |
| 54 | + |
| 55 | +const CONTRACT_FORM_SCREEN: ScreenSpec = { |
| 56 | + nodeId: 'screen_contract', |
| 57 | + title: 'New contract', |
| 58 | + kind: 'object-form', |
| 59 | + objectName: 'contract', |
| 60 | + idVariable: 'contractId', |
| 61 | +}; |
| 62 | + |
| 63 | +const NEXT_SCREEN: ScreenSpec = { |
| 64 | + nodeId: 'screen_submit', |
| 65 | + title: 'Send to legal', |
| 66 | + fields: [{ name: 'notes', label: 'Notes', type: 'text' }], |
| 67 | +}; |
| 68 | + |
| 69 | +const pausedBody = { success: true, data: { success: true, status: 'paused', runId: 'run_1', screen: NEXT_SCREEN } }; |
| 70 | +const completedBody = { success: true, data: { success: true } }; |
| 71 | + |
| 72 | +function jsonResponse(body: unknown): Response { |
| 73 | + return new Response(JSON.stringify(body), { status: 200, headers: { 'Content-Type': 'application/json' } }); |
| 74 | +} |
| 75 | + |
| 76 | +function mount(s: ScreenSpec, body: unknown) { |
| 77 | + const onComplete = vi.fn(); |
| 78 | + const authFetch = vi.fn(async () => jsonResponse(body)); |
| 79 | + render( |
| 80 | + <FlowRunner |
| 81 | + state={{ flowName: 'start_contract', runId: 'run_1', screen: s }} |
| 82 | + authFetch={authFetch} |
| 83 | + baseUrl="" |
| 84 | + onClose={vi.fn()} |
| 85 | + onComplete={onComplete} |
| 86 | + />, |
| 87 | + ); |
| 88 | + return { onComplete, authFetch }; |
| 89 | +} |
| 90 | + |
| 91 | +async function typeAndSubmit() { |
| 92 | + const user = userEvent.setup(); |
| 93 | + await user.type(screen.getByRole('textbox'), 'Supply agreement'); |
| 94 | + await user.click(screen.getByRole('button', { name: 'Submit' })); |
| 95 | +} |
| 96 | + |
| 97 | +beforeEach(() => { |
| 98 | + toastError.mockClear(); |
| 99 | + toastSuccess.mockClear(); |
| 100 | +}); |
| 101 | + |
| 102 | +describe('objectstack#26578 — a step between screens toasts only a record it saved', () => { |
| 103 | + it('a flat step that pauses again draws the next screen and toasts nothing', async () => { |
| 104 | + const { authFetch, onComplete } = mount(INFO_SCREEN, pausedBody); |
| 105 | + await typeAndSubmit(); |
| 106 | + |
| 107 | + expect(await screen.findByRole('heading', { name: 'Send to legal' })).toBeInTheDocument(); |
| 108 | + expect(authFetch).toHaveBeenCalledTimes(1); |
| 109 | + expect(toastSuccess).not.toHaveBeenCalled(); |
| 110 | + expect(toastError).not.toHaveBeenCalled(); |
| 111 | + expect(onComplete).not.toHaveBeenCalled(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('an `object-form` step that pauses again keeps the toast for the record it saved', async () => { |
| 115 | + const { authFetch } = mount(CONTRACT_FORM_SCREEN, pausedBody); |
| 116 | + await userEvent.setup().click(screen.getByRole('button', { name: 'Stub save' })); |
| 117 | + |
| 118 | + expect(await screen.findByRole('heading', { name: 'Send to legal' })).toBeInTheDocument(); |
| 119 | + expect(authFetch).toHaveBeenCalledWith( |
| 120 | + '/api/v1/automation/start_contract/runs/run_1/resume', |
| 121 | + expect.objectContaining({ body: JSON.stringify({ inputs: { contractId: 'ctr_1' } }) }), |
| 122 | + ); |
| 123 | + expect(toastSuccess.mock.calls).toEqual([['Saved — next step']]); |
| 124 | + }); |
| 125 | + |
| 126 | + it('a flat step that ends the run still gets the completion toast', async () => { |
| 127 | + const { onComplete } = mount(INFO_SCREEN, completedBody); |
| 128 | + await typeAndSubmit(); |
| 129 | + |
| 130 | + await waitFor(() => expect(onComplete).toHaveBeenCalledTimes(1)); |
| 131 | + expect(toastSuccess.mock.calls).toEqual([['Flow "start_contract" completed']]); |
| 132 | + }); |
| 133 | +}); |
0 commit comments