Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/core/plugins/oas31/components/models/models.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,8 @@ const Models = ({
* Effects.
*/
useEffect(() => {
const includesExpandedSchema = Object.entries(schemas).some(
([schemaName]) =>
layoutSelectors.isShown([...schemasPath, schemaName], false)
)
const isOpenAndExpanded =
isOpen && (defaultModelsExpandDepth > 1 || includesExpandedSchema)
const isResolved = specSelectors.specResolvedSubtree(schemasPath) != null
if (isOpenAndExpanded && !isResolved) {
if (isOpen && !isResolved) {
specActions.requestResolvedSubtree(schemasPath)
}
}, [isOpen, defaultModelsExpandDepth])
Expand Down
290 changes: 290 additions & 0 deletions test/unit/core/plugins/oas31/components/models.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
/**
* @prettier
*/
/* eslint-disable react/jsx-no-bind, react/prop-types */
import React from "react"
import { mount } from "enzyme"

import Models from "core/plugins/oas31/components/models/models"

const fakeComponent =
(name) =>
({ children, ...props }) => {
return (
<div data-component-name={name} {...props}>
{children}
</div>
)
}

describe("<OAS31Models />", function () {
it("should request resolved subtree when schemas section is open with defaultModelsExpandDepth=1", function () {
const requestResolvedSubtree = jest.fn()
const schemas = {
Pet: {
type: "object",
properties: { petType: { type: "string" } },
discriminator: {
propertyName: "petType",
mapping: {
dog: "#/components/schemas/Dog",
cat: "#/components/schemas/Cat",
},
},
},
Dog: {
allOf: [
{ $ref: "#/components/schemas/Pet" },
{
type: "object",
properties: { breed: { type: "string" } },
},
],
},
}

mount(
<Models
specSelectors={{
selectSchemas: () => schemas,
specResolvedSubtree: () => null,
}}
specActions={{
requestResolvedSubtree: requestResolvedSubtree,
}}
layoutSelectors={{
isShown: () => true,
}}
layoutActions={{
show: jest.fn(),
readyToScroll: jest.fn(),
}}
getConfigs={() => ({
docExpansion: "list",
defaultModelsExpandDepth: 1,
})}
getComponent={(name) => fakeComponent(name)}
fn={{
jsonSchema202012: {
useFn: () => ({
getTitle: () => "",
}),
},
}}
/>
)

expect(requestResolvedSubtree).toHaveBeenCalledWith([
"components",
"schemas",
])
})

it("should not request resolved subtree when schemas are already resolved", function () {
const requestResolvedSubtree = jest.fn()
const schemas = {
Pet: { type: "object", properties: { petType: { type: "string" } } },
}

mount(
<Models
specSelectors={{
selectSchemas: () => schemas,
specResolvedSubtree: () => ({}),
}}
specActions={{
requestResolvedSubtree: requestResolvedSubtree,
}}
layoutSelectors={{
isShown: () => true,
}}
layoutActions={{
show: jest.fn(),
readyToScroll: jest.fn(),
}}
getConfigs={() => ({
docExpansion: "list",
defaultModelsExpandDepth: 1,
})}
getComponent={(name) => fakeComponent(name)}
fn={{
jsonSchema202012: {
useFn: () => ({
getTitle: () => "",
}),
},
}}
/>
)

expect(requestResolvedSubtree).not.toHaveBeenCalled()
})

it("should not request resolved subtree when schemas section is closed", function () {
const requestResolvedSubtree = jest.fn()
const schemas = {
Pet: { type: "object", properties: { petType: { type: "string" } } },
}

mount(
<Models
specSelectors={{
selectSchemas: () => schemas,
specResolvedSubtree: () => null,
}}
specActions={{
requestResolvedSubtree: requestResolvedSubtree,
}}
layoutSelectors={{
isShown: () => false,
}}
layoutActions={{
show: jest.fn(),
readyToScroll: jest.fn(),
}}
getConfigs={() => ({
docExpansion: "none",
defaultModelsExpandDepth: 1,
})}
getComponent={(name) => fakeComponent(name)}
fn={{
jsonSchema202012: {
useFn: () => ({
getTitle: () => "",
}),
},
}}
/>
)

expect(requestResolvedSubtree).not.toHaveBeenCalled()
})

it("should request resolved subtree for discriminator+allOf schemas with defaultModelsExpandDepth=1", function () {
const requestResolvedSubtree = jest.fn()
const schemas = {
Pet: {
type: "object",
required: ["petType"],
properties: { petType: { type: "string" } },
discriminator: {
propertyName: "petType",
mapping: {
dog: "#/components/schemas/Dog",
cat: "#/components/schemas/Cat",
},
},
},
Dog: {
allOf: [
{ $ref: "#/components/schemas/Pet" },
{
type: "object",
properties: {
breed: { type: "string" },
barkVolume: {
type: "integer",
description: "Volume of the bark",
},
},
},
],
},
Cat: {
allOf: [
{ $ref: "#/components/schemas/Pet" },
{
type: "object",
properties: {
color: { type: "string" },
purrLoudness: {
type: "integer",
description: "Loudness of the purr",
},
},
},
],
},
}

mount(
<Models
specSelectors={{
selectSchemas: () => schemas,
specResolvedSubtree: () => null,
}}
specActions={{
requestResolvedSubtree: requestResolvedSubtree,
}}
layoutSelectors={{
isShown: () => true,
}}
layoutActions={{
show: jest.fn(),
readyToScroll: jest.fn(),
}}
getConfigs={() => ({
docExpansion: "list",
defaultModelsExpandDepth: 1,
})}
getComponent={(name) => fakeComponent(name)}
fn={{
jsonSchema202012: {
useFn: () => ({
getTitle: () => "",
}),
},
}}
/>
)

expect(requestResolvedSubtree).toHaveBeenCalledWith([
"components",
"schemas",
])
})

it("should render all schema entries from selectSchemas", function () {
const schemas = {
Pet: { type: "object" },
Dog: { type: "object" },
Cat: { type: "object" },
}

const wrapper = mount(
<Models
specSelectors={{
selectSchemas: () => schemas,
specResolvedSubtree: () => ({}),
}}
specActions={{
requestResolvedSubtree: jest.fn(),
}}
layoutSelectors={{
isShown: () => true,
}}
layoutActions={{
show: jest.fn(),
readyToScroll: jest.fn(),
}}
getConfigs={() => ({
docExpansion: "list",
defaultModelsExpandDepth: 1,
})}
getComponent={(name) => fakeComponent(name)}
fn={{
jsonSchema202012: {
useFn: () => ({
getTitle: () => "",
}),
},
}}
/>
)

const jsonSchemaComponents = wrapper.find(
'[data-component-name="JSONSchema202012"]'
)
expect(jsonSchemaComponents).toHaveLength(3)
})
})