@@ -3,7 +3,7 @@ import { fireEvent, render } from '@testing-library/react';
33import KeyCode from 'rc-util/lib/KeyCode' ;
44import React , { Fragment } from 'react' ;
55import Collapse , { Panel } from '../src/index' ;
6- import type { CollapseProps } from '../src/interface' ;
6+ import type { CollapseProps , ItemType } from '../src/interface' ;
77
88describe ( 'collapse' , ( ) => {
99 let changeHook : jest . Mock < any , any > | null ;
@@ -690,4 +690,98 @@ describe('collapse', () => {
690690 ) ;
691691 expect ( container . querySelector ( '.rc-collapse-item' ) . style . color ) . toBe ( 'red' ) ;
692692 } ) ;
693+
694+ describe ( 'props items' , ( ) => {
695+ const items : ItemType [ ] = [
696+ {
697+ header : 'title' ,
698+ children : 'content' ,
699+ } ,
700+ {
701+ header : 'title 2' ,
702+ children : 'content 2' ,
703+ collapsible : 'disabled' ,
704+ } ,
705+ ] ;
706+
707+ it ( 'should work' , ( ) => {
708+ const { container } = render ( < Collapse items = { items } /> ) ;
709+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
710+ } ) ;
711+
712+ it ( 'should work with onItemClick' , ( ) => {
713+ const onItemClick = jest . fn ( ) ;
714+ const { container } = render (
715+ < Collapse
716+ items = { [
717+ ...items ,
718+ {
719+ header : 'title 3' ,
720+ onItemClick,
721+ } ,
722+ ] }
723+ /> ,
724+ ) ;
725+ fireEvent . click ( container . querySelectorAll ( '.rc-collapse-header' ) [ 2 ] ) ;
726+ expect ( onItemClick ) . toHaveBeenCalled ( ) ;
727+ expect ( onItemClick ) . lastCalledWith ( '2' ) ;
728+ } ) ;
729+
730+ it ( 'should work with collapsible' , ( ) => {
731+ const onItemClick = jest . fn ( ) ;
732+ const onChangeFn = jest . fn ( ) ;
733+ const { container } = render (
734+ < Collapse
735+ onChange = { onChangeFn }
736+ items = { [
737+ ...items ,
738+ {
739+ header : 'title 3' ,
740+ onItemClick,
741+ collapsible : 'icon' ,
742+ } ,
743+ ] }
744+ /> ,
745+ ) ;
746+
747+ fireEvent . click ( container . querySelectorAll ( '.rc-collapse-header' ) [ 2 ] ) ;
748+ expect ( onItemClick ) . not . toHaveBeenCalled ( ) ;
749+
750+ fireEvent . click (
751+ container . querySelector ( '.rc-collapse-item:nth-child(3) .rc-collapse-expand-icon' ) ,
752+ ) ;
753+ expect ( onItemClick ) . toHaveBeenCalled ( ) ;
754+ expect ( onChangeFn ) . toBeCalledTimes ( 1 ) ;
755+ expect ( onChangeFn ) . lastCalledWith ( [ '2' ] ) ;
756+ } ) ;
757+
758+ it ( 'should work with custom icon' , ( ) => {
759+ const { container } = render (
760+ < Collapse
761+ items = { [
762+ {
763+ header : 'title' ,
764+ expandIcon : ( ) => < span className = "custom-icon" > i</ span > ,
765+ } ,
766+ ] }
767+ /> ,
768+ ) ;
769+ expect ( container . querySelector ( '.custom-icon' ) ) . toBeTruthy ( ) ;
770+ } ) ;
771+
772+ it ( 'should work with nested' , ( ) => {
773+ const { container } = render (
774+ < Collapse
775+ items = { [
776+ ...items ,
777+ {
778+ header : 'title 3' ,
779+ children : < Collapse items = { items } /> ,
780+ } ,
781+ ] }
782+ /> ,
783+ ) ;
784+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
785+ } ) ;
786+ } ) ;
693787} ) ;
0 commit comments