11import { PureComponent } from 'react' ;
2- import { expect } from 'chai' ;
3- import { Simulate } from 'react-dom/test-utils' ;
4- import { getUiOptions } from '@rjsf/utils' ;
2+ import { ArrayFieldTemplateProps , ArrayFieldItemTemplateProps , RJSFSchema , getUiOptions } from '@rjsf/utils' ;
3+ import { fireEvent } from '@testing-library/react' ;
54
6- import { createFormComponent , createSandbox } from './test_utils ' ;
5+ import { createFormComponent } from './testUtils ' ;
76
8- describe ( 'ArrayFieldTemplate' , ( ) => {
9- let sandbox ;
10-
11- const formData = [ 'one' , 'two' , 'three' ] ;
12-
13- beforeEach ( ( ) => {
14- sandbox = createSandbox ( ) ;
15- } ) ;
16-
17- afterEach ( ( ) => {
18- sandbox . restore ( ) ;
19- } ) ;
7+ const formData = [ 'one' , 'two' , 'three' ] ;
208
9+ describe ( 'ArrayFieldTemplate' , ( ) => {
2110 describe ( 'Custom ArrayFieldTemplate of string array' , ( ) => {
22- function ArrayFieldTemplate ( props ) {
11+ function ArrayFieldTemplate ( props : ArrayFieldTemplateProps ) {
2312 const { classNames } = getUiOptions ( props . uiSchema ) ;
2413 return (
2514 < div className = { classNames } >
@@ -28,7 +17,7 @@ describe('ArrayFieldTemplate', () => {
2817 </ div >
2918 ) ;
3019 }
31- function ArrayFieldItemTemplate ( props ) {
20+ function ArrayFieldItemTemplate ( props : ArrayFieldItemTemplateProps ) {
3221 return (
3322 < div className = 'custom-array-item' >
3423 { props . buttonsProps . hasMoveUp && < button className = 'custom-array-item-move-up' /> }
@@ -40,13 +29,13 @@ describe('ArrayFieldTemplate', () => {
4029 }
4130
4231 describe ( 'Stateful ArrayFieldTemplate' , ( ) => {
43- class ArrayFieldTemplate extends PureComponent {
32+ class ArrayFieldTemplate extends PureComponent < ArrayFieldTemplateProps > {
4433 render ( ) {
4534 return < div className = 'field-content' > { this . props . items } </ div > ;
4635 }
4736 }
4837
49- class ArrayFieldItemTemplate extends PureComponent {
38+ class ArrayFieldItemTemplate extends PureComponent < ArrayFieldItemTemplateProps > {
5039 render ( ) {
5140 return < div > this.props.children</ div > ;
5241 }
@@ -60,7 +49,7 @@ describe('ArrayFieldTemplate', () => {
6049 templates : { ArrayFieldTemplate, ArrayFieldItemTemplate } ,
6150 } ) ;
6251
63- expect ( node . querySelectorAll ( '.rjsf-field-array .field-content div' ) ) . to . have . length . of ( 3 ) ;
52+ expect ( node . querySelectorAll ( '.rjsf-field-array .field-content div' ) ) . toHaveLength ( 3 ) ;
6453 } ) ;
6554 } ) ;
6655 describe ( 'with template configured in ui:ArrayFieldTemplate' , ( ) => {
@@ -74,7 +63,7 @@ describe('ArrayFieldTemplate', () => {
7463 } ,
7564 } ) ;
7665
77- expect ( node . querySelectorAll ( '.rjsf-field-array .field-content div' ) ) . to . have . length . of ( 3 ) ;
66+ expect ( node . querySelectorAll ( '.rjsf-field-array .field-content div' ) ) . toHaveLength ( 3 ) ;
7867 } ) ;
7968 } ) ;
8069 describe ( 'with template configured globally being overriden in ui:ArrayFieldTemplate' , ( ) => {
@@ -90,20 +79,20 @@ describe('ArrayFieldTemplate', () => {
9079 templates : { ArrayFieldTemplate : ( ) => < div /> } ,
9180 } ) ;
9281
93- expect ( node . querySelectorAll ( '.rjsf-field-array .field-content div' ) ) . to . have . length . of ( 3 ) ;
82+ expect ( node . querySelectorAll ( '.rjsf-field-array .field-content div' ) ) . toHaveLength ( 3 ) ;
9483 } ) ;
9584 } ) ;
9685 } ) ;
9786
9887 describe ( 'not fixed items' , ( ) => {
99- const schema = {
88+ const schema : RJSFSchema = {
10089 type : 'array' ,
10190 title : 'my list' ,
10291 description : 'my description' ,
10392 items : { type : 'string' } ,
10493 } ;
10594
106- let node ;
95+ let node : Element ;
10796
10897 describe ( 'with template globally configured' , ( ) => {
10998 const uiSchema = {
@@ -157,42 +146,42 @@ describe('ArrayFieldTemplate', () => {
157146 } ) ;
158147 function sharedIts ( ) {
159148 it ( 'should render one root element for the array' , ( ) => {
160- expect ( node . querySelectorAll ( '.custom-array' ) ) . to . have . length . of ( 1 ) ;
149+ expect ( node . querySelectorAll ( '.custom-array' ) ) . toHaveLength ( 1 ) ;
161150 } ) ;
162151
163152 it ( 'should render one add button' , ( ) => {
164- expect ( node . querySelectorAll ( '.custom-array-add' ) ) . to . have . length . of ( 1 ) ;
153+ expect ( node . querySelectorAll ( '.custom-array-add' ) ) . toHaveLength ( 1 ) ;
165154 } ) ;
166155
167156 it ( 'should render one child for each array item' , ( ) => {
168- expect ( node . querySelectorAll ( '.custom-array-item' ) ) . to . have . length . of ( formData . length ) ;
157+ expect ( node . querySelectorAll ( '.custom-array-item' ) ) . toHaveLength ( formData . length ) ;
169158 } ) ;
170159
171160 it ( 'should render text input for each array item' , ( ) => {
172- expect ( node . querySelectorAll ( '.custom-array-item .rjsf-field input[type=text]' ) ) . to . have . length . of (
161+ expect ( node . querySelectorAll ( '.custom-array-item .rjsf-field input[type=text]' ) ) . toHaveLength (
173162 formData . length ,
174163 ) ;
175164 } ) ;
176165
177166 it ( 'should render move up button for all but one array items' , ( ) => {
178- expect ( node . querySelectorAll ( '.custom-array-item-move-up' ) ) . to . have . length . of ( formData . length - 1 ) ;
167+ expect ( node . querySelectorAll ( '.custom-array-item-move-up' ) ) . toHaveLength ( formData . length - 1 ) ;
179168 } ) ;
180169
181170 it ( 'should render move down button for all but one array items' , ( ) => {
182- expect ( node . querySelectorAll ( '.custom-array-item-move-down' ) ) . to . have . length . of ( formData . length - 1 ) ;
171+ expect ( node . querySelectorAll ( '.custom-array-item-move-down' ) ) . toHaveLength ( formData . length - 1 ) ;
183172 } ) ;
184173 }
185174 } ) ;
186175
187176 describe ( 'fixed items' , ( ) => {
188- const schema = {
177+ const schema : RJSFSchema = {
189178 type : 'array' ,
190179 title : 'my list' ,
191180 description : 'my description' ,
192181 items : [ { type : 'string' } , { type : 'string' } , { type : 'string' } ] ,
193182 } ;
194183
195- let node ;
184+ let node : Element ;
196185
197186 describe ( 'with template globally configured' , ( ) => {
198187 const uiSchema = {
@@ -243,42 +232,42 @@ describe('ArrayFieldTemplate', () => {
243232 } ) ;
244233 function sharedIts ( ) {
245234 it ( 'should render one root element for the array' , ( ) => {
246- expect ( node . querySelectorAll ( '.custom-array' ) ) . to . have . length . of ( 1 ) ;
235+ expect ( node . querySelectorAll ( '.custom-array' ) ) . toHaveLength ( 1 ) ;
247236 } ) ;
248237
249238 it ( 'should not render an add button' , ( ) => {
250- expect ( node . querySelectorAll ( '.custom-array-add' ) ) . to . have . length . of ( 0 ) ;
239+ expect ( node . querySelectorAll ( '.custom-array-add' ) ) . toHaveLength ( 0 ) ;
251240 } ) ;
252241
253242 it ( 'should render one child for each array item' , ( ) => {
254- expect ( node . querySelectorAll ( '.custom-array-item' ) ) . to . have . length . of ( formData . length ) ;
243+ expect ( node . querySelectorAll ( '.custom-array-item' ) ) . toHaveLength ( formData . length ) ;
255244 } ) ;
256245
257246 it ( 'should render text input for each array item' , ( ) => {
258- expect ( node . querySelectorAll ( '.custom-array-item .rjsf-field input[type=text]' ) ) . to . have . length . of (
247+ expect ( node . querySelectorAll ( '.custom-array-item .rjsf-field input[type=text]' ) ) . toHaveLength (
259248 formData . length ,
260249 ) ;
261250 } ) ;
262251
263252 it ( 'should not render any move up buttons' , ( ) => {
264- expect ( node . querySelectorAll ( '.custom-array-item-move-up' ) ) . to . have . length . of ( 0 ) ;
253+ expect ( node . querySelectorAll ( '.custom-array-item-move-up' ) ) . toHaveLength ( 0 ) ;
265254 } ) ;
266255
267256 it ( 'should not render any move down buttons' , ( ) => {
268- expect ( node . querySelectorAll ( '.custom-array-item-move-down' ) ) . to . have . length . of ( 0 ) ;
257+ expect ( node . querySelectorAll ( '.custom-array-item-move-down' ) ) . toHaveLength ( 0 ) ;
269258 } ) ;
270259 }
271260 } ) ;
272261 } ) ;
273262
274263 describe ( 'Stateful ArrayFieldTemplate' , ( ) => {
275- class ArrayFieldTemplate extends PureComponent {
264+ class ArrayFieldTemplate extends PureComponent < ArrayFieldTemplateProps > {
276265 render ( ) {
277266 return < div className = 'field-content' > { this . props . items } </ div > ;
278267 }
279268 }
280269
281- class ArrayFieldItemTemplate extends PureComponent {
270+ class ArrayFieldItemTemplate extends PureComponent < ArrayFieldItemTemplateProps > {
282271 render ( ) {
283272 return < div > this.props.children</ div > ;
284273 }
@@ -290,13 +279,13 @@ describe('ArrayFieldTemplate', () => {
290279 formData,
291280 templates : { ArrayFieldTemplate, ArrayFieldItemTemplate } ,
292281 } ) ;
293- expect ( node . querySelectorAll ( '.rjsf-field-array .field-content div' ) ) . to . have . length . of ( 3 ) ;
282+ expect ( node . querySelectorAll ( '.rjsf-field-array .field-content div' ) ) . toHaveLength ( 3 ) ;
294283 } ) ;
295284 } ) ;
296285
297286 describe ( 'pass right props to ArrayFieldTemplate' , ( ) => {
298287 it ( 'should pass registry prop' , ( ) => {
299- const ArrayFieldTemplate = ( { registry } ) => {
288+ const ArrayFieldTemplate = ( { registry } : ArrayFieldTemplateProps ) => {
300289 if ( ! registry ) {
301290 throw 'Error' ;
302291 }
@@ -310,13 +299,13 @@ describe('ArrayFieldTemplate', () => {
310299 } ) ;
311300
312301 it ( 'should pass formData so it is in sync with items' , ( ) => {
313- const ArrayFieldTemplate = ( { formData, items, onAddClick } ) => {
302+ const ArrayFieldTemplate = ( { formData, items, onAddClick } : ArrayFieldTemplateProps ) => {
314303 if ( formData . length !== items . length ) {
315304 throw 'Error' ;
316305 }
317306 return (
318307 < div >
319- { items . map ( ( item , i ) => (
308+ { items . map ( ( _ , i ) => (
320309 < span key = { i } > value: { formData [ i ] } </ span >
321310 ) ) }
322311 < button className = 'rjsf-array-item-add' onClick = { onAddClick } />
@@ -328,7 +317,9 @@ describe('ArrayFieldTemplate', () => {
328317 formData,
329318 templates : { ArrayFieldTemplate } ,
330319 } ) ;
331- Simulate . click ( node . querySelector ( '.rjsf-array-item-add' ) ) ;
320+ const button = node . querySelector ( '.rjsf-array-item-add' ) ;
321+ expect ( button ) . toBeInTheDocument ( ) ;
322+ fireEvent . click ( button ! ) ;
332323 } ) ;
333324 } ) ;
334325} ) ;
0 commit comments