From 623589a212c31c71fa648ad7bdcf81776e168707 Mon Sep 17 00:00:00 2001 From: shanejonas Date: Mon, 30 Nov 2020 12:12:22 -0800 Subject: [PATCH 1/3] fix: dont use existing object for example pairings fallback --- src/ExamplePairings/ExamplePairings.test.tsx | 82 ++++++++++++++++++++ src/ExamplePairings/ExamplePairings.tsx | 50 +++++++----- 2 files changed, 113 insertions(+), 19 deletions(-) diff --git a/src/ExamplePairings/ExamplePairings.test.tsx b/src/ExamplePairings/ExamplePairings.test.tsx index 5cacfb9..1eede6d 100644 --- a/src/ExamplePairings/ExamplePairings.test.tsx +++ b/src/ExamplePairings/ExamplePairings.test.tsx @@ -113,7 +113,53 @@ it("renders examples with only schema examples with no params", async () => { } /> , div); expect(div.innerHTML.includes("potato")).toBe(true); + expect(div.innerHTML.includes("bob")).toBe(false); + ReactDOM.unmountComponentAtNode(div); +}); + +it("renders examples with multiple param schema examples and no method", async () => { + const div = document.createElement("div"); + const testDoc: OpenrpcDocument = { + info: { + title: "test", + version: "0.0.0", + }, + methods: [ + { + name: "test-method", + params: [ + { + name: "testparam1", + schema: { + examples: ["bob"], + type: "string", + }, + }, + { + name: "testparam2", + schema: { + examples: ["bob2"], + type: "string", + }, + }, + ], + result: { + name: "test-method-result", + schema: { + examples: ["potato"], + type: "string", + }, + }, + }, + ], + openrpc: "1.0.0", + }; + ReactDOM.render( + + , div); + console.log("div", div.innerHTML); //tslint:disable-line expect(div.innerHTML.includes("bob")).toBe(true); + expect(div.innerHTML.includes("bob2")).toBe(true); ReactDOM.unmountComponentAtNode(div); }); @@ -153,6 +199,42 @@ it("renders examples with only schema examples and no method", async () => { ReactDOM.unmountComponentAtNode(div); }); +it("renders examples with only schema examples and no method with number", async () => { + const div = document.createElement("div"); + const testDoc: OpenrpcDocument = { + info: { + title: "test", + version: "0.0.0", + }, + methods: [ + { + name: "test-method", + params: [{ + name: "testparam1", + schema: { + examples: [10101], + type: "number", + }, + }], + result: { + name: "test-method-result", + schema: { + examples: ["potato"], + type: "string", + }, + }, + }, + ], + openrpc: "1.0.0", + }; + ReactDOM.render( + + , div); + ReactDOM.unmountComponentAtNode(div); +}); + it("renders examples and can switch between them", async () => { const simpleMath = await refParser.dereference(examples.simpleMath) as OpenrpcDocument; const { getByText } = render( diff --git a/src/ExamplePairings/ExamplePairings.tsx b/src/ExamplePairings/ExamplePairings.tsx index e32004e..5cbb204 100644 --- a/src/ExamplePairings/ExamplePairings.tsx +++ b/src/ExamplePairings/ExamplePairings.tsx @@ -15,15 +15,6 @@ interface IState { currentExample?: ExamplePairingObject; } -const newExample: ExamplePairingObject = { - name: "generated-example", - params: [ - ], - result: { - name: "example-result", - value: null, - }, -}; const getExamplesFromMethod = (method?: MethodObject): ExamplePairingObject[] => { if (!method) { return []; } if (!method.params) { return []; } @@ -33,12 +24,25 @@ const getExamplesFromMethod = (method?: MethodObject): ExamplePairingObject[] => if (param.schema && param.schema.examples && param.schema.examples.length > 0) { param.schema.examples.forEach((ex: any, i: number) => { if (!examples[i]) { - examples.push({ ...newExample }); + examples.push({ + name: "generated-example", + params: [ + { + name: param.name, + value: ex, + }, + ], + result: { + name: "example-result", + value: null, + }, + }); + } else { + examples[i].params.push({ + name: param.name, + value: ex, + }); } - examples[i].params.push({ - name: param.name, - value: ex, - }); }); } }); @@ -46,12 +50,20 @@ const getExamplesFromMethod = (method?: MethodObject): ExamplePairingObject[] => if (methodResult && methodResult.schema && methodResult.schema.examples && methodResult.schema.examples.length > 0) { methodResult.schema.examples.forEach((ex: any, i: number) => { if (!examples[i]) { - examples.push({ ...newExample }); + examples.push({ + name: "generated-example", + params: [], + result: { + name: methodResult.name, + value: ex, + }, + }); + } else { + examples[i].result = { + name: methodResult.name, + value: ex, + }; } - examples[i].result = { - name: methodResult.name, - value: ex, - }; }); } return examples; From 661d5b8a62f3e40d85d9c7220d5d3bc2e90c2a31 Mon Sep 17 00:00:00 2001 From: shanejonas Date: Mon, 30 Nov 2020 16:29:45 -0800 Subject: [PATCH 2/3] fix: add test for multiple fallback examples --- src/ExamplePairings/ExamplePairings.test.tsx | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/ExamplePairings/ExamplePairings.test.tsx b/src/ExamplePairings/ExamplePairings.test.tsx index 1eede6d..f58028f 100644 --- a/src/ExamplePairings/ExamplePairings.test.tsx +++ b/src/ExamplePairings/ExamplePairings.test.tsx @@ -234,6 +234,41 @@ it("renders examples with only schema examples and no method with number", async , div); ReactDOM.unmountComponentAtNode(div); }); +it("renders examples with only schema examples and no method with multiple number examples", async () => { + const div = document.createElement("div"); + const testDoc: OpenrpcDocument = { + info: { + title: "test", + version: "0.0.0", + }, + methods: [ + { + name: "test-method", + params: [{ + name: "testparam1", + schema: { + examples: [10101, 102], + type: "number", + }, + }], + result: { + name: "test-method-result", + schema: { + examples: ["potato", "bar"], + type: "string", + }, + }, + }, + ], + openrpc: "1.0.0", + }; + ReactDOM.render( + + , div); + ReactDOM.unmountComponentAtNode(div); +}); it("renders examples and can switch between them", async () => { const simpleMath = await refParser.dereference(examples.simpleMath) as OpenrpcDocument; From 81d33e09a76ac09c2de6ce8820585e3e344e3e87 Mon Sep 17 00:00:00 2001 From: shanejonas Date: Mon, 30 Nov 2020 17:50:07 -0800 Subject: [PATCH 3/3] fix: improve test coverage of Documentation component --- src/Documentation.test.tsx | 6 ++++++ src/Documentation.tsx | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Documentation.test.tsx b/src/Documentation.test.tsx index 1af7d80..e1aefcd 100644 --- a/src/Documentation.test.tsx +++ b/src/Documentation.test.tsx @@ -7,3 +7,9 @@ it("renders without crashing", () => { ReactDOM.render(, div); ReactDOM.unmountComponentAtNode(div); }); + +it("renders without crashing with no schema", () => { + const div = document.createElement("div"); + ReactDOM.render(, div); + ReactDOM.unmountComponentAtNode(div); +}); diff --git a/src/Documentation.tsx b/src/Documentation.tsx index 90737fe..0ae3272 100644 --- a/src/Documentation.tsx +++ b/src/Documentation.tsx @@ -6,7 +6,7 @@ import ContentDescriptors from "./ContentDescriptors/ContentDescriptors"; import { OpenrpcDocument } from "@open-rpc/meta-schema"; interface IProps { - schema: OpenrpcDocument; + schema?: OpenrpcDocument; uiSchema?: any; reactJsonOptions?: any; methodPlugins?: Array>;