@@ -2,7 +2,7 @@ import { fireEvent, render } from '@testing-library/react';
22import KeyCode from '@rc-component/util/lib/KeyCode' ;
33import { resetWarned } from '@rc-component/util/lib/warning' ;
44import React from 'react' ;
5- import Cascader from '../src' ;
5+ import Cascader , { type DefaultOptionType } from '../src' ;
66import { optionsForActiveMenuItems } from './demoOptions' ;
77import { expectOpen , doSearch , keyDown } from './util' ;
88
@@ -444,4 +444,68 @@ describe('Cascader.Search', () => {
444444 fireEvent . click ( document . querySelector ( '.rc-cascader-checkbox' ) as HTMLElement ) ;
445445 expect ( inputNode ) . toHaveValue ( 'little' ) ;
446446 } ) ;
447+
448+ it ( 'should display notFoundContent when search has no results with optionRender' , ( ) => {
449+ const { container } = render (
450+ < Cascader
451+ options = { options }
452+ open
453+ showSearch
454+ optionRender = { option => `${ option . label } - custom render` }
455+ notFoundContent = "未找到相关内容"
456+ /> ,
457+ ) ;
458+
459+ // Search for something that doesn't exist
460+ doSearch ( container , 'nonexistent' ) ;
461+ const itemList = container . querySelectorAll ( '.rc-cascader-menu-item' ) ;
462+ expect ( itemList ) . toHaveLength ( 1 ) ;
463+ // Should display notFoundContent, not use optionRender
464+ expect ( itemList [ 0 ] . textContent ) . toEqual ( '未找到相关内容' ) ;
465+ expect ( itemList [ 0 ] . querySelector ( '.rc-cascader-menu-item-content' ) ?. textContent ) . toEqual (
466+ '未找到相关内容' ,
467+ ) ;
468+ } ) ;
469+
470+ it ( 'should use optionRender for search results when both optionRender and notFoundContent are provided' , ( ) => {
471+ const { container } = render (
472+ < Cascader
473+ options = { options }
474+ open
475+ showSearch
476+ optionRender = { option => `${ option . label } - custom render` }
477+ notFoundContent = "未找到相关内容"
478+ /> ,
479+ ) ;
480+
481+ // Search for something that exists
482+ doSearch ( container , 'toy' ) ;
483+ const itemList = container . querySelectorAll ( '.rc-cascader-menu-item' ) ;
484+ expect ( itemList . length ) . toBeGreaterThan ( 0 ) ;
485+
486+ // Should use optionRender for actual options
487+ const firstItemContent = itemList [ 0 ] . querySelector ( '.rc-cascader-menu-item-content' ) ;
488+ expect ( firstItemContent ?. textContent ) . toContain ( 'custom render' ) ;
489+ expect ( firstItemContent ?. textContent ) . not . toEqual ( '未找到相关内容' ) ;
490+ } ) ;
491+
492+ it ( 'should display notFoundContent when options array is empty with optionRender' , ( ) => {
493+ const { container } = render (
494+ < Cascader
495+ options = { [ ] as DefaultOptionType [ ] }
496+ open
497+ showSearch
498+ optionRender = { option => `${ option . label } - custom render` }
499+ notFoundContent = "空列表"
500+ /> ,
501+ ) ;
502+
503+ const itemList = container . querySelectorAll ( '.rc-cascader-menu-item' ) ;
504+ expect ( itemList ) . toHaveLength ( 1 ) ;
505+ // Should display notFoundContent, not use optionRender
506+ expect ( itemList [ 0 ] . textContent ) . toEqual ( '空列表' ) ;
507+ expect ( itemList [ 0 ] . querySelector ( '.rc-cascader-menu-item-content' ) ?. textContent ) . toEqual (
508+ '空列表' ,
509+ ) ;
510+ } ) ;
447511} ) ;
0 commit comments