Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.
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
304 changes: 295 additions & 9 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"build": "npm run update:emojis; ./build.sh; lerna run build",
"clean": "lerna clean",
"deploy": "gh-pages --dotfiles -d example/dist",
"lint": "eslint example example/ lib/ scripts/ *.js --ext js --ext jsx",
"lint": "eslint example/ lib/ scripts/ *.js --ext js --ext jsx",
"postinstall": "npm run bootstrap",
"publish": "lerna publish",
"pretest": "npm run update:examples; npm run lint",
Expand Down
192 changes: 77 additions & 115 deletions packages/api-explorer/__tests__/CodeSample.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ const petstore = require('@readme/oas-examples/3.0/json/petstore.json');

const CodeSample = require('../src/CodeSample');

const { Operation } = Oas;
const oas = new Oas(petstore);

const props = {
auth: {},
formData: {},
language: 'node',
setLanguage: () => {},
oas,
oasUrl: 'https://example.com/openapi.json',
operation: new Operation({}, '/pet/{petId}', 'get'),
operation: oas.operation('/pet/{petId}', 'get'),
};

describe('tabs', () => {
Expand All @@ -27,9 +29,9 @@ describe('tabs', () => {
{...props}
oas={
new Oas({
...petstore,
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: languages,
servers: [{ url: 'http://example.com' }],
})
}
/>
Expand Down Expand Up @@ -58,15 +60,10 @@ describe('tabs', () => {

describe('code examples', () => {
it('should support the `node-simple` language', () => {
const docProps = {
...props,
language: 'node-simple',
operation: new Operation({}, '/pet/{petId}', 'get'),
};

const codeSample = shallow(
<CodeSample
{...docProps}
{...props}
language={'node-simple'}
oas={
new Oas({
...petstore,
Expand All @@ -83,25 +80,20 @@ describe('code examples', () => {
});

it('should display custom examples over pre-filled examples', () => {
const docProps = {
...props,
operation: new Operation({}, '/pet/{petId}', 'get'),
examples: [
{
language: 'javascript',
code: 'console.log(1);',
},
],
};

const codeSample = shallow(
<CodeSample
{...docProps}
{...props}
examples={[
{
language: 'javascript',
code: 'console.log(1);',
},
]}
oas={
new Oas({
...petstore,
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: ['node', 'curl'],
servers: [{ url: 'http://example.com' }],
})
}
/>
Expand All @@ -112,24 +104,19 @@ describe('code examples', () => {
});

it('should display custom examples even if SAMPLES_ENABLED is false', () => {
const docProps = {
...props,
operation: new Operation({}, '/pet/{petId}', 'get'),
examples: [
{
language: 'javascript',
code: 'console.log(1);',
},
],
};

const codeSample = shallow(
<CodeSample
{...docProps}
{...props}
examples={[
{
language: 'javascript',
code: 'console.log(1);',
},
]}
oas={
new Oas({
...petstore,
[extensions.SAMPLES_ENABLED]: false,
servers: [{ url: 'http://example.com' }],
})
}
/>
Expand All @@ -140,20 +127,15 @@ describe('code examples', () => {
});

it('should not error if no code given', () => {
const docProps = {
...props,
operation: new Operation({}, '/pet/{petId}', 'get'),
examples: [
{
language: 'javascript',
},
],
};

expect(() =>
shallow(
<CodeSample
{...docProps}
{...props}
examples={[
{
language: 'javascript',
},
]}
oas={
new Oas({
[extensions.SAMPLES_ENABLED]: true,
Expand All @@ -167,20 +149,15 @@ describe('code examples', () => {
});

it('should not error if language requested cannot be auto-generated', () => {
const docProps = {
...props,
operation: new Operation({}, '/pet/{petId}', 'get'),
language: 'css',
};

const component = (
<CodeSample
{...docProps}
{...props}
language={'css'}
oas={
new Oas({
...petstore,
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: ['css'],
servers: [{ url: 'http://example.com' }],
})
}
/>
Expand All @@ -195,28 +172,23 @@ describe('code examples', () => {
});

it('should not render sample if language is missing', () => {
const docProps = {
...props,
operation: new Operation({}, '/pet/{petId}', 'get'),
examples: [
{
code: 'console.log(1);',
},
{
language: 'curl',
code: 'curl example.com',
},
],
};

const codeSample = shallow(
<CodeSample
{...docProps}
{...props}
examples={[
{
code: 'console.log(1);',
},
{
language: 'curl',
code: 'curl example.com',
},
]}
oas={
new Oas({
...petstore,
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: ['node', 'curl'],
servers: [{ url: 'http://example.com' }],
})
}
/>
Expand All @@ -227,28 +199,23 @@ describe('code examples', () => {
});

it('should render first of examples if language does not exist', () => {
const docProps = {
...props,
operation: new Operation({}, '/pet/{petId}', 'get'),
examples: [
{
language: 'javascript',
},
{
language: 'typescript',
},
],
language: 'perl',
};

const codeSample = shallow(
<CodeSample
{...docProps}
{...props}
examples={[
{
language: 'javascript',
},
{
language: 'typescript',
},
]}
language={'perl'}
oas={
new Oas({
...petstore,
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: ['css'],
servers: [{ url: 'http://example.com' }],
})
}
/>
Expand All @@ -258,74 +225,69 @@ describe('code examples', () => {
});

it('should display examples if SAMPLES_ENABLED is true', () => {
const languages = ['node', 'curl'];
const codeSample = shallow(
<CodeSample
{...props}
oas={
new Oas({
...petstore,
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: languages,
servers: [{ url: 'http://example.com' }],
[extensions.SAMPLES_LANGUAGES]: ['node', 'curl'],
})
}
/>
);

expect(codeSample.find('.hub-code-auto')).toHaveLength(1);

// We only render one language at a time
expect(codeSample.find('.hub-code-auto pre')).toHaveLength(1);
expect(codeSample.find('.hub-lang-switch-node').text()).toBe('Node');
});

// Skipped until https://github.com/readmeio/api-explorer/issues/965 is resolved.
it.skip('should check the operation level extensions first', () => {
const operationSamplesEnabled = new Operation({}, '/pet/{petId}', 'get');
operationSamplesEnabled[extensions.SAMPLES_ENABLED] = true;
const languages = ['node', 'curl'];
const operationSamplesEnabled = oas.operation('/pet/{petId}', 'get');
operationSamplesEnabled.schema[extensions.SAMPLES_ENABLED] = true;

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

expect(codeSample.find('.hub-code-auto')).toHaveLength(1);

// We only render one language at a time
expect(codeSample.find('.hub-code-auto pre')).toHaveLength(1);
expect(codeSample.find('.hub-lang-switch-node').text()).toBe('Node');
});

it('should not display more than one example block at a time', () => {
const docProps = {
...props,
operation: new Operation({}, '/pet/{petId}', 'get'),
language: 'javascript',
examples: [
{
name: 'Javascript/Node.js',
code: 'console.log(1);',
language: 'javascript',
},
{
name: 'TypeScript',
code: 'console.log(1)',
language: 'javascript',
},
],
};

const codeSample = shallow(
<CodeSample
{...docProps}
{...props}
examples={[
{
name: 'Javascript/Node.js',
code: 'console.log(1);',
language: 'javascript',
},
{
name: 'TypeScript',
code: 'console.log(1)',
language: 'javascript',
},
]}
language={'javascript'}
oas={
new Oas({
[extensions.SAMPLES_ENABLED]: true,
Expand All @@ -349,9 +311,9 @@ describe('updating language', () => {
{...props}
oas={
new Oas({
...petstore,
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: ['node'],
servers: [{ url: 'http://example.com' }],
})
}
setLanguage={setLanguage}
Expand Down
4 changes: 2 additions & 2 deletions packages/api-explorer/__tests__/Params.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('form id attribute', () => {
</div>
)
.html()
.match(new RegExp(`form-path-${operation.operationId}`, 'g'))
.match(new RegExp(`form-path-${operation.getOperationId()}`, 'g'))
).toHaveLength(1);
});
});
Expand Down Expand Up @@ -417,7 +417,7 @@ describe('x-explorer-enabled', () => {

it('should check the operation level extensions first', () => {
const operationExplorerEnabled = oas.operation('/pet/{petId}/uploadImage', 'post');
operationExplorerEnabled[extensions.EXPLORER_ENABLED] = true;
operationExplorerEnabled.schema[extensions.EXPLORER_ENABLED] = true;

const Component = createParams(oasWithExplorerDisabled, operationExplorerEnabled);

Expand Down
Loading