@asteby/metacore-runtime-react 49.10.0 → 49.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/README.md +40 -0
- package/dist/column-filter-field.d.ts +21 -0
- package/dist/column-filter-field.d.ts.map +1 -0
- package/dist/column-filter-field.js +51 -0
- package/dist/dialogs/dynamic-record.d.ts +5 -2
- package/dist/dialogs/dynamic-record.d.ts.map +1 -1
- package/dist/dialogs/dynamic-record.js +96 -14
- package/dist/dynamic-columns.d.ts.map +1 -1
- package/dist/dynamic-columns.js +11 -1
- package/dist/dynamic-form.d.ts.map +1 -1
- package/dist/dynamic-form.js +10 -2
- package/dist/dynamic-icon.d.ts +16 -1
- package/dist/dynamic-icon.d.ts.map +1 -1
- package/dist/dynamic-icon.js +36 -5
- package/dist/dynamic-multi-select-field.d.ts +13 -1
- package/dist/dynamic-multi-select-field.d.ts.map +1 -1
- package/dist/dynamic-multi-select-field.js +19 -4
- package/dist/dynamic-table.d.ts.map +1 -1
- package/dist/dynamic-table.js +2 -1
- package/dist/field-widget-registry.d.ts +35 -0
- package/dist/field-widget-registry.d.ts.map +1 -0
- package/dist/field-widget-registry.js +66 -0
- package/dist/icon-aliases.d.ts +2 -0
- package/dist/icon-aliases.d.ts.map +1 -0
- package/dist/icon-aliases.js +594 -0
- package/dist/icon-resolvers.d.ts +13 -0
- package/dist/icon-resolvers.d.ts.map +1 -0
- package/dist/icon-resolvers.js +36 -0
- package/dist/icon-spec.d.ts +17 -0
- package/dist/icon-spec.d.ts.map +1 -0
- package/dist/icon-spec.js +165 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/column-filter-field.test.ts +105 -0
- package/src/__tests__/dynamic-record-field-types.test.tsx +221 -0
- package/src/__tests__/field-widget-registry.test.tsx +165 -0
- package/src/__tests__/icon-component.test.tsx +145 -0
- package/src/__tests__/icon-spec.test.ts +155 -0
- package/src/column-filter-field.ts +66 -0
- package/src/dialogs/dynamic-record.tsx +145 -13
- package/src/dynamic-columns.tsx +22 -1
- package/src/dynamic-form.tsx +12 -1
- package/src/dynamic-icon.tsx +68 -3
- package/src/dynamic-multi-select-field.tsx +29 -4
- package/src/dynamic-table.tsx +2 -1
- package/src/field-widget-registry.tsx +98 -0
- package/src/icon-aliases.ts +602 -0
- package/src/icon-resolvers.ts +59 -0
- package/src/icon-spec.ts +169 -0
- package/src/index.ts +12 -0
- package/src/types.ts +3 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
//
|
|
3
|
+
// DynamicRecordDialog renderer: password / phone / tel / checkbox / time /
|
|
4
|
+
// hidden / multiselect / file used to fall through to a plain text input. Each
|
|
5
|
+
// type now renders its proper control and the value reaches onCreate/onUpdate.
|
|
6
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
7
|
+
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
8
|
+
import { I18nextProvider } from 'react-i18next'
|
|
9
|
+
import i18next from 'i18next'
|
|
10
|
+
|
|
11
|
+
import { DynamicRecordDialog, dropEmptyPasswords, filterVisibleFields, stripHiddenFieldValues } from '../dialogs/dynamic-record'
|
|
12
|
+
import { ApiProvider } from '../api-context'
|
|
13
|
+
import { toast } from 'sonner'
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
cleanup()
|
|
17
|
+
vi.mocked(toast.error).mockClear()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
vi.mock('sonner', () => ({ toast: { error: vi.fn(), success: vi.fn() } }))
|
|
21
|
+
|
|
22
|
+
// Stable i18n instance (a new one per render would loop the dialog).
|
|
23
|
+
const i18n = (() => {
|
|
24
|
+
const inst = i18next.createInstance()
|
|
25
|
+
void inst.init({
|
|
26
|
+
lng: 'es',
|
|
27
|
+
fallbackLng: 'es',
|
|
28
|
+
react: { useSuspense: false },
|
|
29
|
+
resources: { es: { translation: {} } },
|
|
30
|
+
})
|
|
31
|
+
return inst
|
|
32
|
+
})()
|
|
33
|
+
|
|
34
|
+
const api = {
|
|
35
|
+
get: vi.fn(async () => ({ data: { data: {} } })),
|
|
36
|
+
post: vi.fn(async () => ({ data: { data: { url: '/files/logo.pdf' } } })),
|
|
37
|
+
put: async () => ({ data: { success: true } }),
|
|
38
|
+
delete: async () => ({ data: { success: true } }),
|
|
39
|
+
} as never
|
|
40
|
+
|
|
41
|
+
const fields = [
|
|
42
|
+
{ key: 'name', label: 'Nombre', type: 'text' },
|
|
43
|
+
{ key: 'secret', label: 'Clave', type: 'password' },
|
|
44
|
+
{ key: 'phone', label: 'Teléfono', type: 'phone' },
|
|
45
|
+
{ key: 'tel2', label: 'Tel2', type: 'tel' },
|
|
46
|
+
{ key: 'opens', label: 'Abre', type: 'time' },
|
|
47
|
+
{ key: 'active', label: 'Activo', type: 'checkbox' },
|
|
48
|
+
{
|
|
49
|
+
key: 'tags',
|
|
50
|
+
label: 'Etiquetas',
|
|
51
|
+
type: 'multiselect',
|
|
52
|
+
options: [
|
|
53
|
+
{ value: 'a', label: 'Alfa' },
|
|
54
|
+
{ value: 'b', label: 'Beta' },
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
{ key: 'tenant', label: 'Tenant', type: 'hidden', defaultValue: 't-1' },
|
|
58
|
+
{ key: 'doc', label: 'Documento', type: 'file' },
|
|
59
|
+
] as never[]
|
|
60
|
+
|
|
61
|
+
function renderDialog(props: Record<string, unknown>) {
|
|
62
|
+
return render(
|
|
63
|
+
<I18nextProvider i18n={i18n}>
|
|
64
|
+
<ApiProvider client={api}>
|
|
65
|
+
<DynamicRecordDialog
|
|
66
|
+
open
|
|
67
|
+
onOpenChange={() => {}}
|
|
68
|
+
model="clinics"
|
|
69
|
+
schema={{ title: 'Clínica', fields } as never}
|
|
70
|
+
{...(props as object)}
|
|
71
|
+
/>
|
|
72
|
+
</ApiProvider>
|
|
73
|
+
</I18nextProvider>,
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const inputOf = (label: string) => {
|
|
78
|
+
const el = screen.getByText(label).closest('div')?.parentElement?.querySelector('input')
|
|
79
|
+
if (!el) throw new Error(`no input for ${label}`)
|
|
80
|
+
return el as HTMLInputElement
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
describe('DynamicRecordDialog field types', () => {
|
|
84
|
+
it('renders each type with its control and sends the values on create', async () => {
|
|
85
|
+
const onCreate = vi.fn(async () => undefined)
|
|
86
|
+
renderDialog({ mode: 'create', onCreate })
|
|
87
|
+
await waitFor(() => expect(screen.getByText('Nombre')).toBeTruthy())
|
|
88
|
+
|
|
89
|
+
expect(inputOf('Clave').type).toBe('password')
|
|
90
|
+
expect(inputOf('Teléfono').type).toBe('tel')
|
|
91
|
+
expect(inputOf('Tel2').type).toBe('tel')
|
|
92
|
+
expect(inputOf('Abre').type).toBe('time')
|
|
93
|
+
expect(screen.getByRole('checkbox')).toBeTruthy()
|
|
94
|
+
expect(screen.queryByText('Tenant')).toBeNull() // hidden: not rendered
|
|
95
|
+
expect(document.querySelector('input[type="file"]')).toBeTruthy()
|
|
96
|
+
|
|
97
|
+
fireEvent.change(inputOf('Clave'), { target: { value: 's3cret' } })
|
|
98
|
+
fireEvent.change(inputOf('Teléfono'), { target: { value: '5551234' } })
|
|
99
|
+
fireEvent.change(inputOf('Abre'), { target: { value: '09:30' } })
|
|
100
|
+
fireEvent.click(screen.getByRole('checkbox'))
|
|
101
|
+
fireEvent.click(screen.getByRole('button', { name: 'Crear' }))
|
|
102
|
+
|
|
103
|
+
await waitFor(() => expect(onCreate).toHaveBeenCalled())
|
|
104
|
+
const payload = (onCreate.mock.calls[0] as unknown[])[0] as Record<string, unknown>
|
|
105
|
+
expect(payload.secret).toBe('s3cret')
|
|
106
|
+
expect(payload.phone).toBe('5551234')
|
|
107
|
+
expect(payload.opens).toBe('09:30')
|
|
108
|
+
expect(payload.active).toBe(true)
|
|
109
|
+
expect(payload.tenant).toBe('t-1') // hidden default still in the payload
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('never prefills a password on edit and does not send it when left empty', async () => {
|
|
113
|
+
const onUpdate = vi.fn(async () => undefined)
|
|
114
|
+
renderDialog({
|
|
115
|
+
mode: 'edit',
|
|
116
|
+
recordId: '1',
|
|
117
|
+
onUpdate,
|
|
118
|
+
initialRecord: {
|
|
119
|
+
id: 1,
|
|
120
|
+
name: 'Clínica',
|
|
121
|
+
secret: '$2a$hash',
|
|
122
|
+
phone: '1',
|
|
123
|
+
tel2: '2',
|
|
124
|
+
opens: '08:00',
|
|
125
|
+
active: true,
|
|
126
|
+
tags: ['a'],
|
|
127
|
+
tenant: 't-9',
|
|
128
|
+
doc: '/f.pdf',
|
|
129
|
+
},
|
|
130
|
+
})
|
|
131
|
+
await waitFor(() => expect(inputOf('Clave').type).toBe('password'))
|
|
132
|
+
expect(inputOf('Clave').value).toBe('')
|
|
133
|
+
expect(document.body.innerHTML).not.toContain('$2a$hash')
|
|
134
|
+
|
|
135
|
+
fireEvent.click(screen.getByRole('button', { name: /Guardar|Actualizar/ }))
|
|
136
|
+
await waitFor(() => expect(onUpdate).toHaveBeenCalled())
|
|
137
|
+
const payload = (onUpdate.mock.calls[0] as unknown[])[1] as Record<string, unknown>
|
|
138
|
+
expect('secret' in payload).toBe(false)
|
|
139
|
+
expect(payload.tenant).toBe('t-9')
|
|
140
|
+
expect(payload.tags).toEqual(['a'])
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('keeps already supported types unchanged (text/boolean)', () => {
|
|
144
|
+
const visible = filterVisibleFields(
|
|
145
|
+
[
|
|
146
|
+
{ key: 'a', label: 'A', type: 'text' },
|
|
147
|
+
{ key: 'b', label: 'B', type: 'boolean' },
|
|
148
|
+
{ key: 'c', label: 'C', type: 'hidden' },
|
|
149
|
+
] as never[],
|
|
150
|
+
'edit',
|
|
151
|
+
)
|
|
152
|
+
expect(visible.map(f => f.key)).toEqual(['a', 'b'])
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('dropEmptyPasswords only drops empty password fields', () => {
|
|
156
|
+
const out = dropEmptyPasswords(
|
|
157
|
+
{ p: '', q: 'x', name: '' },
|
|
158
|
+
[
|
|
159
|
+
{ key: 'p', label: 'P', type: 'password' },
|
|
160
|
+
{ key: 'q', label: 'Q', type: 'password' },
|
|
161
|
+
{ key: 'name', label: 'N', type: 'text' },
|
|
162
|
+
] as never[],
|
|
163
|
+
)
|
|
164
|
+
expect(out).toEqual({ q: 'x', name: '' })
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
it('edit: an empty required password does not block saving and is not sent', async () => {
|
|
168
|
+
const onUpdate = vi.fn(async () => undefined)
|
|
169
|
+
renderDialog({
|
|
170
|
+
mode: 'edit',
|
|
171
|
+
recordId: '1',
|
|
172
|
+
onUpdate,
|
|
173
|
+
schema: {
|
|
174
|
+
title: 'Clínica',
|
|
175
|
+
fields: [
|
|
176
|
+
{ key: 'name', label: 'Nombre', type: 'text' },
|
|
177
|
+
{ key: 'secret', label: 'Clave', type: 'password', required: true },
|
|
178
|
+
],
|
|
179
|
+
},
|
|
180
|
+
initialRecord: { id: 1, name: 'Clínica' },
|
|
181
|
+
})
|
|
182
|
+
await waitFor(() => expect(inputOf('Clave').type).toBe('password'))
|
|
183
|
+
fireEvent.click(screen.getByRole('button', { name: /Guardar|Actualizar/ }))
|
|
184
|
+
await waitFor(() => expect(onUpdate).toHaveBeenCalled())
|
|
185
|
+
const payload = (onUpdate.mock.calls[0] as unknown[])[1] as Record<string, unknown>
|
|
186
|
+
expect('secret' in payload).toBe(false)
|
|
187
|
+
expect(toast.error).not.toHaveBeenCalled()
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
it('create: a required password is still mandatory', async () => {
|
|
191
|
+
const onCreate = vi.fn(async () => undefined)
|
|
192
|
+
renderDialog({
|
|
193
|
+
mode: 'create',
|
|
194
|
+
onCreate,
|
|
195
|
+
schema: {
|
|
196
|
+
title: 'Clínica',
|
|
197
|
+
fields: [
|
|
198
|
+
{ key: 'name', label: 'Nombre', type: 'text' },
|
|
199
|
+
{ key: 'secret', label: 'Clave', type: 'password', required: true },
|
|
200
|
+
],
|
|
201
|
+
},
|
|
202
|
+
})
|
|
203
|
+
await waitFor(() => expect(inputOf('Clave').type).toBe('password'))
|
|
204
|
+
fireEvent.click(screen.getByRole('button', { name: 'Crear' }))
|
|
205
|
+
await waitFor(() => expect(toast.error).toHaveBeenCalled())
|
|
206
|
+
expect(onCreate).not.toHaveBeenCalled()
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
it('hidden + visible_when: current behaviour is always stripped; plain hidden passes through', () => {
|
|
210
|
+
const flds = [
|
|
211
|
+
{ key: 'kind', label: 'K', type: 'text' },
|
|
212
|
+
{ key: 'h_when', label: 'H', type: 'hidden', visible_when: { field: 'kind', equals: 'a' } },
|
|
213
|
+
{ key: 'h_plain', label: 'P', type: 'hidden' },
|
|
214
|
+
] as never[]
|
|
215
|
+
for (const kind of ['a', 'b']) {
|
|
216
|
+
const out = stripHiddenFieldValues({ kind, h_when: 'x', h_plain: 'y' }, flds, 'create')
|
|
217
|
+
expect('h_when' in out).toBe(false) // even when the predicate is true
|
|
218
|
+
expect(out.h_plain).toBe('y')
|
|
219
|
+
}
|
|
220
|
+
})
|
|
221
|
+
})
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
//
|
|
3
|
+
// Registro de widgets de campo: un host enchufa `widget: "<nombre>"` en
|
|
4
|
+
// DynamicForm y en el diálogo de registro (EditField / ReadonlyEditField).
|
|
5
|
+
// Sin registro, el render por `type` queda intacto.
|
|
6
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
7
|
+
import { act, cleanup, render, screen, fireEvent } from '@testing-library/react'
|
|
8
|
+
|
|
9
|
+
// Referencia ESTABLE: un `t` nuevo por render provoca un bucle de render.
|
|
10
|
+
const i18nMock = { t: (k: string, o?: any) => o?.defaultValue ?? k, i18n: { language: 'es' } }
|
|
11
|
+
vi.mock('react-i18next', () => ({
|
|
12
|
+
useTranslation: () => i18nMock,
|
|
13
|
+
}))
|
|
14
|
+
|
|
15
|
+
import { DynamicForm } from '../dynamic-form'
|
|
16
|
+
import { EditField, ReadonlyEditField } from '../dialogs/dynamic-record'
|
|
17
|
+
import {
|
|
18
|
+
registerFieldWidget,
|
|
19
|
+
getFieldWidget,
|
|
20
|
+
clearFieldWidgets,
|
|
21
|
+
listFieldWidgets,
|
|
22
|
+
type FieldWidgetProps,
|
|
23
|
+
} from '../field-widget-registry'
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
cleanup()
|
|
27
|
+
clearFieldWidgets()
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
function PinWidget({ field, value, onChange, disabled, error, record }: FieldWidgetProps) {
|
|
31
|
+
return (
|
|
32
|
+
<div data-testid="pin" data-disabled={String(!!disabled)} data-record={record?.name ?? ''}>
|
|
33
|
+
<span data-testid="pin-value">{String(value ?? '')}</span>
|
|
34
|
+
<span data-testid="pin-key">{field.key}</span>
|
|
35
|
+
{error && <span data-testid="pin-error">{error}</span>}
|
|
36
|
+
<button type="button" onClick={() => onChange('19.43,-99.13')}>
|
|
37
|
+
fijar
|
|
38
|
+
</button>
|
|
39
|
+
</div>
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const pinField = { key: 'location', label: 'Ubicación', type: 'text', widget: 'map-pin' }
|
|
44
|
+
|
|
45
|
+
describe('field widget registry', () => {
|
|
46
|
+
it('registra, resuelve y el disposer lo quita', () => {
|
|
47
|
+
const dispose = registerFieldWidget('map-pin', PinWidget)
|
|
48
|
+
expect(getFieldWidget('map-pin')).toBe(PinWidget)
|
|
49
|
+
expect(listFieldWidgets()).toEqual(['map-pin'])
|
|
50
|
+
dispose()
|
|
51
|
+
expect(getFieldWidget('map-pin')).toBeUndefined()
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('un disposer viejo no quita un reemplazo posterior', () => {
|
|
55
|
+
const Other = () => null
|
|
56
|
+
const first = registerFieldWidget('map-pin', PinWidget)
|
|
57
|
+
registerFieldWidget('map-pin', Other)
|
|
58
|
+
first()
|
|
59
|
+
expect(getFieldWidget('map-pin')).toBe(Other)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('nombres desconocidos o vacíos no rompen', () => {
|
|
63
|
+
expect(getFieldWidget('nope')).toBeUndefined()
|
|
64
|
+
expect(getFieldWidget(undefined)).toBeUndefined()
|
|
65
|
+
expect(getFieldWidget('')).toBeUndefined()
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
describe('DynamicForm con widget registrado', () => {
|
|
70
|
+
it('renderiza el widget y propaga onChange al payload', async () => {
|
|
71
|
+
registerFieldWidget('map-pin', PinWidget)
|
|
72
|
+
const onSubmit = vi.fn()
|
|
73
|
+
render(
|
|
74
|
+
<DynamicForm
|
|
75
|
+
fields={[pinField as any]}
|
|
76
|
+
initialValues={{ location: 'a' }}
|
|
77
|
+
onSubmit={onSubmit}
|
|
78
|
+
/>,
|
|
79
|
+
)
|
|
80
|
+
expect(screen.getByTestId('pin-value').textContent).toBe('a')
|
|
81
|
+
fireEvent.click(screen.getByText('fijar'))
|
|
82
|
+
expect(screen.getByTestId('pin-value').textContent).toBe('19.43,-99.13')
|
|
83
|
+
await act(async () => {
|
|
84
|
+
screen.getByText('Guardar').click()
|
|
85
|
+
})
|
|
86
|
+
expect(onSubmit).toHaveBeenCalledTimes(1)
|
|
87
|
+
expect(onSubmit.mock.calls[0][0]).toMatchObject({ location: '19.43,-99.13' })
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('record son los valores vivos del form: un widget ve el cambio de un campo hermano (modo crear)', () => {
|
|
91
|
+
function SiblingWidget({ record }: FieldWidgetProps) {
|
|
92
|
+
return <span data-testid="sib">{String(record?.name ?? '')}</span>
|
|
93
|
+
}
|
|
94
|
+
registerFieldWidget('sib', SiblingWidget)
|
|
95
|
+
render(
|
|
96
|
+
<DynamicForm
|
|
97
|
+
fields={[
|
|
98
|
+
{ key: 'name', label: 'Nombre', type: 'text' } as any,
|
|
99
|
+
{ key: 'extra', label: 'Extra', type: 'text', widget: 'sib' } as any,
|
|
100
|
+
]}
|
|
101
|
+
onSubmit={vi.fn()}
|
|
102
|
+
/>,
|
|
103
|
+
)
|
|
104
|
+
expect(screen.getByTestId('sib').textContent).toBe('')
|
|
105
|
+
fireEvent.change(document.querySelector('input#name') as HTMLInputElement, { target: { value: 'Ana' } })
|
|
106
|
+
expect(screen.getByTestId('sib').textContent).toBe('Ana')
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('sin registro conserva el render por type', () => {
|
|
110
|
+
render(<DynamicForm fields={[pinField as any]} initialValues={{ location: 'a' }} onSubmit={vi.fn()} />)
|
|
111
|
+
expect(screen.queryByTestId('pin')).toBeNull()
|
|
112
|
+
expect(document.querySelector('input#location')).not.toBeNull()
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('registrar después del primer render re-renderiza el campo', () => {
|
|
116
|
+
render(<DynamicForm fields={[pinField as any]} initialValues={{ location: 'a' }} onSubmit={vi.fn()} />)
|
|
117
|
+
expect(screen.queryByTestId('pin')).toBeNull()
|
|
118
|
+
act(() => {
|
|
119
|
+
registerFieldWidget('map-pin', PinWidget)
|
|
120
|
+
})
|
|
121
|
+
expect(screen.getByTestId('pin')).toBeTruthy()
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('tras el disposer vuelve al render por type', () => {
|
|
125
|
+
const dispose = registerFieldWidget('map-pin', PinWidget)
|
|
126
|
+
render(<DynamicForm fields={[pinField as any]} initialValues={{ location: 'a' }} onSubmit={vi.fn()} />)
|
|
127
|
+
expect(screen.getByTestId('pin')).toBeTruthy()
|
|
128
|
+
act(() => dispose())
|
|
129
|
+
expect(screen.queryByTestId('pin')).toBeNull()
|
|
130
|
+
expect(document.querySelector('input#location')).not.toBeNull()
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
describe('diálogo de registro (EditField)', () => {
|
|
135
|
+
it('renderiza el widget con value/record/error y propaga onChange', () => {
|
|
136
|
+
registerFieldWidget('map-pin', PinWidget)
|
|
137
|
+
const onChange = vi.fn()
|
|
138
|
+
render(
|
|
139
|
+
<EditField
|
|
140
|
+
field={pinField as any}
|
|
141
|
+
value="x"
|
|
142
|
+
onChange={onChange}
|
|
143
|
+
record={{ name: 'Clínica' }}
|
|
144
|
+
error="Requerido"
|
|
145
|
+
/>,
|
|
146
|
+
)
|
|
147
|
+
expect(screen.getByTestId('pin-value').textContent).toBe('x')
|
|
148
|
+
expect(screen.getByTestId('pin').getAttribute('data-record')).toBe('Clínica')
|
|
149
|
+
expect(screen.getByTestId('pin-error').textContent).toBe('Requerido')
|
|
150
|
+
fireEvent.click(screen.getByText('fijar'))
|
|
151
|
+
expect(onChange).toHaveBeenCalledWith('19.43,-99.13')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('sin registro conserva el input por type', () => {
|
|
155
|
+
render(<EditField field={pinField as any} value="x" onChange={vi.fn()} />)
|
|
156
|
+
expect(screen.queryByTestId('pin')).toBeNull()
|
|
157
|
+
expect(document.querySelector('input')).not.toBeNull()
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('campo readonly: el widget llega deshabilitado', () => {
|
|
161
|
+
registerFieldWidget('map-pin', PinWidget)
|
|
162
|
+
render(<ReadonlyEditField field={pinField as any} value="x" />)
|
|
163
|
+
expect(screen.getByTestId('pin').getAttribute('data-disabled')).toBe('true')
|
|
164
|
+
})
|
|
165
|
+
})
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
3
|
+
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
4
|
+
|
|
5
|
+
vi.mock('@tanstack/react-router', () => ({ useNavigate: () => () => {} }))
|
|
6
|
+
const I18N = { t: (k: string, o?: { defaultValue?: string }) => o?.defaultValue ?? k, i18n: { language: 'es' } }
|
|
7
|
+
vi.mock('react-i18next', () => ({ useTranslation: () => I18N }))
|
|
8
|
+
|
|
9
|
+
import { Icon, DynamicIcon } from '../dynamic-icon'
|
|
10
|
+
import { registerIconResolver, resolveIconSpec } from '../icon-resolvers'
|
|
11
|
+
import { DynamicTable } from '../dynamic-table'
|
|
12
|
+
import { ApiProvider, type ApiClient } from '../api-context'
|
|
13
|
+
import { useMetadataCache } from '../metadata-cache'
|
|
14
|
+
import type { TableMetadata } from '../types'
|
|
15
|
+
|
|
16
|
+
afterEach(cleanup)
|
|
17
|
+
|
|
18
|
+
describe('registerIconResolver', () => {
|
|
19
|
+
it('el resolutor del host gana a los built-ins y el disposer lo retira', () => {
|
|
20
|
+
const dispose = registerIconResolver((s) => (s === 'fas fa-brain' ? { kind: 'emoji', value: '🧠' } : null))
|
|
21
|
+
expect(resolveIconSpec('fas fa-brain')).toEqual({ kind: 'emoji', value: '🧠' })
|
|
22
|
+
expect(resolveIconSpec('fas fa-bone')).toEqual({ kind: 'lucide', name: 'Bone' })
|
|
23
|
+
dispose()
|
|
24
|
+
expect(resolveIconSpec('fas fa-brain')).toEqual({ kind: 'lucide', name: 'Brain' })
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('el más reciente tiene prioridad; null cede al siguiente', () => {
|
|
28
|
+
const a = registerIconResolver(() => ({ kind: 'emoji', value: 'A' }))
|
|
29
|
+
const b = registerIconResolver((s) => (s === 'x' ? { kind: 'emoji', value: 'B' } : null))
|
|
30
|
+
expect(resolveIconSpec('x')).toEqual({ kind: 'emoji', value: 'B' })
|
|
31
|
+
expect(resolveIconSpec('y')).toEqual({ kind: 'emoji', value: 'A' })
|
|
32
|
+
b()
|
|
33
|
+
expect(resolveIconSpec('x')).toEqual({ kind: 'emoji', value: 'A' })
|
|
34
|
+
a()
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('un resolutor que lanza se ignora; un ReactNode se renderiza tal cual', () => {
|
|
38
|
+
const a = registerIconResolver(() => {
|
|
39
|
+
throw new Error('boom')
|
|
40
|
+
})
|
|
41
|
+
const b = registerIconResolver((s) => (s === 'sprite:x' ? <svg data-testid="sprite" /> : null))
|
|
42
|
+
expect(resolveIconSpec('Brain')).toEqual({ kind: 'lucide', name: 'Brain' })
|
|
43
|
+
render(<Icon name="sprite:x" />)
|
|
44
|
+
expect(screen.getByTestId('sprite')).toBeTruthy()
|
|
45
|
+
a()
|
|
46
|
+
b()
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
describe('Icon', () => {
|
|
51
|
+
it('lucide: svg, decorativo por defecto', async () => {
|
|
52
|
+
const { container } = render(<Icon name="fas fa-brain" className="h-4" />)
|
|
53
|
+
await waitFor(() => expect(container.querySelector('svg')).toBeTruthy())
|
|
54
|
+
expect(container.querySelector('svg')?.getAttribute('aria-hidden')).toBe('true')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('label vuelve el icono accesible', async () => {
|
|
58
|
+
const { container } = render(<Icon name="Brain" label="Cerebro" />)
|
|
59
|
+
await waitFor(() => expect(container.querySelector('svg')).toBeTruthy())
|
|
60
|
+
expect(container.querySelector('svg')?.getAttribute('aria-label')).toBe('Cerebro')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('emoji: span aria-hidden', () => {
|
|
64
|
+
const { container } = render(<Icon name="🩺" className="x" />)
|
|
65
|
+
const span = container.querySelector('span')!
|
|
66
|
+
expect(span.textContent).toBe('🩺')
|
|
67
|
+
expect(span.getAttribute('aria-hidden')).toBe('true')
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('image: img lazy con alt vacío y se oculta si falla', () => {
|
|
71
|
+
const { container } = render(<Icon name="https://x.test/a.png" className="h-5" />)
|
|
72
|
+
const img = container.querySelector('img')!
|
|
73
|
+
expect(img.getAttribute('src')).toBe('https://x.test/a.png')
|
|
74
|
+
expect(img.getAttribute('alt')).toBe('')
|
|
75
|
+
expect(img.getAttribute('loading')).toBe('lazy')
|
|
76
|
+
expect(img.getAttribute('decoding')).toBe('async')
|
|
77
|
+
fireEvent.error(img)
|
|
78
|
+
expect(container.querySelector('img')).toBeNull()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('image: resolveImageSrc transforma la ruta', () => {
|
|
82
|
+
const { container } = render(<Icon name="/u/a.png" resolveImageSrc={(s) => `/api${s}`} />)
|
|
83
|
+
expect(container.querySelector('img')?.getAttribute('src')).toBe('/api/u/a.png')
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('unknown: nunca texto crudo; fallback opcional', () => {
|
|
87
|
+
const a = render(<Icon name="algo raro" />)
|
|
88
|
+
expect(a.container.textContent).toBe('')
|
|
89
|
+
a.unmount()
|
|
90
|
+
const b = render(<Icon name="javascript:alert(1)" fallback={<i data-testid="fb" />} />)
|
|
91
|
+
expect(screen.getByTestId('fb')).toBeTruthy()
|
|
92
|
+
expect(b.container.querySelector('img')).toBeNull()
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('DynamicIcon conserva su contrato con nombres Lucide', async () => {
|
|
96
|
+
const { container } = render(<DynamicIcon name="CreditCard" className="mr-2 h-4 w-4" />)
|
|
97
|
+
expect(container.firstElementChild?.getAttribute('class')).toContain('mr-2 h-4 w-4')
|
|
98
|
+
await waitFor(() => expect(container.querySelector('svg:not([data-glyph-pending])')).toBeTruthy())
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
describe('DynamicTable columna type "icon"', () => {
|
|
103
|
+
const metaIcon: TableMetadata = {
|
|
104
|
+
title: 'Specialties',
|
|
105
|
+
endpoint: '/data/specialty',
|
|
106
|
+
columns: [
|
|
107
|
+
{ key: 'name', label: 'Nombre', type: 'text', sortable: false, filterable: false, searchable: false },
|
|
108
|
+
{ key: 'icon', label: 'Icono', type: 'icon', sortable: false, filterable: false, searchable: false },
|
|
109
|
+
],
|
|
110
|
+
actions: [],
|
|
111
|
+
perPageOptions: [10],
|
|
112
|
+
defaultPerPage: 10,
|
|
113
|
+
searchPlaceholder: 'Buscar...',
|
|
114
|
+
enableCRUDActions: false,
|
|
115
|
+
hasActions: false,
|
|
116
|
+
}
|
|
117
|
+
const rows = [
|
|
118
|
+
{ id: 1, name: 'Neurología', icon: 'fas fa-brain' },
|
|
119
|
+
{ id: 2, name: 'Raro', icon: 'no se entiende' },
|
|
120
|
+
{ id: 3, name: 'Vacio', icon: '' },
|
|
121
|
+
]
|
|
122
|
+
const api: ApiClient = {
|
|
123
|
+
get: vi.fn(async (url: string) => {
|
|
124
|
+
if (url.startsWith('/metadata/table/')) return { data: { success: true, data: metaIcon } }
|
|
125
|
+
if (url.endsWith('/facets')) return { data: { success: true, data: [] } }
|
|
126
|
+
return { data: { success: true, data: rows, meta: { total: rows.length } } }
|
|
127
|
+
}),
|
|
128
|
+
post: vi.fn(async () => ({ data: { success: true, data: null } })),
|
|
129
|
+
put: vi.fn(async () => ({ data: { success: true, data: null } })),
|
|
130
|
+
delete: vi.fn(async () => ({ data: { success: true, data: null } })),
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
it('"fas fa-brain" dibuja un glifo y nunca el texto crudo', async () => {
|
|
134
|
+
useMetadataCache.getState().setMetadata('specialty', metaIcon)
|
|
135
|
+
const { container } = render(
|
|
136
|
+
<ApiProvider client={api}>
|
|
137
|
+
<DynamicTable model="specialty" enableUrlSync={false} />
|
|
138
|
+
</ApiProvider>,
|
|
139
|
+
)
|
|
140
|
+
await waitFor(() => expect(screen.getAllByText('Neurología').length).toBeGreaterThan(0))
|
|
141
|
+
expect(container.textContent).not.toContain('fas fa-brain')
|
|
142
|
+
expect(container.textContent).not.toContain('no se entiende')
|
|
143
|
+
await waitFor(() => expect(container.querySelector('tbody svg.lucide:not([data-glyph-pending])')).toBeTruthy())
|
|
144
|
+
})
|
|
145
|
+
})
|