@@ -56,9 +56,11 @@ describe("actions", () => {
56
56
) ;
57
57
} ;
58
58
render ( < App > </ App > , container ) ;
59
- _api . select ( '2' ) ;
59
+ if ( _api . isSelected ( '2' ) == false )
60
+ _api . select ( '2' ) ;
60
61
} ) ;
61
62
expect ( document . querySelector ( '[tab-id="2"]' ) . className . includes ( 'rc-dyn-tabs-selected' ) ) . toBe ( true ) ;
63
+ expect ( op . onChange . mock . calls . length ) . toBe ( 1 ) ;
62
64
expect ( op . onSelect . mock . calls . length ) . toBe ( 1 ) ;
63
65
expect ( op . onLoad . mock . calls . length ) . toBe ( 1 ) ;
64
66
} ) ;
@@ -76,13 +78,15 @@ describe("actions", () => {
76
78
) ;
77
79
} ;
78
80
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' ) ;
86
90
} ) ;
87
91
expect ( _api . isOpen ( '3' ) ) . toBe ( true ) ;
88
92
expect ( _api . isOpen ( '1' ) ) . toBe ( false ) ;
@@ -106,30 +110,33 @@ describe("actions", () => {
106
110
) ;
107
111
} ;
108
112
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
+ }
133
140
} ) ;
134
141
expect ( document . getElementById ( 'updatedPanel1' ) != null ) . toBe ( true ) ;
135
142
expect ( document . querySelector ( 'li[tab-id="1"] .rc-dyn-tabs-close' ) == null ) . toBe ( true ) ;
@@ -139,6 +146,47 @@ describe("actions", () => {
139
146
expect ( op . onInit . mock . calls . length ) . toBe ( 3 ) ;
140
147
} ) ;
141
148
} ) ;
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
+ } ) ;
142
190
describe ( "events" , ( ) => {
143
191
test ( 'checking events execution count and their parameters ' , ( ) => {
144
192
let _api , contextProps = '' ;
@@ -183,14 +231,16 @@ describe("events", () => {
183
231
) ;
184
232
} ;
185
233
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' ) ;
194
244
} ) ;
195
245
//onload
196
246
expect ( op . onLoad . mock . calls . length ) . toBe ( 1 ) ;
@@ -201,6 +251,7 @@ describe("events", () => {
201
251
expect ( Object . prototype . toString . call ( op . onChange . mock . calls [ 0 ] [ 0 ] ) === '[object Object]' ) . toBe ( true ) ;
202
252
expect ( op . onChange . mock . calls [ 0 ] [ 0 ] . hasOwnProperty ( 'currentData' ) ) . toBe ( true ) ;
203
253
expect ( op . onChange . mock . calls [ 0 ] [ 0 ] . hasOwnProperty ( 'perviousData' ) ) . toBe ( true ) ;
254
+
204
255
//onSelect
205
256
expect ( onSelectHandler . mock . calls . length ) . toBe ( 1 ) ;
206
257
expect ( op . onSelect . mock . calls . length ) . toBe ( 1 ) ;
@@ -210,6 +261,7 @@ describe("events", () => {
210
261
expect ( onSelectHandler . mock . calls [ 0 ] [ 0 ] . hasOwnProperty ( 'currentSelectedTabId' ) ) . toBe ( true ) ;
211
262
expect ( op . onSelect . mock . calls [ 0 ] [ 0 ] . hasOwnProperty ( 'perviousSelectedTabId' ) ) . toBe ( true ) ;
212
263
expect ( onSelectHandler . mock . calls [ 0 ] [ 0 ] . hasOwnProperty ( 'perviousSelectedTabId' ) ) . toBe ( true ) ;
264
+
213
265
//onclose
214
266
expect ( op . onClose . mock . calls . length ) . toBe ( 1 ) ;
215
267
expect ( op . onClose . mock . calls [ 0 ] [ 0 ] . constructor === Array && op . onClose . mock . calls [ 0 ] [ 0 ] [ 0 ] === '1' ) . toBe ( true ) ;
0 commit comments