Skip to content

Commit 44ece46

Browse files
committed
Fixes #3633
Make sure PrimitiveModel uses the schema's title first and then falls back to the passed-in `name` property. Added enzyme test for functionality.
1 parent f63f022 commit 44ece46

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/core/components/primitive-model.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ export default class Primitive extends Component {
2323
let format = schema.get("format")
2424
let xml = schema.get("xml")
2525
let enumArray = schema.get("enum")
26+
let title = schema.get("title") || name
2627
let description = schema.get("description")
2728
let properties = schema.filter( ( v, key) => ["enum", "type", "format", "description", "$$ref"].indexOf(key) === -1 )
2829
const Markdown = getComponent("Markdown")
2930
const EnumModel = getComponent("EnumModel")
3031

3132
return <span className="model">
3233
<span className="prop">
33-
{ name && <span className={`${depth === 1 && "model-title"} prop-name`}>{ name }</span> }
34+
{ name && <span className={`${depth === 1 && "model-title"} prop-name`}>{ title }</span> }
3435
<span className="prop-type">{ type }</span>
3536
{ format && <span className="prop-format">(${format})</span>}
3637
{

test/components/primitive-model.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* eslint-env mocha */
2+
import React from "react"
3+
import expect from "expect"
4+
import { shallow } from "enzyme"
5+
import { fromJS } from "immutable"
6+
import PrimitiveModel from "components/primitive-model"
7+
8+
describe("<PrimitiveModel/>", function() {
9+
describe("Model name", function() {
10+
const dummyComponent = () => null
11+
const components = {
12+
Markdown: dummyComponent,
13+
EnumModel: dummyComponent
14+
}
15+
const props = {
16+
getComponent: c => components[c],
17+
name: "Name from props",
18+
depth: 1,
19+
schema: fromJS({
20+
type: "string",
21+
title: "Custom model title"
22+
})
23+
}
24+
25+
it("renders the schema's title", function() {
26+
// When
27+
const wrapper = shallow(<PrimitiveModel {...props}/>)
28+
const modelTitleEl = wrapper.find("span.model-title")
29+
expect(modelTitleEl.length).toEqual(1)
30+
31+
// Then
32+
expect( modelTitleEl.text() ).toEqual( "Custom model title" )
33+
})
34+
35+
it("falls back to the passed-in `name` prop for the title", function() {
36+
// When
37+
props.schema = fromJS({
38+
type: "string"
39+
})
40+
const wrapper = shallow(<PrimitiveModel {...props}/>)
41+
const modelTitleEl = wrapper.find("span.model-title")
42+
expect(modelTitleEl.length).toEqual(1)
43+
44+
// Then
45+
expect( modelTitleEl.text() ).toEqual( "Name from props" )
46+
})
47+
48+
})
49+
} )

0 commit comments

Comments
 (0)