|
1 | 1 | import { expect } from "chai";
|
2 | 2 | import sinon from "sinon";
|
3 | 3 | import React, { createRef } from "react";
|
4 |
| -import { renderIntoDocument, Simulate } from "react-dom/test-utils"; |
| 4 | +import { renderIntoDocument, act, Simulate } from "react-dom/test-utils"; |
5 | 5 | import { render, findDOMNode } from "react-dom";
|
6 | 6 | import { Portal } from "react-portal";
|
7 | 7 | import validator, { customizeValidator } from "@rjsf/validator-ajv6";
|
@@ -1012,6 +1012,52 @@ describeRepeated("Form common", (createFormComponent) => {
|
1012 | 1012 | uiSchema,
|
1013 | 1013 | });
|
1014 | 1014 | });
|
| 1015 | + it("should call last provided change handler", async () => { |
| 1016 | + const schema = { |
| 1017 | + type: "object", |
| 1018 | + properties: { |
| 1019 | + foo: { |
| 1020 | + type: "string", |
| 1021 | + default: "bar", |
| 1022 | + }, |
| 1023 | + }, |
| 1024 | + }; |
| 1025 | + |
| 1026 | + const secondOnChange = sandbox.spy(); |
| 1027 | + |
| 1028 | + const { comp, onChange } = createFormComponent({ |
| 1029 | + schema, |
| 1030 | + formData: { foo: "bar1" }, |
| 1031 | + }); |
| 1032 | + |
| 1033 | + act(() => { |
| 1034 | + setProps(comp, { |
| 1035 | + schema, |
| 1036 | + formData: {}, |
| 1037 | + onChange, |
| 1038 | + }); |
| 1039 | + }); |
| 1040 | + |
| 1041 | + sinon.assert.callCount(onChange, 1); |
| 1042 | + |
| 1043 | + act(() => { |
| 1044 | + setProps(comp, { |
| 1045 | + schema, |
| 1046 | + formData: { foo: "bar2" }, |
| 1047 | + }); |
| 1048 | + }); |
| 1049 | + |
| 1050 | + act(() => { |
| 1051 | + setProps(comp, { |
| 1052 | + schema, |
| 1053 | + formData: {}, |
| 1054 | + onChange: secondOnChange, |
| 1055 | + }); |
| 1056 | + }); |
| 1057 | + |
| 1058 | + sinon.assert.callCount(onChange, 1); |
| 1059 | + sinon.assert.callCount(secondOnChange, 1); |
| 1060 | + }); |
1015 | 1061 | });
|
1016 | 1062 | describe("Blur handler", () => {
|
1017 | 1063 | it("should call provided blur handler on form input blur event", () => {
|
@@ -3402,4 +3448,62 @@ describe("Form omitExtraData and liveOmit", () => {
|
3402 | 3448 |
|
3403 | 3449 | expect(node.querySelectorAll(".error-detail li")).to.have.length.of(2);
|
3404 | 3450 | });
|
| 3451 | + describe("Calling onChange right after updating a Form with props formData", () => { |
| 3452 | + const schema = { |
| 3453 | + type: "array", |
| 3454 | + items: { |
| 3455 | + type: "string", |
| 3456 | + }, |
| 3457 | + }; |
| 3458 | + |
| 3459 | + let changed = false; |
| 3460 | + class ArrayThatTriggersOnChangeRightAfterUpdated extends React.Component { |
| 3461 | + componentDidUpdate = () => { |
| 3462 | + if (changed) { |
| 3463 | + return; |
| 3464 | + } |
| 3465 | + changed = true; |
| 3466 | + this.props.onChange([...this.props.formData, "test"]); |
| 3467 | + }; |
| 3468 | + render() { |
| 3469 | + const { ArrayField } = this.props.registry.fields; |
| 3470 | + return <ArrayField {...this.props} />; |
| 3471 | + } |
| 3472 | + } |
| 3473 | + |
| 3474 | + const uiSchema = { |
| 3475 | + "ui:field": ArrayThatTriggersOnChangeRightAfterUpdated, |
| 3476 | + }; |
| 3477 | + |
| 3478 | + const props = { |
| 3479 | + schema, |
| 3480 | + uiSchema, |
| 3481 | + }; |
| 3482 | + |
| 3483 | + class Container extends React.Component { |
| 3484 | + constructor(props) { |
| 3485 | + super(props); |
| 3486 | + this.state = {}; |
| 3487 | + } |
| 3488 | + |
| 3489 | + onChange = ({ formData }) => { |
| 3490 | + this.setState({ formData }); |
| 3491 | + }; |
| 3492 | + |
| 3493 | + render() { |
| 3494 | + return ( |
| 3495 | + <Form {...this.props} {...this.state} onChange={this.onChange} /> |
| 3496 | + ); |
| 3497 | + } |
| 3498 | + } |
| 3499 | + |
| 3500 | + it("doesn't cause a race condition", () => { |
| 3501 | + const { node } = createComponent(Container, { ...props }); |
| 3502 | + |
| 3503 | + Simulate.click(node.querySelector(".array-item-add button")); |
| 3504 | + |
| 3505 | + expect(node.querySelector("#root_0")).to.exist; |
| 3506 | + expect(node.querySelector("#root_1").getAttribute("value")).to.eq("test"); |
| 3507 | + }); |
| 3508 | + }); |
3405 | 3509 | });
|
0 commit comments