@@ -56,9 +56,11 @@ describe("actions", () => {
5656 ) ;
5757 } ;
5858 render ( < App > </ App > , container ) ;
59- _api . select ( '2' ) ;
59+ if ( _api . isSelected ( '2' ) == false )
60+ _api . select ( '2' ) ;
6061 } ) ;
6162 expect ( document . querySelector ( '[tab-id="2"]' ) . className . includes ( 'rc-dyn-tabs-selected' ) ) . toBe ( true ) ;
63+ expect ( op . onChange . mock . calls . length ) . toBe ( 1 ) ;
6264 expect ( op . onSelect . mock . calls . length ) . toBe ( 1 ) ;
6365 expect ( op . onLoad . mock . calls . length ) . toBe ( 1 ) ;
6466 } ) ;
@@ -76,13 +78,15 @@ describe("actions", () => {
7678 ) ;
7779 } ;
7880 render ( < App > </ App > , container ) ;
79- _api . open ( {
80- id : '3' ,
81- title : 'mock tab 3' ,
82- closable : true ,
83- panelComponent : < p > tab3 content</ p >
84- } ) ;
85- _api . close ( '1' ) ;
81+ if ( _api . isOpen ( '3' ) == false )
82+ _api . open ( {
83+ id : '3' ,
84+ title : 'mock tab 3' ,
85+ closable : true ,
86+ panelComponent : < p > tab3 content</ p >
87+ } ) ;
88+ if ( _api . isOpen ( '1' ) )
89+ _api . close ( '1' ) ;
8690 } ) ;
8791 expect ( _api . isOpen ( '3' ) ) . toBe ( true ) ;
8892 expect ( _api . isOpen ( '1' ) ) . toBe ( false ) ;
@@ -106,30 +110,33 @@ describe("actions", () => {
106110 ) ;
107111 } ;
108112 render ( < App > </ App > , container ) ;
109- _api . setTab ( '1' , { closable : false } ) ;
110- _api . setOption ( 'direction' , 'rtl' ) ;
111- _api . setOption ( 'tabComponent' , function ( props ) {
112- return (
113- < a { ...props . tabProps } >
114- { props . children }
115- {
116- props . hasOwnProperty ( 'iconProps' ) &&
117- < span { ...props . iconProps } > </ span >
118- }
119- </ a >
120- ) ;
121- } ) ;
122- _api . on ( 'onInit' , function ( ) {
123- if ( ! counter ) {
124- counter ++ ;
125- this . setTab ( '1' , {
126- panelComponent : function ( props ) {
127- return < div id = "updatedPanel1" > </ div > ;
128- }
129- } ) . refresh ( ) ;
130- }
131- } ) ;
132- _api . refresh ( ) ;
113+ if ( _api . getTab ( '1' ) . closable == true )
114+ _api . setTab ( '1' , { closable : false } ) ;
115+ if ( _api . getOption ( 'direction' ) === 'ltr' ) {
116+ _api . setOption ( 'direction' , 'rtl' ) ;
117+ _api . setOption ( 'tabComponent' , function ( props ) {
118+ return (
119+ < a { ...props . tabProps } >
120+ { props . children }
121+ {
122+ props . hasOwnProperty ( 'iconProps' ) &&
123+ < span { ...props . iconProps } > </ span >
124+ }
125+ </ a >
126+ ) ;
127+ } ) ;
128+ _api . on ( 'onInit' , function ( ) {
129+ if ( ! counter ) {
130+ counter ++ ;
131+ this . setTab ( '1' , {
132+ panelComponent : function ( props ) {
133+ return < div id = "updatedPanel1" > </ div > ;
134+ }
135+ } ) . refresh ( ) ;
136+ }
137+ } ) ;
138+ _api . refresh ( ) ;
139+ }
133140 } ) ;
134141 expect ( document . getElementById ( 'updatedPanel1' ) != null ) . toBe ( true ) ;
135142 expect ( document . querySelector ( 'li[tab-id="1"] .rc-dyn-tabs-close' ) == null ) . toBe ( true ) ;
@@ -139,6 +146,47 @@ describe("actions", () => {
139146 expect ( op . onInit . mock . calls . length ) . toBe ( 3 ) ;
140147 } ) ;
141148} ) ;
149+ describe ( "calling some action inside the events options" , ( ) => {
150+ let _api ;
151+ test ( "calling select method inside the onLoad option" , ( ) => {
152+ const op = {
153+ tabs : [ {
154+ id : '1' ,
155+ title : 'mock tab 1' ,
156+ closable : true ,
157+ panelComponent : < p > tab1 content</ p >
158+ } , {
159+ id : '2' ,
160+ title : 'mock tab 2' ,
161+ iconClass : 'ui-icon ui-icon-seek-end' ,
162+ closable : false ,
163+ panelComponent : < p > tab2 content</ p >
164+ } ] ,
165+ selectedTabID : '1' ,
166+ onLoad : jest . fn ( function ( ) { this . select ( '2' ) ; } ) ,
167+ onSelect : jest . fn ( function ( ) { } ) ,
168+ onChange : jest . fn ( function ( ) { } ) ,
169+ onInit : jest . fn ( function ( ) { } )
170+ } ;
171+ act ( ( ) => {
172+ const App = function ( props ) {
173+ const [ Tablist , Panellist , api ] = useDynTabs ( op ) ;
174+ _api = api ;
175+ return (
176+ < div >
177+ < Tablist > </ Tablist >
178+ < Panellist > </ Panellist >
179+ </ div >
180+ ) ;
181+ } ;
182+ render ( < App > </ App > , container ) ;
183+ } ) ;
184+ expect ( op . onLoad . mock . calls . length === 1 ) . toBe ( true ) ;
185+ expect ( op . onInit . mock . calls . length === 2 ) . toBe ( true ) ;
186+ expect ( op . onChange . mock . calls . length === 1 ) . toBe ( true ) ;
187+ expect ( op . onSelect . mock . calls . length === 1 ) . toBe ( true ) ;
188+ } ) ;
189+ } ) ;
142190describe ( "events" , ( ) => {
143191 test ( 'checking events execution count and their parameters ' , ( ) => {
144192 let _api , contextProps = '' ;
@@ -183,14 +231,16 @@ describe("events", () => {
183231 ) ;
184232 } ;
185233 render ( < App > </ App > , container ) ;
186- _api . on ( 'onSelect' , onSelectHandler ) ;
187- _api . open ( {
188- id : '3' ,
189- title : 'mock tab 3' ,
190- closable : true ,
191- panelComponent : < p > tab3 content</ p >
192- } ) ;
193- _api . close ( '1' ) ;
234+ _api . one ( 'onSelect' , onSelectHandler ) ;
235+ if ( _api . isOpen ( '3' ) == false )
236+ _api . open ( {
237+ id : '3' ,
238+ title : 'mock tab 3' ,
239+ closable : true ,
240+ panelComponent : < p > tab3 content</ p >
241+ } ) ;
242+ if ( _api . isSelected ( '2' ) == false )
243+ _api . close ( '1' ) ;
194244 } ) ;
195245 //onload
196246 expect ( op . onLoad . mock . calls . length ) . toBe ( 1 ) ;
@@ -201,6 +251,7 @@ describe("events", () => {
201251 expect ( Object . prototype . toString . call ( op . onChange . mock . calls [ 0 ] [ 0 ] ) === '[object Object]' ) . toBe ( true ) ;
202252 expect ( op . onChange . mock . calls [ 0 ] [ 0 ] . hasOwnProperty ( 'currentData' ) ) . toBe ( true ) ;
203253 expect ( op . onChange . mock . calls [ 0 ] [ 0 ] . hasOwnProperty ( 'perviousData' ) ) . toBe ( true ) ;
254+
204255 //onSelect
205256 expect ( onSelectHandler . mock . calls . length ) . toBe ( 1 ) ;
206257 expect ( op . onSelect . mock . calls . length ) . toBe ( 1 ) ;
@@ -210,6 +261,7 @@ describe("events", () => {
210261 expect ( onSelectHandler . mock . calls [ 0 ] [ 0 ] . hasOwnProperty ( 'currentSelectedTabId' ) ) . toBe ( true ) ;
211262 expect ( op . onSelect . mock . calls [ 0 ] [ 0 ] . hasOwnProperty ( 'perviousSelectedTabId' ) ) . toBe ( true ) ;
212263 expect ( onSelectHandler . mock . calls [ 0 ] [ 0 ] . hasOwnProperty ( 'perviousSelectedTabId' ) ) . toBe ( true ) ;
264+
213265 //onclose
214266 expect ( op . onClose . mock . calls . length ) . toBe ( 1 ) ;
215267 expect ( op . onClose . mock . calls [ 0 ] [ 0 ] . constructor === Array && op . onClose . mock . calls [ 0 ] [ 0 ] [ 0 ] === '1' ) . toBe ( true ) ;
0 commit comments