Skip to content

Commit

Permalink
fix(query): fix commands throwing error with empty sourceText.
Browse files Browse the repository at this point in the history
closes #78
  • Loading branch information
Mayank1791989 committed Dec 10, 2017
1 parent 8068120 commit d93ec6c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`[Bug] should works for empty sourceText 1`] = `
Object {
"end": 1,
"prevChar": "",
"start": 0,
"state": Object {
"indentLevel": 0,
"kind": "Document",
"level": 0,
"name": null,
"needsSeperator": false,
"prevState": Object {
"kind": null,
"level": 0,
"name": null,
"needsSeperator": false,
"prevState": null,
"rule": null,
"step": 0,
"type": null,
},
"rule": Array [
Object {
"isList": true,
"ofRule": "Definition",
"separator": undefined,
},
],
"step": 0,
"type": null,
},
"string": " ",
"style": "ws",
}
`;

exports[`[Bug] should works for start position 1`] = `
Object {
"end": 8,
Expand Down
7 changes: 6 additions & 1 deletion src/query/_shared/__tests__/getTokenAtPosition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,16 @@ describe('should able to parse Relay.QL queries with interpolation', () => {

expect(() => getTokenAtPosition(sourceText, position, relayQLParser)).not.toThrow();
});

});

it('[Bug] should works for start position', () => {
const sourceText = 'fragment';
const token = getTokenAtPosition(sourceText, { line: 1, column: 1 }, 'QueryParser');
expect(token).toMatchSnapshot();
});

it('[Bug] should works for empty sourceText', () => {
const sourceText = '';
const token = getTokenAtPosition(sourceText, { line: 1, column: 1 }, 'QueryParser');
expect(token).toMatchSnapshot();
});
7 changes: 7 additions & 0 deletions src/query/commands/__tests__/getHintsAtPosition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,10 @@ describe('[Bug] should work if position is first character', () => {
).toEqual([{ text: 'fragment' }]);
});
});

test('[Bug] should work with empty sourceText', () => {
const sourceText = '';
expect(
getHintsAtPosition(schema, sourceText, { line: 1, column: 1 }, queryConfig),
).toEqual(hints.DocumentLevel);
});
3 changes: 2 additions & 1 deletion src/shared/getTokenAtPosition.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import toOffset from './toOffset';

export default function getTokenAtPosition(
parser: IParser,
sourceText: string,
_sourceText: string,
position: Position,
): Token {
const sourceText = _sourceText ? _sourceText : ' ';
const state = parser.startState();
const offset = toOffset(sourceText, position);
const stream = new MultilineCharacterStream(sourceText);
Expand Down

0 comments on commit d93ec6c

Please sign in to comment.