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

Commit

Permalink
Still stuck on testing onSubmit for Doc
Browse files Browse the repository at this point in the history
  • Loading branch information
uppal101 committed Oct 23, 2017
1 parent 526dd09 commit f4e60cf
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 175 deletions.
2 changes: 1 addition & 1 deletion example/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs');
const path = require('path');
const CircularJSON = require('circular-json');

const oas = fs.readFileSync(path.join(__dirname, '/../packages/api-explorer-ui/__tests__/fixtures/example-results/circular-oas.json'), 'utf8');
const oas = fs.readFileSync(path.join(__dirname, '/../packages/api-explorer-ui/__tests__/fixtures/petstore/circular-oas.json'), 'utf8');
const createDocs = require('../packages/api-explorer-ui/lib/create-docs');

const ApiExplorer = require('../packages/api-explorer-ui/src/index.jsx');
Expand Down
25 changes: 19 additions & 6 deletions packages/api-explorer-ui/__tests__/CodeSampleResponse.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,31 @@ describe('Results body', () => {
});

describe('examples', () => {
test('if endpoint has an example it is shown', () => {
test('if endpoint has an example tabs and body should show', () => {
const oas2 = new Oas(example);
const props4 = {
result: null,
// url: 'http://example.com',
operation: new Operation({}, '/results', 'get'),
operation: oas2.operation('/results', 'get'),
};
const codeSampleResponseTabs = shallow(<CodeSampleResponseTabs {...props4} oas={oas2} />);

expect(
codeSampleResponseTabs.containsMatchingElement(<div className="code-sample-body" />),
).toEqual(true);
const firstTab = codeSampleResponseTabs.find('a').first();
const secondTab = codeSampleResponseTabs.find('a').last();

expect(codeSampleResponseTabs.state('exampleTab')).toBe(0);
expect(firstTab.hasClass('hub-reference-results-header-item tabber-tab selected')).toEqual(
true,
);

secondTab.simulate('click', { preventDefault() {} });

expect(codeSampleResponseTabs.state('exampleTab')).toBe(1);
expect(firstTab.hasClass('hub-reference-results-header-item tabber-tab')).toEqual(true);

expect(firstTab.find('span.httpsuccess').length).toBe(1);
expect(secondTab.find('span.httperror').length).toBe(1);

expect(codeSampleResponseTabs.find('pre').length).toBe(2);
});

test('if endpoint does not have example ', () => {
Expand Down
60 changes: 9 additions & 51 deletions packages/api-explorer-ui/__tests__/Doc.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,7 @@ describe('onSubmit', () => {
expect(doc.state('needsAuth')).toBe(true);
});

xtest('should hide authBox on successful submit', () => {
const doc = mount(<Doc {...props} />);
doc.instance().onSubmit();
doc.instance().onChange({ auth: { api_key: 'api-key' } });
doc.instance().onSubmit();

expect(doc.state('showAuthBox')).toBe(false);
expect(doc.state('needsAuth')).toBe(false);
});

xtest('should make request on Submit', () => {
test('should make request on Submit', () => {
const props2 = {
doc: {
title: 'Title',
Expand All @@ -111,18 +101,17 @@ describe('onSubmit', () => {
//

window.fetch = jest.fn(req => Promise.resolve({ test: 1 }));
const doc = mount(<Doc {...props2} />);
doc.instance().onSubmit();
doc.instance().onChange({ auth: { petstore_auth: 'api-key' } });
doc.instance().onSubmit();

// await doc
// .instance()
// .onSubmit()
// .should.be.fulfilled();
const doc = mount(<Doc {...props2} />);
// doc.instance().onSubmit();
// doc.instance().onChange({ auth: { api_key: 'api-key' } });
// doc.instance().onSubmit();

// doc.update();
// expect(doc.state('loading')).toBe(true);
expect(doc.state('showAuthBox')).toBe(false);
expect(doc.state('needsAuth')).toBe(false);

expect(Doc.prototype.onSubmit).toBeCalled();
// expect(doc.state('result')).toEqual({
// init: true,
// isBinary: false,
Expand All @@ -139,9 +128,6 @@ describe('onSubmit', () => {
// },
// url: 'http://petstore.swagger.io/v2/pet',
// });
// expect(doc.state('loading')).toBe(false);

expect(Doc.prototype.onSubmit).toBeCalled();
});
});

Expand All @@ -161,38 +147,10 @@ describe('toggleAuth', () => {
});
});

describe('hideResults', () => {
xtest('responseTabClass should change state of responseTabClass', () => {
const doc = shallow(<Doc {...props} />);

doc.setState({
responseTabClass: 'hub-reference-right hub-reference-results tabber-parent on',
});

expect(doc.state('responseTabClass')).toBe(
'hub-reference-right hub-reference-results tabber-parent on',
);

doc.instance().hideResults();

expect(doc.state('responseTabClass')).toBe(
'hub-reference-right hub-reference-results tabber-parent',
);
});
});

describe('state.loading', () => {
test('should default to false', () => {
const doc = shallow(<Doc {...props} />);

expect(doc.state('loading')).toBe(false);
});

xtest('should switch to true on form submit', () => {
const doc = shallow(<Doc {...props} />);
doc.instance().onChange({ auth: { api_key: 'api-key' } });
doc.instance().onSubmit();

expect(doc.state('loading')).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@
"400": {
"description": "Validation failed",
"content": {
"application/json": {
"application/xml": {
"examples": {
"response": {
"value": {
"email": "invalid email",
"name": "name already taken"
}
"value":
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@
"400": {
"description": "Validation failed",
"content": {
"application/json": {
"application/xml": {
"examples": {
"response": {
"value": {
"email": "invalid email",
"name": "name already taken"
}
"value":
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"
}
}
}
Expand All @@ -49,6 +47,11 @@
},
"summary": "Update Password"
}
},
"/nolang": {
"get": {
"description": ""
}
}
},
"components": {
Expand Down
53 changes: 53 additions & 0 deletions packages/api-explorer-ui/__tests__/lib/create-code-shower.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const createCodeShower = require('../../src/lib/create-code-shower')('results');
const createCodeShower2 = require('../../src/lib/create-code-shower')('examples');

const Oas = require('../../src/lib/Oas.js');
const example = require('../fixtures/example-results/oas');
const petstore = require('../fixtures/petstore/oas');

const oas = new Oas(example);
const oas2 = new Oas(petstore);

describe('createCodeShower', () => {
it('should return codes array if there are examples for the operation endpoint', () => {
const operation = oas.operation('/results', 'get');

expect(createCodeShower(operation)).toEqual([
{
code: JSON.stringify(
{
user: {
email: 'test@example.com',
name: 'Test user name',
},
},
undefined,
2,
),
language: 'application/json',
status: '200',
},
{
code:
'<?xml version="1.0" encoding="UTF-8"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don\'t forget me this weekend!</body></note>',
language: 'application/xml',
status: '400',
},
]);
});

it('should return early if there is no example', () => {
const operation = oas2.operation('/pet/findByStatus', 'get');
expect(createCodeShower(operation)).toEqual([]);
});

it('should return early if there is no response', () => {
const operation = oas.operation('/nolang', 'get');
expect(createCodeShower(operation)).toEqual([]);
});

it('should return codes if type is not results', () => {
const operation = oas.operation('/results', 'get');
expect(createCodeShower2(operation)).toEqual([]);
});
});
34 changes: 34 additions & 0 deletions packages/api-explorer-ui/__tests__/lib/get-path.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const getPath = require('../../src/lib/get-path');
const Oas = require('../../src/lib/Oas.js');

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

const swagger = new Oas(petstore);

describe('getSchema', () => {
it('should return path from swagger file', () => {
const doc = { swagger: { path: '/user/logout' }, api: { method: 'get' } };

expect(getPath(swagger, doc)).toEqual({
get: {
tags: ['user'],
summary: 'Logs out current logged in user session',
description: '',
operationId: 'logoutUser',
parameters: [],
responses: {
default: {
description: 'successful operation',
},
},
},
});
});
it('should return parameters object if doc does not have swagger property', () => {
const doc = { api: { method: 'get' } };

expect(getPath(swagger, doc)).toEqual({
parameters: [],
});
});
});
24 changes: 24 additions & 0 deletions packages/api-explorer-ui/__tests__/lib/get-schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const getSchema = require('../../src/lib/get-schema');
const Oas = require('../../src/lib/Oas.js');

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

const oas = new Oas(petstore);

describe('getSchema', () => {
it('should return schema if requestBody content is application json', () => {
const operation = oas.operation('/user/{username}', 'put');

expect(getSchema(operation)).toEqual({
$ref: '#/components/schemas/User',
});
});

it('should return requestBody if content is not application json', () => {
const operation = oas.operation('/user/createWithList', 'post');

expect(getSchema(operation)).toEqual({
$ref: '#/components/requestBodies/UserArray',
});
});
});
42 changes: 21 additions & 21 deletions packages/api-explorer-ui/__tests__/lib/replace-vars.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const { replaceVars } = require('../../src/lib/replace-vars');

describe('replaceVars', () => {
it('should return true if auth data is passed in correctly for api key condition', () => {
// const operation = oas.operation('/api-key', 'post');
//
expect(
replaceVars({
user: {
email: 'test@example.com',
name: 'Test user name',
},
}),
).toEqual({
user: {
email: 'test@example.com',
name: 'Test user name',
},
});
});
});
// const { replaceVars } = require('../../src/lib/replace-vars');
//
// describe('replaceVars', () => {
// it('should return true if auth data is passed in correctly for api key condition', () => {
// // const operation = oas.operation('/api-key', 'post');
// //
// expect(
// replaceVars({
// user: {
// email: 'test@example.com',
// name: 'Test user name',
// },
// }),
// ).toEqual({
// user: {
// email: 'test@example.com',
// name: 'Test user name',
// },
// });
// });
// });
6 changes: 3 additions & 3 deletions packages/api-explorer-ui/src/CodeSampleResponse.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const classNames = require('classnames');
const PropTypes = require('prop-types');
const showCodeResults = require('./lib/show-code-results');
const statusCodes = require('./lib/statuscodes');
const { replaceVars } = require('./lib/replace-vars');
// const { replaceVars } = require('./lib/replace-vars');
const extensions = require('../../readme-oas-extensions');
const generateCodeSnippets = require('./lib/generate-code-snippets');
const syntaxHighlighter = require('../../readme-syntax-highlighter');
Expand Down Expand Up @@ -203,7 +203,7 @@ class CodeSampleResponse extends React.Component {
{showCodeResults(operation).map((example, index) => {
const status = statusCodes(example.status);
const title = example.name ? example.name : status[1];
console.log(codemirror(example.code, example.language, true));

return (
<a
className={
Expand Down Expand Up @@ -242,7 +242,7 @@ class CodeSampleResponse extends React.Component {
className={`tomorrow night tabber-body tabber-body-${index}`}
style={{ display: index === this.state.exampleTab ? 'block' : '' }}
>
{replaceVars(codemirror(example.code, example.language, true))}
{codemirror(example.code, example.language, true)}
</pre>
);
})}
Expand Down
1 change: 1 addition & 0 deletions packages/api-explorer-ui/src/Doc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Doc extends React.Component {
this.oas.operation(this.props.doc.swagger.path, this.props.doc.api.method),
this.state.formData,
);

if (
!isAuthReady(
this.oas.operation(this.props.doc.swagger.path, this.props.doc.api.method),
Expand Down
Loading

0 comments on commit f4e60cf

Please sign in to comment.