Skip to content

Commit

Permalink
style: fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed Jan 30, 2025
1 parent e35ef7f commit 44dbc8e
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 180 deletions.
48 changes: 38 additions & 10 deletions __tests__/JSONSchemaViewer/array.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,63 @@ import JSONSchemaViewer from "../../src/theme/JSONSchemaViewer/index"
import type { JSONSchema } from "../../src/theme/JSONSchemaViewer/types"
import type { RenderResult } from "@testing-library/react"


const testcases: JSONSchema[] = [
{ type: "array" },
{ type: "array", contains: { type: "number" } },
{ type: "array", items: { type: "number" } },
{ type: "array", minItems: 2, maxItems: 3 },
{ type: "array", contains: { type: "integer" }, minContains: 2, maxContains: 3 },
{
type: "array",
description: "Represent a street address such as ['1600','Pennsylvania','Avenue','NW']",
contains: { type: "integer" },
minContains: 2,
maxContains: 3,
},
{
type: "array",
description:
"Represent a street address such as ['1600','Pennsylvania','Avenue','NW']",
items: false,
prefixItems: [
{ type: "number", description: "The address number" },
{ type: "string", description: "The name of the street" },
{ enum: ["Street", "Avenue", "Boulevard"], description: "The type of street" },
{ enum: ["NW", "NE", "SW", "SE"], description: "The city quadrant of the address" },
{
enum: ["Street", "Avenue", "Boulevard"],
description: "The type of street",
},
{
enum: ["NW", "NE", "SW", "SE"],
description: "The city quadrant of the address",
},
],
},
{ type: "array", uniqueItems: true },
{ minItems: 1 },
{ maxItems: 5 },
{ maxContains: 3 },
{ minContains: 2 },
{ type: "array", items: [{ type: "integer" }, { type: "string" }], minItems: 1 },
{ type: "array", prefixItems: [{ type: "number" }, { type: "string" }], minItems: 3 },
{
type: "array",
items: [{ type: "integer" }, { type: "string" }],
minItems: 1,
},
{
type: "array",
prefixItems: [{ type: "number" }, { type: "string" }],
minItems: 3,
},
{ type: "array", items: { type: "string" }, minItems: 1 },
{ unevaluatedItems: false },
{ type: "array", items: [{ type: "integer" }, { type: "string" }], additionalItems: { type: "boolean" }, minItems: 2 },
{ type: "array", prefixItems: [{ type: "string" }], unevaluatedItems: { type: "number" } },
{
type: "array",
items: [{ type: "integer" }, { type: "string" }],
additionalItems: { type: "boolean" },
minItems: 2,
},
{
type: "array",
prefixItems: [{ type: "string" }],
unevaluatedItems: { type: "number" },
},
]

describe("JSONSchemaViewer - Array type", () => {
Expand All @@ -47,4 +75,4 @@ describe("JSONSchemaViewer - Array type", () => {

expect(result!.asFragment()).toMatchSnapshot()
})
})
})
7 changes: 5 additions & 2 deletions __tests__/JSONSchemaViewer/basics.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ describe("JSONSchemaViewer - basics tests", () => {
})

test("Can render Schema title", async () => {
const fakeSchema2: JSONSchema = { type: "boolean", title: "My Super Schema" }
const fakeSchema2: JSONSchema = {
type: "boolean",
title: "My Super Schema",
}
let result: RenderResult | null = null

await act(async () => {
Expand All @@ -27,4 +30,4 @@ describe("JSONSchemaViewer - basics tests", () => {

expect(result!.asFragment()).toMatchSnapshot()
})
})
})
6 changes: 5 additions & 1 deletion __tests__/JSONSchemaViewer/boolean.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import type { JSONSchema } from "../../src/theme/JSONSchemaViewer/types"

const testcases: JSONSchema[] = [
{ type: "boolean" },
{ enum: [true, false], default: false, description: "Boolean without the explicit type" },
{
enum: [true, false],
default: false,
description: "Boolean without the explicit type",
},
]

describe("JSONSchemaViewer - Boolean type", () => {
Expand Down
37 changes: 27 additions & 10 deletions __tests__/JSONSchemaViewer/constructor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ describe("JSONSchemaViewer - constructor", () => {
<JSONSchemaViewer
schema={fakeSchema}
viewerOptions={{ qualifierMessagesOrder: ["objectProperties"] }}
/>
/>,
)
})

expect(result!.asFragment()).toMatchSnapshot()
})

test("Overwrite default DescriptionComponent value", async () => {
const fakeSchema2: JSONSchema = { type: "object", description: "# Hello, *world*!" }
const fakeSchema2: JSONSchema = {
type: "object",
description: "# Hello, *world*!",
}
let result: RenderResult | null = null

await act(async () => {
Expand All @@ -38,7 +41,7 @@ describe("JSONSchemaViewer - constructor", () => {
</h1>
),
}}
/>
/>,
)
})

Expand All @@ -65,7 +68,7 @@ describe("JSONSchemaViewer - constructor", () => {
viewerOptions={{
UnresolvedRefsComponent: () => <>#node was not resolved</>,
}}
/>
/>,
)
})

Expand All @@ -83,8 +86,16 @@ describe("JSONSchemaViewer - constructor", () => {
type: "string",
description: "A customized or personalized field",
enum: [
"palette", "teddyBear", "tools", "laptop", "thread",
"phone", "puzzle", "scissors", "hammer", "note",
"palette",
"teddyBear",
"tools",
"laptop",
"thread",
"phone",
"puzzle",
"scissors",
"hammer",
"note",
],
default: "palette",
examples: ["tools", "note"],
Expand All @@ -110,14 +121,18 @@ describe("JSONSchemaViewer - constructor", () => {

const component = <code>{`${value}`}</code>

if (typeof schema !== "boolean" && schema.default && value === schema.default) {
if (
typeof schema !== "boolean" &&
schema.default &&
value === schema.default
) {
return <strong>{component}</strong>
}

return component
},
}}
/>
/>,
)
})

Expand All @@ -129,9 +144,11 @@ describe("JSONSchemaViewer - constructor", () => {
let result: RenderResult | null = null

await act(async () => {
result = render(<JSONSchemaViewer schema={fakeSchema} className="jsv-custom" />)
result = render(
<JSONSchemaViewer schema={fakeSchema} className="jsv-custom" />,
)
})

expect(result!.asFragment()).toMatchSnapshot()
})
})
})
42 changes: 21 additions & 21 deletions __tests__/JSONSchemaViewer/corner_cases.test.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import React from "react";
import { expect, test, describe } from "@jest/globals";
import { render, act } from "@testing-library/react";
import React from "react"
import { expect, test, describe } from "@jest/globals"
import { render, act } from "@testing-library/react"

import type { RenderResult } from "@testing-library/react";
import type { JSX } from "react";
import type { RenderResult } from "@testing-library/react"
import type { JSX } from "react"

// Type to prevent creating invalid mocks
import type {
JSONSchema,
JSONSchemaNS,
} from "../../src/theme/JSONSchemaViewer/types";
} from "../../src/theme/JSONSchemaViewer/types"

// Components
import { CreateNodes } from "../../src/theme/JSONSchemaViewer/components/index";
import { CreateNodes } from "../../src/theme/JSONSchemaViewer/components/index"
import {
CreateArray,
CreateObject,
} from "../../src/theme/JSONSchemaViewer/JSONSchemaElements/index";
import { detectedTypes } from "../../src/theme/JSONSchemaViewer/utils/index";
} from "../../src/theme/JSONSchemaViewer/JSONSchemaElements/index"
import { detectedTypes } from "../../src/theme/JSONSchemaViewer/utils/index"

const testcases: [
string,
Expand Down Expand Up @@ -63,27 +63,27 @@ const testcases: [
},
} as JSONSchemaNS.Array,
],
];
]

describe("JSONSchemaViewer - corner cases", () => {
test.each(testcases)("%s", async (_title, Component, fakeSchema) => {
// Render the component
let rendered: RenderResult | null = null;
// Render the component
let rendered: RenderResult | null = null

// Use act to ensure all updates are processed
await act(async () => {
rendered = render(<Component schema={fakeSchema} />);
});
rendered = render(<Component schema={fakeSchema} />)
})

// Capture the snapshot
expect(rendered!.asFragment()).toMatchSnapshot();
});
expect(rendered!.asFragment()).toMatchSnapshot()
})

test("Correctly infers integer when not explicitly expressed", () => {
const foundTypes = detectedTypes({
multipleOf: 1,
});
})

expect(foundTypes).toContain("integer");
});
});
expect(foundTypes).toContain("integer")
})
})
30 changes: 15 additions & 15 deletions __tests__/JSONSchemaViewer/error.test.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import React from "react";
import { expect, test, describe, jest } from "@jest/globals";
import { render, act } from "@testing-library/react";
import JSONSchemaViewer from "../../src/theme/JSONSchemaViewer/index";
import React from "react"
import { expect, test, describe, jest } from "@jest/globals"
import { render, act } from "@testing-library/react"
import JSONSchemaViewer from "../../src/theme/JSONSchemaViewer/index"

import type { RenderResult } from "@testing-library/react";
import type { JSONSchema } from "../../src/theme/JSONSchemaViewer/types";
import type { RenderResult } from "@testing-library/react"
import type { JSONSchema } from "../../src/theme/JSONSchemaViewer/types"

jest.mock("@stoplight/json-ref-resolver", () => {
const resolve = jest.fn(() => Promise.reject(new Error("Resolver error")));
const resolve = jest.fn(() => Promise.reject(new Error("Resolver error")))
return {
Resolver: jest.fn(() => ({ resolve })),
};
});
}
})

describe("JSONSchemaViewer states", () => {
test("Can render error when something bad happens", async () => {
const fakeSchema: JSONSchema = { type: "object" }
let result: RenderResult | null = null;
let result: RenderResult | null = null

// Render the component within act
await act(async () => {
result = render(<JSONSchemaViewer schema={fakeSchema} />);
});
result = render(<JSONSchemaViewer schema={fakeSchema} />)
})

// Capture the snapshot
expect(result!.asFragment()).toMatchSnapshot();
});
});
expect(result!.asFragment()).toMatchSnapshot()
})
})
29 changes: 16 additions & 13 deletions __tests__/JSONSchemaViewer/generateFriendlyName.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import React from "react";
import { expect, test, describe } from "@jest/globals";
import { render, act } from "@testing-library/react";
import type { RenderResult } from "@testing-library/react";
import JSONSchemaViewer from "../../src/theme/JSONSchemaViewer/index";
import React from "react"
import { expect, test, describe } from "@jest/globals"
import { render, act } from "@testing-library/react"
import type { RenderResult } from "@testing-library/react"
import JSONSchemaViewer from "../../src/theme/JSONSchemaViewer/index"

// Type to prevent creating invalid mocks
import type { JSONSchema, JSONSchemaNS } from "../../src/theme/JSONSchemaViewer/types";
import type {
JSONSchema,
JSONSchemaNS,
} from "../../src/theme/JSONSchemaViewer/types"

const testcases: [string, JSONSchema][] = [
[
Expand Down Expand Up @@ -54,18 +57,18 @@ const testcases: [string, JSONSchema][] = [
],
} as JSONSchemaNS.Array,
],
];
]

describe("JSONSchemaViewer - generateFriendlyName cases", () => {
test.each(testcases)("test %s", async (_title, fakeSchema) => {
let result: RenderResult | null = null;
let result: RenderResult | null = null

// Render the component within act
await act(async () => {
result = render(<JSONSchemaViewer schema={fakeSchema} />);
});
result = render(<JSONSchemaViewer schema={fakeSchema} />)
})

// Capture the snapshot
expect(result!.asFragment()).toMatchSnapshot();
});
});
expect(result!.asFragment()).toMatchSnapshot()
})
})
Loading

0 comments on commit 44dbc8e

Please sign in to comment.