Skip to content

Commit ab645af

Browse files
committed
fix(components): a client-side refusal retires the previous outcome banner and toast
Claude-Session: https://claude.ai/code/session_01CzUs1oNtFWhWUXT7rmoGy2 Co-authored-by: Claude <noreply@anthropic.com>
1 parent cf9a965 commit ab645af

3 files changed

Lines changed: 47 additions & 7 deletions

File tree

‎ui/packages/components/src/renderers/form/__tests__/form-server-field-errors.test.tsx‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,34 @@ describe('form renderer — server-rejected fields', () => {
153153
expect(String(toastErrorSpy.mock.calls[0][0])).toMatch(/Owner is required/);
154154
});
155155

156+
it('a later client-side refusal retires the previous refusal`s banner and toast (objectstack#25874)', async () => {
157+
// The client refusal raises no toast of its own any more, so nothing would
158+
// supersede the earlier server refusal under the shared outcome id: the
159+
// attempt has to take the stale banner and toast down itself.
160+
const dismissSpy = vi.spyOn(toast, 'dismiss').mockImplementation(() => 'id' as any);
161+
const forbidden = Object.assign(new Error('insufficient privileges'), {
162+
code: 'FORBIDDEN',
163+
httpStatus: 403,
164+
details: { error: 'insufficient privileges', code: 'FORBIDDEN' },
165+
});
166+
const onSubmit = vi.fn().mockRejectedValue(forbidden);
167+
vi.spyOn(console, 'warn').mockImplementation(() => {});
168+
renderForm([{ name: 'title', label: 'Title', type: 'input', required: true }], onSubmit);
169+
const input = screen.getByLabelText(/title/i);
170+
171+
fireEvent.change(input, { target: { value: 'Filled' } });
172+
submit();
173+
await waitFor(() => expect(screen.getByText(/permission to save/i)).toBeTruthy());
174+
175+
dismissSpy.mockClear();
176+
fireEvent.change(input, { target: { value: '' } });
177+
submit();
178+
179+
await waitFor(() => expect(screen.queryByText(/permission to save/i)).toBeNull());
180+
expect(dismissSpy).toHaveBeenCalled();
181+
expect(onSubmit).toHaveBeenCalledTimes(1);
182+
});
183+
156184
it('leaves a non-field failure on the generic path', async () => {
157185
const forbidden = Object.assign(new Error('insufficient privileges'), {
158186
code: 'FORBIDDEN',

‎ui/packages/components/src/renderers/form/__tests__/visiblewhen-clear-notice-8070.test.tsx‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
* the ruling's default — one form-level notice under the form's existing
2323
* outcome-toast id, naming every cleared column at once, so it retires like the
2424
* form's other outcome messages: a later submit outcome supersedes it, and the
25-
* dismissal at the next attempt takes it down.
25+
* dismissal at the next attempt takes it down. A client-side refusal raises no
26+
* toast of its own (objectstack#25874 — each field's message carries it), so
27+
* that attempt dismisses the id instead of superseding it.
2628
*
2729
* Only a field that HELD something is named: an already-empty value, an empty
2830
* list and an unchecked two-state control (whose empty state is `false`) are
@@ -199,7 +201,7 @@ describe('#8070 — a visibleWhen clear is named to the user', () => {
199201
expect(notices()[1]).not.toContain('Contact');
200202
});
201203

202-
it('a later submit refusal supersedes the notice, and the next accepted attempt retires it — one id throughout', async () => {
204+
it('a later submit attempt retires the notice, refused or accepted — one id throughout', async () => {
203205
const onSubmit = vi.fn();
204206
renderForm({
205207
fields: [{ name: 'subject', label: 'Subject', type: 'input', required: true }, ...FIELDS.slice(0, 2)],
@@ -211,13 +213,15 @@ describe('#8070 — a visibleWhen clear is named to the user', () => {
211213
await waitFor(() => expect(warningSpy).toHaveBeenCalledTimes(1));
212214
const noticeId = (warningSpy.mock.calls[0][1] as { id: string }).id;
213215

214-
// Refused on the client: the refusal is published under the SAME id, which
215-
// sonner treats as an update of the notice rather than a second toast.
216+
// Refused on the client: the refusal is the inline message under Subject,
217+
// and the attempt dismisses the notice's id rather than leaving it up.
216218
save();
217-
await waitFor(() => expect(errorSpy).toHaveBeenCalledTimes(1));
218-
expect(errorSpy.mock.calls[0][1]).toMatchObject({ id: noticeId });
219+
await waitFor(() => expect(dismissSpy).toHaveBeenCalledWith(noticeId));
220+
expect(errorSpy).not.toHaveBeenCalled();
221+
expect(onSubmit).not.toHaveBeenCalled();
219222

220-
// Accepted on the client: the attempt starts by dismissing that id.
223+
// Accepted on the client: the attempt starts by dismissing that id too.
224+
dismissSpy.mockClear();
221225
fireEvent.change(screen.getByLabelText(/subject/i), { target: { value: 'Kickoff' } });
222226
save();
223227
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));

‎ui/packages/components/src/renderers/form/form.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,6 +3075,14 @@ ComponentRegistry.register('form',
30753075
// regardless of scroll position. Every name here has a drawn input:
30763076
// react-hook-form validates only mounted controls, and an undrawn field
30773077
// is unmounted, so the returned list is always empty on this path.
3078+
//
3079+
// This attempt's outcome is those inline messages, so whatever outcome
3080+
// the form showed before it — the previous attempt's banner and its toast
3081+
// twin, or a clear-on-hide notice (objectui#8070) — retires here, as it
3082+
// does at the start of an attempt that clears validation. The refusal
3083+
// toast used to do that by superseding the same id.
3084+
setSubmitError(null);
3085+
toast.dismiss(outcomeToastId);
30783086
announceFieldErrors(Object.keys(validationErrors || {}));
30793087
});
30803088

0 commit comments

Comments
 (0)