|
| 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 | + * objectstack-ai/objectstack#26461 — the full-page record form (`/new` and |
| 11 | + * `/edit`) names the saved record in its success toast, the same way the |
| 12 | + * dialog paths do since objectstack-ai/objectstack#26172 |
| 13 | + * (`ux-conventions.write-feedback`): `Task "Quarterly review" created`, by its |
| 14 | + * ADR-0079 display name. A record with nothing nameable (the resolver's |
| 15 | + * `Record #<id>` floor, or no saved record at all) keeps the object-label |
| 16 | + * toast (`Task created successfully`). |
| 17 | + * |
| 18 | + * The form is a stand-in whose button calls `onSuccess` with the saved record, |
| 19 | + * as `ObjectForm` does after its write (`schema.onSuccess(result)`); the toast |
| 20 | + * is read off sonner's `toast.success`. |
| 21 | + * |
| 22 | + * Direction, written before the run: with the page's toast still the object |
| 23 | + * label alone, every "names the record" cell RED (`Task created |
| 24 | + * successfully`); every "no name" cell GREEN on both sides. |
| 25 | + */ |
| 26 | + |
| 27 | +import '@testing-library/jest-dom/vitest'; |
| 28 | +import * as React from 'react'; |
| 29 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 30 | +import { render, screen, cleanup, waitFor, fireEvent } from '@testing-library/react'; |
| 31 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 32 | +import { I18nProvider } from '@object-ui/i18n'; |
| 33 | +import { RecordFormPage } from './RecordFormPage'; |
| 34 | + |
| 35 | +const h = React.createElement; |
| 36 | + |
| 37 | +const { toastSuccess, saveState } = vi.hoisted(() => ({ |
| 38 | + toastSuccess: vi.fn(), |
| 39 | + saveState: { saved: undefined as Record<string, unknown> | undefined }, |
| 40 | +})); |
| 41 | + |
| 42 | +vi.mock('sonner', () => ({ |
| 43 | + toast: Object.assign(vi.fn(), { |
| 44 | + success: toastSuccess, |
| 45 | + error: vi.fn(), |
| 46 | + info: vi.fn(), |
| 47 | + warning: vi.fn(), |
| 48 | + loading: vi.fn(), |
| 49 | + dismiss: vi.fn(), |
| 50 | + }), |
| 51 | +})); |
| 52 | + |
| 53 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 54 | + ...(await importOriginal<Record<string, unknown>>()), |
| 55 | + useAuth: () => ({ |
| 56 | + user: { id: 'u1', name: 'Ada', email: 'ada@example.com', role: 'admin' }, |
| 57 | + getAuthConfig: async () => ({ features: {} }), |
| 58 | + activeOrganization: null, |
| 59 | + }), |
| 60 | +})); |
| 61 | + |
| 62 | +// Stand-in for the real form: a button that calls `onSuccess` with the saved |
| 63 | +// record, as `ObjectForm` hands its write's result to `schema.onSuccess`. |
| 64 | +vi.mock('@object-ui/plugin-form', async (importOriginal) => ({ |
| 65 | + ...(await importOriginal<Record<string, unknown>>()), |
| 66 | + ObjectForm: ({ schema }: { schema: { onSuccess?: (saved?: unknown) => void } }) => |
| 67 | + h( |
| 68 | + 'button', |
| 69 | + { |
| 70 | + type: 'button', |
| 71 | + 'data-testid': 'fire-success', |
| 72 | + onClick: () => schema.onSuccess?.(saveState.saved), |
| 73 | + }, |
| 74 | + 'ok', |
| 75 | + ), |
| 76 | +})); |
| 77 | + |
| 78 | +/** Declares its name field: a saved task is named by `subject`. */ |
| 79 | +const TASK = { |
| 80 | + name: 'showcase_task', |
| 81 | + label: 'Task', |
| 82 | + nameField: 'subject', |
| 83 | + fields: { subject: { type: 'text', label: 'Subject' }, done: { type: 'boolean', label: 'Done' } }, |
| 84 | +}; |
| 85 | +const METADATA = { objects: [TASK], loading: false }; |
| 86 | +vi.mock('../providers/MetadataProvider', () => ({ |
| 87 | + useMetadata: () => METADATA, |
| 88 | +})); |
| 89 | +vi.mock('../providers/AdapterProvider', () => ({ |
| 90 | + useAdapter: () => null, |
| 91 | +})); |
| 92 | + |
| 93 | +beforeEach(() => { |
| 94 | + toastSuccess.mockClear(); |
| 95 | + saveState.saved = undefined; |
| 96 | +}); |
| 97 | +afterEach(cleanup); |
| 98 | + |
| 99 | +/** Mount the page in `mode`, save `saved` through the form, and return the toast it raised. */ |
| 100 | +async function saveOn( |
| 101 | + mode: 'create' | 'edit', |
| 102 | + saved: Record<string, unknown> | undefined, |
| 103 | + language = 'en', |
| 104 | +): Promise<string> { |
| 105 | + saveState.saved = saved; |
| 106 | + const path = |
| 107 | + mode === 'create' ? '/apps/crm/showcase_task/new' : '/apps/crm/showcase_task/record/t1/edit'; |
| 108 | + const route = |
| 109 | + mode === 'create' |
| 110 | + ? '/apps/:appName/:objectName/new' |
| 111 | + : '/apps/:appName/:objectName/record/:recordId/edit'; |
| 112 | + render( |
| 113 | + h(I18nProvider, { |
| 114 | + config: { defaultLanguage: language, detectBrowserLanguage: false }, |
| 115 | + children: h( |
| 116 | + MemoryRouter, |
| 117 | + { initialEntries: [path] }, |
| 118 | + h(Routes, null, h(Route, { path: route, element: h(RecordFormPage, { mode }) })), |
| 119 | + ), |
| 120 | + }), |
| 121 | + ); |
| 122 | + fireEvent.click(screen.getByTestId('fire-success')); |
| 123 | + await waitFor(() => expect(toastSuccess).toHaveBeenCalledTimes(1)); |
| 124 | + return String(toastSuccess.mock.calls[0][0]); |
| 125 | +} |
| 126 | + |
| 127 | +describe('RecordFormPage — the save toast names the saved record (objectstack-ai/objectstack#26461)', () => { |
| 128 | + it('create: names the record by its declared name field', async () => { |
| 129 | + expect(await saveOn('create', { id: 't1', subject: 'Quarterly review' })).toBe( |
| 130 | + 'Task "Quarterly review" created', |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it('update: names the record by its declared name field', async () => { |
| 135 | + expect(await saveOn('edit', { id: 't1', subject: 'Quarterly review' })).toBe( |
| 136 | + 'Task "Quarterly review" updated', |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + it('create, no name (the `Record #<id>` floor): the object-label toast', async () => { |
| 141 | + expect(await saveOn('create', { id: 't2', done: false })).toBe('Task created successfully'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('update, no name (the `Record #<id>` floor): the object-label toast', async () => { |
| 145 | + expect(await saveOn('edit', { id: 't1', done: true })).toBe('Task updated successfully'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('update, no saved record handed back: the object-label toast', async () => { |
| 149 | + expect(await saveOn('edit', undefined)).toBe('Task updated successfully'); |
| 150 | + }); |
| 151 | + |
| 152 | + it('zh: the pack string names the record', async () => { |
| 153 | + expect(await saveOn('edit', { id: 't1', subject: '季度复盘' }, 'zh')).toBe('Task“季度复盘”已更新'); |
| 154 | + }); |
| 155 | +}); |
0 commit comments