Skip to content

Commit

Permalink
fix(GraphiQL) fallback to defaultQuery if there's no stored query
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Apr 28, 2016
1 parent e7a684e commit 2cf8443
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/GraphiQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class GraphiQL extends React.Component {
// Determine the initial query to display.
const query =
props.query !== undefined ? props.query :
this._storageGet('query') !== undefined ? this._storageGet('query') :
this._storageGet('query') !== null ? this._storageGet('query') :
props.defaultQuery !== undefined ? props.defaultQuery :
defaultQuery;

Expand Down
34 changes: 34 additions & 0 deletions src/components/__tests__/GraphiQL-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ import { renderIntoDocument } from 'react-addons-test-utils';

import { GraphiQL } from '../GraphiQL';

const mockStorage = (function () {
let store = {};
return {
getItem(key) {
return store.hasOwnProperty(key) ? store[key] : null;
},
setItem(key, value) {
store[key] = value.toString();
},
clear() {
store = {};
}
};
}());

Object.defineProperty(window, 'localStorage', {
value: mockStorage,
});

describe('GraphiQL', () => {
const noOpFetcher = () => {};
Expand All @@ -36,4 +54,20 @@ describe('GraphiQL', () => {
<GraphiQL fetcher={noOpFetcher} query="{}" />
)).to.not.throw();
});

it('defaults to the built-in default query', () => {
const graphiQL = renderIntoDocument(<GraphiQL fetcher={noOpFetcher} />);
expect(graphiQL.state.query).to.include('# Welcome to GraphiQL');
});

it('accepts a custom default query', () => {
const graphiQL = renderIntoDocument(
<GraphiQL
fetcher={noOpFetcher}
defaultQuery='GraphQL Party!!'
/>
);

expect(graphiQL.state.query).to.equal('GraphQL Party!!');
});
});

0 comments on commit 2cf8443

Please sign in to comment.