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

Commit

Permalink
start cookie testing
Browse files Browse the repository at this point in the history
  • Loading branch information
uppal101 committed Sep 18, 2017
1 parent abb5d39 commit 8022aad
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
20 changes: 20 additions & 0 deletions packages/api-explorer-ui/__tests__/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const React = require('react');
const { shallow } = require('enzyme');
const Cookie = require('js-cookie');
const extensions = require('../../readme-oas-extensions');
const ApiExplorer = require('../src');

Expand Down Expand Up @@ -56,4 +57,23 @@ describe('selected language', () => {
expect(explorer.state('language')).toBe('language');
});
});

describe('Cookie', () => {
test('the state of language should be set to Cookie value if provided', () => {
const languages = ['node', 'curl'];
const explorer = shallow(
<ApiExplorer
docs={docs}
oasFiles={{
'api-setting': Object.assign({}, oas, {
[extensions.SAMPLES_LANGUAGES]: languages,
}),
}}
/>,
);

explorer.instance().setLanguage('language');
expect(explorer.state('language')).toBe('language');
});
});
});
1 change: 0 additions & 1 deletion packages/api-explorer-ui/src/CodeSample.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const React = require('react');
const PropTypes = require('prop-types');
const Cookies = require('js-cookie');
const extensions = require('../../readme-oas-extensions');
const Oas = require('./lib/Oas');

Expand Down
6 changes: 5 additions & 1 deletion packages/api-explorer-ui/src/Doc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,9 @@ Doc.propTypes = {
}).isRequired,
oas: PropTypes.shape({}).isRequired,
setLanguage: PropTypes.func.isRequired,
language: PropTypes.string.isRequired,
language: PropTypes.string,
};

Doc.defaultProps = {
language: 'curl',
};
27 changes: 13 additions & 14 deletions packages/api-explorer-ui/src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
const React = require('react');
const Cookie = require('js-cookie');
const PropTypes = require('prop-types');
const Cookies = require('js-cookie');
const extensions = require('../../readme-oas-extensions');

const Doc = require('./Doc');

class ApiExplorer extends React.Component {
constructor(props) {
super(props);
this.state = {
language: 'curl',
};

try {
const firstOas = Object.keys(this.props.oasFiles)[0]; // apisetting
// console.log(this.props.oasFiles[firstOas]);
this.state.language = this.props.oasFiles[firstOas][extensions.SAMPLES_LANGUAGES][0];
} catch (e) {} // eslint-disable-line no-empty
this.state = { language: Cookie.get('readme_language') || this.getDefaultLanguage() };

this.setLanguage = this.setLanguage.bind(this);
// console.log('this is state', this.state.language);
this.getDefaultLanguage = this.getDefaultLanguage.bind(this);
}

setLanguage(language) {
this.setState({ language }, () => {
Cookies.set('readme_language', language);
Cookie.set('readme_language', language);
});
}

getDefaultLanguage() {
try {
const firstOas = Object.keys(this.props.oasFiles)[0];
return this.props.oasFiles[firstOas][extensions.SAMPLES_LANGUAGES][0];
} catch (e) {
return 'curl';
}
}
render() {
return (
<div className={`is-lang-${this.state.language}`}>
Expand All @@ -37,7 +37,6 @@ class ApiExplorer extends React.Component {
doc={doc}
oas={doc.category.apiSetting ? this.props.oasFiles[doc.category.apiSetting] : {}}
setLanguage={this.setLanguage}
language={this.state.language}
/>
))}
</div>
Expand Down

0 comments on commit 8022aad

Please sign in to comment.