Skip to content

Commit

Permalink
Add first pass at code samples rendering https://github.com/readmeio/…
Browse files Browse the repository at this point in the history
…api-explorer/issues/1

Still missing highlighting and values
  • Loading branch information
Dom Harrington committed Aug 10, 2017
1 parent 87eecf0 commit f770940
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 58 deletions.
2 changes: 1 addition & 1 deletion packages/api-explorer-ui/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "airbnb",
"rules": {

"arrow-body-style": 0
}
}
22 changes: 17 additions & 5 deletions packages/api-explorer-ui/__tests__/CodeSample.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const CodeSample = require('../src/CodeSample');

const oas = require('./fixtures/petstore/oas');

const operation = oas.paths['/pet/{petId}'].get;

describe('tabs', () => {
// TODO this doesnt work in readme
test('should display tabs if there are examples in the oas file');
Expand All @@ -19,9 +17,13 @@ describe('tabs', () => {
oas={{
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: languages,
servers: [
{ url: 'http://example.com' },
],
}}
operation={operation}
setLanguage={() => {}}
path="/pet/{id}"
method="get"
/>,
);

Expand All @@ -36,8 +38,9 @@ describe('tabs', () => {
[extensions.SAMPLES_ENABLED]: false,
[extensions.SAMPLES_LANGUAGES]: ['node'],
}}
operation={operation}
setLanguage={() => {}}
path="/pet/{id}"
method="get"
/>,
);

Expand All @@ -53,9 +56,13 @@ describe('code examples', () => {
oas={{
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: languages,
servers: [
{ url: 'http://example.com' },
],
}}
operation={operation}
setLanguage={() => {}}
path="/pet/{id}"
method="get"
/>,
);

Expand All @@ -73,8 +80,13 @@ describe('updating language', () => {
oas={{
[extensions.SAMPLES_ENABLED]: true,
[extensions.SAMPLES_LANGUAGES]: ['node'],
servers: [
{ url: 'http://example.com' },
],
}}
setLanguage={setLanguage}
path="/pet/{id}"
method="get"
/>,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const generateCodeSnippets = require('../../src/lib/generate-code-snippets');

test('should generate a HTML snippet for each lang', () => {
const languages = ['node', 'curl'];

const snippets = generateCodeSnippets({
servers: [
{ url: 'http://example.com' },
],
}, '/path', 'get', languages);

expect(Object.keys(snippets)).toEqual(languages);

Object.keys(snippets).forEach((lang) => {
expect(typeof snippets[lang]).toBe('string');
// expect(snippets[lang]).toEqual(expect.stringMatching(/cm-s-tomorrow-night/));
});
});
37 changes: 37 additions & 0 deletions packages/api-explorer-ui/__tests__/lib/oas-to-har.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const oasToHar = require('../../src/lib/oas-to-har');

test('should output a har format', () => {
expect(oasToHar({})).toEqual({
headers: [],
queryString: [],
postData: {},
method: '',
url: '',
});
});

test('should uppercase the method', () => {
expect(oasToHar({}, '/', 'get').method).toBe('GET');
});

describe('url', () => {
test('should default to ""', () => {
expect(oasToHar({}, '', '').url).toBe('');
expect(oasToHar({}, '/path', '').url).toBe('/path');
});

test('should be constructed from servers[0]', () => {
expect(oasToHar({
servers: [{ url: 'http://example.com' }],
}, '/path', '').url).toBe('http://example.com/path');
});

it('should replace whitespace with %20', () => {
expect(oasToHar({
servers: [{ url: 'http://example.com' }],
}, '/path with spaces', '').url).toBe('http://example.com/path%20with%20spaces');
});

it('should replace path params with values');
});

Loading

0 comments on commit f770940

Please sign in to comment.