Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ should change the heading of the (upcoming) version to include a major version b
- 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)
- Fixed issue with schema array with nested dependent fixed-length, fixing [#3754](https://github.com/rjsf-team/react-jsonschema-form/issues/3754)

## Dev / docs / playground

- Updated unit tests for `@rjsf/core` to convert them to typescript and jest

# 6.1.2

Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/core/jest.config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"verbose": true,
"testEnvironment": "jsdom",
"setupFilesAfterEnv": ["./test/setup-jest-env.js", "../../testing/testSetup.ts"],
"setupFilesAfterEnv": ["./test/setup-jest-env.ts", "../../testing/testSetup.ts"],
"testMatch": ["**/test/**/*.test.[jt]s?(x)"],
"transformIgnorePatterns": ["/node_modules/(?!(@x0k/json-schema-merge|deep-freeze-es6))"]
}
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.0",
"@testing-library/user-event": "^14.6.1",
"@types/html": "^1.0.4",
"ajv": "^8.17.1",
"atob": "^2.1.2",
"chai": "^3.5.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
import { PureComponent } from 'react';
import { expect } from 'chai';
import { Simulate } from 'react-dom/test-utils';
import { getUiOptions } from '@rjsf/utils';
import { ArrayFieldTemplateProps, ArrayFieldItemTemplateProps, RJSFSchema, getUiOptions } from '@rjsf/utils';
import { fireEvent } from '@testing-library/react';

import { createFormComponent, createSandbox } from './test_utils';
import { createFormComponent } from './testUtils';

describe('ArrayFieldTemplate', () => {
let sandbox;

const formData = ['one', 'two', 'three'];

beforeEach(() => {
sandbox = createSandbox();
});

afterEach(() => {
sandbox.restore();
});
const formData = ['one', 'two', 'three'];

describe('ArrayFieldTemplate', () => {
describe('Custom ArrayFieldTemplate of string array', () => {
function ArrayFieldTemplate(props) {
function ArrayFieldTemplate(props: ArrayFieldTemplateProps) {
const { classNames } = getUiOptions(props.uiSchema);
return (
<div className={classNames}>
Expand All @@ -28,7 +17,7 @@ describe('ArrayFieldTemplate', () => {
</div>
);
}
function ArrayFieldItemTemplate(props) {
function ArrayFieldItemTemplate(props: ArrayFieldItemTemplateProps) {
return (
<div className='custom-array-item'>
{props.buttonsProps.hasMoveUp && <button className='custom-array-item-move-up' />}
Expand All @@ -40,13 +29,13 @@ describe('ArrayFieldTemplate', () => {
}

describe('Stateful ArrayFieldTemplate', () => {
class ArrayFieldTemplate extends PureComponent {
class ArrayFieldTemplate extends PureComponent<ArrayFieldTemplateProps> {
render() {
return <div className='field-content'>{this.props.items}</div>;
}
}

class ArrayFieldItemTemplate extends PureComponent {
class ArrayFieldItemTemplate extends PureComponent<ArrayFieldItemTemplateProps> {
render() {
return <div>this.props.children</div>;
}
Expand All @@ -60,7 +49,7 @@ describe('ArrayFieldTemplate', () => {
templates: { ArrayFieldTemplate, ArrayFieldItemTemplate },
});

expect(node.querySelectorAll('.rjsf-field-array .field-content div')).to.have.length.of(3);
expect(node.querySelectorAll('.rjsf-field-array .field-content div')).toHaveLength(3);
});
});
describe('with template configured in ui:ArrayFieldTemplate', () => {
Expand All @@ -74,7 +63,7 @@ describe('ArrayFieldTemplate', () => {
},
});

expect(node.querySelectorAll('.rjsf-field-array .field-content div')).to.have.length.of(3);
expect(node.querySelectorAll('.rjsf-field-array .field-content div')).toHaveLength(3);
});
});
describe('with template configured globally being overriden in ui:ArrayFieldTemplate', () => {
Expand All @@ -90,20 +79,20 @@ describe('ArrayFieldTemplate', () => {
templates: { ArrayFieldTemplate: () => <div /> },
});

expect(node.querySelectorAll('.rjsf-field-array .field-content div')).to.have.length.of(3);
expect(node.querySelectorAll('.rjsf-field-array .field-content div')).toHaveLength(3);
});
});
});

describe('not fixed items', () => {
const schema = {
const schema: RJSFSchema = {
type: 'array',
title: 'my list',
description: 'my description',
items: { type: 'string' },
};

let node;
let node: Element;

describe('with template globally configured', () => {
const uiSchema = {
Expand Down Expand Up @@ -157,42 +146,42 @@ describe('ArrayFieldTemplate', () => {
});
function sharedIts() {
it('should render one root element for the array', () => {
expect(node.querySelectorAll('.custom-array')).to.have.length.of(1);
expect(node.querySelectorAll('.custom-array')).toHaveLength(1);
});

it('should render one add button', () => {
expect(node.querySelectorAll('.custom-array-add')).to.have.length.of(1);
expect(node.querySelectorAll('.custom-array-add')).toHaveLength(1);
});

it('should render one child for each array item', () => {
expect(node.querySelectorAll('.custom-array-item')).to.have.length.of(formData.length);
expect(node.querySelectorAll('.custom-array-item')).toHaveLength(formData.length);
});

it('should render text input for each array item', () => {
expect(node.querySelectorAll('.custom-array-item .rjsf-field input[type=text]')).to.have.length.of(
expect(node.querySelectorAll('.custom-array-item .rjsf-field input[type=text]')).toHaveLength(
formData.length,
);
});

it('should render move up button for all but one array items', () => {
expect(node.querySelectorAll('.custom-array-item-move-up')).to.have.length.of(formData.length - 1);
expect(node.querySelectorAll('.custom-array-item-move-up')).toHaveLength(formData.length - 1);
});

it('should render move down button for all but one array items', () => {
expect(node.querySelectorAll('.custom-array-item-move-down')).to.have.length.of(formData.length - 1);
expect(node.querySelectorAll('.custom-array-item-move-down')).toHaveLength(formData.length - 1);
});
}
});

describe('fixed items', () => {
const schema = {
const schema: RJSFSchema = {
type: 'array',
title: 'my list',
description: 'my description',
items: [{ type: 'string' }, { type: 'string' }, { type: 'string' }],
};

let node;
let node: Element;

describe('with template globally configured', () => {
const uiSchema = {
Expand Down Expand Up @@ -243,42 +232,42 @@ describe('ArrayFieldTemplate', () => {
});
function sharedIts() {
it('should render one root element for the array', () => {
expect(node.querySelectorAll('.custom-array')).to.have.length.of(1);
expect(node.querySelectorAll('.custom-array')).toHaveLength(1);
});

it('should not render an add button', () => {
expect(node.querySelectorAll('.custom-array-add')).to.have.length.of(0);
expect(node.querySelectorAll('.custom-array-add')).toHaveLength(0);
});

it('should render one child for each array item', () => {
expect(node.querySelectorAll('.custom-array-item')).to.have.length.of(formData.length);
expect(node.querySelectorAll('.custom-array-item')).toHaveLength(formData.length);
});

it('should render text input for each array item', () => {
expect(node.querySelectorAll('.custom-array-item .rjsf-field input[type=text]')).to.have.length.of(
expect(node.querySelectorAll('.custom-array-item .rjsf-field input[type=text]')).toHaveLength(
formData.length,
);
});

it('should not render any move up buttons', () => {
expect(node.querySelectorAll('.custom-array-item-move-up')).to.have.length.of(0);
expect(node.querySelectorAll('.custom-array-item-move-up')).toHaveLength(0);
});

it('should not render any move down buttons', () => {
expect(node.querySelectorAll('.custom-array-item-move-down')).to.have.length.of(0);
expect(node.querySelectorAll('.custom-array-item-move-down')).toHaveLength(0);
});
}
});
});

describe('Stateful ArrayFieldTemplate', () => {
class ArrayFieldTemplate extends PureComponent {
class ArrayFieldTemplate extends PureComponent<ArrayFieldTemplateProps> {
render() {
return <div className='field-content'>{this.props.items}</div>;
}
}

class ArrayFieldItemTemplate extends PureComponent {
class ArrayFieldItemTemplate extends PureComponent<ArrayFieldItemTemplateProps> {
render() {
return <div>this.props.children</div>;
}
Expand All @@ -290,13 +279,13 @@ describe('ArrayFieldTemplate', () => {
formData,
templates: { ArrayFieldTemplate, ArrayFieldItemTemplate },
});
expect(node.querySelectorAll('.rjsf-field-array .field-content div')).to.have.length.of(3);
expect(node.querySelectorAll('.rjsf-field-array .field-content div')).toHaveLength(3);
});
});

describe('pass right props to ArrayFieldTemplate', () => {
it('should pass registry prop', () => {
const ArrayFieldTemplate = ({ registry }) => {
const ArrayFieldTemplate = ({ registry }: ArrayFieldTemplateProps) => {
if (!registry) {
throw 'Error';
}
Expand All @@ -310,13 +299,13 @@ describe('ArrayFieldTemplate', () => {
});

it('should pass formData so it is in sync with items', () => {
const ArrayFieldTemplate = ({ formData, items, onAddClick }) => {
const ArrayFieldTemplate = ({ formData, items, onAddClick }: ArrayFieldTemplateProps) => {
if (formData.length !== items.length) {
throw 'Error';
}
return (
<div>
{items.map((item, i) => (
{items.map((_, i) => (
<span key={i}>value: {formData[i]}</span>
))}
<button className='rjsf-array-item-add' onClick={onAddClick} />
Expand All @@ -328,7 +317,9 @@ describe('ArrayFieldTemplate', () => {
formData,
templates: { ArrayFieldTemplate },
});
Simulate.click(node.querySelector('.rjsf-array-item-add'));
const button = node.querySelector('.rjsf-array-item-add');
expect(button).toBeInTheDocument();
fireEvent.click(button!);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This click (which we were already doing) is meaningless since we don't assert/verify anything afterwards

});
});
});
Loading