From 2c887bcad3a417d2ce83889f68806e11d9c93ebd Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Wed, 13 Jun 2018 17:10:46 -0700 Subject: [PATCH 1/3] Move some mocha options to mocha.opts This allows you to simply run `mocha test-file.js` without having to remember to add all the extra stuff --- package.json | 4 ++-- test/mocha.opts | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a2c913eeb9..5f6f398ff7 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,8 @@ "publish-to-npm": "npm run build:readme && npm run dist && npm publish", "preversion": "npm run build:playground && npm run dist && npm run build:readme && npm run cs-check && npm run lint", "start": "node devServer.js", - "tdd": "cross-env NODE_ENV=test mocha --compilers js:babel-register --watch --require ./test/setup-jsdom.js test/**/*_test.js", - "test": "cross-env NODE_ENV=test mocha --compilers js:babel-register --require ./test/setup-jsdom.js test/**/*_test.js" + "tdd": "cross-env NODE_ENV=test mocha --watch test/**/*_test.js", + "test": "cross-env NODE_ENV=test mocha test/**/*_test.js" }, "prettierOptions": "--jsx-bracket-same-line --trailing-comma es5 --semi --tab-width 2", "lint-staged": { diff --git a/test/mocha.opts b/test/mocha.opts index cf80ee74bc..afbeee1278 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1 +1,3 @@ --timeout 5000 +--compilers js:babel-register +--require ./test/setup-jsdom.js From f518abc2ae9c12d09c2e77692274edc93903c87d Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Wed, 13 Jun 2018 17:15:56 -0700 Subject: [PATCH 2/3] Allow schema $refs to not have to start with `#/definitions` We're using this module to render forms for Swagger/OAS files. $refs in swagger are stored in `components/schemas` at the top level of the object. This change allows $ref strings to not always start with `#/definitions/` which is a much easier solution than us having to recursively modify $refs to prefix with this value. All existing tests are passing, with a new test for this functionality. The change was to make the definitions part of the regular expression an optional non-capturing group. --- src/utils.js | 2 +- test/utils_test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index e741510f10..19ab1c08c9 100644 --- a/src/utils.js +++ b/src/utils.js @@ -383,7 +383,7 @@ export function optionsList(schema) { function findSchemaDefinition($ref, definitions = {}) { // Extract and use the referenced definition if we have it. - const match = /^#\/definitions\/(.*)$/.exec($ref); + const match = /^#(?:\/definitions)?\/(.*)$/.exec($ref); if (match && match[1]) { const parts = match[1].split("/"); let current = definitions; diff --git a/test/utils_test.js b/test/utils_test.js index 2ada075147..5cef0564aa 100644 --- a/test/utils_test.js +++ b/test/utils_test.js @@ -497,6 +497,22 @@ describe("utils", () => { expect(retrieveSchema(schema, definitions)).eql(address); }); + it("should 'resolve' a schema which contains definitions not in `#/definitions`", () => { + const schema = { $ref: "#/components/schemas/address" }; + const address = { + type: "object", + properties: { + street_address: { type: "string" }, + city: { type: "string" }, + state: { type: "string" }, + }, + required: ["street_address", "city", "state"], + }; + const definitions = { components: { schemas: { address } } }; + + expect(retrieveSchema(schema, definitions)).eql(address); + }); + it("should 'resolve' escaped JSON Pointers", () => { const schema = { $ref: "#/definitions/a~0complex~1name" }; const address = { type: "string" }; From d8b5e3a9a1e528315cd9cbbb687f14ad3f088604 Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Tue, 7 Aug 2018 13:50:02 -0700 Subject: [PATCH 3/3] Add wrapper element around required asterisk in TitleField This is so that it can be separately styled easily via CSS. It is already done this way in https://github.com/mozilla-services/react-jsonschema-form/blob/aadbbe9701c4db9df761e86f0280e1ecafc509f8/src/components/fields/SchemaField.js#L59 --- src/components/fields/TitleField.js | 8 ++++++-- test/TitleField_test.js | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/components/fields/TitleField.js b/src/components/fields/TitleField.js index 30ed634df5..f33f983845 100644 --- a/src/components/fields/TitleField.js +++ b/src/components/fields/TitleField.js @@ -5,8 +5,12 @@ const REQUIRED_FIELD_SYMBOL = "*"; function TitleField(props) { const { id, title, required } = props; - const legend = required ? title + REQUIRED_FIELD_SYMBOL : title; - return {legend}; + return ( + + {title} + {required && {REQUIRED_FIELD_SYMBOL}} + + ); } if (process.env.NODE_ENV !== "production") { diff --git a/test/TitleField_test.js b/test/TitleField_test.js index ed689ecddc..1c62e49889 100644 --- a/test/TitleField_test.js +++ b/test/TitleField_test.js @@ -65,5 +65,7 @@ describe("TitleField", () => { const { node } = createComponent(TitleFieldWrapper, props); expect(node.textContent).to.equal(props.title + "*"); + + expect(node.querySelector("span.required").textContent).to.equal("*"); }); });