diff --git a/__tests__/JSONSchemaViewer/__snapshots__/error.test.tsx.snap b/__tests__/JSONSchemaViewer/__snapshots__/error.test.tsx.snap new file mode 100644 index 00000000..8464480c --- /dev/null +++ b/__tests__/JSONSchemaViewer/__snapshots__/error.test.tsx.snap @@ -0,0 +1,7 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`JSONSchemaViewer states Can render error when something bad happens 1`] = ` +
+ Something bad happens : Resolver error +
+`; diff --git a/__tests__/JSONSchemaViewer/error.test.tsx b/__tests__/JSONSchemaViewer/error.test.tsx new file mode 100644 index 00000000..f36ff44a --- /dev/null +++ b/__tests__/JSONSchemaViewer/error.test.tsx @@ -0,0 +1,37 @@ +import React from "react"; + +// For typings autocomplete whatever your IDE +import { expect, test, describe, jest } from "@jest/globals" + +import { create, act } from "react-test-renderer" + +import JSONSchemaViewer from "../../src/theme/JSONSchemaViewer/index"; + +// Type to prevent creating invalid mocks +import type { JSONSchema } from "../../src/theme/JSONSchemaViewer/types" + +// Type for react-test-renderer +import type { ReactTestRenderer } from "react-test-renderer" + +jest.mock('@stoplight/json-ref-resolver', () => { + const resolve = jest.fn<() => Promise>().mockRejectedValue(new Error('Resolver error')); + return { + Resolver: jest.fn(() => ({ resolve })) + }; +}); + +describe("JSONSchemaViewer states", () => { + + test("Can render error when something bad happens", async () => { + const fakeSchema3 : JSONSchema = { "type": "object" } + + // render the component + let root: ReactTestRenderer | undefined + await act(async () => { + root = create() + }) + + // make assertions on root + expect(root?.toJSON()).toMatchSnapshot() + }) +}); diff --git a/src/theme/JSONSchemaViewer/index.tsx b/src/theme/JSONSchemaViewer/index.tsx index d797464a..620245f0 100644 --- a/src/theme/JSONSchemaViewer/index.tsx +++ b/src/theme/JSONSchemaViewer/index.tsx @@ -1,5 +1,6 @@ import React, { useState, useEffect } from "react" import { Resolver } from "@stoplight/json-ref-resolver" +import Translate from "@docusaurus/Translate" import { CreateNodes, Collapsible } from "./components/index" import { JSVOptionsContextProvider } from "./contexts/index" @@ -28,12 +29,43 @@ type InnerViewerProperties = { viewerOptions?: Omit } +// Translated labels +function Loading(): JSX.Element { + return ( +
+ + {"Loading ...."} + +
+ ) +} + +function ErrorOccurred(props: { error: Error }): JSX.Element { + const { error } = props + return ( +
+ + {"Something bad happens : {message}"} + +
+ ) +} + // Internal function JSONSchemaInnerViewer(props: InnerViewerProperties): JSX.Element { const { schema, viewerOptions } = props // Title of the schema, for user friendliness const title = - typeof schema !== "boolean" && schema?.title !== undefined + typeof schema !== "boolean" && schema.title !== undefined ? schema.title : "Schema" @@ -80,9 +112,9 @@ export default function JSONSchemaViewer(props: Props): JSX.Element { }, []) if (error !== undefined) { - return
Something bad happens : {error.message}
+ return } else if (resolvedSchema === undefined) { - return
Loading ....
+ return } else { return (