|
| 1 | +import '@testing-library/jest-dom/vitest'; |
| 2 | + |
| 3 | +import { render, screen } from '@testing-library/react'; |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +import type { DetailResponse, FormSpecPayload } from '@dar/data'; |
| 7 | + |
| 8 | +// Mocked SWR state the mocked useFormSpec returns, set per test. |
| 9 | +let specState: { data: FormSpecPayload | null; loading: boolean; error: Error | null }; |
| 10 | + |
| 11 | +vi.mock('@dar/data', async (importOriginal) => { |
| 12 | + const actual = await importOriginal<typeof import('@dar/data')>(); |
| 13 | + return { |
| 14 | + ...actual, |
| 15 | + useApiClient: () => ({}), |
| 16 | + useFormSpec: () => specState, |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +// Import AFTER the mock so ChangeForm picks up the mocked hooks. |
| 21 | +const { ChangeForm } = await import('./ChangeForm'); |
| 22 | + |
| 23 | +function detail(): DetailResponse { |
| 24 | + return { |
| 25 | + app_label: 'auth', |
| 26 | + model_name: 'group', |
| 27 | + pk: 1, |
| 28 | + label: 'editors', |
| 29 | + permissions: { view: true, add: true, change: true, delete: true }, |
| 30 | + fieldsets: [{ title: null, fields: [] }], |
| 31 | + fields: {}, |
| 32 | + inlines: [], |
| 33 | + save_options: { show_save: true }, |
| 34 | + } as unknown as DetailResponse; |
| 35 | +} |
| 36 | + |
| 37 | +function renderChangeForm() { |
| 38 | + return render( |
| 39 | + <ChangeForm |
| 40 | + data={detail()} |
| 41 | + appLabel="auth" |
| 42 | + modelName="group" |
| 43 | + pk="1" |
| 44 | + query="" |
| 45 | + onCancel={() => {}} |
| 46 | + onSave={async () => {}} |
| 47 | + />, |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +beforeEach(() => { |
| 52 | + specState = { data: null, loading: false, error: null }; |
| 53 | +}); |
| 54 | + |
| 55 | +describe('ChangeForm (#659)', () => { |
| 56 | + it('embeds the legacy admin in an iframe when the backend returns legacy-iframe', () => { |
| 57 | + specState = { |
| 58 | + data: { renderer: 'legacy-iframe', legacy_url: '/admin/auth/group/1/change/' }, |
| 59 | + loading: false, |
| 60 | + error: null, |
| 61 | + }; |
| 62 | + renderChangeForm(); |
| 63 | + const iframe = screen.getByTitle('Legacy admin form') as HTMLIFrameElement; |
| 64 | + expect(iframe).toBeInTheDocument(); |
| 65 | + expect(iframe.src).toContain('/admin/auth/group/1/change/'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('renders the form-spec fields (request-aware get_form / fieldsets) via EditForm', () => { |
| 69 | + specState = { |
| 70 | + data: { |
| 71 | + renderer: 'form-spec', |
| 72 | + fieldsets: [{ title: 'Identity', fields: ['name'], classes: ['collapse'] }], |
| 73 | + fields: { |
| 74 | + name: { |
| 75 | + label: 'Name', |
| 76 | + help_text: '', |
| 77 | + required: true, |
| 78 | + readonly: false, |
| 79 | + type: 'string', |
| 80 | + widget: { kind: 'text', attrs: { maxlength: 150 } }, |
| 81 | + initial: 'editors', |
| 82 | + errors: [], |
| 83 | + }, |
| 84 | + }, |
| 85 | + variant: 'myapp.forms.GroupForm', |
| 86 | + }, |
| 87 | + loading: false, |
| 88 | + error: null, |
| 89 | + }; |
| 90 | + renderChangeForm(); |
| 91 | + const input = screen.getByLabelText('Name', { exact: false }) as HTMLInputElement; |
| 92 | + expect(input).toBeInTheDocument(); |
| 93 | + expect(input.value).toBe('editors'); |
| 94 | + expect(input).toHaveAttribute('maxlength', '150'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('falls back to a default input + note for an unregistered custom widget', () => { |
| 98 | + specState = { |
| 99 | + data: { |
| 100 | + renderer: 'form-spec', |
| 101 | + fieldsets: [{ title: null, fields: ['bio'] }], |
| 102 | + fields: { |
| 103 | + bio: { |
| 104 | + label: 'Bio', |
| 105 | + help_text: '', |
| 106 | + required: false, |
| 107 | + readonly: false, |
| 108 | + type: 'text', |
| 109 | + widget: { kind: 'custom', attrs: {}, widget_class: 'mypkg.widgets.Markdown' }, |
| 110 | + initial: '', |
| 111 | + errors: [], |
| 112 | + }, |
| 113 | + }, |
| 114 | + variant: 'x', |
| 115 | + }, |
| 116 | + loading: false, |
| 117 | + error: null, |
| 118 | + }; |
| 119 | + renderChangeForm(); |
| 120 | + expect(screen.getByText(/is not registered/i)).toBeInTheDocument(); |
| 121 | + expect(screen.getByText('mypkg.widgets.Markdown')).toBeInTheDocument(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('falls back to the detail-driven form when the spec errors (older backend)', () => { |
| 125 | + specState = { data: null, loading: false, error: new Error('404') }; |
| 126 | + // Should not throw; renders the EditForm shell (a <form> with the Save button). |
| 127 | + renderChangeForm(); |
| 128 | + expect(screen.getByRole('button', { name: /save/i })).toBeInTheDocument(); |
| 129 | + }); |
| 130 | +}); |
0 commit comments