Skip to content

Commit

Permalink
Fix file widget tests (#1285)
Browse files Browse the repository at this point in the history
* react-dom is external peerDependency, just like react.

fixes version conflicts, e.g. I got an error

  Uncaught TypeError: this.updater.enqueueCallback is not a function when using setState callback

because I use React 16.x and react-jsonschema-form bundled react-dom 15.x

See facebook/react#10320 (comment)

* Fix for new submit() method (PR #1058) also submitting the HTML form

..., navigating away from current page at least in Firefox. Reason:
dispatched event was not cancelable, so preventDefault in onSubmit
couldn't cancel it.

Links:

* <https://stackoverflow.com/a/40916998>
* <https://developer.mozilla.org/en-US/docs/Web/API/Event/cancelable>

* Synchronous call to onSubmit() from props

Due to the use of setImmediate() hack in setState utility function (utils.js),
onSubmit() handler from props is called asynchronously. This leads to massive
problems for operations requiring "trusted events", like window.open() or
programmatically submitting forms with target "_blank" (which we needed)

Because onSubmit() should not need the performance-related setImmiate() hack, I
replaced call to setState utility function with proper this.setState() from
React.

* fix failing FileWidget tests

by changing to proper use of setState() without setImmediate() hacks (see #1197)

* fix input type, fix test name

* Test with newer node versions

* Add node 12
  • Loading branch information
sbusch authored and epicfaace committed May 22, 2019
1 parent 67d17dd commit 48742bf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ language:
- node_js
node_js:
- "6"
- "7"
- "8"
- "9"
- "10"
- "11"
- "12"
env:
- ACTION=test
- ACTION="run lint"
Expand Down
4 changes: 2 additions & 2 deletions src/components/widgets/FileWidget.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from "react";
import PropTypes from "prop-types";

import { dataURItoBlob, shouldRender, setState } from "../../utils";
import { dataURItoBlob, shouldRender } from "../../utils";

function addNameToDataURL(dataURL, name) {
return dataURL.replace(";base64", `;name=${encodeURIComponent(name)};base64`);
Expand Down Expand Up @@ -79,7 +79,7 @@ class FileWidget extends Component {
values: filesInfo.map(fileInfo => fileInfo.dataURL),
filesInfo,
};
setState(this, state, () => {
this.setState(state, () => {
if (multiple) {
onChange(state.values);
} else {
Expand Down
4 changes: 2 additions & 2 deletions test/StringField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ describe("StringField", () => {
describe("FileWidget", () => {
const initialValue = "data:text/plain;name=file1.txt;base64,dGVzdDE=";

it("should render a color field", () => {
it("should render a file field", () => {
const { node } = createFormComponent({
schema: {
type: "string",
Expand All @@ -1581,7 +1581,7 @@ describe("StringField", () => {
const { comp } = createFormComponent({
schema: {
type: "string",
format: "color",
format: "data-url",
default: initialValue,
},
});
Expand Down

0 comments on commit 48742bf

Please sign in to comment.