Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Add some more tests and reduce duplication a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Mar 13, 2018
1 parent 76211d7 commit 72610f9
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 24 deletions.
63 changes: 61 additions & 2 deletions packages/api-explorer/__tests__/CodeSample.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ describe('code examples', () => {
},
],
};
const languages = ['node', 'curl'];
const codeSample = shallow(
<CodeSample
{...docProps}
oas={
new Oas({
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: languages,
[extensions.SAMPLES_LANGUAGES]: ['node', 'curl'],
servers: [{ url: 'http://example.com' }],
})
}
Expand All @@ -84,6 +83,66 @@ describe('code examples', () => {
expect(codeSample.find('.code-sample-body').length).toBe(1);
expect(codeSample.find('pre.tomorrow-night.tabber-body').length).toBe(1);
});

test('should not error if no code given', () => {
const docProps = {
setLanguage: () => {},
operation: new Operation({}, '/pet/{id}', 'get'),
formData: {},
language: 'node',
customCodeSamples: [
{
language: 'javascript',
},
],
};
expect(() => shallow(
<CodeSample
{...docProps}
oas={
new Oas({
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: ['node', 'curl'],
servers: [{ url: 'http://example.com' }],
})
}
/>,
)).not.toThrow(/Cannot read property 'split' of undefined/);
});

test('should not render sample if language is missing', () => {
const docProps = {
setLanguage: () => {},
operation: new Operation({}, '/pet/{id}', 'get'),
formData: {},
language: 'node',
customCodeSamples: [
{
code: 'console.log(1);',
},
{
language: 'curl',
code: 'curl example.com',
},
],
};

const codeSample = shallow(
<CodeSample
{...docProps}
oas={
new Oas({
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: ['node', 'curl'],
servers: [{ url: 'http://example.com' }],
})
}
/>,
);

expect(codeSample.find('.code-sample-tabs a').length).toBe(1);
expect(codeSample.find('.code-sample-body pre').length).toBe(1);
});
});

describe('code examples', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/api-explorer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions packages/api-explorer/src/CodeSample.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ class CodeSample extends React.Component {
render() {
const { oas, setLanguage, operation, formData, language, customCodeSamples } = this.props;

const codeSamplesWithLanguages = customCodeSamples.filter(example => example.language);

return (
<div className="code-sample tabber-parent">
{(() => {
if (!oas[extensions.SAMPLES_ENABLED]) {
return <div className="hub-no-code">No code samples available</div>;
}

const snippet = generateCodeSnippet(oas, operation, formData, language);

if (customCodeSamples.length) {
return (
<div>
<ul className="code-sample-tabs">
{customCodeSamples.map((example, index) => (
{codeSamplesWithLanguages.map((example, index) => (
<li key={example.language}>
{
// eslint-disable-next-line jsx-a11y/href-no-hash
Expand All @@ -57,15 +57,15 @@ class CodeSample extends React.Component {
))}
</ul>
<div className="code-sample-body">
{customCodeSamples.map((example, index) => {
{codeSamplesWithLanguages.map((example, index) => {
return (
<pre
className="tomorrow-night tabber-body"
style={{ display: index === this.state.customCodeSampleTab ? 'block' : '' }}
key={index} // eslint-disable-line react/no-array-index-key
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: syntaxHighlighter(example.code, example.language, true),
__html: syntaxHighlighter(example.code || '', example.language, true),
}}
/>
);
Expand All @@ -74,6 +74,9 @@ class CodeSample extends React.Component {
</div>
);
}

const snippet = generateCodeSnippet(oas, operation, formData, language);

return (
<div>
<ul className="code-sample-tabs">
Expand Down Expand Up @@ -117,7 +120,10 @@ CodeSample.propTypes = {
setLanguage: PropTypes.func.isRequired,
operation: PropTypes.instanceOf(Operation).isRequired,
formData: PropTypes.shape({}).isRequired,
customCodeSamples: PropTypes.shape([]),
customCodeSamples: PropTypes.arrayOf(PropTypes.shape({
language: PropTypes.string.isRequired,
code: PropTypes.string.isRequired,
})),
language: PropTypes.string.isRequired,
};

Expand Down
23 changes: 11 additions & 12 deletions packages/api-explorer/src/Doc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,21 @@ class Doc extends React.Component {
}

renderCodeSample() {
if (this.props.doc.api.examples) {
return (
<CodeSample
oas={this.oas}
setLanguage={this.props.setLanguage}
operation={this.getOperation()}
formData={this.state.formData}
language={this.props.language}
customCodeSamples={this.props.doc.api.examples.codes}
/>
);
let examples;
try {
examples = this.props.doc.api.examples.codes
} catch(e) {
examples = [];
}

return (
<CodeSample
oas={this.oas}
setLanguage={this.props.setLanguage}
operation={this.getOperation()}
formData={this.state.formData}
language={this.props.language}
customCodeSamples={examples}
/>
);
}
Expand Down Expand Up @@ -319,7 +315,10 @@ Doc.propTypes = {
api: PropTypes.shape({
method: PropTypes.string.isRequired,
examples: PropTypes.shape({
codes: PropTypes.arrayOf(PropTypes.shape({})),
codes: PropTypes.arrayOf(PropTypes.shape({
language: PropTypes.string.isRequired,
code: PropTypes.string.isRequired,
})),
}),
}),
swagger: PropTypes.shape({
Expand Down

0 comments on commit 72610f9

Please sign in to comment.