Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-pex committed May 16, 2022
1 parent 724fb0b commit 1688d1e
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/bootstrap-4/test/Array.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ import React from 'react';
import Form from "../src/index";
import { JSONSchema7 } from "json-schema";
import renderer from "react-test-renderer";
import { ArrayFieldTemplateProps, UiSchema } from '@rjsf/core';

const ArrayFieldTemplate = (props: ArrayFieldTemplateProps) => {
const title: string = props.uiSchema['ui:title'] || props.title || '';
const description: string = props.uiSchema['ui:description'] || props.schema.description || '';
const { TitleField, DescriptionField } = props;
return (
<div id="custom-tpl" className={props.className}>
{title && <TitleField
id={`${props.idSchema.$id}__title`}
title={title}
required={props.required}
/>}
{description && <DescriptionField
id={`${props.idSchema.$id}-description`}
description={description}
/>}
{props.items.map(item => <div key={item.key}>{item.children}</div>)}
</div>
);
}

describe("array fields", () => {
test("array", () => {
Expand Down Expand Up @@ -47,4 +68,32 @@ describe("array fields", () => {
.toJSON();
expect(tree).toMatchSnapshot();
});
test("customization", () => {
const schema: JSONSchema7 = {
type: "array",
title: "my-title",
description: "my-description",
items: [
{
type: "boolean",
default: true
}
]
};
const uiSchema: UiSchema = {
"ui:ArrayFieldTemplate": ArrayFieldTemplate
}
const fields = {
TitleField: ({ title }: any) => (
<div id="custom-title">{title}</div>
),
DescriptionField: ({ description }: any) => (
<div id="custom-description">{description}</div>
),
}
const tree = renderer
.create(<Form schema={schema} uiSchema={uiSchema} fields={fields} />)
.toJSON();
expect(tree).toMatchSnapshot();
});
});
63 changes: 63 additions & 0 deletions packages/bootstrap-4/test/__snapshots__/Array.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,69 @@ exports[`array fields checkboxes 1`] = `
</form>
`;

exports[`array fields customization 1`] = `
<form
className="rjsf"
noValidate={false}
onSubmit={[Function]}
>
<div
className="form-group"
>
<div
className="field field-array field-array-fixed-items"
id="custom-tpl"
>
<div
id="custom-title"
>
my-title
</div>
<div
id="custom-description"
>
my-description
</div>
<div>
<div
className="form-group"
>
<div
className="checkbox form-group"
>
<div
className="form-check"
>
<input
autoFocus={false}
checked={true}
className="form-check-input position-static"
disabled={false}
id="root_0"
onBlur={[Function]}
onChange={[Function]}
onFocus={[Function]}
required={true}
type="checkbox"
/>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<button
className="btn btn-primary"
disabled={false}
type="submit"
>
Submit
</button>
</div>
</form>
`;

exports[`array fields fixed array 1`] = `
<form
className="rjsf"
Expand Down

0 comments on commit 1688d1e

Please sign in to comment.