Skip to content

Commit 6e7a870

Browse files
Chore: Convert some core tests to typescript using jest
For consistency and to support react 19 conversion, upgraded a set of tests in core to typescript and jest - In `@rjsf/core`, added a typescript version of `test_utils.js` named `testUtils.tsx` - Renamed the `setup-jest-env.js` file to `.ts` updating it in the `jest.config.json` - Renamed the following test files to `.tsx`, converting to using `jest` and `@testing-library` constructs and matchers: - `ArrayFieldTemplate`, `FieldTemplate`, `FormContext`, `NullField`, `NumberField`, `ObjectFieldTemplate`, `allOf`, `const`, `ifthenelse` and `withTheme` - The `DescriptionField` and `TitleField` tests changed enough so that they appear as a delete/new rather than rename - Updated the `CHANGELOG.md` accordingly
1 parent f21f043 commit 6e7a870

18 files changed

+557
-675
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ should change the heading of the (upcoming) version to include a major version b
2323
- Fixed issue with default value not being prefilled when object with if/then is nested inside another object, fixing [#4222](https://github.com/rjsf-team/react-jsonschema-form/issues/4222)
2424
- Fixed issue with schema array with nested dependent fixed-length, fixing [#3754](https://github.com/rjsf-team/react-jsonschema-form/issues/3754)
2525

26+
## Dev / docs / playground
27+
28+
- Updated unit tests for `@rjsf/core` to convert them to typescript and jest
2629

2730
# 6.1.2
2831

packages/core/jest.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"verbose": true,
33
"testEnvironment": "jsdom",
4-
"setupFilesAfterEnv": ["./test/setup-jest-env.js", "../../testing/testSetup.ts"],
4+
"setupFilesAfterEnv": ["./test/setup-jest-env.ts", "../../testing/testSetup.ts"],
55
"testMatch": ["**/test/**/*.test.[jt]s?(x)"],
66
"transformIgnorePatterns": ["/node_modules/(?!(@x0k/json-schema-merge|deep-freeze-es6))"]
77
}

packages/core/test/ArrayFieldTemplate.test.jsx renamed to packages/core/test/ArrayFieldTemplate.test.tsx

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
import { 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
});

packages/core/test/DescriptionField.test.jsx

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { render } from '@testing-library/react';
2+
import { DescriptionFieldProps } from '@rjsf/utils';
3+
4+
import DescriptionField from '../src/components/templates/DescriptionField';
5+
import { getTestRegistry } from '../src';
6+
7+
const registry = getTestRegistry({});
8+
9+
describe('DescriptionField', () => {
10+
let node: Element;
11+
let props: DescriptionFieldProps;
12+
beforeAll(() => {
13+
props = {
14+
id: 'sample_id',
15+
description: 'Field description',
16+
schema: {},
17+
registry,
18+
};
19+
const { container } = render(<DescriptionField {...props} />);
20+
node = container.firstElementChild!;
21+
});
22+
it('should return a div for a custom component', () => {
23+
expect(node.tagName).toEqual('DIV');
24+
});
25+
26+
it('should have the expected id', () => {
27+
expect(node).toHaveAttribute('id', 'sample_id');
28+
});
29+
});

0 commit comments

Comments
 (0)