Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to hide the example code when using the SchemaDefinition component #2157

Merged
Merged
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
18 changes: 12 additions & 6 deletions src/components/SchemaDefinition/SchemaDefinition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface ObjectDescriptionProps {
exampleRef?: string;
showReadOnly?: boolean;
showWriteOnly?: boolean;
showExample?: boolean;
parser: OpenAPIParser;
options: RedocNormalizedOptions;
}
Expand Down Expand Up @@ -53,7 +54,7 @@ export class SchemaDefinition extends React.PureComponent<ObjectDescriptionProps
}

render() {
const { showReadOnly = true, showWriteOnly = false } = this.props;
const { showReadOnly = true, showWriteOnly = false, showExample = true } = this.props;
return (
<Section>
<Row>
Expand All @@ -64,11 +65,16 @@ export class SchemaDefinition extends React.PureComponent<ObjectDescriptionProps
schema={this.mediaModel.schema}
/>
</MiddlePanel>
<DarkRightPanel>
<MediaSamplesWrap>
<MediaTypeSamples renderDropdown={this.renderDropdown} mediaType={this.mediaModel} />
</MediaSamplesWrap>
</DarkRightPanel>
{showExample && (
<DarkRightPanel>
<MediaSamplesWrap>
<MediaTypeSamples
renderDropdown={this.renderDropdown}
mediaType={this.mediaModel}
/>
</MediaSamplesWrap>
</DarkRightPanel>
)}
</Row>
</Section>
);
Expand Down
82 changes: 82 additions & 0 deletions src/components/__tests__/SchemaDefinition.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* tslint:disable:no-implicit-dependencies */

import { shallow } from 'enzyme';
import * as React from 'react';

import { SchemaDefinition } from '..';
import { OpenAPIParser } from '../../services';
import { RedocNormalizedOptions } from '../../services/RedocNormalizedOptions';
import { withTheme } from '../testProviders';

const options = new RedocNormalizedOptions({});
describe('Components', () => {
describe('SchemaDefinition', () => {
const parser = new OpenAPIParser(
{
openapi: '3.0',
info: {
title: 'test',
version: '0',
},
paths: {},
components: {
schemas: {
test: {
type: 'object',
properties: {
id: {
type: 'string',
},
},
},
},
},
},
undefined,
options,
);

describe('Show example constraints', () => {
it('should show the example as default', () => {
const component = shallow(
withTheme(
<SchemaDefinition
schemaRef="#/components/schemas/test"
parser={parser}
options={options}
/>,
),
);
expect(component.html().includes('<code>')).toBe(true);
});

it('should show the example if `showExample` is `true`', () => {
const component = shallow(
withTheme(
<SchemaDefinition
schemaRef="#/components/schemas/test"
parser={parser}
options={options}
showExample={true}
/>,
),
);
expect(component.html().includes('<code>')).toBe(true);
});

it('should hide the example if `showExample` is `false`', () => {
const component = shallow(
withTheme(
<SchemaDefinition
schemaRef="#/components/schemas/test"
parser={parser}
options={options}
showExample={false}
/>,
),
);
expect(component.html().includes('<code>')).toBe(false);
});
});
});
});