@open-mercato/ui 0.10.1-develop.8130.1.8964ae359f → 0.10.1-develop.8143.1.bf633ea932

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.
@@ -199,14 +199,33 @@ describe('LookupSelect keyboard accessibility', () => {
199
199
  />,
200
200
  )
201
201
 
202
- const selected = await view.findByRole('option', { selected: true })
203
- expect(selected).toHaveTextContent(ITEMS[0].title)
202
+ // A value that is there on mount is a finished pick (#5109): the summary carries the
203
+ // fetched title and the clear control, the list stays shut.
204
+ const summary = await view.findByTestId('lookup-select-selected')
205
+ expect(summary).toHaveTextContent(ITEMS[0].title)
206
+ expect(view.queryByRole('listbox')).toBeNull()
204
207
 
205
208
  const clear = view.getByRole('button', { name: 'Clear selection' })
206
209
  fireEvent.click(clear)
207
210
  expect(onChange).toHaveBeenCalledWith(null)
208
211
  })
209
212
 
213
+ it('collapses onto the summary when the host seeds a value after mount', async () => {
214
+ function SeededHarness() {
215
+ const [value, setValue] = React.useState<string | null>(null)
216
+ React.useEffect(() => {
217
+ setValue('plot-1')
218
+ }, [])
219
+ return <LookupSelect value={value} onChange={setValue} fetchItems={async () => ITEMS} minQuery={1} />
220
+ }
221
+ const view = render(<SeededHarness />)
222
+ expect((await view.findByTestId('lookup-select-selected'))).toHaveTextContent(ITEMS[0].title)
223
+ expect(view.queryByRole('listbox')).toBeNull()
224
+
225
+ fireEvent.click(view.getByRole('button', { name: 'Change' }))
226
+ expect(await view.findAllByRole('option')).toHaveLength(2)
227
+ })
228
+
210
229
  it('stays collapsed at minQuery >= 1 once the selection is cleared', async () => {
211
230
  const view = render(
212
231
  <LookupSelect value={null} onChange={() => {}} fetchItems={async () => ITEMS} minQuery={1} />,
@@ -308,6 +327,118 @@ describe('LookupSelect selected value display', () => {
308
327
  })
309
328
  })
310
329
 
330
+ // Picking a result ends the interaction (#6196): the list used to stay open with the
331
+ // typed query still in the box and only a thin border on one of several identical
332
+ // cards, so users clicked the same customer again, unsure whether it registered.
333
+ describe('LookupSelect closes on pick (#6196)', () => {
334
+ const ITEMS = [
335
+ { id: 'cust-1', title: 'ExcelMed', subtitle: 'excel@med.test' },
336
+ { id: 'cust-2', title: 'Fazenda Sul' },
337
+ ]
338
+
339
+ function Harness({ onPick }: { onPick?: (next: string | null) => void }) {
340
+ const [value, setValue] = React.useState<string | null>(null)
341
+ return (
342
+ <LookupSelect
343
+ value={value}
344
+ onChange={(next) => {
345
+ setValue(next)
346
+ onPick?.(next)
347
+ }}
348
+ fetchItems={async () => ITEMS}
349
+ />
350
+ )
351
+ }
352
+
353
+ it('closes the list, clears the query and shows the picked item with Change and Clear', async () => {
354
+ const { container } = render(<Harness />)
355
+ const input = getInput(container)
356
+
357
+ fireEvent.change(input, { target: { value: 'Ex' } })
358
+ fireEvent.click(await screen.findByRole('option', { name: /ExcelMed/ }))
359
+
360
+ expect(screen.queryByRole('listbox')).toBeNull()
361
+ // The summary row stands in for the search box until Change is clicked.
362
+ expect(container.querySelector('input')).toBeNull()
363
+ const summary = screen.getByTestId('lookup-select-selected')
364
+ expect(summary).toHaveTextContent('ExcelMed')
365
+ expect(summary).toHaveTextContent('excel@med.test')
366
+ expect(screen.getByRole('button', { name: 'Change' })).toBeInTheDocument()
367
+ expect(screen.getByRole('button', { name: 'Clear selection' })).toBeInTheDocument()
368
+ })
369
+
370
+ it('reopens the list with the selection marked when Change is clicked', async () => {
371
+ const { container } = render(<Harness />)
372
+ const input = getInput(container)
373
+
374
+ fireEvent.change(input, { target: { value: 'Ex' } })
375
+ fireEvent.click(await screen.findByRole('option', { name: /ExcelMed/ }))
376
+ fireEvent.click(screen.getByRole('button', { name: 'Change' }))
377
+
378
+ const selected = await screen.findByRole('option', { selected: true })
379
+ expect(selected).toHaveTextContent('ExcelMed')
380
+ expect(selected).toHaveTextContent('Selected')
381
+ expect(document.activeElement).toBe(getInput(container))
382
+ })
383
+
384
+ it('commits from the keyboard too', async () => {
385
+ const onPick = jest.fn()
386
+ const { container } = render(<Harness onPick={onPick} />)
387
+ const input = getInput(container)
388
+
389
+ fireEvent.change(input, { target: { value: 'Faz' } })
390
+ await screen.findAllByRole('option')
391
+ fireEvent.keyDown(input, { key: 'ArrowDown' })
392
+ fireEvent.keyDown(input, { key: 'ArrowDown' })
393
+ fireEvent.keyDown(input, { key: 'Enter' })
394
+
395
+ expect(onPick).toHaveBeenCalledWith('cust-2')
396
+ expect(screen.queryByRole('listbox')).toBeNull()
397
+ expect(screen.getByTestId('lookup-select-selected')).toHaveTextContent('Fazenda Sul')
398
+ expect(screen.getByRole('button', { name: 'Change' })).toHaveFocus()
399
+
400
+ fireEvent.click(screen.getByRole('button', { name: 'Clear selection' }))
401
+
402
+ expect(screen.queryByTestId('lookup-select-selected')).toBeNull()
403
+ expect(getInput(container)).toHaveFocus()
404
+ })
405
+
406
+ it('keeps browsing when the parent does not adopt the pick (multi-add pickers)', async () => {
407
+ const addPlot = jest.fn()
408
+ const { container } = render(
409
+ <LookupSelect value={null} onChange={addPlot} fetchItems={async () => ITEMS} minQuery={0} />,
410
+ )
411
+
412
+ fireEvent.click((await screen.findAllByRole('option'))[0])
413
+ expect(addPlot).toHaveBeenCalledWith('cust-1')
414
+ expect(screen.getByRole('listbox')).toBeInTheDocument()
415
+ expect(screen.queryByTestId('lookup-select-selected')).toBeNull()
416
+ expect(getInput(container).value).toBe('')
417
+ })
418
+
419
+ it('never shows a hint that embeds the raw id in the summary', async () => {
420
+ const RECORD_ID = 'd7f88312-f4b3-44b7-b03a-dc10e561cf8e'
421
+ function SalesHarness() {
422
+ const [value, setValue] = React.useState<string | null>(null)
423
+ return (
424
+ <LookupSelect
425
+ value={value}
426
+ onChange={setValue}
427
+ fetchItems={async () => [{ id: RECORD_ID, title: 'ExcelMed' }]}
428
+ selectedHintLabel={(id) => `Selected customer: ${id}`}
429
+ />
430
+ )
431
+ }
432
+ const { container } = render(<SalesHarness />)
433
+
434
+ fireEvent.change(getInput(container), { target: { value: 'Ex' } })
435
+ fireEvent.click(await screen.findByRole('option'))
436
+
437
+ expect(screen.getByTestId('lookup-select-selected')).toHaveTextContent('ExcelMed')
438
+ expect(container.textContent ?? '').not.toContain(RECORD_ID)
439
+ })
440
+ })
441
+
311
442
  // A set `value` opens the list and fires a browse fetch with an empty query. Its
312
443
  // result used to replace the items outright, so an edit form hydrated with
313
444
  // `value={storedId}` + `options={[storedOption]}` lost the stored selection from
@@ -319,6 +450,11 @@ describe('LookupSelect keeps the selection across browse fetches', () => {
319
450
  { id: 'contractor-1', title: 'Alpha' },
320
451
  { id: 'contractor-2', title: 'Beta' },
321
452
  ]
453
+ // A value present on mount starts on the collapsed summary (#5109); browsing resumes
454
+ // through its Change control.
455
+ async function openList() {
456
+ fireEvent.click(await screen.findByRole('button', { name: 'Change' }))
457
+ }
322
458
 
323
459
  it('keeps a hydrated selection that is missing from the first page', async () => {
324
460
  const fetchItems = jest.fn(async () => FIRST_PAGE)
@@ -331,6 +467,8 @@ describe('LookupSelect keeps the selection across browse fetches', () => {
331
467
  />,
332
468
  )
333
469
 
470
+ expect(await screen.findByTestId('lookup-select-selected')).toHaveTextContent(STORED.title)
471
+ await openList()
334
472
  await screen.findByText('Alpha')
335
473
  expect(fetchItems).toHaveBeenCalledWith('')
336
474
  expect(screen.getByRole('option', { selected: true })).toHaveTextContent(STORED.title)
@@ -351,6 +489,7 @@ describe('LookupSelect keeps the selection across browse fetches', () => {
351
489
  />,
352
490
  )
353
491
 
492
+ await openList()
354
493
  await screen.findByText('Alpha')
355
494
  expect(screen.getAllByRole('option')).toHaveLength(3)
356
495
  expect(screen.getByRole('option', { selected: true })).toHaveTextContent('Stored Contractor (renamed)')
@@ -366,6 +505,7 @@ describe('LookupSelect keeps the selection across browse fetches', () => {
366
505
  fetchItems={fetchItems}
367
506
  />,
368
507
  )
508
+ await openList()
369
509
  await screen.findByText('Beta')
370
510
 
371
511
  fireEvent.change(getInput(container), { target: { value: 'Alp' } })
@@ -385,6 +525,7 @@ describe('LookupSelect keeps the selection across browse fetches', () => {
385
525
  fetchItems={fetchItems}
386
526
  />,
387
527
  )
528
+ await openList()
388
529
  const input = getInput(container)
389
530
  await screen.findByText('Beta')
390
531
 
@@ -407,7 +548,7 @@ describe('LookupSelect keeps the selection across browse fetches', () => {
407
548
 
408
549
  fireEvent.change(input, { target: { value: 'Stored' } })
409
550
  fireEvent.click(await screen.findByRole('option'))
410
- fireEvent.change(input, { target: { value: '' } })
551
+ fireEvent.click(screen.getByRole('button', { name: 'Change' }))
411
552
 
412
553
  await screen.findByText('Alpha')
413
554
  expect(screen.getByRole('option', { selected: true })).toHaveTextContent(STORED.title)
@@ -424,7 +565,7 @@ describe('LookupSelect keeps the selection across browse fetches', () => {
424
565
 
425
566
  fireEvent.change(input, { target: { value: 'Stored' } })
426
567
  fireEvent.click(await screen.findByRole('option'))
427
- fireEvent.change(input, { target: { value: '' } })
568
+ fireEvent.click(screen.getByRole('button', { name: 'Change' }))
428
569
 
429
570
  await waitFor(() => expect(fetchItems).toHaveBeenLastCalledWith(''))
430
571
  await waitFor(() => expect(screen.queryByText('Searching…')).toBeNull())
@@ -443,6 +584,7 @@ describe('LookupSelect keeps the selection across browse fetches', () => {
443
584
  fetchItems={fetchItems}
444
585
  />,
445
586
  )
587
+ await openList()
446
588
  await screen.findByText('Beta')
447
589
 
448
590
  view.rerender(
@@ -500,31 +642,18 @@ describe('LookupSelect disabled', () => {
500
642
 
501
643
  it('still shows the current selection so the value stays readable', () => {
502
644
  renderDisabled(() => {})
503
- expect(screen.getByRole('option')).toHaveTextContent('Product One')
645
+ expect(screen.getByTestId('lookup-select-selected')).toHaveTextContent('Product One')
504
646
  })
505
647
 
506
- it('ignores clicks on the option row', () => {
648
+ it('offers no option row and no Change control while disabled', () => {
507
649
  const onChange = jest.fn()
508
650
  renderDisabled(onChange)
509
651
 
510
- fireEvent.click(screen.getByRole('option'))
511
-
652
+ expect(screen.queryByRole('option')).toBeNull()
653
+ expect(screen.queryByRole('button', { name: 'Change' })).toBeNull()
512
654
  expect(onChange).not.toHaveBeenCalled()
513
655
  })
514
656
 
515
- it('ignores Enter and Space on the option row and keeps it out of the tab order', () => {
516
- const onChange = jest.fn()
517
- renderDisabled(onChange)
518
- const option = screen.getByRole('option')
519
-
520
- fireEvent.keyDown(option, { key: 'Enter' })
521
- fireEvent.keyDown(option, { key: ' ' })
522
-
523
- expect(onChange).not.toHaveBeenCalled()
524
- expect(option).toHaveAttribute('tabindex', '-1')
525
- expect(option).toHaveAttribute('aria-disabled', 'true')
526
- })
527
-
528
657
  it('hides the clear-selection button so the value cannot be nulled', () => {
529
658
  renderDisabled(() => {})
530
659
  expect(screen.queryByRole('button', { name: /clear selection/i })).toBeNull()
@@ -535,15 +664,72 @@ describe('LookupSelect disabled', () => {
535
664
  expect(screen.queryByTestId('quick-create')).toBeNull()
536
665
  })
537
666
 
538
- it('keeps the search box disabled and its keyboard path inert', () => {
667
+ it('offers no search box at all while disabled with a selection', () => {
539
668
  const onChange = jest.fn()
540
669
  const { container } = renderDisabled(onChange)
670
+
671
+ expect(container.querySelector('input')).toBeNull()
672
+ expect(screen.getByTestId('lookup-select-selected')).toHaveTextContent('Product One')
673
+ expect(onChange).not.toHaveBeenCalled()
674
+ })
675
+ })
676
+
677
+ describe('LookupSelect after clearing a pick', () => {
678
+ const OPTIONS = [
679
+ { id: 'one', title: 'Product One' },
680
+ { id: 'two', title: 'Product Two' },
681
+ ]
682
+
683
+ function ControlledPicker(props: { defaultOpen?: boolean; minQuery?: number; onChange?: (next: string | null) => void }) {
684
+ const [value, setValue] = React.useState<string | null>(null)
685
+ return (
686
+ <LookupSelect
687
+ value={value}
688
+ onChange={(next) => {
689
+ props.onChange?.(next)
690
+ setValue(next)
691
+ }}
692
+ fetchItems={async () => OPTIONS}
693
+ defaultOpen={props.defaultOpen}
694
+ minQuery={props.minQuery}
695
+ />
696
+ )
697
+ }
698
+
699
+ it.each([
700
+ ['defaultOpen', { defaultOpen: true }],
701
+ ['minQuery 0', { minQuery: 0 }],
702
+ ])('brings the browse list and the keyboard back with %s', async (_label, pickerProps) => {
703
+ const onChange = jest.fn()
704
+ const { container } = render(<ControlledPicker {...pickerProps} onChange={onChange} />)
705
+ fireEvent.click(await screen.findByRole('option', { name: /Product One/ }))
706
+ expect(screen.queryByRole('listbox')).toBeNull()
707
+
708
+ fireEvent.click(await screen.findByRole('button', { name: 'Clear selection' }))
709
+
710
+ expect(await screen.findByRole('option', { name: /Product Two/ })).toBeVisible()
541
711
  const input = getInput(container)
712
+ expect(input).toHaveAttribute('aria-expanded', 'true')
542
713
 
543
- expect(input.disabled).toBe(true)
544
714
  fireEvent.keyDown(input, { key: 'ArrowDown' })
545
715
  fireEvent.keyDown(input, { key: 'Enter' })
716
+ await waitFor(() => expect(onChange).toHaveBeenLastCalledWith('one'))
717
+ expect(await screen.findByTestId('lookup-select-selected')).toHaveTextContent('Product One')
718
+ })
719
+
720
+ it('does not commit a hidden option from the keyboard once Escape closed the list', async () => {
721
+ const onChange = jest.fn()
722
+ const { container } = render(<ControlledPicker defaultOpen onChange={onChange} />)
723
+ await screen.findByRole('option', { name: /Product One/ })
724
+ const input = getInput(container)
725
+
726
+ fireEvent.keyDown(input, { key: 'ArrowDown' })
727
+ fireEvent.keyDown(input, { key: 'Escape' })
728
+ expect(screen.queryByRole('listbox')).toBeNull()
729
+ expect(input).toHaveAttribute('aria-expanded', 'false')
546
730
 
731
+ fireEvent.keyDown(input, { key: 'ArrowDown' })
732
+ fireEvent.keyDown(input, { key: 'Enter' })
547
733
  expect(onChange).not.toHaveBeenCalled()
548
734
  })
549
735
  })